Skip to content

Commit 233a3e3

Browse files
authored
[7.x] Properly handle session index initialization failures. (#81977)
1 parent eba6804 commit 233a3e3

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

x-pack/plugins/security/server/session_management/session_index.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ describe('Session index', () => {
155155

156156
await sessionIndex.initialize();
157157
});
158+
159+
it('works properly after failure', async () => {
160+
const unexpectedError = new Error('Uh! Oh!');
161+
mockClusterClient.callAsInternalUser.mockImplementationOnce(() =>
162+
Promise.reject(unexpectedError)
163+
);
164+
mockClusterClient.callAsInternalUser.mockImplementationOnce(() => Promise.resolve(true));
165+
166+
await expect(sessionIndex.initialize()).rejects.toBe(unexpectedError);
167+
await expect(sessionIndex.initialize()).resolves.toBe(undefined);
168+
});
158169
});
159170

160171
describe('cleanUp', () => {

x-pack/plugins/security/server/session_management/session_index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ export class SessionIndex {
276276
}
277277

278278
const sessionIndexTemplateName = `${this.options.kibanaIndexName}_security_session_index_template_${SESSION_INDEX_TEMPLATE_VERSION}`;
279-
return (this.indexInitialization = new Promise(async (resolve) => {
279+
return (this.indexInitialization = new Promise<void>(async (resolve, reject) => {
280280
// Check if required index template exists.
281281
let indexTemplateExists = false;
282282
try {
@@ -288,7 +288,7 @@ export class SessionIndex {
288288
this.options.logger.error(
289289
`Failed to check if session index template exists: ${err.message}`
290290
);
291-
throw err;
291+
return reject(err);
292292
}
293293

294294
// Create index template if it doesn't exist.
@@ -303,7 +303,7 @@ export class SessionIndex {
303303
this.options.logger.debug('Successfully created session index template.');
304304
} catch (err) {
305305
this.options.logger.error(`Failed to create session index template: ${err.message}`);
306-
throw err;
306+
return reject(err);
307307
}
308308
}
309309

@@ -316,7 +316,7 @@ export class SessionIndex {
316316
});
317317
} catch (err) {
318318
this.options.logger.error(`Failed to check if session index exists: ${err.message}`);
319-
throw err;
319+
return reject(err);
320320
}
321321

322322
// Create index if it doesn't exist.
@@ -334,13 +334,14 @@ export class SessionIndex {
334334
this.options.logger.debug('Session index already exists.');
335335
} else {
336336
this.options.logger.error(`Failed to create session index: ${err.message}`);
337-
throw err;
337+
return reject(err);
338338
}
339339
}
340340
}
341341

342342
// Notify any consumers that are awaiting on this promise and immediately reset it.
343343
resolve();
344+
}).finally(() => {
344345
this.indexInitialization = undefined;
345346
}));
346347
}

0 commit comments

Comments
 (0)