From b4f13f930719a535eab2ee897f6a236d58d6d60c Mon Sep 17 00:00:00 2001 From: Dzmitry Lemechko Date: Sat, 22 Jun 2024 14:33:01 +0200 Subject: [PATCH] throw error when retry is exited --- packages/kbn-test/src/auth/saml_auth.test.ts | 19 ++++++++ packages/kbn-test/src/auth/saml_auth.ts | 47 +++++++++++--------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/packages/kbn-test/src/auth/saml_auth.test.ts b/packages/kbn-test/src/auth/saml_auth.test.ts index 6b6ba28ef47fa..6ad972d8f3b5b 100644 --- a/packages/kbn-test/src/auth/saml_auth.test.ts +++ b/packages/kbn-test/src/auth/saml_auth.test.ts @@ -148,6 +148,25 @@ describe('saml_auth', () => { ); expect(axiosRequestMock).toBeCalledTimes(3); }); + + test(`throws error when retry 'attemptsCount' is below 1`, async () => { + await expect( + createCloudSession( + { + hostname: 'cloud', + email: 'viewer@elastic.co', + password: 'changeme', + log, + }, + { + attemptsCount: 0, + attemptDelay: 100, + } + ) + ).rejects.toThrow( + 'Failed to create the new cloud session, check retry arguments: {"attemptsCount":0,"attemptDelay":100}' + ); + }); }); describe('createSAMLRequest', () => { diff --git a/packages/kbn-test/src/auth/saml_auth.ts b/packages/kbn-test/src/auth/saml_auth.ts index 02513a4ffe7ee..fc84c6081dfe1 100644 --- a/packages/kbn-test/src/auth/saml_auth.ts +++ b/packages/kbn-test/src/auth/saml_auth.ts @@ -90,7 +90,7 @@ export const createCloudSession = async ( attemptsCount: 3, attemptDelay: 15_000, } -) => { +): Promise => { const { hostname, email, password, log } = params; const cloudLoginUrl = getCloudUrl(hostname, '/api/v1/saas/auth/_login'); let sessionResponse: AxiosResponse | undefined; @@ -120,30 +120,30 @@ export const createCloudSession = async ( throw new Error( `Failed to create the new cloud session: 'POST ${cloudLoginUrl}' returned ${sessionResponse?.status}` ); - } - const token = sessionResponse?.data?.token as string; - if (!token) { - const keysToRedact = ['user_id', 'okta_session_id']; - const data = sessionResponse?.data; - if (data !== null && typeof data === 'object') { - Object.keys(data).forEach((key) => { - if (keysToRedact.includes(key)) { - data[key] = 'REDACTED'; - } - }); + } else { + const token = sessionResponse?.data?.token as string; + if (token) { + return token; + } else { + const keysToRedact = ['user_id', 'okta_session_id']; + const data = sessionResponse?.data; + if (data !== null && typeof data === 'object') { + Object.keys(data).forEach((key) => { + if (keysToRedact.includes(key)) { + data[key] = 'REDACTED'; + } + }); + } + throw new Error( + `Failed to create the new cloud session: token is missing in response data\n${JSON.stringify( + data + )}` + ); } - throw new Error( - `Failed to create the new cloud session: token is missing in response data\n${JSON.stringify( - data - )}` - ); } - return token; } catch (ex) { cleanException(cloudLoginUrl, ex); - - attemptsLeft--; - if (attemptsLeft > 0) { + if (--attemptsLeft > 0) { // log only error message log.error(`${ex.message}\nWaiting ${retryParams.attemptDelay} ms before the next attempt`); await new Promise((resolve) => setTimeout(resolve, retryParams.attemptDelay)); @@ -156,6 +156,11 @@ export const createCloudSession = async ( } } } + + // should never be reached + throw new Error( + `Failed to create the new cloud session, check retry arguments: ${JSON.stringify(retryParams)}` + ); }; export const createSAMLRequest = async (kbnUrl: string, kbnVersion: string, log: ToolingLog) => {