Skip to content
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

fix(credential-provider-ini): support sso-session based profile as source_profile #4820

Merged
merged 1 commit into from
Jan 30, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ describe(resolveProfileData.name, () => {
(resolveSsoCredentials as jest.Mock).mockImplementation(() => Promise.resolve(mockCreds));
const receivedCreds = await resolveProfileData(mockProfileName, mockProfiles, mockOptions);
expect(receivedCreds).toStrictEqual(mockCreds);
expect(resolveSsoCredentials).toHaveBeenCalledWith(mockProfiles[mockProfileName]);
expect(resolveSsoCredentials).toHaveBeenCalledWith(mockProfileName);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const resolveProfileData = async (
}

if (isSsoProfile(data)) {
return await resolveSsoCredentials(data);
return await resolveSsoCredentials(profileName);
}

// If the profile cannot be parsed or contains neither static credentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,107 +19,40 @@ describe(isSsoProfile.name, () => {
});

describe(resolveSsoCredentials.name, () => {
const getMockOriginalSsoProfile = () => ({
sso_start_url: "mock_sso_start_url",
sso_account_id: "mock_sso_account_id",
sso_region: "mock_sso_region",
sso_role_name: "mock_sso_role_name",
});

const getMockValidatedSsoProfile = <T>(add: T = {} as T) => ({
sso_start_url: "mock_validated_sso_start_url",
sso_account_id: "mock_validated_sso_account_id",
sso_region: "mock_validated_sso_region",
sso_role_name: "mock_validated_sso_role_name",
...add,
});

afterEach(() => {
jest.clearAllMocks();
});

it("throws error when validation fails", async () => {
const mockProfile = getMockOriginalSsoProfile();
const expectedError = new Error("error from validateSsoProfile");
(validateSsoProfile as jest.Mock).mockImplementation(() => {
throw expectedError;
});
try {
await resolveSsoCredentials(mockProfile);
fail(`expected ${expectedError}`);
} catch (error) {
expect(error).toStrictEqual(expectedError);
}
expect(validateSsoProfile).toHaveBeenCalledWith(mockProfile);
});

it("throws error when fromSSO throws error", async () => {
const mockProfile = getMockOriginalSsoProfile();
const mockValidatedProfile = getMockValidatedSsoProfile();
const mockProfileName = "mockProfileName";
const expectedError = new Error("error from fromSSO");

(validateSsoProfile as jest.Mock).mockReturnValue(mockValidatedProfile);
(fromSSO as jest.Mock).mockReturnValue(() => Promise.reject(expectedError));

try {
await resolveSsoCredentials(mockProfile);
await resolveSsoCredentials(mockProfileName);
fail(`expected ${expectedError}`);
} catch (error) {
expect(error).toStrictEqual(expectedError);
}
expect(validateSsoProfile).toHaveBeenCalledWith(mockProfile);
expect(fromSSO).toHaveBeenCalledWith({
ssoStartUrl: mockValidatedProfile.sso_start_url,
ssoAccountId: mockValidatedProfile.sso_account_id,
ssoRegion: mockValidatedProfile.sso_region,
ssoRoleName: mockValidatedProfile.sso_role_name,
profile: mockProfileName,
});
});

it("calls fromSSO when validation succeeds", async () => {
const mockProfile = getMockOriginalSsoProfile();
const mockValidatedProfile = getMockValidatedSsoProfile();

it("calls fromSSO", async () => {
const mockProfileName = "mockProfileName";
const mockCreds: AwsCredentialIdentity = {
accessKeyId: "mockAccessKeyId",
secretAccessKey: "mockSecretAccessKey",
};

(validateSsoProfile as jest.Mock).mockReturnValue(mockValidatedProfile);
(fromSSO as jest.Mock).mockReturnValue(() => Promise.resolve(mockCreds));

const receivedCreds = await resolveSsoCredentials(mockProfile);
const receivedCreds = await resolveSsoCredentials(mockProfileName);
expect(receivedCreds).toStrictEqual(mockCreds);
expect(validateSsoProfile).toHaveBeenCalledWith(mockProfile);
expect(fromSSO).toHaveBeenCalledWith({
ssoStartUrl: mockValidatedProfile.sso_start_url,
ssoAccountId: mockValidatedProfile.sso_account_id,
ssoRegion: mockValidatedProfile.sso_region,
ssoRoleName: mockValidatedProfile.sso_role_name,
});
});

it("calls fromSSO with optional sso session name", async () => {
const mockProfile = getMockOriginalSsoProfile();
const mockValidatedProfile = getMockValidatedSsoProfile({
sso_session: "test-session",
});

const mockCreds: AwsCredentialIdentity = {
accessKeyId: "mockAccessKeyId",
secretAccessKey: "mockSecretAccessKey",
};

(validateSsoProfile as jest.Mock).mockReturnValue(mockValidatedProfile);
(fromSSO as jest.Mock).mockReturnValue(() => Promise.resolve(mockCreds));

await resolveSsoCredentials(mockProfile);
expect(fromSSO).toHaveBeenCalledWith({
ssoStartUrl: mockValidatedProfile.sso_start_url,
ssoAccountId: mockValidatedProfile.sso_account_id,
ssoRegion: mockValidatedProfile.sso_region,
ssoRoleName: mockValidatedProfile.sso_role_name,
ssoSession: mockValidatedProfile.sso_session,
profile: mockProfileName,
});
});
});
11 changes: 3 additions & 8 deletions packages/credential-provider-ini/src/resolveSsoCredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import type { Profile } from "@smithy/types";
/**
* @internal
*/
export const resolveSsoCredentials = async (data: Partial<SsoProfile>) => {
const { fromSSO, validateSsoProfile } = await import("@aws-sdk/credential-provider-sso");
const { sso_start_url, sso_account_id, sso_session, sso_region, sso_role_name } = validateSsoProfile(data);
export const resolveSsoCredentials = async (profile: string) => {
const { fromSSO } = await import("@aws-sdk/credential-provider-sso");
return fromSSO({
ssoStartUrl: sso_start_url,
ssoAccountId: sso_account_id,
ssoSession: sso_session,
ssoRegion: sso_region,
ssoRoleName: sso_role_name,
profile,
})();
};

Expand Down
Loading