1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542 | // Copyright (C) 2022-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.
#include <config.h>
#include <rbac_acl.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/gtest_utils.h>
using namespace isc;
using namespace isc::data;
using namespace isc::rbac;
using namespace std;
namespace {
/// @brief Test fixture for checking RBAC access control lists.
class AclTest : public ::testing::Test {
public:
/// @brief Constructor.
AclTest() {
aclTable.clear();
Acl::initTable();
apiTable.clear();
}
/// @brief Destructor.
virtual ~AclTest() {
aclTable.clear();
apiTable.clear();
}
};
/// @brief This test verifies that commands access control list works
/// as expected.
TEST_F(AclTest, commands) {<--- syntax error
set<string> commands = { "foo", "bar" };
AclPtr acl(new CommandsAcl(commands));
ASSERT_TRUE(acl);
EXPECT_EQ("commands", acl->getClassName());
boost::shared_ptr<CommandsAcl> nacl =
boost::dynamic_pointer_cast<CommandsAcl>(acl);
ASSERT_TRUE(nacl);
set<string> got = nacl->getCommands();
EXPECT_EQ(got, commands);
EXPECT_TRUE(acl->match("foo"));
EXPECT_TRUE(acl->match("bar"));
EXPECT_FALSE(acl->match("xyz"));
ASSERT_NO_THROW(nacl->add("foo"));
EXPECT_EQ(2, nacl->getCommands().size());
ASSERT_NO_THROW(nacl->add("xyz"));
EXPECT_EQ(3, nacl->getCommands().size());
EXPECT_TRUE(acl->match("xyz"));
}
/// @brief This test verifies that all access control list works as expected.
TEST_F(AclTest, all) {
AclPtr acl(new AllAcl());
ASSERT_TRUE(acl);
EXPECT_EQ("all", acl->getClassName());
EXPECT_TRUE(acl->match("foo"));
EXPECT_TRUE(acl->match("bar"));
EXPECT_TRUE(acl->match("xyz"));
EXPECT_TRUE(acl->match("-e-v-e-r-y-t-h-i-n-g-"));
// Get the ALL ACL from the table.
acl = aclTable["ALL"];
ASSERT_TRUE(acl);
EXPECT_EQ("all", acl->getClassName());
EXPECT_TRUE(acl->match("foo"));
EXPECT_TRUE(acl->match("bar"));
EXPECT_TRUE(acl->match("xyz"));
EXPECT_TRUE(acl->match("-e-v-e-r-y-t-h-i-n-g-"));
}
/// @brief This test verifies that none access control list works as expected.
TEST_F(AclTest, none) {
AclPtr acl(new NoneAcl());
ASSERT_TRUE(acl);
EXPECT_EQ("none", acl->getClassName());
EXPECT_FALSE(acl->match("foo"));
EXPECT_FALSE(acl->match("bar"));
EXPECT_FALSE(acl->match("xyz"));
EXPECT_FALSE(acl->match("-e-v-e-r-y-t-h-i-n-g-"));
// Get the NONE ACL from the table.
acl = aclTable["NONE"];
ASSERT_TRUE(acl);
EXPECT_EQ("none", acl->getClassName());
EXPECT_FALSE(acl->match("foo"));
EXPECT_FALSE(acl->match("bar"));
EXPECT_FALSE(acl->match("xyz"));
EXPECT_FALSE(acl->match("-e-v-e-r-y-t-h-i-n-g-"));
}
/// @brief This test verifies that alias access control list works as expected.
TEST_F(AclTest, alias) {
set<string> commands = { "foo", "bar" };
AclPtr acl0(new CommandsAcl(commands));
AclPtr acl;
EXPECT_THROW_MSG(acl.reset(new AliasAcl("bad", AclPtr())), Unexpected,
"null acl in alias");
EXPECT_NO_THROW(acl.reset(new AliasAcl("my-alias", acl0)));
ASSERT_TRUE(acl);
EXPECT_EQ("alias", acl->getClassName());
boost::shared_ptr<AliasAcl> aacl =
boost::dynamic_pointer_cast<AliasAcl>(acl);
ASSERT_TRUE(aacl);
EXPECT_EQ("my-alias", aacl->getName());
AclPtr got = aacl->getAcl();
ASSERT_TRUE(got);
EXPECT_EQ(got->getClassName(), acl0->getClassName());
EXPECT_TRUE(acl->match("foo"));
EXPECT_TRUE(acl->match("bar"));
EXPECT_FALSE(acl->match("xyz"));
}
/// @brief This test verifies that not access control list works as expected.
TEST_F(AclTest, not) {
set<string> commands = { "foo", "bar" };
AclPtr acl0(new CommandsAcl(commands));
AclPtr acl;
EXPECT_THROW_MSG(acl.reset(new NotAcl(AclPtr())), Unexpected,
"null acl in not");
EXPECT_NO_THROW(acl.reset(new NotAcl(acl0)));
ASSERT_TRUE(acl);
EXPECT_EQ("not", acl->getClassName());
boost::shared_ptr<NotAcl> nacl =
boost::dynamic_pointer_cast<NotAcl>(acl);
ASSERT_TRUE(nacl);
AclPtr got = nacl->getAcl();
ASSERT_TRUE(got);
EXPECT_EQ(got->getClassName(), acl0->getClassName());
EXPECT_FALSE(acl->match("foo"));
EXPECT_FALSE(acl->match("bar"));
EXPECT_TRUE(acl->match("xyz"));
}
/// @brief This test verifies that and access control list works as expected.
TEST_F(AclTest, and) {
AclList acll;
acll.push_back(AclPtr());
AclPtr acl;
EXPECT_THROW_MSG(acl.reset(new AndAcl(acll)), Unexpected,
"null acl in and");
acll.clear();
EXPECT_NO_THROW(acl.reset(new AndAcl(acll)));
ASSERT_TRUE(acl);
EXPECT_EQ("and", acl->getClassName());
boost::shared_ptr<AndAcl> aacl =
boost::dynamic_pointer_cast<AndAcl>(acl);
ASSERT_TRUE(aacl);
AclList got = aacl->getAcls();
EXPECT_TRUE(got.empty());
EXPECT_TRUE(acl->match("foo"));
EXPECT_TRUE(acl->match("bar"));
EXPECT_TRUE(acl->match("xyz"));
EXPECT_TRUE(acl->match("-e-v-e-r-y-t-h-i-n-g-"));
set<string> commands1 = { "foo", "bar" };
AclPtr acl1(new CommandsAcl(commands1));
ASSERT_TRUE(acl1);
EXPECT_NO_THROW(aacl->add(acl1));
EXPECT_EQ(1, aacl->getAcls().size());
EXPECT_TRUE(acl->match("foo"));
EXPECT_TRUE(acl->match("bar"));
EXPECT_FALSE(acl->match("xyz"));
set<string> commands2 = { "foo", "xyz" };
AclPtr acl2(new CommandsAcl(commands2));
ASSERT_TRUE(acl2);
acll.push_back(acl1);
acll.push_back(acl2);
EXPECT_NO_THROW(acl.reset(new AndAcl(acll)));
ASSERT_TRUE(acl);
aacl = boost::dynamic_pointer_cast<AndAcl>(acl);
ASSERT_TRUE(aacl);
EXPECT_EQ(2, aacl->getAcls().size());
EXPECT_TRUE(acl->match("foo"));
EXPECT_FALSE(acl->match("bar"));
EXPECT_FALSE(acl->match("xyz"));
EXPECT_FALSE(acl->match("abc"));
}
/// @brief This test verifies that or access control list works as expected.
TEST_F(AclTest, or) {
AclList acll;
acll.push_back(AclPtr());
AclPtr acl;
EXPECT_THROW_MSG(acl.reset(new OrAcl(acll)), Unexpected,
"null acl in or");
acll.clear();
EXPECT_NO_THROW(acl.reset(new OrAcl(acll)));
ASSERT_TRUE(acl);
EXPECT_EQ("or", acl->getClassName());
boost::shared_ptr<OrAcl> oacl =
boost::dynamic_pointer_cast<OrAcl>(acl);
ASSERT_TRUE(oacl);
AclList got = oacl->getAcls();
EXPECT_TRUE(got.empty());
EXPECT_FALSE(acl->match("foo"));
EXPECT_FALSE(acl->match("bar"));
EXPECT_FALSE(acl->match("xyz"));
EXPECT_FALSE(acl->match("-e-v-e-r-y-t-h-i-n-g-"));
set<string> commands1 = { "foo", "bar" };
AclPtr acl1(new CommandsAcl(commands1));
ASSERT_TRUE(acl1);
EXPECT_NO_THROW(oacl->add(acl1));
EXPECT_EQ(1, oacl->getAcls().size());
EXPECT_TRUE(acl->match("foo"));
EXPECT_TRUE(acl->match("bar"));
EXPECT_FALSE(acl->match("xyz"));
set<string> commands2 = { "foo", "xyz" };
AclPtr acl2(new CommandsAcl(commands2));
ASSERT_TRUE(acl2);
acll.push_back(acl1);
acll.push_back(acl2);
EXPECT_NO_THROW(acl.reset(new OrAcl(acll)));
ASSERT_TRUE(acl);
oacl = boost::dynamic_pointer_cast<OrAcl>(acl);
ASSERT_TRUE(oacl);
EXPECT_EQ(2, oacl->getAcls().size());
EXPECT_TRUE(acl->match("foo"));
EXPECT_TRUE(acl->match("bar"));
EXPECT_TRUE(acl->match("xyz"));
EXPECT_FALSE(acl->match("abc"));
}
/// @brief This test verifies that access access control list works as expected.
TEST_F(AclTest, access) {
ASSERT_NO_THROW(Api::fillApiTable(string(API_DIR)));
AclPtr acl(new AccessAcl("read"));
ASSERT_TRUE(acl);
EXPECT_EQ("access", acl->getClassName());
boost::shared_ptr<AccessAcl> aacl =
boost::dynamic_pointer_cast<AccessAcl>(acl);
ASSERT_TRUE(aacl);
EXPECT_EQ("read", aacl->getAccess());
EXPECT_FALSE(acl->match("foo"));
EXPECT_TRUE(acl->match("list-commands"));
EXPECT_TRUE(acl->match("lease4-get"));
EXPECT_FALSE(acl->match("lease6-add"));
// Get the WRITE ACL from the table.
acl = aclTable["WRITE"];
ASSERT_TRUE(acl);
EXPECT_EQ("access", acl->getClassName());
aacl = boost::dynamic_pointer_cast<AccessAcl>(acl);
ASSERT_TRUE(aacl);
EXPECT_EQ("write", aacl->getAccess());
EXPECT_FALSE(acl->match("foo"));
EXPECT_FALSE(acl->match("list-commands"));
EXPECT_FALSE(acl->match("lease6-get"));
EXPECT_TRUE(acl->match("lease4-add"));
}
/// @brief This test verifies that hook access control list works as expected.
TEST_F(AclTest, hook) {
ASSERT_NO_THROW(Api::fillApiTable(string(API_DIR)));
AclPtr acl(new HookAcl("lease_cmds"));
ASSERT_TRUE(acl);
EXPECT_EQ("hook", acl->getClassName());
boost::shared_ptr<HookAcl> hacl =
boost::dynamic_pointer_cast<HookAcl>(acl);
ASSERT_TRUE(hacl);
EXPECT_EQ("lease_cmds", hacl->getHook());
EXPECT_FALSE(acl->match("foo"));
EXPECT_FALSE(acl->match("list-commands"));
EXPECT_TRUE(acl->match("lease4-get"));
EXPECT_TRUE(acl->match("lease6-add"));
}
/// @brief This test verifies that the access control list table works
/// as expected.
TEST_F(AclTest, table) {
/// ALL, NONE, READ and WRITE.
EXPECT_EQ(4, aclTable.size());
set<string> commands = { "foo", "bar" };
AclPtr acl(new CommandsAcl(commands));
ASSERT_TRUE(acl);
ASSERT_EQ(0, aclTable.count("foobar"));
aclTable["foobar"] = acl;
EXPECT_EQ(5, aclTable.size());
EXPECT_EQ(1, aclTable.count("foobar"));
AclPtr got = aclTable["foobar"];
ASSERT_TRUE(got);
EXPECT_EQ("commands", got->getClassName());
}
/// @brief This test verifies that the access control list name parsing
/// works as expected.
TEST_F(AclTest, parseName) {
ElementPtr cfg;
string expected = "parse null access control list";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
cfg = Element::createList();
expected = "access control list is not a string or a map";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
cfg = Element::create(string("foobar"));
expected = "unknown access control list name 'foobar'";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
set<string> commands = { "foo", "bar" };
AclPtr nacl(new CommandsAcl(commands));
ASSERT_TRUE(nacl);
aclTable["foobar"] = nacl;
AclPtr acl;
EXPECT_NO_THROW(acl = Acl::parse(cfg));
ASSERT_TRUE(acl);
EXPECT_EQ("commands", acl->getClassName());
}
/// @brief This test verifies that the access control list not literal parsing
/// works as expected.
TEST_F(AclTest, parseNot) {
ElementPtr cfg = Element::createMap();
cfg->set("not", Element::create(string("ALL")));
cfg->set("comment", Element::create(string("not ALL is NONE")));
AclPtr acl;
EXPECT_NO_THROW(acl = Acl::parse(cfg));
ASSERT_TRUE(acl);
EXPECT_EQ("not", acl->getClassName());
}
/// @brief This test verifies that the access control list commands literal
/// parsing works as expected.
TEST_F(AclTest, parseCommands) {
ASSERT_NO_THROW(Api::fillApiTable(string(API_DIR)));
ElementPtr cfg = Element::createMap();
cfg->set("commands", Element::create(string("foo")));
string expected = "commands access control list is not a list";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
ElementPtr list = Element::createList();
cfg->set("commands", list);
AclPtr acl;
EXPECT_NO_THROW(acl = Acl::parse(cfg));
ASSERT_TRUE(acl);
EXPECT_EQ("commands", acl->getClassName());
cfg->set("not", Element::create(string("READ")));
expected = "spurious 'not' entry in ";
expected += "{ \"commands\": [ ], \"not\": \"READ\" }";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
acl.reset();
cfg->remove("not");
list->add(Element::create(1));
expected = "commands access control list item is not a string";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
list = Element::createList();
list->add(Element::create(string("foobar")));
cfg->set("commands", list);
expected = "unknown command 'foobar'";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
list = Element::createList();
list->add(Element::create(string("list-commands")));
list->add(Element::create(string("status-get")));
cfg->set("commands", list);
EXPECT_NO_THROW(acl = Acl::parse(cfg));
ASSERT_TRUE(acl);
EXPECT_EQ("commands", acl->getClassName());
EXPECT_FALSE(acl->match("foo"));
EXPECT_TRUE(acl->match("list-commands"));
EXPECT_TRUE(acl->match("status-get"));
EXPECT_FALSE(acl->match("lease4-get"));
}
/// @brief This test verifies that the access control list access literal
/// parsing works as expected.
TEST_F(AclTest, parseAccess) {
ASSERT_NO_THROW(Api::fillApiTable(string(API_DIR)));
ElementPtr cfg = Element::createMap();
cfg->set("access", Element::create(true));
string expected = "access access control list is not a string";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
cfg->set("access", Element::create(string()));
expected = "access control list access is empty";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
cfg->set("access", Element::create(string("foobar")));
expected = "unknown access 'foobar'";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
cfg->set("access", Element::create(string("read")));
AclPtr acl;
EXPECT_NO_THROW(acl = Acl::parse(cfg));
ASSERT_TRUE(acl);
EXPECT_EQ("access", acl->getClassName());
}
/// @brief This test verifies that the access control list hook literal
/// parsing works as expected.
TEST_F(AclTest, parseHook) {
ASSERT_NO_THROW(Api::fillApiTable(string(API_DIR)));
ElementPtr cfg = Element::createMap();
cfg->set("hook", Element::create(123));
string expected = "hook access control list is not a string";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
cfg->set("hook", Element::create(string("foobar")));
expected = "unknown hook 'foobar'";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
cfg->set("hook", Element::create(string()));
AclPtr acl;
EXPECT_NO_THROW(acl = Acl::parse(cfg));
ASSERT_TRUE(acl);
EXPECT_EQ("hook", acl->getClassName());
cfg->set("hook", Element::create(string("lease_cmds")));
acl.reset();
EXPECT_NO_THROW(acl = Acl::parse(cfg));
ASSERT_TRUE(acl);
EXPECT_EQ("hook", acl->getClassName());
}
/// @brief This test verifies that the parsing of a list of access control
/// lists works as expected.
TEST_F(AclTest, parseList) {
ElementPtr cfg;
string expected = "parse null list of access control lists";
EXPECT_THROW_MSG(Acl::parseList(cfg), BadValue, expected);
cfg = Element::createMap();
expected = "list of access control lists is not a list";
EXPECT_THROW_MSG(Acl::parseList(cfg), BadValue, expected);
cfg = Element::createList();
AclList acll;
EXPECT_NO_THROW(acll = Acl::parseList(cfg));
EXPECT_TRUE(acll.empty());
ElementPtr elem1 = Element::create(string("ALL"));
cfg->add(elem1);
ElementPtr elem2 = Element::createMap();
elem2->set("not", Element::create(string("NONE")));
cfg->add(elem2);
EXPECT_NO_THROW(acll = Acl::parseList(cfg));
EXPECT_EQ(2, acll.size());
}
/// @brief This test verifies that the access control list and literal
/// parsing works as expected.
TEST_F(AclTest, parseAnd) {
ElementPtr cfg = Element::createMap();
cfg->set("foo", Element::create(string("bar")));
string expected = "unknown access control list keyword 'foo'";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
cfg = Element::createMap();
ElementPtr list = Element::createList();
ElementPtr elem1 = Element::create(string("ALL"));
list->add(elem1);
ElementPtr elem2 = Element::createMap();
elem2->set("not", Element::create(string("NONE")));
list->add(elem2);
cfg->set("and", list);
AclPtr acl;
EXPECT_NO_THROW(acl = Acl::parse(cfg));
ASSERT_TRUE(acl);
EXPECT_EQ("and", acl->getClassName());
}
/// @brief This test verifies that the access control list or literal
/// parsing works as expected.
TEST_F(AclTest, parseOr) {
ElementPtr cfg = Element::createMap();
string expected = "bad access control list { }";
EXPECT_THROW_MSG(Acl::parse(cfg), BadValue, expected);
ElementPtr list = Element::createList();
ElementPtr elem1 = Element::create(string("ALL"));
list->add(elem1);
ElementPtr elem2 = Element::createMap();
elem2->set("not", Element::create(string("NONE")));
list->add(elem2);
cfg->set("or", list);
AclPtr acl;
EXPECT_NO_THROW(acl = Acl::parse(cfg));
ASSERT_TRUE(acl);
EXPECT_EQ("or", acl->getClassName());
}
/// @brief This test verifies that the access control list definition
/// parsing works as expected.
TEST_F(AclTest, parseDefinition) {
EXPECT_EQ(4, aclTable.size());
ElementPtr def;
string expected = "null access control list definition";
EXPECT_THROW_MSG(AliasAcl::parse(def), BadValue, expected);
def = Element::createList();
expected = "access control list definition is not a map";
EXPECT_THROW_MSG(AliasAcl::parse(def), BadValue, expected);
def = Element::createMap();
def->set("one", Element::create(string("foo")));
def->set("two", Element::create(string("bar")));
expected = "access control list definition must have one entry";
EXPECT_THROW_MSG(AliasAcl::parse(def), BadValue, expected);
def = Element::createMap();
def->set("", Element::create(string("foo")));
expected = "access control list definition name is empty";
EXPECT_THROW_MSG(AliasAcl::parse(def), BadValue, expected);
def = Element::createMap();
ElementPtr val = Element::createMap();
def->set("foo", val);
val->set("access", Element::create(string("write")));
EXPECT_NO_THROW(AliasAcl::parse(def));
EXPECT_EQ(5, aclTable.size());
EXPECT_EQ(1, aclTable.count("foo"));
val->set("access", Element::create(string("read")));
expected = "access control list 'foo' is already defined";
EXPECT_THROW_MSG(AliasAcl::parse(def), BadValue, expected);
}
} // end of anonymous namespace
|