summaryrefslogtreecommitdiff
path: root/jstests/auth/auth-metrics.js
blob: 68ea5937d50a772638ea21047a9fd4084f3dd67a (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Test for auth counters in serverStatus.

(function() {
'use strict';

let expectedSuccessLogs = 0;
let expectedFailureLogs = 0;

function authnSuccessIncrementsServerStatusTotalAuthTime(mongodRunner) {
    jsTest.log(
        "============================ authnSuccessIncrementsServerStatusTotalAuthTime ============================");
    const admin = mongodRunner.getDB("admin");

    admin.auth('admin', 'pwd');

    const expected = assert.commandWorked(admin.runCommand({serverStatus: 1}))
                         .security.authentication.totalAuthenticationTimeMicros;

    assert.gte(expected, 0);

    admin.logout();

    admin.auth('admin', 'pwd');

    const nextExpected = assert.commandWorked(admin.runCommand({serverStatus: 1}))
                             .security.authentication.totalAuthenticationTimeMicros;

    assert.gt(nextExpected, expected);

    admin.logout();
}

function authnFailureIncrementsServerStatusTotalAuthTime(mongodRunner) {
    jsTest.log(
        "============================ authnFailureIncrementsServerStatusTotalAuthTime ============================");
    const admin = mongodRunner.getDB("admin");

    admin.auth('admin', 'pwd');

    // Count the number of authentications performed during setup
    const expected = assert.commandWorked(admin.runCommand({serverStatus: 1}))
                         .security.authentication.totalAuthenticationTimeMicros;

    assert.gte(expected, 0);

    admin.auth('admin', 'wrong');

    const nextExpected = assert.commandWorked(admin.runCommand({serverStatus: 1}))
                             .security.authentication.totalAuthenticationTimeMicros;

    assert.gt(nextExpected, expected);

    admin.logout();
}

// Test that a successful authentication is logged correctly
function authnSuccessLogsMetricsReportWithSuccessStatus(mongodRunner) {
    jsTest.log(
        "============================ authnSuccessLogsMetricsReportWithSuccessStatus ============================");

    const admin = mongodRunner.getDB("admin");

    // If the test is operating inside the same session as other tests, we can find duplicate log
    // entries so we increment the total # of expected log entries instead of using a fixed count
    expectedSuccessLogs += 1;

    admin.auth('admin', 'pwd');
    assert.soon(
        () => checkLog.checkContainsWithCountJson(
            mongodRunner,
            5286306,
            {
                "result": 0,
                "metrics": {"conversation_duration": {"summary": [{"step": 1}, {"step": 2}]}}
            },
            expectedSuccessLogs,
            null,
            true),
        "Did not find expected 1 successful metric log entries");
    admin.logout();
}

// Test that authentication failure is logged correctly
function authnFailureLogsMetricsReportWithFailedStatus(mongodRunner) {
    jsTest.log(
        "============================ authnFailureLogsMetricsReportWithFailedStatus ============================");

    const admin = mongodRunner.getDB("admin");
    admin.auth('admin', 'wrong');
    expectedFailureLogs += 1;
    assert.soon(
        () => checkLog.checkContainsWithCountJson(
            mongodRunner,
            5286307,
            {"result": 18, "metrics": {"conversation_duration": {"summary": [{"step": 1}]}}},
            expectedFailureLogs,
            null,
            true),
        "Did not find expected 1 failure metric log entries");
    admin.logout();
}

// Test that multiple successful authentications across the same client produce the correct log
// output and do not e.g. combine summaries or results
function multipleAuthnSuccessLogsMultipleCorrectReports(mongodRunner) {
    jsTest.log(
        "============================ multipleAuthnSuccessLogsMultipleCorrectReports ============================");

    const admin = mongodRunner.getDB("admin");

    admin.auth('admin', 'pwd');
    admin.logout();
    admin.auth('admin', 'pwd');

    expectedSuccessLogs += 2;

    assert.soon(
        () => checkLog.checkContainsWithCountJson(
            mongodRunner,
            5286306,
            {
                "result": 0,
                "metrics": {"conversation_duration": {"summary": [{"step": 1}, {"step": 2}]}}
            },
            expectedSuccessLogs,
            null,
            true),
        "Did not find expected 2 successful metric log entries");
}

// Test that multiple authentication failure is logged correctly
function authnFailureLogsMetricsReportWithFailedStatus(mongodRunner) {
    jsTest.log(
        "============================ authnFailureLogsMetricsReportWithFailedStatus ============================");

    const admin = mongodRunner.getDB("admin");
    admin.auth('admin', 'wrong');
    admin.auth('admin', 'wrong_2');

    expectedFailureLogs += 2;
    assert.soon(
        () => checkLog.checkContainsWithCountJson(
            mongodRunner,
            5286307,
            {"result": 18, "metrics": {"conversation_duration": {"summary": [{"step": 1}]}}},
            expectedFailureLogs,
            null,
            true),
        "Did not find expected 2 failure metric log entries");
    admin.logout();
}

// Test that multiple mixed authentications across the same client produce the correct log output
// and do not e.g. combine summaries or results
function multipleAuthnMixedLogsMultipleCorrectReports(mongodRunner) {
    jsTest.log(
        "============================ multipleAuthnMixedLogsMultipleCorrectReports ============================");

    const admin = mongodRunner.getDB("admin");

    admin.auth('admin', 'pwd');
    admin.logout();

    expectedSuccessLogs += 1;

    assert.soon(
        () => checkLog.checkContainsWithCountJson(
            mongodRunner,
            5286306,
            {
                "result": 0,
                "metrics": {"conversation_duration": {"summary": [{"step": 1}, {"step": 2}]}}
            },
            expectedSuccessLogs,
            null,
            true),
        "Did not find expected 1 successful metric log entries");

    expectedFailureLogs += 1;

    admin.auth('admin', 'wrong');

    assert.soon(
        () => checkLog.checkContainsWithCountJson(
            mongodRunner,
            5286307,
            {"result": 18, "metrics": {"conversation_duration": {"summary": [{"step": 1}]}}},
            expectedFailureLogs,
            null,
            true),
        "Did not find expected 1 failure metric log entries");
}

const mongod = MongoRunner.runMongod();

try {
    mongod.getDB("admin").createUser(
        {user: 'admin', pwd: 'pwd', roles: ['root'], mechanisms: ['SCRAM-SHA-256']});

    authnSuccessLogsMetricsReportWithSuccessStatus(mongod);
    authnFailureLogsMetricsReportWithFailedStatus(mongod);
    multipleAuthnSuccessLogsMultipleCorrectReports(mongod);
    multipleAuthnMixedLogsMultipleCorrectReports(mongod);

    authnSuccessIncrementsServerStatusTotalAuthTime(mongod);
    authnFailureIncrementsServerStatusTotalAuthTime(mongod);

} finally {
    MongoRunner.stopMongod(mongod);
}
})();