Skip to content

fix(credential-provider-sso): add existence check to profile #4186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/credential-provider-sso/src/fromSSO.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ describe(fromSSO.name, () => {
jest.clearAllMocks();
});

it("throws error if profile is not found", async () => {
const mockInit = { profile: mockProfileName };
const mockProfiles = {};
(parseKnownFiles as jest.Mock).mockResolvedValue(mockProfiles);
(getProfileName as jest.Mock).mockReturnValue(mockProfileName);
const expectedError = new CredentialsProviderError(`Profile ${mockProfileName} was not found.`);

try {
await fromSSO(mockInit)();
fail(`expected ${expectedError}`);
} catch (error) {
expect(error).toStrictEqual(expectedError);
}
expect(parseKnownFiles).toHaveBeenCalledWith(mockInit);
});

describe("all sso* values are not set", () => {
const mockInit = { profile: mockProfileName };
const mockProfiles = { [mockProfileName]: mockSsoProfile };
Expand Down
14 changes: 9 additions & 5 deletions packages/credential-provider-sso/src/fromSSO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ export const fromSSO =
const profiles = await parseKnownFiles(init);
const profile = profiles[profileName];

if (profile.sso_session) {
if (!profile) {
throw new CredentialsProviderError(`Profile ${profileName} was not found.`);
}

if (!isSsoProfile(profile)) {
throw new CredentialsProviderError(`Profile ${profileName} is not configured with SSO credentials.`);
}

if (profile?.sso_session) {
const ssoSessions = await loadSsoSessionData(init);
const session = ssoSessions[profile.sso_session];
const conflictMsg = ` configurations in profile ${profileName} and sso-session ${profile.sso_session}`;
Expand All @@ -98,10 +106,6 @@ export const fromSSO =
profile.sso_start_url = session.sso_start_url;
}

if (!isSsoProfile(profile)) {
throw new CredentialsProviderError(`Profile ${profileName} is not configured with SSO credentials.`);
}

const { sso_start_url, sso_account_id, sso_region, sso_role_name, sso_session } = validateSsoProfile(profile);
return resolveSSOCredentials({
ssoStartUrl: sso_start_url,
Expand Down