summaryrefslogtreecommitdiff
path: root/jstests/auth/sharding_scram_cache.js
blob: b40ed420075df0bc964bc21cced36db6f0e4eb3a (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
/**
 * Validate client side scram cache is used
 *
 * Ensure that for each cache miss, an entry is added
 *
 * @tags: [requires_sharding]
 */
(function() {
'use strict';

function setup(conn) {
    const adminDB = conn.getDB('admin');

    adminDB.createUser({user: 'root', pwd: "pwd", roles: ["root", "clusterMonitor"]});
}

function runTests(conn) {
    const adminDB = conn.getDB('admin');

    assert.eq(1, adminDB.auth('root', "pwd"));

    adminDB.collTest.insert({x: 1});

    const ss = adminDB.serverStatus();
    jsTestLog(tojson(ss));

    const sc = ss.scramCache;

    // Validate that for each miss, we get an entry
    // If the cache was not used, the missed count would exceed the count of entries by a
    // significant margin
    assert.gte(sc["SCRAM-SHA-1"].count + 2, sc["SCRAM-SHA-1"].misses);

    assert.gte(sc["SCRAM-SHA-256"].count + 2, sc["SCRAM-SHA-256"].misses);

    assert(sc["SCRAM-SHA-1"].count > 0 || sc["SCRAM-SHA-256"].count > 0,
           "Cache was not used at all");

    adminDB.logout();
}

const st = new ShardingTest({shards: 1, keyFile: 'jstests/libs/key1'});
setup(st.s);

// Validate mongos
runTests(st.s);

// Validate a RS member
runTests(st.configRS.getPrimary());

st.stop();
})();