summaryrefslogtreecommitdiff
path: root/jstests/ssl/ssl_cert_selector.js
blob: f53c784f7991851540ae95ff5faa5dc4fd5797ae (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
/**
 * Validate that the shell can load certificates from the certificate store and connect to the
 * server.
 */

load('jstests/ssl/libs/ssl_helpers.js');

requireSSLProvider('windows', function() {
    'use strict';

    if (_isWindows()) {
        // SChannel backed follows Windows rules and only trusts Root in LocalMachine
        runProgram("certutil.exe", "-addstore", "-f", "Root", "jstests\\libs\\trusted-ca.pem");

        // Import a pfx file since it contains both a cert and private key and is easy to import
        // via command line.
        runProgram("certutil.exe",
                   "-importpfx",
                   "-f",
                   "-p",
                   "qwerty",
                   "jstests\\libs\\trusted-client.pfx");
    }

    const conn = MongoRunner.runMongod(
        {sslMode: 'requireSSL', sslPEMKeyFile: "jstests\\libs\\trusted-server.pem"});

    const testWithCert = function(certSelector) {
        jsTest.log(`Testing with SSL cert ${certSelector}`);
        const argv = [
            'mongo',
            '--ssl',
            '--sslCertificateSelector',
            certSelector,
            '--port',
            conn.port,
            '--eval',
            'db.runCommand({buildInfo: 1})'
        ];

        const exitStatus = runMongoProgram.apply(null, argv);
        assert.eq(exitStatus, 0, "successfully connected with SSL");
    };

    const trusted_client_thumbprint = cat('jstests/libs/trusted-client.pem.digest.sha1');

    assert.doesNotThrow(function() {
        testWithCert("thumbprint=" + trusted_client_thumbprint);
    });

    assert.doesNotThrow(function() {
        testWithCert("subject=Trusted Kernel Test Client");
    });

    MongoRunner.stopMongod(conn);
});