diff options
Diffstat (limited to 'jstests/auth/auth-counters.js')
-rw-r--r-- | jstests/auth/auth-counters.js | 85 |
1 files changed, 26 insertions, 59 deletions
diff --git a/jstests/auth/auth-counters.js b/jstests/auth/auth-counters.js index f3f2af6a758..bbb66a619ae 100644 --- a/jstests/auth/auth-counters.js +++ b/jstests/auth/auth-counters.js @@ -3,15 +3,9 @@ (function() { 'use strict'; -const keyfile = 'jstests/libs/key1'; -const badKeyfile = 'jstests/libs/key2'; -let replTest = new ReplSetTest({nodes: 1, keyFile: keyfile, nodeOptions: {auth: ""}}); -replTest.startSet(); -replTest.initiate(); -let primary = replTest.getPrimary(); - -const admin = primary.getDB('admin'); -const test = primary.getDB('test'); +const mongod = MongoRunner.runMongod({auth: ''}); +const admin = mongod.getDB('admin'); +const test = mongod.getDB('test'); admin.createUser({user: 'admin', pwd: 'pwd', roles: ['root'], mechanisms: ['SCRAM-SHA-256']}); admin.auth('admin', 'pwd'); @@ -21,22 +15,21 @@ test.createUser({user: 'user256', pwd: 'pwd', roles: [], mechanisms: ['SCRAM-SHA test.createUser( {user: 'user', pwd: 'pwd', roles: [], mechanisms: ['SCRAM-SHA-1', 'SCRAM-SHA-256']}); -// Count the number of authentications performed during setup -const expected = - assert.commandWorked(admin.runCommand({serverStatus: 1})).security.authentication.mechanisms; +// admin.auth() above provides an initial count for SCRAM-SHA-256 +const expected = { + 'SCRAM-SHA-256': { + received: 1, + successful: 1, + }, +}; function assertStats() { const mechStats = assert.commandWorked(admin.runCommand({serverStatus: 1})) .security.authentication.mechanisms; Object.keys(expected).forEach(function(mech) { try { - assert.eq(mechStats[mech].authenticate.received, expected[mech].authenticate.received); - assert.eq(mechStats[mech].authenticate.successful, - expected[mech].authenticate.successful); - assert.eq(mechStats[mech].clusterAuthenticate.received, - expected[mech].clusterAuthenticate.received); - assert.eq(mechStats[mech].clusterAuthenticate.successful, - expected[mech].clusterAuthenticate.successful); + assert.eq(mechStats[mech].authenticate.received, expected[mech].received); + assert.eq(mechStats[mech].authenticate.successful, expected[mech].successful); } catch (e) { print("Mechanism: " + mech); print("mechStats: " + tojson(mechStats)); @@ -46,42 +39,23 @@ function assertStats() { }); } -function assertSuccess(creds, mech, db = test) { - assert.eq(db.auth(creds), true); - if (db !== admin) { - db.logout(); +function assertSuccess(creds, mech) { + if (expected[mech] === undefined) { + expected[mech] = {received: 0, successful: 0}; } - ++expected[mech].authenticate.received; - ++expected[mech].authenticate.successful; - assertStats(); -} - -function assertFailure(creds, mech, db = test) { - assert.eq(db.auth(creds), false); - ++expected[mech].authenticate.received; + assert.eq(test.auth(creds), true); + test.logout(); + ++expected[mech].received; + ++expected[mech].successful; assertStats(); } -function assertSuccessInternal() { - const mech = "SCRAM-SHA-1"; - // asCluster exiting cleanly indicates successful auth - assert.eq(authutil.asCluster(replTest.nodes, keyfile, () => true), true); - ++expected[mech].authenticate.received; - ++expected[mech].authenticate.successful; - ++expected[mech].clusterAuthenticate.received; - ++expected[mech].clusterAuthenticate.successful; - // we have to re-auth as admin to get stats, which are validated at the end of assertSuccess - assertSuccess({user: 'admin', pwd: 'pwd'}, 'SCRAM-SHA-256', admin); -} - -function assertFailureInternal() { - const mech = "SCRAM-SHA-1"; - // If asCluster fails, it explodes. - assert.throws(authutil.asCluster, [replTest.nodes, badKeyfile, () => true]); - ++expected[mech].authenticate.received; - ++expected[mech].clusterAuthenticate.received; - // we have to re-auth as admin to get stats, which are validated at the end of assertSuccess - assertSuccess({user: 'admin', pwd: 'pwd'}, 'SCRAM-SHA-256', admin); +function assertFailure(creds, mech) { + if (expected[mech] === undefined) { + expected[mech] = {received: 0, successful: 0}; + } + assert.eq(test.auth(creds), false); + ++expected[mech].received; assertStats(); } @@ -112,16 +86,9 @@ assertFailure({user: 'user', pwd: 'haxx', mechanism: 'SCRAM-SHA-1'}, 'SCRAM-SHA- assertFailure({user: 'user1', pwd: 'pwd', mechanism: 'SCRAM-SHA-256'}, 'SCRAM-SHA-256'); assertFailure({user: 'user256', pwd: 'pwd', mechanism: 'SCRAM-SHA-1'}, 'SCRAM-SHA-1'); -// Cluster auth counter checks. -assertSuccessInternal(); -assertFailureInternal(); - -// Need to auth as admin one more time to get final stats. -admin.auth('admin', 'pwd'); - const finalStats = assert.commandWorked(admin.runCommand({serverStatus: 1})).security.authentication.mechanisms; -replTest.stopSet(); +MongoRunner.stopMongod(mongod); printjson(finalStats); })(); |