Kea 3.3.4
rbac.cc
Go to the documentation of this file.
1// Copyright (C) 2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <rbac/rbac.h>
10#include <rbac_log.h>
11
12using namespace isc;
13using namespace isc::data;
14using namespace isc::http;
15using namespace isc::log;
16using namespace isc::rbac;
17using namespace std;
18
19namespace isc {
20namespace rbac {
21
23
25
27
29
31 { "name", Element::string },
32 { "accept-commands", Element::any },
33 { "reject-commands", Element::any },
34 { "other-commands", Element::string },
35 { "list-match-first", Element::string },
36 { "response-filters", Element::list },
37 { "comment", Element::string }
38};
39
41RoleConfig::getConfig(const string& role) {
42 if (role.empty()) {
43 return (defaultRoleConfig);
44 }
45 auto it = roleConfigTable.find(role);
46 if (it == roleConfigTable.end()) {
47 return (unknownRoleConfig);
48 }
49 return (it->second);
50}
51
54 if (!cfg) {
55 isc_throw(BadValue, "parse null role config");
56 }
57 if (cfg->getType() != Element::map) {
58 isc_throw(BadValue, "role config is not a map");
59 }
60
61 // Keywords.
63
64 // Name.
65 ConstElementPtr name_elem = cfg->get("name");
66 if (!name.empty() && name_elem) {
67 isc_throw(BadValue, "spurious 'name' parameter for " << name
68 << " role");
69 } else if (name.empty() && !name_elem) {
70 isc_throw(BadValue, "'name' parameter is required in " << cfg->str());
71 }
72 if (name_elem) {
73 name = name_elem->stringValue();
74 if (name.empty()) {
75 isc_throw(BadValue, "role config name is empty");
76 }
77 }
78
79 // Accept ACL.
80 ConstElementPtr accept_elem = cfg->get("accept-commands");
81 AclPtr accept;
82 if (accept_elem) {
83 accept = Acl::parse(accept_elem);
84 }
85
86 // Reject ACL.
87 ConstElementPtr reject_elem = cfg->get("reject-commands");
88 AclPtr reject;
89 if (reject_elem) {
90 reject = Acl::parse(reject_elem);
91 }
92
93 // Others.
94 bool others = false;
95 ConstElementPtr others_elem = cfg->get("other-commands");
96 if (others_elem) {
97 const string& action = others_elem->stringValue();
98 if ((action!= "accept") && (action != "reject")) {
99 isc_throw(BadValue, "other-commands '" << action
100 << "' is not 'accept' or 'reject'");
101 }
102 others = (action == "accept");
103 }
104
105 // Preference.
106 bool preference = true;
107 ConstElementPtr pref_elem = cfg->get("list-match-first");
108 if (pref_elem) {
109 const string& pref = pref_elem->stringValue();
110 if ((pref != "accept") && (pref != "reject")) {
111 isc_throw(BadValue, "list-match-first '" << pref
112 << "' is not 'accept' or 'reject'");
113 }
114 preference = (pref == "accept");
115 }
116
117 // Response filters.
118 ConstElementPtr resp_filters = cfg->get("response-filters");
119 ResponseFilterList response_filters;
120 if (resp_filters) {
121 response_filters = ResponseFilter::parse(resp_filters);
122 }
123
124 return (RoleConfigPtr(new RoleConfig(name, accept, reject, others,
125 preference, response_filters)));
126}
127
130 const HttpStatusCode& status_code) {
131 if (!request) {
132 // Should not happen!
133 isc_throw(Unexpected, "null request");
134 }
135 // From CmdResponseCreator::createStockHttpResponseInternal.
136 HttpVersion http_version(request->context()->http_version_major_,
137 request->context()->http_version_minor_);
138 if ((http_version < HttpVersion(1, 0)) ||
139 (HttpVersion(1, 1) < http_version)) {
140 http_version.major_ = 1;
141 http_version.minor_ = 0;
142 }
143 HttpResponseJsonPtr response(new HttpResponseJson(http_version,
144 status_code));
146 uint16_t result = HttpResponse::statusCodeToNumber(status_code);
147 body->set("result", Element::create(static_cast<long long>(result)));
148 const string& text = HttpResponse::statusCodeToString(status_code);
149 body->set("text", Element::create(text));
150 response->setBodyAsJson(body);
151 response->finalize();
152 return (response);
153}
154
155} // end of namespace rbac
156} // end of namespace isc
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:300
@ map
Definition data.h:160
@ any
Definition data.h:161
@ list
Definition data.h:159
@ string
Definition data.h:157
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:355
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when an unexpected error condition occurs.
static void checkKeywords(const SimpleKeywords &keywords, isc::data::ConstElementPtr scope)
Checks acceptable keywords with their expected type.
Represents HTTP response with JSON content.
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
Definition response.cc:201
static std::string statusCodeToString(const HttpStatusCode &status_code)
Converts status code to string.
Definition response.cc:190
static AclPtr parse(data::ConstElementPtr cfg)
Parse an access control list.
Definition rbac_acl.cc:35
static ResponseFilterList parse(data::ConstElementPtr cfg)
Parse a response filter list.
RoleConfig(const std::string &name, const AclPtr &accept, const AclPtr &reject, bool others, bool preference, const ResponseFilterList &response_filters)
Constructor.
Definition rbac.h:47
static http::HttpResponseJsonPtr createReject(const http::HttpRequestPtr &request, const http::HttpStatusCode &status_code)
Create a reject response.
Definition rbac.cc:129
static const data::SimpleKeywords ROLE_PARAMETERS
Role configuration parameters.
Definition rbac.h:37
static RoleConfigPtr parse(data::ConstElementPtr cfg, std::string name="")
Parse a role config.
Definition rbac.cc:53
static RoleConfigPtr getConfig(const std::string &role)
Return the role config.
Definition rbac.cc:41
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
std::map< std::string, isc::data::Element::types > SimpleKeywords
This specifies all accepted keywords with their types.
HttpStatusCode
HTTP status codes (cf RFC 2068).
Definition response.h:31
boost::shared_ptr< HttpResponseJson > HttpResponseJsonPtr
Pointer to the HttpResponseJson object.
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition request.h:30
boost::shared_ptr< RoleConfig > RoleConfigPtr
Type of shared pointers to role configurations.
Definition rbac.h:23
RolePtr roleAssign
The role assignment.
Definition rbac.cc:22
boost::shared_ptr< Acl > AclPtr
Type of shared pointers to Acl.
Definition rbac_acl.h:29
std::list< ResponseFilterPtr > ResponseFilterList
The type of list of shared pointers to response filters.
boost::shared_ptr< Role > RolePtr
Type of shared pointers to Role.
Definition rbac_role.h:20
std::map< std::string, RoleConfigPtr > RoleConfigTable
Type of the role configurations table.
Definition rbac.h:26
RoleConfigPtr defaultRoleConfig
The default (role is "") role configuration.
Definition rbac.cc:26
RoleConfigPtr unknownRoleConfig
The unknown (role is not in the table) role configuration.
Definition rbac.cc:28
RoleConfigTable roleConfigTable
The role configurations table.
Definition rbac.cc:24
Defines the logger used by the top-level component of kea-lfc.
HTTP protocol version.
Definition http_types.h:14
unsigned minor_
Minor HTTP version.
Definition http_types.h:16
unsigned major_
Major HTTP version.
Definition http_types.h:15