summaryrefslogtreecommitdiff
path: root/jstests/auth/speculative-auth-sharding.js
blob: d8bcae94ed9e8bb95da28addb506c3eea99c0f1f (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
// Verify that clients can speculatively authenticate to mongos.
// @tags: [requires_sharding]

(function() {
'use strict';

const fallbackMech = 'SCRAM-SHA-256';
const keyfile = 'jstests/libs/key1';
const st = new ShardingTest({
    mongos: 1,
    keyFile: keyfile,
    other: {mongosOptions: {auth: null}, configOptions: {auth: null}, shardOptions: {auth: null}}
});

const admin = st.s.getDB('admin');
admin.createUser({user: 'admin', pwd: 'pwd', roles: ['root']});
admin.auth('admin', 'pwd');

let lastStats =
    assert.commandWorked(admin.runCommand({serverStatus: 1})).security.authentication.mechanisms;
jsTest.log('Inintial stats: ' + lastStats);

function test(uri, incrMech, isClusterAuth = false) {
    jsTest.log('Connecting to: ' + uri);
    assert.eq(runMongoProgram('mongo', uri, '--eval', ';'), 0);

    const stats = assert.commandWorked(admin.runCommand({serverStatus: 1}))
                      .security.authentication.mechanisms;
    try {
        assert.eq(Object.keys(lastStats).length, Object.keys(stats).length);
        Object.keys(lastStats).forEach(function(mech) {
            const inc = (mech === incrMech) ? 1 : 0;
            const clusterInc = (mech === incrMech && isClusterAuth) ? 1 : 0;

            const specBefore = lastStats[mech].speculativeAuthenticate;
            const specAfter = stats[mech].speculativeAuthenticate;
            assert.eq(specAfter.received, specBefore.received + inc);
            assert.eq(specAfter.successful, specBefore.successful + inc);

            const clusterBefore = lastStats[mech].clusterAuthenticate;
            const clusterAfter = stats[mech].clusterAuthenticate;
            assert.eq(clusterAfter.received, clusterBefore.received + clusterInc);
            assert.eq(clusterAfter.successful, clusterBefore.successful + clusterInc);

            const allBefore = lastStats[mech].authenticate;
            const allAfter = stats[mech].authenticate;
            assert.eq(allAfter.received, allBefore.received + inc);
            assert.eq(allAfter.successful, allBefore.successful + inc);
        });
    } catch (e) {
        print("Stats: " + tojson(stats));
        throw e;
    }
    lastStats = stats;
}

const baseURI = 'mongodb://admin:pwd@' + st.s.host + '/admin';

test(baseURI, fallbackMech);
test(baseURI + '?authMechanism=SCRAM-SHA-1', 'SCRAM-SHA-1');
test(baseURI + '?authMechanism=SCRAM-SHA-256', 'SCRAM-SHA-256');
const systemPass = cat(keyfile).replace(/\s/g, '');
test('mongodb://__system:' + systemPass + '@' + st.s.host + '/admin?authMechanisms=SCRAM-SHA-256',
     'SCRAM-SHA-256',
     true);

admin.logout();
st.stop();
}());