Kea 3.3.4
rbac_acl.h
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#ifndef RBAC_ACL_H
8#define RBAC_ACL_H
9
10#include <cc/data.h>
12
13#include <list>
14#include <map>
15#include <set>
16#include <string>
17
18#include <boost/shared_ptr.hpp>
19
20#include <rbac/rbac_api.h>
21
22namespace isc {
23namespace rbac {
24
26class Acl;
27
29typedef boost::shared_ptr<Acl> AclPtr;
30
32typedef std::list<AclPtr> AclList;
33
35typedef std::map<std::string, AclPtr> AclTable;
36
38class Acl {
39public:
40
45 Acl(const std::string& name) : name_(name) {
46 if (name.empty()) {
47 isc_throw(Unexpected, "empty access control list name");
48 }
49 };
50
52 virtual ~Acl() = default;
53
57 std::string getClassName() const {
58 return (name_);
59 }
60
65 virtual bool match(const std::string& command) = 0;
66
68 static void initTable();
69
75
81
82protected:
84 std::string name_;
85};
86
89class AliasAcl : public Acl {
90public:
91
97 AliasAcl(const std::string& name, const AclPtr& acl)
98 : Acl("alias"), name_(name), acl_(acl) {
99 if (!acl) {
100 isc_throw(Unexpected, "null acl in alias");
101 }
102 }
103
108 virtual bool match(const std::string& command) final {
109 return (acl_->match(command));
110 }
111
115 std::string getName() const {
116 return (name_);
117 }
118
122 const AclPtr& getAcl() const {
123 return (acl_);
124 }
125
129 static void parse(data::ConstElementPtr def);
130
131private:
133 std::string name_;
134
136 AclPtr acl_;
137};
138
140
142class NotAcl : public Acl {
143public:
144
149 NotAcl(const AclPtr& acl) : Acl("not"), acl_(acl) {
150 if (!acl) {
151 isc_throw(Unexpected, "null acl in not");
152 }
153 }
154
159 virtual bool match(const std::string& command) final {
160 return (!acl_->match(command));
161 }
162
166 const AclPtr& getAcl() const {
167 return (acl_);
168 }
169
170private:
172 AclPtr acl_;
173};
174
176class AndAcl : public Acl {
177public:
178
183 AndAcl(const AclList& acls) : Acl("and"), acls_(acls) {
184 for (const AclPtr& acl : acls) {
185 if (!acl) {
186 isc_throw(Unexpected, "null acl in and");
187 }
188 }
189 }
190
195 void add(const AclPtr& acl) {
196 if (!acl) {
197 isc_throw(Unexpected, "add null acl in and");
198 }
199 acls_.push_back(acl);
200 }
201
206 virtual bool match(const std::string& command) final {
207 for (const AclPtr& acl : acls_) {
208 if (!acl->match(command)) {
209 return (false);
210 }
211 }
212 return (true);
213 }
214
218 const AclList& getAcls() const {
219 return (acls_);
220 }
221
222private:
224 AclList acls_;
225};
226
228class OrAcl : public Acl {
229public:
230
235 OrAcl(const AclList& acls) : Acl("or"), acls_(acls) {
236 for (const AclPtr& acl : acls) {
237 if (!acl) {
238 isc_throw(Unexpected, "null acl in or");
239 }
240 }
241 }
242
247 void add(const AclPtr& acl) {
248 if (!acl) {
249 isc_throw(Unexpected, "add null acl in or");
250 }
251 acls_.push_back(acl);
252 }
253
258 virtual bool match(const std::string& command) final {
259 for (const AclPtr& acl : acls_) {
260 if (acl->match(command)) {
261 return (true);
262 }
263 }
264 return (false);
265 }
266
270 const AclList& getAcls() const {
271 return (acls_);
272 }
273
274private:
276 AclList acls_;
277};
278
280class AllAcl : public Acl {
281public:
282
284 AllAcl() : Acl("all") {
285 }
286
290 virtual bool match(const std::string&) final {
291 return (true);
292 }
293};
294
296class NoneAcl : public Acl {
297public:
298
300 NoneAcl() : Acl("none") {
301 }
302
306 virtual bool match(const std::string&) final {
307 return (false);
308 }
309};
310
312class CommandsAcl : public Acl {
313public:
314
318 CommandsAcl(const std::set<std::string>& commands)
319 : Acl("commands"), commands_(commands) {
320 }
321
325 void add(const std::string& command) {
326 // Insert an already existing member is a noop.
327 static_cast<void>(commands_.insert(command));
328 }
329
333 virtual bool match(const std::string& command) final {
334 return (commands_.count(command) != 0);
335 }
336
340 const std::set<std::string>& getCommands() const {
341 return (commands_);
342 }
343
344private:
346 std::set<std::string> commands_;
347};
348
350class AccessAcl : public Acl {
351public:
352
356 AccessAcl(const std::string& access) : Acl("access"), access_(access) {
357 }
358
362 virtual bool match(const std::string& command) final {
363 ApiPtr api(Api::getApiByName(command));
364 return (api && (api->getAccess() == access_));
365 }
366
370 std::string getAccess() const {
371 return (access_);
372 }
373
374private:
376 std::string access_;
377};
378
380class HookAcl : public Acl {
381public:
382
386 HookAcl(const std::string& hook) : Acl("hook"), hook_(hook) {
387 }
388
392 virtual bool match(const std::string& command) final {
393 ApiPtr api(Api::getApiByName(command));
394 return (api && (api->getHook() == hook_));
395 }
396
400 std::string getHook() const {
401 return (hook_);
402 }
403
404private:
406 std::string hook_;
407};
408
410extern AclTable aclTable;
411
412} // end of namespace rbac
413} // end of namespace isc
414#endif
A generic exception that is thrown when an unexpected error condition occurs.
virtual bool match(const std::string &command) final
Match the access control list.
Definition rbac_acl.h:362
std::string getAccess() const
Returns the access.
Definition rbac_acl.h:370
AccessAcl(const std::string &access)
Constructor.
Definition rbac_acl.h:356
Access control list base class.
Definition rbac_acl.h:38
static AclPtr parse(data::ConstElementPtr cfg)
Parse an access control list.
Definition rbac_acl.cc:35
virtual bool match(const std::string &command)=0
Match the access control list.
std::string getClassName() const
Returns the class name.
Definition rbac_acl.h:57
static void initTable()
Initialize the access control list table.
Definition rbac_acl.cc:26
std::string name_
The class name.
Definition rbac_acl.h:84
virtual ~Acl()=default
Destructor.
static AclList parseList(data::ConstElementPtr cfg)
Parse a list of access control lists.
Definition rbac_acl.cc:119
Acl(const std::string &name)
Constructor.
Definition rbac_acl.h:45
AliasAcl(const std::string &name, const AclPtr &acl)
Constructor.
Definition rbac_acl.h:97
static void parse(data::ConstElementPtr def)
Parse an access control list definition.
Definition rbac_acl.cc:134
std::string getName() const
Returns the name.
Definition rbac_acl.h:115
const AclPtr & getAcl() const
Returns the acl.
Definition rbac_acl.h:122
virtual bool match(const std::string &command) final
Match the access control list.
Definition rbac_acl.h:108
virtual bool match(const std::string &) final
Match the access control list.
Definition rbac_acl.h:290
AllAcl()
Constructor.
Definition rbac_acl.h:284
virtual bool match(const std::string &command) final
Match the access control list.
Definition rbac_acl.h:206
const AclList & getAcls() const
Returns the acls.
Definition rbac_acl.h:218
AndAcl(const AclList &acls)
Constructor.
Definition rbac_acl.h:183
void add(const AclPtr &acl)
Add an access control list.
Definition rbac_acl.h:195
static ApiPtr getApiByName(const std::string &name)
Lookup in the table.
Definition rbac_api.cc:35
virtual bool match(const std::string &command) final
Match the access control list.
Definition rbac_acl.h:333
CommandsAcl(const std::set< std::string > &commands)
Constructor.
Definition rbac_acl.h:318
void add(const std::string &command)
Add a command name.
Definition rbac_acl.h:325
const std::set< std::string > & getCommands() const
Returns the command names.
Definition rbac_acl.h:340
virtual bool match(const std::string &command) final
Match the access control list.
Definition rbac_acl.h:392
HookAcl(const std::string &hook)
Constructor.
Definition rbac_acl.h:386
std::string getHook() const
Returns the hook.
Definition rbac_acl.h:400
virtual bool match(const std::string &) final
Match the access control list.
Definition rbac_acl.h:306
NoneAcl()
Constructor.
Definition rbac_acl.h:300
virtual bool match(const std::string &command) final
Match the access control list.
Definition rbac_acl.h:159
NotAcl(const AclPtr &acl)
Constructor.
Definition rbac_acl.h:149
const AclPtr & getAcl() const
Returns the acl.
Definition rbac_acl.h:166
OrAcl(const AclList &acls)
Constructor.
Definition rbac_acl.h:235
void add(const AclPtr &acl)
Add an access control list.
Definition rbac_acl.h:247
const AclList & getAcls() const
Returns the acls.
Definition rbac_acl.h:270
virtual bool match(const std::string &command) final
Match the access control list.
Definition rbac_acl.h:258
#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< Acl > AclPtr
Type of shared pointers to Acl.
Definition rbac_acl.h:29
std::map< std::string, AclPtr > AclTable
Type of the access control list table.
Definition rbac_acl.h:35
boost::shared_ptr< Api > ApiPtr
Type of shared pointer to Api.
Definition rbac_api.h:26
std::list< AclPtr > AclList
Type of list of shared pointers to Acl.
Definition rbac_acl.h:32
AclTable aclTable
The access control list table.
Definition rbac_acl.cc:23
Defines the logger used by the top-level component of kea-lfc.