-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathcleanup.ts
465 lines (387 loc) · 17.4 KB
/
cleanup.ts
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type {
AggregateName,
AggregationsMultiTermsAggregate,
AggregationsMultiTermsBucket,
AggregationsTopHitsAggregate,
SearchTotalHits,
} from '@elastic/elasticsearch/lib/api/types';
import { setTimeout as setTimeoutAsync } from 'timers/promises';
import type { Cookie } from 'tough-cookie';
import { parse as parseCookie } from 'tough-cookie';
import expect from '@kbn/expect';
import {
getSAMLRequestId,
getSAMLResponse,
} from '@kbn/security-api-integration-helpers/saml/saml_tools';
import type { AuthenticationProvider } from '@kbn/security-plugin/common';
import { adminTestUser } from '@kbn/test';
import type { FtrProviderContext } from '../../ftr_provider_context';
export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertestWithoutAuth');
const esSupertest = getService('esSupertest');
const es = getService('es');
const security = getService('security');
const esDeleteAllIndices = getService('esDeleteAllIndices');
const config = getService('config');
const retry = getService('retry');
const log = getService('log');
const randomness = getService('randomness');
const testUser = { username: 'test_user', password: 'changeme' };
const basicProvider = { type: 'basic', name: 'basic1' };
const samlProvider = { type: 'saml', name: 'saml1' };
const anonymousProvider = { type: 'anonymous', name: 'anonymous1' };
const kibanaServerConfig = config.get('servers.kibana');
async function checkSessionCookie(
sessionCookie: Cookie,
username: string,
provider: AuthenticationProvider
) {
const apiResponse = await supertest
.get('/internal/security/me')
.set('kbn-xsrf', 'xxx')
.set('Cookie', sessionCookie.cookieString())
.expect(200);
expect(apiResponse.body.username).to.be(username);
expect(apiResponse.body.authentication_provider).to.eql(provider);
return Array.isArray(apiResponse.headers['set-cookie'])
? parseCookie(apiResponse.headers['set-cookie'][0])!
: undefined;
}
async function checkSessionCookieInvalid(sessionCookie: Cookie) {
await supertest
.get('/internal/security/me')
.set('kbn-xsrf', 'xxx')
.set('Cookie', sessionCookie.cookieString())
.expect(401);
}
async function getNumberOfSessionDocuments() {
await es.indices.refresh({ index: '.kibana_security_session*' });
const sessionDocuments = await es.search({ index: '.kibana_security_session*' });
log.debug(`Existing sessions: ${JSON.stringify(sessionDocuments.hits)}.`);
return (sessionDocuments.hits.total as SearchTotalHits).value;
}
async function loginWithBasic(credentials: { username: string; password: string }) {
const authenticationResponse = await supertest
.post('/internal/security/login')
.set('kbn-xsrf', 'xxx')
.send({
providerType: basicProvider.type,
providerName: basicProvider.name,
currentURL: '/',
params: credentials,
})
.expect(200);
return parseCookie(authenticationResponse.headers['set-cookie'][0])!;
}
async function startSAMLHandshake() {
const handshakeResponse = await supertest
.post('/internal/security/login')
.set('kbn-xsrf', 'xxx')
.send({ providerType: samlProvider.type, providerName: samlProvider.name, currentURL: '' })
.expect(200);
return {
cookie: parseCookie(handshakeResponse.headers['set-cookie'][0])!,
location: handshakeResponse.body.location,
};
}
async function finishSAMLHandshake(handshakeCookie: Cookie, handshakeLocation: string) {
const authenticationResponse = await supertest
.post('/api/security/saml/callback')
.set('kbn-xsrf', 'xxx')
.set('Cookie', handshakeCookie.cookieString())
.send({
SAMLResponse: await getSAMLResponse({
destination: `http://localhost:${kibanaServerConfig.port}/api/security/saml/callback`,
sessionIndex: String(randomness.naturalNumber()),
inResponseTo: await getSAMLRequestId(handshakeLocation),
}),
})
.expect(302);
return parseCookie(authenticationResponse.headers['set-cookie'][0])!;
}
async function loginWithSAML() {
const { cookie, location } = await startSAMLHandshake();
return finishSAMLHandshake(cookie, location);
}
async function loginWithAnonymous() {
const authenticationResponse = await supertest
.post('/internal/security/login')
.set('kbn-xsrf', 'xxx')
.send({
providerType: anonymousProvider.type,
providerName: anonymousProvider.name,
currentURL: '/',
})
.expect(200);
return parseCookie(authenticationResponse.headers['set-cookie'][0])!;
}
async function runCleanupTaskSoon() {
// In most cases, an error would mean the task is currently running so let's run it again
await retry.tryForTime(30000, async () => {
await supertest
.post('/session/_run_cleanup')
.set('kbn-xsrf', 'xxx')
.auth(adminTestUser.username, adminTestUser.password)
.send()
.expect(200);
});
}
async function addESDebugLoggingSettings() {
const addLogging = {
persistent: {
'logger.org.elasticsearch.xpack.security.authc': 'debug',
},
};
await esSupertest.put('/_cluster/settings').send(addLogging).expect(200);
}
describe('Session Concurrent Limit cleanup', () => {
before(async () => {
await security.user.create('anonymous_user', {
password: 'changeme',
roles: [],
full_name: 'Guest',
});
});
after(async () => {
await security.user.delete('anonymous_user');
});
beforeEach(async function () {
this.timeout(120000);
await es.cluster.health({ index: '.kibana_security_session*', wait_for_status: 'green' });
await addESDebugLoggingSettings();
await esDeleteAllIndices('.kibana_security_session*');
});
it('should properly clean up sessions that exceeded concurrent session limit', async function () {
this.timeout(100000);
log.debug(`Log in as ${testUser.username} 3 times with a 0.5s delay.`);
const basicSessionCookieOne = await loginWithBasic(testUser);
await setTimeoutAsync(500);
const basicSessionCookieTwo = await loginWithBasic(testUser);
await setTimeoutAsync(500);
const basicSessionCookieThree = await loginWithBasic(testUser);
log.debug('Waiting for all sessions to be persisted...');
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(3);
});
// Poke the background task to run
await runCleanupTaskSoon();
log.debug('Waiting for cleanup job to run...');
await retry.tryForTime(30000, async () => {
// The oldest session should have been removed, but the rest should still be valid.
expect(await getNumberOfSessionDocuments()).to.be(2);
});
await checkSessionCookieInvalid(basicSessionCookieOne);
await checkSessionCookie(basicSessionCookieTwo, testUser.username, basicProvider);
await checkSessionCookie(basicSessionCookieThree, testUser.username, basicProvider);
});
it('should properly clean up sessions that exceeded concurrent session limit even for multiple providers', async function () {
this.timeout(160000);
log.debug(`Log in as ${testUser.username} and SAML user 3 times each with a 0.5s delay.`);
const basicSessionCookieOne = await loginWithBasic(testUser);
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(1);
});
const samlSessionCookieOne = await loginWithSAML();
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(2);
});
const basicSessionCookieTwo = await loginWithBasic(testUser);
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(3);
});
const samlSessionCookieTwo = await loginWithSAML();
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(4);
});
const basicSessionCookieThree = await loginWithBasic(testUser);
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(5);
});
const samlSessionCookieThree = await loginWithSAML();
log.debug('Waiting for all sessions to be persisted...');
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(6);
});
// Poke the background task to run
await runCleanupTaskSoon();
log.debug('Waiting for cleanup job to run...');
await retry.tryForTime(30000, async () => {
// The oldest sessions should have been removed, but the rest should still be valid.
expect(await getNumberOfSessionDocuments()).to.be(4);
});
await checkSessionCookieInvalid(basicSessionCookieOne);
await checkSessionCookie(basicSessionCookieTwo, testUser.username, basicProvider);
await checkSessionCookie(basicSessionCookieThree, testUser.username, basicProvider);
await checkSessionCookieInvalid(samlSessionCookieOne);
await checkSessionCookie(samlSessionCookieTwo, '[email protected]', samlProvider);
await checkSessionCookie(samlSessionCookieThree, '[email protected]', samlProvider);
});
it('should properly clean up sessions that exceeded concurrent session limit when legacy sessions are present', async function () {
this.timeout(100000);
log.debug(`Log in as ${testUser.username} and SAML user 3 times each with a 0.5s delay.`);
const basicSessionCookieOne = await loginWithBasic(testUser);
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(1);
});
const samlSessionCookieOne = await loginWithSAML();
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(2);
});
const basicSessionCookieTwo = await loginWithBasic(testUser);
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(3);
});
const samlSessionCookieTwo = await loginWithSAML();
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(4);
});
const basicSessionCookieThree = await loginWithBasic(testUser);
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(5);
});
const samlSessionCookieThree = await loginWithSAML();
log.debug('Waiting for all sessions to be persisted...');
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(6);
});
// Remove `createdAt` field from the most recent sessions to emulate legacy sessions.
// 1. Get the latest session for every unique credentials.
const aggResponse = await es.search<
unknown,
Record<AggregateName, AggregationsMultiTermsAggregate>
>({
index: '.kibana_security_session*',
size: 0,
filter_path: 'aggregations.sessions.buckets.top.hits.hits._id',
aggs: {
sessions: {
multi_terms: { terms: [{ field: 'usernameHash' }, { field: 'provider.type' }] },
aggs: { top: { top_hits: { sort: [{ createdAt: { order: 'desc' } }], size: 1 } } },
},
},
});
// 2. Extract session IDs from the nested top_hits aggregation.
const sessionIds =
(aggResponse.aggregations?.sessions.buckets as AggregationsMultiTermsBucket[]).flatMap(
(bucket) => {
const sessionId = (bucket.top as AggregationsTopHitsAggregate).hits?.hits?.[0]?._id;
return sessionId ? [sessionId] : [];
}
) ?? [];
expect(sessionIds.length).to.be(2);
// 3. Remove `createdAt` field for the latest sessions emulating legacy sessions.
await es.updateByQuery({
index: '.kibana_security_session*',
script: 'ctx._source.remove("createdAt")',
query: { ids: { values: sessionIds } },
refresh: true,
});
// Poke the background task to run
await runCleanupTaskSoon();
log.debug('Waiting for cleanup job to run...');
await retry.tryForTime(30000, async () => {
// The oldest session should have been removed, but the rest should still be valid.
expect(await getNumberOfSessionDocuments()).to.be(4);
});
await checkSessionCookie(basicSessionCookieOne, testUser.username, basicProvider);
await checkSessionCookie(basicSessionCookieTwo, testUser.username, basicProvider);
await checkSessionCookieInvalid(basicSessionCookieThree);
await checkSessionCookie(samlSessionCookieOne, '[email protected]', samlProvider);
await checkSessionCookie(samlSessionCookieTwo, '[email protected]', samlProvider);
await checkSessionCookieInvalid(samlSessionCookieThree);
});
it('should not clean up session if the limit is not exceeded', async function () {
this.timeout(100000);
log.debug(`Log in as ${testUser.username} 2 times with a 0.5s delay.`);
const basicSessionCookieOne = await loginWithBasic(testUser);
await setTimeoutAsync(500);
const basicSessionCookieTwo = await loginWithBasic(testUser);
log.debug('Waiting for all sessions to be persisted...');
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(2);
});
// Poke the background task to run
await runCleanupTaskSoon();
log.debug('Waiting for cleanup job to run...');
await retry.tryForTime(30000, async () => {
// The oldest session should have been removed, but the rest should still be valid.
expect(await getNumberOfSessionDocuments()).to.be(2);
});
await checkSessionCookie(basicSessionCookieOne, testUser.username, basicProvider);
await checkSessionCookie(basicSessionCookieTwo, testUser.username, basicProvider);
});
it('should not clean up sessions of the anonymous users', async function () {
this.timeout(100000);
log.debug(`Log in as anonymous_user 3 times.`);
const anonymousSessionCookieOne = await loginWithAnonymous();
const anonymousSessionCookieTwo = await loginWithAnonymous();
const anonymousSessionCookieThree = await loginWithAnonymous();
log.debug('Waiting for all sessions to be persisted...');
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(3);
});
// Poke the background task to run
await runCleanupTaskSoon();
log.debug('Waiting for cleanup job to run...');
await retry.tryForTime(30000, async () => {
// The oldest session should have been removed, but the rest should still be valid.
expect(await getNumberOfSessionDocuments()).to.be(3);
});
// All sessions should be active.
for (const anonymousSessionCookie of [
anonymousSessionCookieOne,
anonymousSessionCookieTwo,
anonymousSessionCookieThree,
]) {
await checkSessionCookie(anonymousSessionCookie, 'anonymous_user', anonymousProvider);
}
});
it('should not clean up unauthenticated sessions', async function () {
this.timeout(100000);
log.debug(`Starting SAML handshake 3 times.`);
const unauthenticatedSessionOne = await startSAMLHandshake();
await setTimeoutAsync(500);
const unauthenticatedSessionTwo = await startSAMLHandshake();
await setTimeoutAsync(500);
const unauthenticatedSessionThree = await startSAMLHandshake();
await setTimeoutAsync(500);
log.debug('Waiting for all sessions to be persisted...');
await retry.tryForTime(20000, async () => {
expect(await getNumberOfSessionDocuments()).to.be(3);
});
// Poke the background task to run
await runCleanupTaskSoon();
log.debug('Waiting for cleanup job to run...');
await retry.tryForTime(30000, async () => {
// The oldest session should have been removed, but the rest should still be valid.
expect(await getNumberOfSessionDocuments()).to.be(3);
});
// Finish SAML handshake (all should succeed since we don't enforce limit at session creation time).
const samlSessionCookieOne = await finishSAMLHandshake(
unauthenticatedSessionOne.cookie,
unauthenticatedSessionOne.location
);
await setTimeoutAsync(500); // Ensure the order of session cookie timestamps
const samlSessionCookieTwo = await finishSAMLHandshake(
unauthenticatedSessionTwo.cookie,
unauthenticatedSessionTwo.location
);
await setTimeoutAsync(500); // Ensure the order of session cookie timestamps
const samlSessionCookieThree = await finishSAMLHandshake(
unauthenticatedSessionThree.cookie,
unauthenticatedSessionThree.location
);
await es.indices.refresh({ index: '.kibana_security_session*' });
// For authenticated sessions limit should be enforced
await checkSessionCookieInvalid(samlSessionCookieOne);
await checkSessionCookie(samlSessionCookieTwo, '[email protected]', samlProvider);
await checkSessionCookie(samlSessionCookieThree, '[email protected]', samlProvider);
});
});
}