1
// Test the db.auth() shell helper.
3
var conn = MongoRunner.runMongod({ smallfiles: "", auth: "" });
5
var mechanisms, hasMongoCR, hasCramMd5;
7
// Find out if this build supports the authenticationMechanisms startup parameter. If it does,
8
// restart with MONGODB-CR and CRAM-MD5 mechanisms enabled.
9
var cmdOut = conn.getDB('admin').runCommand({getParameter: 1, authenticationMechanisms: 1})
11
MongoRunner.stopMongod(conn);
12
conn = MongoRunner.runMongod({ restart: conn,
13
setParameter: "authenticationMechanisms=MONGODB-CR,CRAM-MD5" });
14
mechanisms = [ "MONGODB-CR", "CRAM-MD5" ];
17
print("test info: Enabling non-default authentication mechanisms.");
20
mechanisms = [ "MONGODB-CR" ];
23
print("test info: Using only default authentication mechanism, MONGODB-CR.");
26
var admin = conn.getDB('admin');
28
var testedSomething = false;
30
admin.addUser('andy', 'a');
32
// If the server supports them MONGODB-CR, try all the ways to call db.auth that use MONGODB-CR.
34
testedSomething = true;
35
assert(admin.auth('andy', 'a'));
37
assert(admin.auth({user: 'andy', pwd: 'a'}));
39
assert(admin.auth({mechanism: 'MONGODB-CR', user: 'andy', pwd: 'a'}));
43
// If the server supports CRAM-MD5, try it out.
45
testedSomething = true;
46
assert(admin.auth({mechanism: 'CRAM-MD5', user: 'andy', pwd: 'a'}));
50
// Sanity check that we tested at least one of MONGODB-CR and CRAM-MD5.
51
assert(testedSomething, "No candidate authentication mechanisms matched.");
53
// Invalid mechanisms shouldn't lead to authentication, but also shouldn't crash.
54
assert(!admin.auth({mechanism: 'this-mechanism-is-fake', user: 'andy', pwd: 'a'}));