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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
|
/**
* Copyright (C) 2013 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects
* for all of the code used other than as permitted herein. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you do not
* wish to do so, delete this exception statement from your version. If you
* delete this exception statement from all source files in the program,
* then also delete it in the license file.
*/
#include "mongo/db/auth/role_graph.h"
#include "mongo/base/init.h"
#include "mongo/base/status.h"
#include "mongo/db/auth/authorization_manager.h"
#include "mongo/db/auth/privilege.h"
#include "mongo/db/auth/role_name.h"
namespace mongo {
const std::string RoleGraph::BUILTIN_ROLE_V0_READ = "read";
const std::string RoleGraph::BUILTIN_ROLE_V0_READ_WRITE = "dbOwner";
const std::string RoleGraph::BUILTIN_ROLE_V0_ADMIN_READ = "readAnyDatabase";
const std::string RoleGraph::BUILTIN_ROLE_V0_ADMIN_READ_WRITE = "root";
namespace {
const std::string ADMIN_DBNAME = "admin";
const std::string BUILTIN_ROLE_READ = "read";
const std::string BUILTIN_ROLE_READ_WRITE = "readWrite";
const std::string BUILTIN_ROLE_USER_ADMIN = "userAdmin";
const std::string BUILTIN_ROLE_DB_ADMIN = "dbAdmin";
const std::string BUILTIN_ROLE_CLUSTER_ADMIN = "clusterAdmin";
const std::string BUILTIN_ROLE_READ_ANY_DB = "readAnyDatabase";
const std::string BUILTIN_ROLE_READ_WRITE_ANY_DB = "readWriteAnyDatabase";
const std::string BUILTIN_ROLE_USER_ADMIN_ANY_DB = "userAdminAnyDatabase";
const std::string BUILTIN_ROLE_DB_ADMIN_ANY_DB = "dbAdminAnyDatabase";
const std::string BUILTIN_ROLE_ROOT = "root";
const std::string BUILTIN_ROLE_INTERNAL = "__system";
const std::string BUILTIN_ROLE_DB_OWNER = "dbOwner";
const std::string BUILTIN_ROLE_CLUSTER_MONITOR = "clusterMonitor";
const std::string BUILTIN_ROLE_HOST_MANAGEMENT = "hostManager";
const std::string BUILTIN_ROLE_CLUSTER_MANAGEMENT = "clusterManager";
const std::string BUILTIN_ROLE_BACKUP = "backup";
const std::string BUILTIN_ROLE_RESTORE = "restore";
const std::string BUILTIN_ROLE_ENABLE_SHARDING = "enableSharding";
/// Actions that the "read" role may perform on a normal resources of a specific database, and
/// that the "readAnyDatabase" role may perform on normal resources of any database.
ActionSet readRoleActions;
/// Actions that the "readWrite" role may perform on a normal resources of a specific database,
/// and that the "readWriteAnyDatabase" role may perform on normal resources of any database.
ActionSet readWriteRoleActions;
/// Actions that the "userAdmin" role may perform on normal resources of a specific database,
/// and that the "userAdminAnyDatabase" role may perform on normal resources of any database.
ActionSet userAdminRoleActions;
/// Actions that the "dbAdmin" role may perform on normal resources of a specific database,
// and that the "dbAdminAnyDatabase" role may perform on normal resources of any database.
ActionSet dbAdminRoleActions;
/// Actions that the "clusterMonitor" role may perform on the cluster resource.
ActionSet clusterMonitorRoleClusterActions;
/// Actions that the "clusterMonitor" role may perform on any database.
ActionSet clusterMonitorRoleDatabaseActions;
/// Actions that the "hostManager" role may perform on the cluster resource.
ActionSet hostManagerRoleClusterActions;
/// Actions that the "hostManager" role may perform on any database.
ActionSet hostManagerRoleDatabaseActions;
/// Actions that the "clusterManager" role may perform on the cluster resource.
ActionSet clusterManagerRoleClusterActions;
/// Actions that the "clusterManager" role may perform on any database
ActionSet clusterManagerRoleDatabaseActions;
ActionSet& operator<<(ActionSet& target, ActionType source) {
target.addAction(source);
return target;
}
void operator+=(ActionSet& target, const ActionSet& source) {
target.addAllActionsFromSet(source);
}
// This sets up the built-in role ActionSets. This is what determines what actions each role
// is authorized to perform
// Note: we suppress clang-format for this function because we want each enum value on a separate
// line
// clang-format off
MONGO_INITIALIZER(AuthorizationBuiltinRoles)(InitializerContext* context) {
// Read role
readRoleActions
<< ActionType::collStats
<< ActionType::dbHash
<< ActionType::dbStats
<< ActionType::find
<< ActionType::killCursors
<< ActionType::listCollections
<< ActionType::listIndexes
<< ActionType::planCacheRead;
// Read-write role
readWriteRoleActions += readRoleActions;
readWriteRoleActions
<< ActionType::convertToCapped // db admin gets this also
<< ActionType::createCollection // db admin gets this also
<< ActionType::dropCollection
<< ActionType::dropIndex
<< ActionType::emptycapped
<< ActionType::createIndex
<< ActionType::insert
<< ActionType::remove
<< ActionType::renameCollectionSameDB // db admin gets this also
<< ActionType::update;
// User admin role
userAdminRoleActions
<< ActionType::changeCustomData
<< ActionType::changePassword
<< ActionType::createUser
<< ActionType::createRole
<< ActionType::dropUser
<< ActionType::dropRole
<< ActionType::grantRole
<< ActionType::revokeRole
<< ActionType::viewUser
<< ActionType::viewRole;
// DB admin role
dbAdminRoleActions
<< ActionType::bypassDocumentValidation
<< ActionType::collMod
<< ActionType::collStats // clusterMonitor gets this also
<< ActionType::compact
<< ActionType::convertToCapped // read_write gets this also
<< ActionType::createCollection // read_write gets this also
<< ActionType::dbStats // clusterMonitor gets this also
<< ActionType::dropCollection
<< ActionType::dropDatabase // clusterAdmin gets this also TODO(spencer): should
// readWriteAnyDatabase?
<< ActionType::dropIndex
<< ActionType::createIndex
<< ActionType::enableProfiler
<< ActionType::listCollections
<< ActionType::listIndexes
<< ActionType::planCacheIndexFilter
<< ActionType::planCacheRead
<< ActionType::planCacheWrite
<< ActionType::reIndex
<< ActionType::renameCollectionSameDB // read_write gets this also
<< ActionType::repairDatabase
<< ActionType::storageDetails
<< ActionType::validate;
// clusterMonitor role actions that target the cluster resource
clusterMonitorRoleClusterActions
<< ActionType::connPoolStats
<< ActionType::getCmdLineOpts
<< ActionType::getLog
<< ActionType::getParameter
<< ActionType::getShardMap
<< ActionType::hostInfo
<< ActionType::listDatabases
<< ActionType::listShards // clusterManager gets this also
<< ActionType::netstat
<< ActionType::replSetGetConfig // clusterManager gets this also
<< ActionType::replSetGetStatus // clusterManager gets this also
<< ActionType::serverStatus
<< ActionType::top
<< ActionType::inprog
<< ActionType::shardingState;
// clusterMonitor role actions that target a database (or collection) resource
clusterMonitorRoleDatabaseActions
<< ActionType::collStats // dbAdmin gets this also
<< ActionType::dbStats // dbAdmin gets this also
<< ActionType::getShardVersion
<< ActionType::indexStats;
// hostManager role actions that target the cluster resource
hostManagerRoleClusterActions
<< ActionType::applicationMessage // clusterManager gets this also
<< ActionType::connPoolSync
<< ActionType::cpuProfiler
<< ActionType::logRotate
<< ActionType::setParameter
<< ActionType::shutdown
<< ActionType::touch
<< ActionType::unlock
<< ActionType::diagLogging
<< ActionType::flushRouterConfig // clusterManager gets this also
<< ActionType::fsync
<< ActionType::invalidateUserCache // userAdminAnyDatabase gets this also
<< ActionType::killop
<< ActionType::replSetResizeOplog
<< ActionType::resync; // clusterManager gets this also
// hostManager role actions that target the database resource
hostManagerRoleDatabaseActions
<< ActionType::killCursors
<< ActionType::repairDatabase;
// clusterManager role actions that target the cluster resource
clusterManagerRoleClusterActions
<< ActionType::appendOplogNote // backup gets this also
<< ActionType::applicationMessage // hostManager gets this also
<< ActionType::replSetConfigure
<< ActionType::replSetGetConfig // clusterMonitor gets this also
<< ActionType::replSetGetStatus // clusterMonitor gets this also
<< ActionType::replSetStateChange
<< ActionType::resync // hostManager gets this also
<< ActionType::addShard
<< ActionType::removeShard
<< ActionType::listShards // clusterMonitor gets this also
<< ActionType::flushRouterConfig // hostManager gets this also
<< ActionType::cleanupOrphaned;
clusterManagerRoleDatabaseActions
<< ActionType::splitChunk
<< ActionType::moveChunk
<< ActionType::enableSharding
<< ActionType::splitVector;
return Status::OK();
}
// clang-format on
void addReadOnlyDbPrivileges(PrivilegeVector* privileges, StringData dbName) {
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forDatabaseName(dbName), readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(NamespaceString(dbName, "system.indexes")),
readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(NamespaceString(dbName, "system.js")),
readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(NamespaceString(dbName, "system.namespaces")),
readRoleActions));
}
void addReadWriteDbPrivileges(PrivilegeVector* privileges, StringData dbName) {
addReadOnlyDbPrivileges(privileges, dbName);
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forDatabaseName(dbName), readWriteRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(NamespaceString(dbName, "system.js")),
readWriteRoleActions));
}
void addUserAdminDbPrivileges(PrivilegeVector* privileges, StringData dbName) {
privileges->push_back(
Privilege(ResourcePattern::forDatabaseName(dbName), userAdminRoleActions));
}
void addDbAdminDbPrivileges(PrivilegeVector* privileges, StringData dbName) {
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forDatabaseName(dbName), dbAdminRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(NamespaceString(dbName, "system.indexes")),
readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(NamespaceString(dbName, "system.namespaces")),
readRoleActions));
ActionSet profileActions = readRoleActions;
profileActions.addAction(ActionType::convertToCapped);
profileActions.addAction(ActionType::createCollection);
profileActions.addAction(ActionType::dropCollection);
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(NamespaceString(dbName, "system.profile")),
profileActions));
}
void addDbOwnerPrivileges(PrivilegeVector* privileges, StringData dbName) {
addReadWriteDbPrivileges(privileges, dbName);
addDbAdminDbPrivileges(privileges, dbName);
addUserAdminDbPrivileges(privileges, dbName);
}
void addEnableShardingPrivileges(PrivilegeVector* privileges) {
ActionSet enableShardingActions;
enableShardingActions.addAction(ActionType::enableSharding);
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyNormalResource(), enableShardingActions));
}
void addReadOnlyAnyDbPrivileges(PrivilegeVector* privileges) {
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyNormalResource(), readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forClusterResource(), ActionType::listDatabases));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.indexes"), readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forCollectionName("system.js"), readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.namespaces"), readRoleActions));
}
void addReadWriteAnyDbPrivileges(PrivilegeVector* privileges) {
addReadOnlyAnyDbPrivileges(privileges);
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyNormalResource(), readWriteRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.js"), readWriteRoleActions));
}
void addUserAdminAnyDbPrivileges(PrivilegeVector* privileges) {
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyNormalResource(), userAdminRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forClusterResource(), ActionType::listDatabases));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forClusterResource(), ActionType::authSchemaUpgrade));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forClusterResource(), ActionType::invalidateUserCache));
ActionSet readRoleAndIndexActions;
readRoleAndIndexActions += readRoleActions;
readRoleAndIndexActions << ActionType::createIndex << ActionType::dropIndex;
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forCollectionName("system.users"), readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(
ResourcePattern::forExactNamespace(AuthorizationManager::usersCollectionNamespace),
readRoleAndIndexActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(
ResourcePattern::forExactNamespace(AuthorizationManager::rolesCollectionNamespace),
readRoleAndIndexActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(
ResourcePattern::forExactNamespace(AuthorizationManager::versionCollectionNamespace),
readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(
ResourcePattern::forExactNamespace(AuthorizationManager::usersAltCollectionNamespace),
readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(
AuthorizationManager::usersBackupCollectionNamespace),
readRoleActions));
}
void addDbAdminAnyDbPrivileges(PrivilegeVector* privileges) {
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forClusterResource(), ActionType::listDatabases));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyNormalResource(), dbAdminRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.indexes"), readRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.namespaces"), readRoleActions));
ActionSet profileActions = readRoleActions;
profileActions.addAction(ActionType::convertToCapped);
profileActions.addAction(ActionType::createCollection);
profileActions.addAction(ActionType::dropCollection);
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.profile"), profileActions));
}
void addClusterMonitorPrivileges(PrivilegeVector* privileges) {
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forClusterResource(), clusterMonitorRoleClusterActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forAnyNormalResource(), clusterMonitorRoleDatabaseActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forDatabaseName("config"), clusterMonitorRoleDatabaseActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forDatabaseName("local"), clusterMonitorRoleDatabaseActions));
addReadOnlyDbPrivileges(privileges, "local");
addReadOnlyDbPrivileges(privileges, "config");
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(NamespaceString("local", "system.replset")),
ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.profile"), ActionType::find));
}
void addHostManagerPrivileges(PrivilegeVector* privileges) {
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forClusterResource(), hostManagerRoleClusterActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forAnyNormalResource(), hostManagerRoleDatabaseActions));
}
void addClusterManagerPrivileges(PrivilegeVector* privileges) {
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forClusterResource(), clusterManagerRoleClusterActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forAnyNormalResource(), clusterManagerRoleDatabaseActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forDatabaseName("config"), clusterManagerRoleDatabaseActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forDatabaseName("local"), clusterManagerRoleDatabaseActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(NamespaceString("local", "system.replset")),
readRoleActions));
addReadOnlyDbPrivileges(privileges, "config");
ActionSet writeActions;
writeActions << ActionType::insert << ActionType::update << ActionType::remove;
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forDatabaseName("config"), writeActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forDatabaseName("local"), writeActions));
// Fake collection used for setFeatureCompatibilityVersion permissions.
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(
NamespaceString("$setFeatureCompatibilityVersion", "version")),
writeActions));
}
void addClusterAdminPrivileges(PrivilegeVector* privileges) {
addClusterMonitorPrivileges(privileges);
addHostManagerPrivileges(privileges);
addClusterManagerPrivileges(privileges);
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyNormalResource(), ActionType::dropDatabase));
}
void addBackupPrivileges(PrivilegeVector* privileges) {
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyResource(), ActionType::collStats));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyNormalResource(), ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyResource(), ActionType::listCollections));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyResource(), ActionType::listIndexes));
ActionSet clusterActions;
clusterActions << ActionType::getParameter // To check authSchemaVersion
<< ActionType::listDatabases << ActionType::appendOplogNote; // For BRS
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forClusterResource(), clusterActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forDatabaseName("config"), ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forDatabaseName("local"), ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.indexes"), ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.namespaces"), ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forCollectionName("system.js"), ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.users"), ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.profile"), ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(
ResourcePattern::forExactNamespace(AuthorizationManager::usersAltCollectionNamespace),
ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(
AuthorizationManager::usersBackupCollectionNamespace),
ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(
ResourcePattern::forExactNamespace(AuthorizationManager::rolesCollectionNamespace),
ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(
ResourcePattern::forExactNamespace(AuthorizationManager::versionCollectionNamespace),
ActionType::find));
ActionSet configSettingsActions;
configSettingsActions << ActionType::insert << ActionType::update << ActionType::find;
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(NamespaceString("config", "settings")),
configSettingsActions));
}
void addRestorePrivileges(PrivilegeVector* privileges) {
ActionSet actions;
actions << ActionType::bypassDocumentValidation << ActionType::collMod
<< ActionType::createCollection << ActionType::createIndex << ActionType::dropCollection
<< ActionType::insert;
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyNormalResource(), actions));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forCollectionName("system.js"), actions));
// Need to be able to query system.namespaces to check existing collection options.
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forCollectionName("system.namespaces"), ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyResource(), ActionType::listCollections));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forDatabaseName("config"), actions));
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forDatabaseName("local"), actions));
// Privileges for user/role management
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyNormalResource(), userAdminRoleActions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(
AuthorizationManager::defaultTempUsersCollectionNamespace),
ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(
AuthorizationManager::defaultTempRolesCollectionNamespace),
ActionType::find));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(
ResourcePattern::forExactNamespace(AuthorizationManager::usersAltCollectionNamespace),
actions));
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(ResourcePattern::forExactNamespace(
AuthorizationManager::usersBackupCollectionNamespace),
actions));
actions << ActionType::find;
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(
ResourcePattern::forExactNamespace(AuthorizationManager::versionCollectionNamespace),
actions));
// Need additional actions on system.users.
actions << ActionType::update << ActionType::remove;
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forCollectionName("system.users"), actions));
// Need to be able to run getParameter to check authSchemaVersion
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forClusterResource(), ActionType::getParameter));
// Need to be able to create an index on the system.roles collection.
Privilege::addPrivilegeToPrivilegeVector(
privileges,
Privilege(
ResourcePattern::forExactNamespace(AuthorizationManager::rolesCollectionNamespace),
ActionType::createIndex));
}
void addRootRolePrivileges(PrivilegeVector* privileges) {
addClusterAdminPrivileges(privileges);
addUserAdminAnyDbPrivileges(privileges);
addDbAdminAnyDbPrivileges(privileges);
addReadWriteAnyDbPrivileges(privileges);
addBackupPrivileges(privileges);
addRestorePrivileges(privileges);
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forAnyResource(), ActionType::validate));
}
void addInternalRolePrivileges(PrivilegeVector* privileges) {
RoleGraph::generateUniversalPrivileges(privileges);
}
void addAnyBuiltinRolePrivileges(PrivilegeVector* privileges) {
Privilege::addPrivilegeToPrivilegeVector(
privileges, Privilege(ResourcePattern::forClusterResource(), ActionType::startSession));
}
} // namespace
bool RoleGraph::addPrivilegesForBuiltinRole(const RoleName& roleName, PrivilegeVector* result) {
const bool isAdminDB = (roleName.getDB() == ADMIN_DBNAME);
if (roleName.getRole() == BUILTIN_ROLE_READ) {
addReadOnlyDbPrivileges(result, roleName.getDB());
} else if (roleName.getRole() == BUILTIN_ROLE_READ_WRITE) {
addReadWriteDbPrivileges(result, roleName.getDB());
} else if (roleName.getRole() == BUILTIN_ROLE_USER_ADMIN) {
addUserAdminDbPrivileges(result, roleName.getDB());
} else if (roleName.getRole() == BUILTIN_ROLE_DB_ADMIN) {
addDbAdminDbPrivileges(result, roleName.getDB());
} else if (roleName.getRole() == BUILTIN_ROLE_DB_OWNER) {
addDbOwnerPrivileges(result, roleName.getDB());
} else if (roleName.getRole() == BUILTIN_ROLE_ENABLE_SHARDING) {
addEnableShardingPrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_READ_ANY_DB) {
addReadOnlyAnyDbPrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_READ_WRITE_ANY_DB) {
addReadWriteAnyDbPrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_USER_ADMIN_ANY_DB) {
addUserAdminAnyDbPrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_DB_ADMIN_ANY_DB) {
addDbAdminAnyDbPrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_CLUSTER_MONITOR) {
addClusterMonitorPrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_HOST_MANAGEMENT) {
addHostManagerPrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_CLUSTER_MANAGEMENT) {
addClusterManagerPrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_CLUSTER_ADMIN) {
addClusterAdminPrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_BACKUP) {
addBackupPrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_RESTORE) {
addRestorePrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_ROOT) {
addRootRolePrivileges(result);
} else if (isAdminDB && roleName.getRole() == BUILTIN_ROLE_INTERNAL) {
addInternalRolePrivileges(result);
} else {
return false;
}
// One of the roles has matched, otherwise we would have returned already.
addAnyBuiltinRolePrivileges(result);
return true;
}
void RoleGraph::generateUniversalPrivileges(PrivilegeVector* privileges) {
ActionSet allActions;
allActions.addAllActions();
privileges->push_back(Privilege(ResourcePattern::forAnyResource(), allActions));
}
bool RoleGraph::isBuiltinRole(const RoleName& role) {
if (!NamespaceString::validDBName(role.getDB(),
NamespaceString::DollarInDbNameBehavior::Allow) ||
role.getDB() == "$external") {
return false;
}
bool isAdminDB = role.getDB() == ADMIN_DBNAME;
if (role.getRole() == BUILTIN_ROLE_READ) {
return true;
} else if (role.getRole() == BUILTIN_ROLE_READ_WRITE) {
return true;
} else if (role.getRole() == BUILTIN_ROLE_USER_ADMIN) {
return true;
} else if (role.getRole() == BUILTIN_ROLE_DB_ADMIN) {
return true;
} else if (role.getRole() == BUILTIN_ROLE_DB_OWNER) {
return true;
} else if (role.getRole() == BUILTIN_ROLE_ENABLE_SHARDING) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_READ_ANY_DB) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_READ_WRITE_ANY_DB) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_USER_ADMIN_ANY_DB) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_DB_ADMIN_ANY_DB) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_CLUSTER_MONITOR) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_HOST_MANAGEMENT) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_CLUSTER_MANAGEMENT) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_CLUSTER_ADMIN) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_BACKUP) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_RESTORE) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_ROOT) {
return true;
} else if (isAdminDB && role.getRole() == BUILTIN_ROLE_INTERNAL) {
return true;
}
return false;
}
void RoleGraph::_createBuiltinRolesForDBIfNeeded(const std::string& dbname) {
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_READ, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_READ_WRITE, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_USER_ADMIN, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_DB_ADMIN, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_DB_OWNER, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_ENABLE_SHARDING, dbname));
if (dbname == "admin") {
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_READ_ANY_DB, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_READ_WRITE_ANY_DB, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_USER_ADMIN_ANY_DB, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_DB_ADMIN_ANY_DB, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_CLUSTER_MONITOR, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_HOST_MANAGEMENT, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_CLUSTER_MANAGEMENT, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_CLUSTER_ADMIN, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_BACKUP, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_RESTORE, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_ROOT, dbname));
_createBuiltinRoleIfNeeded(RoleName(BUILTIN_ROLE_INTERNAL, dbname));
}
}
void RoleGraph::_createBuiltinRoleIfNeeded(const RoleName& role) {
if (!isBuiltinRole(role) || _roleExistsDontCreateBuiltin(role)) {
return;
}
_createRoleDontCheckIfRoleExists(role);
PrivilegeVector privileges;
fassert(17145, addPrivilegesForBuiltinRole(role, &privileges));
for (size_t i = 0; i < privileges.size(); ++i) {
_addPrivilegeToRoleNoChecks(role, privileges[i]);
_allPrivilegesForRole[role].push_back(privileges[i]);
}
}
} // namespace mongo
|