Skip to content

Commit

Permalink
Refresh session index after cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Portner committed Mar 4, 2022
1 parent 2336649 commit 40b7cbf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ describe('Session index', () => {
expect(mockElasticsearchClient.search).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.bulk).not.toHaveBeenCalled();
expect(mockElasticsearchClient.closePointInTime).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.indices.refresh).not.toHaveBeenCalled(); // since the search failed, we don't refresh the index
});

it('throws if bulk delete call to Elasticsearch fails', async () => {
Expand All @@ -227,7 +228,20 @@ describe('Session index', () => {
expect(mockElasticsearchClient.openPointInTime).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.search).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.bulk).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.closePointInTime).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.closePointInTime).toHaveBeenCalledTimes(1); // since we attempted to delete sessions, we still refresh the index
});

it('does not throw if index refresh call to Elasticsearch fails', async () => {
const failureReason = new errors.ResponseError(
securityMock.createApiResponse(securityMock.createApiResponse({ body: { type: 'Uh oh.' } }))
);
mockElasticsearchClient.indices.refresh.mockRejectedValue(failureReason);

await sessionIndex.cleanUp();
expect(mockElasticsearchClient.openPointInTime).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.search).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.bulk).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.closePointInTime).toHaveBeenCalledTimes(1); // since we attempted to delete sessions, we still refresh the index
});

it('when neither `lifespan` nor `idleTimeout` is configured', async () => {
Expand Down Expand Up @@ -388,6 +402,7 @@ describe('Session index', () => {
}
);
expect(mockElasticsearchClient.closePointInTime).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.indices.refresh).toHaveBeenCalledTimes(1);
});

it('when only `idleTimeout` is configured', async () => {
Expand Down Expand Up @@ -474,6 +489,7 @@ describe('Session index', () => {
}
);
expect(mockElasticsearchClient.closePointInTime).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.indices.refresh).toHaveBeenCalledTimes(1);
});

it('when both `lifespan` and `idleTimeout` are configured', async () => {
Expand Down Expand Up @@ -570,6 +586,7 @@ describe('Session index', () => {
}
);
expect(mockElasticsearchClient.closePointInTime).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.indices.refresh).toHaveBeenCalledTimes(1);
});

it('when both `lifespan` and `idleTimeout` are configured and multiple providers are enabled', async () => {
Expand Down Expand Up @@ -714,6 +731,7 @@ describe('Session index', () => {
}
);
expect(mockElasticsearchClient.closePointInTime).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.indices.refresh).toHaveBeenCalledTimes(1);
});

it('should clean up sessions in batches of 10,000', async () => {
Expand All @@ -729,6 +747,7 @@ describe('Session index', () => {
expect(mockElasticsearchClient.search).toHaveBeenCalledTimes(2);
expect(mockElasticsearchClient.bulk).toHaveBeenCalledTimes(2);
expect(mockElasticsearchClient.closePointInTime).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.indices.refresh).toHaveBeenCalledTimes(1);
});

it('should limit number of batches to 10', async () => {
Expand All @@ -742,6 +761,7 @@ describe('Session index', () => {
expect(mockElasticsearchClient.search).toHaveBeenCalledTimes(10);
expect(mockElasticsearchClient.bulk).toHaveBeenCalledTimes(10);
expect(mockElasticsearchClient.closePointInTime).toHaveBeenCalledTimes(1);
expect(mockElasticsearchClient.indices.refresh).toHaveBeenCalledTimes(1);
});

it('should log audit event', async () => {
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/security/server/session_management/session_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ export class SessionIndex {
logger.debug(`Running cleanup routine.`);

let error: Error | undefined;
let haveDeletedSessions = false;
try {
for await (const sessionValues of this.getSessionValuesInBatches()) {
const operations: Array<Required<Pick<BulkOperationContainer, 'delete'>>> = [];
Expand All @@ -457,6 +458,7 @@ export class SessionIndex {
operations.push({ delete: { _id } });
});
if (operations.length > 0) {
haveDeletedSessions = true;
const bulkResponse = await elasticsearchClient.bulk(
{
index: this.indexName,
Expand Down Expand Up @@ -489,6 +491,15 @@ export class SessionIndex {
error = err;
}

if (haveDeletedSessions) {
try {
await elasticsearchClient.indices.refresh({ index: this.indexName });
logger.debug(`Refreshed session index.`);
} catch (err) {
logger.error(`Failed to refresh session index: ${err.message}`);
}
}

if (error) {
// If we couldn't fetch or delete sessions, throw an error so the task will be retried.
throw error;
Expand Down

0 comments on commit 40b7cbf

Please sign in to comment.