summaryrefslogtreecommitdiff
path: root/jstests/ssl/cluster_x509_rotate.js
blob: 07bad91738c9bc4504d9f0e41cac563ee81c1354 (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
// Check that rotation works for the cluster certificate

(function() {
"use strict";

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

const dbPath = MongoRunner.toRealDir("$dataDir/cluster_x509_rotate_test/");
mkdir(dbPath);

copyCertificateFile("jstests/libs/ca.pem", dbPath + "/ca-test.pem");
copyCertificateFile("jstests/libs/client.pem", dbPath + "/client-test.pem");
copyCertificateFile("jstests/libs/server.pem", dbPath + "/server-test.pem");

// Make replset with old certificates, rotate to new certificates, and try to add
// a node with new certificates.
const rst = new ReplSetTest({nodes: 2});
rst.startSet({
    sslMode: "requireSSL",
    sslPEMKeyFile: dbPath + "/server-test.pem",
    sslCAFile: dbPath + "/ca-test.pem",
    sslClusterFile: dbPath + "/client-test.pem",
    sslAllowInvalidHostnames: "",
});

rst.initiate();
rst.awaitReplication();

copyCertificateFile("jstests/libs/trusted-ca.pem", dbPath + "/ca-test.pem");
copyCertificateFile("jstests/libs/trusted-client.pem", dbPath + "/client-test.pem");
copyCertificateFile("jstests/libs/trusted-server.pem", dbPath + "/server-test.pem");

for (let node of rst.nodes) {
    assert.commandWorked(node.adminCommand({rotateCertificates: 1}));
}

const newnode = rst.add({
    sslMode: "requireSSL",
    sslPEMKeyFile: "jstests/libs/trusted-server.pem",
    sslCAFile: "jstests/libs/trusted-ca.pem",
    sslClusterFile: "jstests/libs/trusted-client.pem",
    sslAllowInvalidHostnames: "",
    // IMPORTANT: shell will not be able to talk to the new node due to cert rotation
    // therefore we set "waitForConnect:false" to ensure shell does not try to acess it
    waitForConnect: false,
});

// Emulate waitForConnect so we wait for new node to come up before killing rst
const host = "localhost:" + newnode.port;
assert.soon(() => {
    print(`Testing that ${host} is up`);
    return 0 ===
        runMongoProgram("mongo",
                        "--ssl",
                        "--sslAllowInvalidHostnames",
                        "--host",
                        host,
                        "--sslPEMKeyFile",
                        "jstests/libs/trusted-client.pem",
                        "--sslCAFile",
                        "jstests/libs/trusted-ca.pem",
                        "--eval",
                        ";");
});

print("Reinitiating replica set");
rst.reInitiate();

assert.soon(() => {
    print(`Waiting for ${host} to join replica set`);
    return 0 ===
        runMongoProgram("mongo",
                        "--ssl",
                        "--sslAllowInvalidHostnames",
                        "--host",
                        host,
                        "--sslPEMKeyFile",
                        "jstests/libs/trusted-client.pem",
                        "--sslCAFile",
                        "jstests/libs/trusted-ca.pem",
                        "--eval",
                        `const PRIMARY = 1;
                        const SECONDARY = 2;
                        const s = db.adminCommand({replSetGetStatus: 1});
                        if (!s.ok) {
                            print('replSetGetStatus is not ok');
                            quit(1);
                        }
                        if (s.myState != PRIMARY && s.myState != SECONDARY) {
                            print('node is not primary or secondary');
                            quit(1);
                        };
                        print('node is online and in cluster');
                        `);
});

// Make sure each node can connect to each other node
for (let node of rst.nodeList()) {
    for (let target of rst.nodeList()) {
        if (node !== target) {
            print(`Testing connectivity of ${node} to ${target}`);
            assert.eq(0,
                      runMongoProgram(
                          "mongo",
                          "--ssl",
                          "--sslAllowInvalidHostnames",
                          "--host",
                          node,
                          "--sslPEMKeyFile",
                          "jstests/libs/trusted-client.pem",
                          "--sslCAFile",
                          "jstests/libs/trusted-ca.pem",
                          "--eval",
                          `assert.commandWorked(db.adminCommand({replSetTestEgress: 1, target: "${
                              target}", timeoutSecs: NumberInt(15)}));`));
        }
    }
}

rst.stopSet();
}());