Skip to content

Commit

Permalink
throw error when retry is exited
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlemeshko committed Jun 22, 2024
1 parent f33fd74 commit b4f13f9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
19 changes: 19 additions & 0 deletions packages/kbn-test/src/auth/saml_auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]',
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', () => {
Expand Down
47 changes: 26 additions & 21 deletions packages/kbn-test/src/auth/saml_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const createCloudSession = async (
attemptsCount: 3,
attemptDelay: 15_000,
}
) => {
): Promise<string> => {
const { hostname, email, password, log } = params;
const cloudLoginUrl = getCloudUrl(hostname, '/api/v1/saas/auth/_login');
let sessionResponse: AxiosResponse | undefined;
Expand Down Expand Up @@ -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));
Expand All @@ -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) => {
Expand Down

0 comments on commit b4f13f9

Please sign in to comment.