summaryrefslogtreecommitdiff
path: root/jstests/ssl/x509_invalid.js
blob: 77098e7d64dde448c4fd1e366498f74192665f1a (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
// Test X509 auth when --sslAllowInvalidCertificates is enabled

(function() {
    'use strict';

    const CLIENT_NAME = 'C=US,ST=New York,L=New York City,O=MongoDB,OU=KernelUser,CN=client';
    const CLIENT_CERT = 'jstests/libs/client.pem';
    const SERVER_CERT = 'jstests/libs/server.pem';
    const CA_CERT = 'jstests/libs/ca.pem';
    const SELF_SIGNED_CERT = 'jstests/libs/client-self-signed.pem';

    function testClient(conn, cert, name, shouldSucceed) {
        const auth = {
            mechanism: 'MONGODB-X509',
            user: name
        };
        const script = 'assert(db.getSiblingDB(\'$external\').auth(' + tojson(auth) + '));';
        clearRawMongoProgramOutput();
        const exitCode = runMongoProgram('mongo',
                                         '--ssl',
                                         '--sslAllowInvalidHostnames',
                                         '--sslPEMKeyFile',
                                         cert,
                                         '--sslCAFile',
                                         CA_CERT,
                                         '--port',
                                         conn.port,
                                         '--eval',
                                         script);

        assert.eq(shouldSucceed, exitCode === 0, "exitCode = " + tojson(exitCode));
        assert.eq(
            !shouldSucceed,
            rawMongoProgramOutput().includes('No verified subject name available from client'));
    }

    function runTest(conn) {
        const admin = conn.getDB('admin');
        admin.createUser({user: "admin", pwd: "admin", roles: ["root"]});
        admin.auth('admin', 'admin');

        const external = conn.getDB('$external');
        external.createUser({user: CLIENT_NAME, roles: [{'role': 'readWrite', 'db': 'test'}]});

        testClient(conn, CLIENT_CERT, CLIENT_NAME, true);
        testClient(conn, SELF_SIGNED_CERT, CLIENT_NAME, false);
    }

    // Standalone.
    const mongod = MongoRunner.runMongod({
        auth: '',
        sslMode: 'requireSSL',
        sslPEMKeyFile: SERVER_CERT,
        sslCAFile: CA_CERT,
        sslAllowInvalidCertificates: '',
    });
    runTest(mongod);
    MongoRunner.stopMongod(mongod);
})();