summaryrefslogtreecommitdiff
path: root/jstests/auth/commands_user_defined_roles.js
blob: 3c9b33cd54aa9789e9e72629536008e8127d8ccd (plain)
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
/*

Exhaustive test for authorization of commands with user-defined roles.

The test logic implemented here operates on the test cases defined
in jstests/auth/lib/commands_lib.js.

@tags: [requires_sharding]

*/

// This test involves killing all sessions, which will not work as expected if the kill command is
// sent with an implicit session.
TestData.disableImplicitSessions = true;

// constants
var testUser = "userDefinedRolesTestUser";
var testRole = "userDefinedRolesTestRole";

load("jstests/auth/lib/commands_lib.js");
load("jstests/libs/fail_point_util.js");

function doTestSetup(conn, t, testcase, privileges) {
    const admin = conn.getDB('admin');
    const runOnDb = conn.getDB(testcase.runOnDb);
    const state = authCommandsLib.setup(conn, t, runOnDb);

    assert(admin.auth('admin', 'password'));
    assert.commandWorked(admin.runCommand({updateRole: testRole, privileges: privileges}));
    admin.logout();

    return state;
}

function doTestTeardown(conn, t, testcase, res) {
    const runOnDb = conn.getDB(testcase.runOnDb);
    authCommandsLib.teardown(conn, t, runOnDb, res);
}

/**
 * Run the command specified in 't' with the privileges specified in 'privileges'.
 */
function testProperAuthorization(conn, t, testcase, privileges) {
    const runOnDb = conn.getDB(testcase.runOnDb);
    const state = doTestSetup(conn.sidechannel, t, testcase, privileges);
    authCommandsLib.authenticatedSetup(t, runOnDb);

    let command = t.command;
    if (typeof (command) === "function") {
        command = t.command(state, testcase.commandArgs);
    }
    const res = runOnDb.runCommand(command);

    let out = "";
    if (!testcase.expectFail && (res.ok != 1) && (res.code != commandNotSupportedCode)) {
        // don't error if the test failed with code commandNotSupported since
        // some storage engines don't support some commands.
        out = "command failed with " + tojson(res) + " on db " + testcase.runOnDb +
            " with privileges " + tojson(privileges);
    } else if (testcase.expectFail && res.code == authErrCode) {
        out = "expected authorization success" +
            " but received " + tojson(res) + " on db " + testcase.runOnDb + " with privileges " +
            tojson(privileges);
    }

    doTestTeardown(conn.sidechannel, t, testcase, res);
    return out;
}

function testInsufficientPrivileges(conn, t, testcase, privileges) {
    const runOnDb = conn.getDB(testcase.runOnDb);
    const state = doTestSetup(conn.sidechannel, t, testcase, privileges);
    authCommandsLib.authenticatedSetup(t, runOnDb);

    let command = t.command;
    if (typeof (command) === "function") {
        command = t.command(state, testcase.commandArgs);
    }
    const res = runOnDb.runCommand(command);

    let out = "";
    if ((res.ok == 1) || (res.code != authErrCode)) {
        out = "expected authorization failure " +
            " but received " + tojson(res) + " with privileges " + tojson(privileges);
    }

    doTestTeardown(conn.sidechannel, t, testcase, res);
    return out;
}

