Skip to content

Commit 4531cab

Browse files
committed
PSMDB-1922 Require authorization for auditGetOptions command
1 parent 55a0be8 commit 4531cab

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// test that auditGetOptions command works as expected
2+
3+
if (TestData.testData !== undefined) {
4+
load(TestData.testData + '/audit/_audit_helpers.js');
5+
} else {
6+
load('jstests/audit/_audit_helpers.js');
7+
}
8+
9+
const testDBName = "audit_getoptions_command";
10+
11+
auditTest('auditGetOptions', function(m) {
12+
let adminDB = m.getDB('admin');
13+
let testDB = m.getDB(testDBName);
14+
createAdminUserForAudit(m);
15+
createNoPermissionUserForAudit(m, adminDB);
16+
17+
// Admin user logs in
18+
adminDB.auth('admin', 'admin');
19+
// Should fail if not executed on 'admin' database
20+
assert.commandFailedWithCode(testDB.runCommand({'auditGetOptions': 1}),
21+
ErrorCodes.Unauthorized);
22+
assert.commandWorked(adminDB.runCommand({'auditGetOptions': 1}));
23+
adminDB.logout();
24+
25+
// User (tom) with no permissions logs in.
26+
assert(adminDB.auth('tom', 'tom'));
27+
// Should fail if current user has no 'getParameter' privilege
28+
assert.commandFailedWithCode(testDB.runCommand({'auditGetOptions': 1}),
29+
ErrorCodes.Unauthorized);
30+
assert.commandFailedWithCode(adminDB.runCommand({'auditGetOptions': 1}),
31+
ErrorCodes.Unauthorized);
32+
adminDB.logout();
33+
}, {auth: ""});

src/mongo/db/audit/audit_commands.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,19 @@ class AuditGetOptionsCommand : public AuditCommand {
133133
"Example: { auditGetOptions: 1 }";
134134
}
135135

136-
Status checkAuthForOperation(OperationContext*,
137-
const DatabaseName&,
136+
bool adminOnly() const override {
137+
return true;
138+
}
139+
140+
Status checkAuthForOperation(OperationContext* opCtx,
141+
const DatabaseName& dbName,
138142
const BSONObj&) const override {
143+
auto* as = AuthorizationSession::get(opCtx->getClient());
144+
if (!as->isAuthorizedForActionsOnResource(ResourcePattern::forClusterResource(),
145+
ActionType::getParameter)) {
146+
return {ErrorCodes::Unauthorized, "unauthorized"};
147+
}
148+
139149
return Status::OK();
140150
}
141151

0 commit comments

Comments
 (0)