Leosac  0.8.0
Open Source Access Control
AuditGet.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "AuditGet.hpp"
21 #include "Exceptions.hpp"
22 #include "WSServer.hpp"
23 #include "api/APISession.hpp"
24 #include "core/CoreUtils.hpp"
26 #include "core/audit/AuditEntry_odb.h"
29 #include "tools/JSONUtils.hpp"
30 #include "tools/LogEntry_odb.h"
31 #include "tools/db/DBService.hpp"
32 #include "tools/enforce.hpp"
33 #include "tools/log.hpp"
34 #include <odb/pgsql/query.hxx>
35 
36 using namespace Leosac;
37 using namespace Leosac::Module;
38 using namespace Leosac::Module::WebSockAPI;
39 
41  : MethodHandler(ctx)
42 {
43 }
44 
46 {
47  return std::make_unique<AuditGet>(ctx);
48 }
49 
51 {
52  json rep;
53  DBPtr db = ctx_.dbsrv->db();
54  if (db)
55  {
56  using namespace Tools;
57  using namespace JSONUtil;
58  using Query = odb::query<Audit::AuditEntry>;
59 
60  int page = extract_with_default(req, "p", 1);
61  int page_size = extract_with_default(req, "ps", 20);
62 
63  LEOSAC_ENFORCE_ARGUMENT(page > 0, page, "Page must be >0");
64  LEOSAC_ENFORCE_ARGUMENT(page_size > 0, page_size, "Page size must be >0");
65 
66  odb::transaction t(db->begin());
68  db->query_value<Audit::AuditEntryCount>(build_in_clause(req)));
69  rep["meta"]["count"] = view.count;
70  if (view.count)
71  {
72  rep["meta"]["total_page"] =
73  (view.count / page_size) + (view.count % page_size ? 1 : 0);
74  }
75  else
76  rep["meta"]["total_page"] = 0;
77 
78  auto query = Query(build_request_string(req, page, page_size));
79  auto ret = db->query<Audit::AuditEntry>(query);
80  rep["data"] = json::array();
81  for (const auto &audit : ret)
82  {
84  audit, security_context());
85  rep["data"].push_back(audit_json);
86  }
87  }
88  else
89  {
90  rep["status"] = -1;
91  }
92  return rep;
93 }
94 
95 std::vector<ActionActionParam> AuditGet::required_permission(const json &) const
96 {
97  std::vector<ActionActionParam> perm_;
99 
100  perm_.push_back({SecurityContext::Action::AUDIT_READ, ap});
101  return perm_;
102 }
103 
104 std::string AuditGet::build_request_string(const json &req, int page,
105  int page_size) const
106 {
107  std::stringstream request_builder;
108 
109  request_builder << build_in_clause(req);
110  request_builder << " ORDER BY id DESC";
111  request_builder << " LIMIT " << page_size;
112  request_builder << " OFFSET " << page_size * (page - 1);
113  DEBUG("QUERY: " << request_builder.str());
114  return request_builder.str();
115 }
116 
117 bool AuditGet::is_stringtype_sane(const std::string &str) const
118 {
119  for (const auto &c : str)
120  {
121  if (c != ':' && !isalpha(c))
122  return false;
123  }
124  return true;
125 }
126 
127 std::string AuditGet::build_in_clause(const json &req) const
128 {
129  std::stringstream request_builder;
130 
131  if (req.find("enabled_type") != req.end() && req.at("enabled_type").is_array() &&
132  req.at("enabled_type").size())
133  {
134  const auto &enabled_types = req.at("enabled_type");
135  request_builder << "WHERE typeid IN (";
136  for (size_t i = 0; i < enabled_types.size(); ++i)
137  {
138  auto enabled_type = enabled_types[i].get<std::string>();
139  if (!is_stringtype_sane(enabled_type))
140  {
141  throw LEOSACException(
142  BUILD_STR("Audit type string is invalid: " << enabled_type));
143  }
144  request_builder << "'" << enabled_type << "'";
145  if (i != enabled_types.size() - 1)
146  request_builder << ",";
147  }
148  request_builder << ")";
149  }
150  else
151  {
152  request_builder << "1 = 1";
153  }
154  return request_builder.str();
155 }
Leosac::Audit::AuditEntryCount
Definition: AuditEntry.hpp:171
Leosac::JSONUtil::extract_with_default
std::chrono::system_clock::time_point extract_with_default(const nlohmann::json &obj, const std::string &key, const std::chrono::system_clock::time_point &tp)
Extract an ISO 8601 datetime string from a json object.
Definition: JSONUtils.cpp:45
Exceptions.hpp
BUILD_STR
#define BUILD_STR(param)
Internal macro.
Definition: log.hpp:63
Leosac::Module::WebSockAPI::AuditGet::build_in_clause
std::string build_in_clause(const json &req) const
Build the "WHERE typeid IN (...)" string based on the enabled types.
Definition: AuditGet.cpp:127
Leosac::Module::WebSockAPI::AuditGet::required_permission
std::vector< ActionActionParam > required_permission(const json &req) const override
Return a list of "Action" / "ActionParam" that must pass before the request is processed.
Definition: AuditGet.cpp:95
Leosac::Module::WebSockAPI::AuditGet::build_request_string
std::string build_request_string(const json &req, int page, int page_size) const
Definition: AuditGet.cpp:104
AuditEntry.hpp
DEBUG
@ DEBUG
Definition: log.hpp:35
Leosac::Module::WebSockAPI::AuditGet::is_stringtype_sane
bool is_stringtype_sane(const std::string &str) const
Check that a given string representing an audit type is sane.
Definition: AuditGet.cpp:117
Leosac::Module::WebSockAPI::RequestContext::dbsrv
DBServicePtr dbsrv
Definition: RequestContext.hpp:39
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Module
All modules that provides features to Leosac shall be in this namespace.
Query
odb::query< Tools::LogEntry > Query
Definition: LogEntry.cpp:36
Leosac::SecurityContext::ActionParam
Definition: SecurityContext.hpp:231
Leosac::Module::WebSockAPI::MethodHandler
The base class for API method handler implementation.
Definition: MethodHandler.hpp:46
Leosac::Module::WebSockAPI::AuditGet::create
static MethodHandlerUPtr create(RequestContext)
Definition: AuditGet.cpp:45
enforce.hpp
PolymorphicAuditSerializer.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
LEOSACException
A base class for Leosac specific exception.
Definition: leosacexception.hpp:40
Leosac::Module::WebSockAPI::MethodHandler::ctx_
RequestContext ctx_
Definition: MethodHandler.hpp:90
JSONUtils.hpp
Leosac::Module::WebSockAPI::AuditGet::AuditGet
AuditGet(RequestContext ctx)
Definition: AuditGet.cpp:40
WSServer.hpp
Leosac::Module::WebSockAPI::MethodHandlerUPtr
std::unique_ptr< MethodHandler > MethodHandlerUPtr
Definition: WebSockFwd.hpp:36
AuditGet.hpp
DBService.hpp
APISession.hpp
log.hpp
CoreUtils.hpp
Leosac::Module::WebSockAPI::MethodHandler::security_context
UserSecurityContext & security_context()
Definition: MethodHandler.cpp:44
LEOSAC_ENFORCE_ARGUMENT
#define LEOSAC_ENFORCE_ARGUMENT(cond, var, msg)
A macro to perform argument checking that results in an exception being thrown on failure.
Definition: enforce.hpp:59
Leosac::Module::WebSockAPI::json
nlohmann::json json
Definition: AccessOverview.hpp:30
Leosac::Audit::Serializer::PolymorphicAuditJSON::serialize
static json serialize(const Audit::IAuditEntry &in, const SecurityContext &sc)
Perform deep serialization of the AuditEntry in.
Definition: PolymorphicAuditSerializer.cpp:44
Leosac::SecurityContext::Action::AUDIT_READ
@ AUDIT_READ
Read the audit log.
Leosac::Module::WebSockAPI::RequestContext
Holds valuable pointer to provide context to a request.
Definition: RequestContext.hpp:36
Leosac::Audit::AuditEntry
Implementation of IAuditEntry, backed by ODB.
Definition: AuditEntry.hpp:45
Leosac::Module::WebSockAPI
Definition: ActionActionParam.hpp:28
Leosac::Audit::AuditEntryCount::count
std::size_t count
Definition: AuditEntry.hpp:174
InvalidArgument.hpp
Leosac::Module::WebSockAPI::AuditGet::process_impl
virtual json process_impl(const json &req) override
The API method implementation.
Definition: AuditGet.cpp:50