function runOneTest(conn, t) {
    const failures = [];
    let msg;

    // Some tests requires mongot, however, setting this failpoint will make search queries to
    // return EOF, that way all the hassle of setting it up can be avoided.
    let disableSearchFailpoint;
    if (t.disableSearch) {
        disableSearchFailpoint = configureFailPoint(conn.rs0 ? conn.rs0.getPrimary() : conn,
                                                    'searchReturnEofImmediately');
    }
    for (let i = 0; i < t.testcases.length; i++) {
        const testcase = t.testcases[i];
        if (!("privileges" in testcase)) {
            continue;
        }

        if (testcase.expectAuthzFailure) {
            msg = testInsufficientPrivileges(conn, t, testcase, testcase.privileges);
            if (msg) {
                failures.push(t.testname + ": " + msg);
            }
            continue;
        }

        if ((testcase.privileges.length == 1 && testcase.privileges[0].actions.length > 1) ||
            testcase.privileges.length > 1) {
            for (var j = 0; j < testcase.privileges.length; j++) {
                const p = testcase.privileges[j];
                const resource = p.resource;
                const actions = p.actions;

                // A particular privilege can explicitly specify that it should not be removed when
                // testing for authorization failure. This accommodates special-case behavior for
                // views in conjunction with the create and collMod commands.
                if (p.removeWhenTestingAuthzFailure === false) {
                    continue;
                }

                for (let k = 0; k < actions.length; k++) {
                    const privDoc = {resource: resource, actions: [actions[k]]};
                    msg = testInsufficientPrivileges(conn, t, testcase, [privDoc]);
                    if (msg) {
                        failures.push(t.testname + ": " + msg);
                    }
                }
            }
        }

        // Test for proper authorization with the privileges specified in the test case.
        msg = testProperAuthorization(conn, t, testcase, testcase.privileges);
        if (msg) {
            failures.push(t.testname + ": " + msg);
        }

        function specialResource(resource) {
            if (!resource) {
                return true;
            }

            // Tests which use {db: "local", collection: "oplog.rs"} will not work with
            // {db: "", collection: "oplog.rs"}. oplog.rs is special, and does not match with
            // forDatabaseName or anyNormalResource ResourcePatterns. The same is true of
            // oplog.$main, but oplog.$main is also an illegal collection name on any database
            // other than local. The other collections checked for here in the local database have
            // the same property as oplog.rs.
            return !resource.db || !resource.collection ||
                resource.collection.startsWith("system.") || resource.db == "local";
        }

        // Test for proper authorization with the test case's privileges where non-system
        // collections are modified to be the empty string.
        msg = testProperAuthorization(conn, t, testcase, testcase.privileges.map(function(priv) {
            // Make a copy of the privilege so as not to modify the original array.
            const modifiedPrivilege = Object.extend({}, priv, true);
            if (modifiedPrivilege.resource.collection && !specialResource(priv.resource)) {
                modifiedPrivilege.resource.collection = "";
            }
            return modifiedPrivilege;
        }));
        if (msg) {
            failures.push(t.testname + ": " + msg);
        }

        // Test for proper authorization with the test case's privileges where the database is the
        // empty string.
        msg = testProperAuthorization(conn, t, testcase, testcase.privileges.map(function(priv) {
            // Make a copy of the privilege so as not to modify the original array.
            const modifiedPrivilege = Object.extend({}, priv, true);
            if (!specialResource(priv.resource)) {
                modifiedPrivilege.resource.db = "";
            }
            return modifiedPrivilege;
        }));
        if (msg) {
            failures.push(t.testname + ": " + msg);
        }
    }

    if (disableSearchFailpoint) {
        disableSearchFailpoint.off();
    }

    return failures;
}

function createUsers(conn) {
    const adminDb = conn.getDB(adminDbName);
    adminDb.createUser({user: "admin", pwd: "password", roles: ["__system"]});

    assert(adminDb.auth("admin", "password"));

    assert.commandWorked(adminDb.runCommand({createRole: testRole, privileges: [], roles: []}));
    assert.commandWorked(adminDb.runCommand(
        {createUser: testUser, pwd: "password", roles: [{role: testRole, db: adminDbName}]}));

    adminDb.logout();

    // Primary connection will now act as test user only.
    assert(adminDb.auth(testUser, "password"));
}

const opts = {
    auth: "",
    enableExperimentalStorageDetailsCmd: ""
};
const impls = {
    createUsers: createUsers,
    runOneTest: runOneTest
};

// run all tests standalone
{
    const conn = MongoRunner.runMongod(opts);

    // Create secondary connection to be intermittently authed
    // with admin privileges for setup/teardown.
    conn.sidechannel = new Mongo(conn.host);
    authCommandsLib.runTests(conn, impls);
    MongoRunner.stopMongod(conn);
}

// run all tests sharded
{
    const conn = new ShardingTest({
        shards: 1,
        mongos: 1,
        config: 1,
        keyFile: "jstests/libs/key1",
        other: {shardOptions: opts}
    });
    conn.sidechannel = new Mongo(conn.s0.host);
    authCommandsLib.runTests(conn, impls);
    conn.stop();
}