summaryrefslogtreecommitdiff
path: root/jstests/auth/authentication_restrictions.js
blob: e69a411795df26e7fdd72946523133e09da6db16 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
 * This test checks that authentication restrictions can be set and respected.
 * @tags: [requires_sharding, requires_replication]
 */

(function() {
    'use strict';

    function testConnection(
        conn, eventuallyConsistentConn, sleepUntilUserDataPropagated, sleepUntilUserDataRefreshed) {
        load("jstests/libs/host_ipaddr.js");

        // Create a session which observes an eventually consistent view of user data
        var eventualDb = eventuallyConsistentConn.getDB("admin");

        // Create a session for modifying user data during the life of the test
        var adminSession = new Mongo("localhost:" + conn.port);
        var admin = adminSession.getDB("admin");
        assert.commandWorked(admin.runCommand(
            {createUser: "admin", pwd: "admin", roles: [{role: "root", db: "admin"}]}));
        assert(admin.auth("admin", "admin"));

        // Create a strongly consistent session for consuming user data
        var db = conn.getDB("admin");

        // Create a strongly consistent session for consuming user data, with a non-localhost
        // source IP.
        var externalMongo = new Mongo(get_ipaddr() + ":" + conn.port);
        var externalDb = externalMongo.getDB("admin");

        print("=== Feature compatibility tests");
        assert.commandWorked(admin.runCommand({setFeatureCompatibilityVersion: "3.4"}));

        print(
            "Given a server with 3.4 featureCompatibilityVersions, it is not possible to make a user with authenticationRestrictions");
        assert.commandFailed(admin.runCommand({
            createUser: "user",
            pwd: "user",
            roles: [],
            authenticationRestrictions: [{clientSource: ["127.0.0.1"]}]
        }));
        assert.commandWorked(admin.runCommand({createUser: "user", pwd: "user", roles: []}));
        assert.commandFailed(admin.runCommand(
            {updateUser: "user", authenticationRestrictions: [{serverAddress: ["127.0.0.1"]}]}));

        print(
            "When the server is upgraded to 3.6 featureCompatibilityVersion, users may be created with authenticationRestrictions");
        assert.commandWorked(admin.runCommand({setFeatureCompatibilityVersion: "3.6"}));
        assert.commandWorked(admin.runCommand({
            createUser: "user2",
            pwd: "user",
            roles: [],
            authenticationRestrictions: [{clientSource: ["127.0.0.1"]}]
        }));
        assert.commandWorked(admin.runCommand({createUser: "user3", pwd: "user", roles: []}));
        assert.commandWorked(admin.runCommand(
            {updateUser: "user3", authenticationRestrictions: [{serverAddress: ["127.0.0.1"]}]}));

        print("=== User creation tests");
        print(
            "When a client creates users with empty authenticationRestrictions, the operation succeeds, though it has no effect");
        assert.commandWorked(admin.runCommand(
            {createUser: "user4", pwd: "user", roles: [], authenticationRestrictions: []}));
        assert(!Object.keys(admin.system.users.findOne({user: "user4"}))
                    .includes("authenticationRestrictions"));

        print(
            "When a client updates a user's authenticationRestrictions to be empty, the operation succeeds, and removes the authenticationRestrictions field");
        assert.commandWorked(admin.runCommand({createUser: "user5", pwd: "user", roles: []}));
        assert.commandWorked(
            admin.runCommand({updateUser: "user5", authenticationRestrictions: []}));
        assert(!Object.keys(admin.system.users.findOne({user: "user5"}))
                    .includes("authenticationRestrictions"));
        assert.commandWorked(admin.runCommand(
            {updateUser: "user5", authenticationRestrictions: [{clientSource: ["127.0.0.1"]}]}));
        assert(Object.keys(admin.system.users.findOne({user: "user5"}))
                   .includes("authenticationRestrictions"));
        assert.commandWorked(
            admin.runCommand({updateUser: "user5", authenticationRestrictions: []}));
        assert(!Object.keys(admin.system.users.findOne({user: "user5"}))
                    .includes("authenticationRestrictions"));

        print(
            "When a client updates a user's authenticationRestrictions to be null or undefined, the operation fails");
        assert.commandWorked(admin.runCommand(
            {updateUser: "user5", authenticationRestrictions: [{clientSource: ["127.0.0.1"]}]}));
        assert(Object.keys(admin.system.users.findOne({user: "user5"}))
                   .includes("authenticationRestrictions"));
        assert.commandFailed(
            admin.runCommand({updateUser: "user5", authenticationRestrictions: null}));
        assert(Object.keys(admin.system.users.findOne({user: "user5"}))
                   .includes("authenticationRestrictions"));
        assert.commandFailed(
            admin.runCommand({updateUser: "user5", authenticationRestrictions: undefined}));
        assert(Object.keys(admin.system.users.findOne({user: "user5"}))
                   .includes("authenticationRestrictions"));

        print(
            "When a client creates users, it may use clientSource and serverAddress authenticationRestrictions");
        assert.commandWorked(admin.runCommand({
            createUser: "user6",
            pwd: "user",
            roles: [],
            authenticationRestrictions: [{clientSource: ["127.0.0.1"]}]
        }));
        assert.commandWorked(admin.runCommand({
            createUser: "user7",
            pwd: "user",
            roles: [],
            authenticationRestrictions: [{serverAddress: ["127.0.0.1"]}]
        }));
        assert.commandWorked(admin.runCommand({
            createUser: "user8",
            pwd: "user",
            roles: [],
            authenticationRestrictions:
                [{clientSource: ["127.0.0.1"], serverAddress: ["127.0.0.1"]}]
        }));
        assert.commandWorked(admin.runCommand({
            createUser: "user9",
            pwd: "user",
            roles: [],
            authenticationRestrictions:
                [{clientSource: ["127.0.0.1"]}, {serverAddress: ["127.0.0.1"]}]
        }));
        assert.commandFailed(admin.runCommand({
            createUser: "user10",
            pwd: "user",
            roles: [],
            authenticationRestrictions: [{invalidRestriction: ["127.0.0.1"]}]
        }));

        print("=== Localhost access tests");

        print(
            "When a client on the loopback authenticates to a user with {clientSource: \"127.0.0.1\"}, it will succeed");
        assert(db.auth("user6", "user"));

        print(
            "When a client on the loopback authenticates to a user with {serverAddress: \"127.0.0.1\"}, it will succeed");
        assert(db.auth("user7", "user"));

        print(
            "When a client on the loopback authenticates to a user with {clientSource: \"127.0.0.1\", serverAddress: \"127.0.0.1\"}, it will succeed");
        assert(db.auth("user8", "user"));

        print("=== Remote access tests");
        print(
            "When a client on the external interface authenticates to a user with {clientSource: \"127.0.0.1\"}, it will fail");
        assert(!externalDb.auth("user6", "user"));

        print(
            "When a client on the external interface authenticates to a user with {serverAddress: \"127.0.0.1\"}, it will fail");
        assert(!externalDb.auth("user7", "user"));

        print(
            "When a client on the external interface authenticates to a user with {clientSource: \"127.0.0.1\", serverAddress: \"127.0.0.1\"}, it will fail");
        assert(!externalDb.auth("user8", "user"));

        print("=== Invalidation tests");
        print(
            "When a client removes all authenticationRestrictions from a user, authentication will succeed");
        assert.commandWorked(admin.runCommand({
            createUser: "user11",
            pwd: "user",
            roles: [],
            authenticationRestrictions:
                [{clientSource: ["127.0.0.1"], serverAddress: ["127.0.0.1"]}]
        }));
        assert(!externalDb.auth("user11", "user"));
        assert.commandWorked(
            admin.runCommand({updateUser: "user11", authenticationRestrictions: []}));
        assert(externalDb.auth("user11", "user"));

        print(
            "When a client sets authenticationRestrictions on a user, authorization privileges are revoked");
        assert.commandWorked(admin.runCommand(
            {createUser: "user12", pwd: "user", roles: [{role: "readWrite", db: "test"}]}));

        assert(db.auth("user12", "user"));
        assert.commandWorked(db.getSiblingDB("test").runCommand({find: "foo", batchSize: 0}));

        sleepUntilUserDataPropagated();
        assert(eventualDb.auth("user12", "user"));
        assert.commandWorked(
            eventualDb.getSiblingDB("test").runCommand({find: "foo", batchSize: 0}));

        assert.commandWorked(admin.runCommand(
            {updateUser: "user12", authenticationRestrictions: [{clientSource: ["192.0.2.0"]}]}));

        assert.commandFailed(db.getSiblingDB("test").runCommand({find: "foo", batchSize: 0}));

        sleepUntilUserDataRefreshed();
        assert.commandFailed(
            eventualDb.getSiblingDB("test").runCommand({find: "foo", batchSize: 0}));

        print(
            "When a client downgrades featureCompatibilityVersion, authenticationRestrictions are still enforced");
        assert.commandWorked(admin.runCommand({
            createUser: "user13",
            pwd: "user",
            roles: [{role: "readWrite", db: "test"}],
            authenticationRestrictions: [{clientSource: ["127.0.0.1"]}]
        }));
        assert(db.auth("user13", "user"));
        assert.commandWorked(db.getSiblingDB("test").runCommand({find: "foo", batchSize: 0}));

        sleepUntilUserDataPropagated();
        assert(eventualDb.auth("user13", "user"));
        assert.commandWorked(
            eventualDb.getSiblingDB("test").runCommand({find: "foo", batchSize: 0}));

        assert.commandWorked(admin.runCommand({setFeatureCompatibilityVersion: "3.4"}));

        sleepUntilUserDataRefreshed();
        assert(db.auth("user13", "user"));
        assert.commandWorked(db.getSiblingDB("test").runCommand({find: "foo", batchSize: 0}));
        assert(eventualDb.auth("user13", "user"));
        assert.commandWorked(
            eventualDb.getSiblingDB("test").runCommand({find: "foo", batchSize: 0}));
        assert(!externalDb.auth("user13", "user"));
    }

    print("Testing standalone");
    var conn = MongoRunner.runMongod({bind_ip_all: "", auth: ""});
    testConnection(conn, conn, function() {}, function() {});
    MongoRunner.stopMongod(conn);

    var keyfile = "jstests/libs/key1";

    print("Testing replicaset");
    var rst = new ReplSetTest(
        {name: 'testset', nodes: 2, nodeOptions: {bind_ip_all: "", auth: ""}, keyFile: keyfile});
    var nodes = rst.startSet();
    rst.initiate();
    rst.awaitSecondaryNodes();
    var awaitReplication = function() {
        authutil.asCluster(nodes, "jstests/libs/key1", function() {
            rst.awaitReplication();
        });
    };

    testConnection(rst.getPrimary(), rst.getSecondary(), awaitReplication, awaitReplication);
    rst.stopSet();

    print("Testing sharded cluster");
    var st = new ShardingTest({
        mongos: 2,
        config: 3,
        shard: 1,
        keyFile: keyfile,
        other: {
            mongosOptions: {bind_ip_all: "", auth: null},
            configOptions: {auth: null},
            shardOptions: {auth: null}
        }
    });
    testConnection(st.s0,
                   st.s1,
                   function() {},
                   function() {
                       sleep(40 * 1000);  // Wait for mongos user cache invalidation
                   });
    st.stop();

}());