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

feat(credential-provider-imds): source accountId from IMDS #1247

Merged
merged 6 commits into from
Jul 24, 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
5 changes: 5 additions & 0 deletions .changeset/stale-ladybugs-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/credential-provider-imds": minor
---

sources accountId from IMDS
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ const creds: ImdsCredentials = Object.freeze({
SecretAccessKey: "bar",
Token: "baz",
Expiration: new Date().toISOString(),
AccountId: "123456789012",
siddsriv marked this conversation as resolved.
Show resolved Hide resolved
});

describe("isImdsCredentials", () => {
it("should accept valid ImdsCredentials objects", () => {
expect(isImdsCredentials(creds)).toBe(true);
const { AccountId, ...credsWithoutAccountId } = creds;
expect(AccountId).toBe("123456789012");
expect(isImdsCredentials(credsWithoutAccountId)).toBe(true);
});

it("should reject credentials without an AccessKeyId", () => {
Expand Down Expand Up @@ -44,5 +48,22 @@ describe("fromImdsCredentials", () => {
expect(converted.secretAccessKey).toEqual(creds.SecretAccessKey);
expect(converted.sessionToken).toEqual(creds.Token);
expect(converted.expiration).toEqual(new Date(creds.Expiration));
expect(converted.accountId).toEqual(creds.AccountId);
});

it("should convert IMDS credentials to a credentials object without accountId when it's not provided", () => {
const credsWithoutAccountId: ImdsCredentials = {
AccessKeyId: "foo",
SecretAccessKey: "bar",
Token: "baz",
Expiration: new Date().toISOString(),
// AccountId is omitted
};
const converted: AwsCredentialIdentity = fromImdsCredentials(credsWithoutAccountId);
expect(converted.accessKeyId).toEqual(credsWithoutAccountId.AccessKeyId);
expect(converted.secretAccessKey).toEqual(credsWithoutAccountId.SecretAccessKey);
expect(converted.sessionToken).toEqual(credsWithoutAccountId.Token);
expect(converted.expiration).toEqual(new Date(credsWithoutAccountId.Expiration));
expect(converted.accountId).toBeUndefined(); // Verify accountId is undefined
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ImdsCredentials {
SecretAccessKey: string;
Token: string;
Expiration: string;
AccountId?: string;
}

/**
Expand All @@ -29,4 +30,5 @@ export const fromImdsCredentials = (creds: ImdsCredentials): AwsCredentialIdenti
secretAccessKey: creds.SecretAccessKey,
sessionToken: creds.Token,
expiration: new Date(creds.Expiration),
...(creds.AccountId && { accountId: creds.AccountId }),
});
Loading