summaryrefslogtreecommitdiff
path: root/jstests/ssl/tlsCATrusts.js
blob: bf12c80f78e5a7f3e2ae3c1a896111d0c9ab16e3 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Test restricting role authorization via X509 extensions.
load('jstests/ssl/libs/ssl_helpers.js');

requireSSLProvider('openssl', function() {
    "use strict";

    const SERVER_CERT = 'jstests/libs/server.pem';
    const COMBINED_CA_CERT = 'jstests/ssl/x509/root-and-trusted-ca.pem';
    const CA_HASH = '539D91F8202641BF85C0C36C88FF69F3062D4AB370CECBF9B950A8B97DE72EAE';
    const TRUSTED_CA_HASH = 'AEAEBB1BA947A7C1428D39EF6166B83409D0245D28013C9FDD71DF9E69BEA52B';

    // Common suffix, keep the lines short.
    const RDN_SUFFIX = ',O=MongoDB,L=New York City,ST=New York,C=US';
    const USERS = [];

    const CLIENT = {
        cert: 'jstests/libs/client.pem',
        roles: [],
    };
    USERS.push('CN=client,OU=KernelUser');

    const CLIENT_ROLES = {
        cert: 'jstests/libs/client_roles.pem',
        roles: [{role: 'backup', db: 'admin'}, {role: 'readAnyDatabase', db: 'admin'}],
    };
    USERS.push('CN=Kernel Client Peer Role,OU=Kernel Users');

    const TRUSTED_CLIENT_TESTDB_ROLES = {
        cert: 'jstests/ssl/x509/trusted-client-testdb-roles.pem',
        roles: [{role: 'role1', db: 'testDB'}, {role: 'role2', db: 'testDB'}],
    };
    USERS.push('CN=Trusted Kernel Test Client With Roles,OU=Kernel Users');

    function test(tlsCATrusts, success, failure) {
        const options = {
            auth: '',
            tlsMode: 'requireTLS',
            tlsCertificateKeyFile: SERVER_CERT,
            tlsCAFile: COMBINED_CA_CERT,
        };

        if (tlsCATrusts !== null) {
            options.setParameter = {
                tlsCATrusts: tojson(tlsCATrusts),
            };
        }

        const mongod = MongoRunner.runMongod(options);

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

        const external = mongod.getDB('$external');
        USERS.forEach((u) => external.createUser({user: u + RDN_SUFFIX, roles: []}));

        const testDB = mongod.getDB('test');
        testDB.createRole({role: 'role1', privileges: [], roles: []});
        testDB.createRole({role: 'role2', privileges: [], roles: []});

        // Sorting JS arrays of objects with arbitrary order is... complex.
        const serverTrusts =
            assert.commandWorked(admin.runCommand({getParameter: 1, tlsCATrusts: 1})).tlsCATrusts;
        function sortAndNormalizeRoles(roles) {
            return roles.map((r) => r.role + '.' + r.db).sort().join('/');
        }
        function sortAndNormalizeTrusts(trusts) {
            if (trusts === null) {
                return "(unconfigured)";
            }
            return trusts.map((t) => t.sha256 + '/' + sortAndNormalizeRoles(t.roles)).sort();
        }
        assert.eq(sortAndNormalizeTrusts(tlsCATrusts), sortAndNormalizeTrusts(serverTrusts));

        function impl(user, expect) {
            const snRoles = tojson(sortAndNormalizeRoles(user.roles));
            const uri = 'mongodb://localhost:' + mongod.port + '/admin';
            const script = tojson(sortAndNormalizeRoles) +
                'assert(db.getSiblingDB("$external").auth({mechanism: "MONGODB-X509"}));' +
                'const status = assert.commandWorked(db.runCommand({connectionStatus: 1}));' +
                'const roles = status.authInfo.authenticatedUserRoles;' +
                'assert.eq(' + snRoles + ', sortAndNormalizeRoles(roles));';
            const mongo = runMongoProgram('mongo',
                                          '--tls',
                                          '--tlsCertificateKeyFile',
                                          user.cert,
                                          '--tlsCAFile',
                                          COMBINED_CA_CERT,
                                          uri,
                                          '--eval',
                                          script);
            expect(mongo, 0);
        }

        success.forEach((u) => impl(u, assert.eq));
        failure.forEach((u) => impl(u, assert.neq));

        MongoRunner.stopMongod(mongod);
    }

    // Positive tests.
    const unconfigured = null;
    test(unconfigured, [CLIENT, CLIENT_ROLES, TRUSTED_CLIENT_TESTDB_ROLES], []);

    const allRoles = [
        {sha256: CA_HASH, roles: [{role: '', db: ''}]},
        {sha256: TRUSTED_CA_HASH, roles: [{role: '', db: ''}]}
    ];
    test(allRoles, [CLIENT, CLIENT_ROLES, TRUSTED_CLIENT_TESTDB_ROLES], []);

    const allRolesOnAdmin = [{sha256: CA_HASH, roles: [{role: '', db: 'admin'}]}];
    test(allRolesOnAdmin, [CLIENT, CLIENT_ROLES], [TRUSTED_CLIENT_TESTDB_ROLES]);

    const specificRolesOnAnyDB =
        [{sha256: CA_HASH, roles: [{role: 'backup', db: ''}, {role: 'readAnyDatabase', db: ''}]}];
    test(specificRolesOnAnyDB, [CLIENT, CLIENT_ROLES], [TRUSTED_CLIENT_TESTDB_ROLES]);

    const exactRoles = [{
        sha256: CA_HASH,
        roles: [{role: 'backup', db: 'admin'}, {role: 'readAnyDatabase', db: 'admin'}]
    }];
    test(exactRoles, [CLIENT, CLIENT_ROLES], [TRUSTED_CLIENT_TESTDB_ROLES]);

    const extraRoles = [{
        sha256: CA_HASH,
        roles: [
            {role: 'backup', db: 'admin'},
            {role: 'readAnyDatabase', db: 'admin'},
            {role: 'readWrite', db: 'admin'}
        ]
    }];
    test(extraRoles, [CLIENT, CLIENT_ROLES], [TRUSTED_CLIENT_TESTDB_ROLES]);

    const similarRoles = [
        {
            sha256: CA_HASH,
            roles: [
                {role: 'backup', db: 'test'},
                {role: 'readAnyDatabase', db: ''},
                {role: 'backup', db: 'admin'}
            ]
        },
        {
            sha256: TRUSTED_CA_HASH,
            roles: [
                {role: 'role1', db: 'admin'},
                {role: 'role2', db: 'testDB'},
                {role: 'role1', db: 'testDB'},
            ]
        }
    ];
    test(similarRoles, [CLIENT, CLIENT_ROLES, TRUSTED_CLIENT_TESTDB_ROLES], []);

    const withUntrusted =
        [{sha256: CA_HASH, roles: [{role: '', db: ''}]}, {sha256: TRUSTED_CA_HASH, roles: []}];
    test(withUntrusted, [CLIENT, CLIENT_ROLES], [TRUSTED_CLIENT_TESTDB_ROLES]);

    const customRoles = [{
        sha256: TRUSTED_CA_HASH,
        roles: [
            {role: 'role1', db: 'testDB'},
            {role: 'role2', db: 'testDB'},
        ]
    }];
    test(customRoles, [CLIENT, TRUSTED_CLIENT_TESTDB_ROLES], [CLIENT_ROLES]);

    // Negative tests. CLIENT_CERT is okay because it doesn't ask for roles.
    const noTrustedCAs = [];
    test(noTrustedCAs, [CLIENT], [CLIENT_ROLES, TRUSTED_CLIENT_TESTDB_ROLES]);

    const noRoles = [{sha256: CA_HASH, roles: []}];
    test(noRoles, [CLIENT], [CLIENT_ROLES, TRUSTED_CLIENT_TESTDB_ROLES]);

    const insufficientRoles1 = [
        {sha256: CA_HASH, roles: [{role: 'backup', db: ''}]},
        {sha256: TRUSTED_CA_HASH, roles: [{role: 'role1', db: 'testDB'}]}
    ];
    test(insufficientRoles1, [CLIENT], [CLIENT_ROLES, TRUSTED_CLIENT_TESTDB_ROLES]);

    const insufficientRoles2 = [
        {sha256: CA_HASH, roles: [{role: 'readWriteAnyDatabase', db: ''}]},
        {sha256: TRUSTED_CA_HASH, roles: [{role: 'role2', db: 'testDB'}]}
    ];
    test(insufficientRoles2, [CLIENT], [CLIENT_ROLES, TRUSTED_CLIENT_TESTDB_ROLES]);

    const withTrusted =
        [{sha256: CA_HASH, roles: []}, {sha256: TRUSTED_CA_HASH, roles: [{role: '', db: ''}]}];
    test(withTrusted, [CLIENT, TRUSTED_CLIENT_TESTDB_ROLES], [CLIENT_ROLES]);
});