summaryrefslogtreecommitdiff
path: root/jstests/ssl/ssl_uri.js
blob: 9d43217a46bf73e5f887b89a78678adc08528bf2 (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
// Test that the ssl=true/false option is honored in shell URIs.

(function() {
    "use strict";

    var shouldSucceed = function(uri) {
        var conn = new Mongo(uri);
        var res = conn.getDB('admin').runCommand({"ismaster": 1});
        assert(res.ok);
    };

    var shouldFail = function(uri) {
        assert.throws(function(uri) {
            var conn = new Mongo(uri);
        }, [uri], "network error while attempting to run command");
    };

    // Start up a mongod with ssl required.
    var sslMongo = MongoRunner.runMongod({
        sslMode: "requireSSL",
        sslPEMKeyFile: "jstests/libs/server.pem",
        sslCAFile: "jstests/libs/ca.pem",
    });

    var sslURI = "mongodb://localhost:" + sslMongo.port + "/admin";

    // When talking to a server with SSL, connecting with ssl=false fails.
    shouldSucceed(sslURI);
    shouldSucceed(sslURI + "?ssl=true");
    shouldFail(sslURI + "?ssl=false");

    var connectWithURI = function(uri) {
        return runMongoProgram('./mongo',
                               '--ssl',
                               '--sslAllowInvalidCertificates',
                               '--sslCAFile',
                               'jstests/libs/ca.pem',
                               '--sslPEMKeyFile',
                               'jstests/libs/client.pem',
                               uri,
                               '--eval',
                               'db.runCommand({ismaster: 1})');
    };

    var shouldConnect = function(uri) {
        assert.eq(connectWithURI(uri), 0, "should have been able to connect with " + uri);
    };

    var shouldNotConnect = function(uri) {
        assert.eq(connectWithURI(uri), 1, "should not have been able to connect with " + uri);
    };

    // When talking to a server with SSL, connecting with ssl=false on the command line fails.
    shouldConnect(sslURI);
    shouldNotConnect(sslURI + "?ssl=false");
    shouldConnect(sslURI + "?ssl=true");

    // Connecting with ssl=true without --ssl will not work
    var res =
        runMongoProgram('./mongo', sslURI + "?ssl=true", '--eval', 'db.runCommand({ismaster: 1})');
    assert.eq(res, 1, "should not have been able to connect without --ssl");

    // Clean up
    MongoRunner.stopMongod(sslMongo);
}());