Skip to content

Commit

Permalink
[7.10] Properly handle session index initialization failures. (#81978)
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin authored Oct 29, 2020
1 parent 04a19e3 commit 2734278
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ describe('Session index', () => {

await sessionIndex.initialize();
});

it('works properly after failure', async () => {
const unexpectedError = new Error('Uh! Oh!');
mockClusterClient.callAsInternalUser.mockImplementationOnce(() =>
Promise.reject(unexpectedError)
);
mockClusterClient.callAsInternalUser.mockImplementationOnce(() => Promise.resolve(true));

await expect(sessionIndex.initialize()).rejects.toBe(unexpectedError);
await expect(sessionIndex.initialize()).resolves.toBe(undefined);
});
});

describe('cleanUp', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export class SessionIndex {
}

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

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

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

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

// Notify any consumers that are awaiting on this promise and immediately reset it.
resolve();
}).finally(() => {
this.indexInitialization = undefined;
}));
}
Expand Down

0 comments on commit 2734278

Please sign in to comment.