Skip to content

Commit

Permalink
chore: change credentials.expiration from number to Date (#1040)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 24, 2020
1 parent 88acab0 commit e3c46c0
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("fromCognitoIdentity", () => {
accessKeyId: "foo",
secretAccessKey: "bar",
sessionToken: "baz",
expiration: Math.floor(expiration.valueOf() / 1000)
expiration
});

expect(send.mock.calls[0][0]).toEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export function fromCognitoIdentity(
secretAccessKey: SecretKey,
sessionToken: SessionToken,
expiration: Expiration
? Math.floor(Expiration.valueOf() / 1000)
: undefined
};
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ jest.mock("./fromCognitoIdentity", () => {
const promiseFunc = jest.fn().mockResolvedValue({
accessKeyId: "foo",
secretAccessKey: "bar",
sessionToken: "baz",
expiration: 946684800
sessionToken: "baz"
});
return { fromCognitoIdentity: jest.fn().mockReturnValue(promiseFunc) };
});
Expand Down Expand Up @@ -52,8 +51,7 @@ describe("fromCognitoIdentityPool", () => {
).toEqual({
accessKeyId: "foo",
secretAccessKey: "bar",
sessionToken: "baz",
expiration: 946684800
sessionToken: "baz"
});

expect(send.mock.calls.length).toBe(1);
Expand Down Expand Up @@ -109,8 +107,7 @@ describe("fromCognitoIdentityPool", () => {
expect(await provider()).toEqual({
accessKeyId: "foo",
secretAccessKey: "bar",
sessionToken: "baz",
expiration: 946684800
sessionToken: "baz"
});
}

Expand Down
5 changes: 3 additions & 2 deletions packages/credential-provider-env/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ afterAll(() => {

describe("fromEnv", () => {
it("should read credentials from known environment variables", async () => {
const dateString = "1970-01-01T07:00:00Z";
process.env[ENV_KEY] = "foo";
process.env[ENV_SECRET] = "bar";
process.env[ENV_SESSION] = "baz";
process.env[ENV_EXPIRATION] = "1970-01-01T07:00:00Z";
process.env[ENV_EXPIRATION] = dateString;

expect(await fromEnv()()).toEqual({
accessKeyId: "foo",
secretAccessKey: "bar",
sessionToken: "baz",
expiration: 25200
expiration: new Date(dateString)
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/credential-provider-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function fromEnv(): CredentialProvider {
accessKeyId,
secretAccessKey,
sessionToken: process.env[ENV_SESSION],
expiration: expiry ? Math.floor(Date.parse(expiry) / 1000) : undefined
expiration: expiry ? new Date(expiry) : undefined
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ describe("fromImdsCredentials", () => {
expect(converted.accessKeyId).toEqual(creds.AccessKeyId);
expect(converted.secretAccessKey).toEqual(creds.SecretAccessKey);
expect(converted.sessionToken).toEqual(creds.Token);
expect(converted.expiration).toEqual(
Math.floor(new Date(creds.Expiration).valueOf() / 1000)
);
expect(converted.expiration).toEqual(new Date(creds.Expiration));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export function fromImdsCredentials(creds: ImdsCredentials): Credentials {
accessKeyId: creds.AccessKeyId,
secretAccessKey: creds.SecretAccessKey,
sessionToken: creds.Token,
expiration: Math.floor(new Date(creds.Expiration).valueOf() / 1000)
expiration: new Date(creds.Expiration)
};
}
2 changes: 1 addition & 1 deletion packages/credential-provider-node/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ describe("defaultProvider", () => {
Promise.resolve({
accessKeyId: "foo",
secretAccessKey: "bar",
expiration: Date.now() + 600 // expires in ten minutes
expiration: new Date(Date.now() + 600000) // expires in ten minutes
})
);
const memoized = defaultProvider();
Expand Down
6 changes: 1 addition & 5 deletions packages/credential-provider-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,11 @@ export function defaultProvider(
providerChain,
credentials =>
credentials.expiration !== undefined &&
credentials.expiration - getEpochTs() < 300,
credentials.expiration.getTime() - Date.now() < 300000,
credentials => credentials.expiration !== undefined
);
}

function getEpochTs() {
return Math.floor(Date.now() / 1000);
}

function remoteProvider(init: RemoteProviderInit): CredentialProvider {
if (process.env[ENV_CMDS_RELATIVE_URI] || process.env[ENV_CMDS_FULL_URI]) {
return fromContainerMetadata(init);
Expand Down
5 changes: 2 additions & 3 deletions packages/types/src/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ export interface Credentials {
readonly sessionToken?: string;

/**
* UNIX epoch timestamp (seconds since 1 January, 1970 00:00:00 GMT) when
* these credentials will no longer be accepted.
* A {Date} when these credentials will no longer be accepted.
*/
readonly expiration?: number;
readonly expiration?: Date;
}

export type CredentialProvider = Provider<Credentials>;

0 comments on commit e3c46c0

Please sign in to comment.