Skip to content

Commit e59b1db

Browse files
rix0rrrHBobertz
authored andcommitted
fix(cli): allow credential plugins to return null for expiration (#32554)
According to the type definitions, the `expiration` field of V3 AWS credentials must be `undefined` or `Date`, but we are running into situations in reality where the value is `null`, leading to the error: ``` TypeError: Cannot read properties of null (reading 'getTime') ``` Survive that specific case. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7ee9b90 commit e59b1db

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Diff for: packages/aws-cdk/lib/api/aws-auth/provider-caching.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ export function makeCachingProvider(provider: AwsCredentialIdentityProvider): Aw
2020

2121
export function credentialsAboutToExpire(token: AwsCredentialIdentity) {
2222
const expiryMarginSecs = 5;
23-
return token.expiration !== undefined && token.expiration.getTime() - Date.now() < expiryMarginSecs * 1000;
23+
return !!token.expiration && token.expiration.getTime() - Date.now() < expiryMarginSecs * 1000;
2424
}

Diff for: packages/aws-cdk/test/api/plugin/credential-plugin.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CredentialPlugins } from '../../../lib/api/aws-auth/credential-plugins';
2+
import { credentialsAboutToExpire } from '../../../lib/api/aws-auth/provider-caching';
23
import { CredentialProviderSource, Mode, SDKv3CompatibleCredentials } from '../../../lib/api/plugin/credential-provider-source';
34
import { PluginHost, markTesting } from '../../../lib/api/plugin/plugin';
45

@@ -134,6 +135,15 @@ test('plugin must not return something that is not a credential', async () => {
134135
await expect(fetchNow()).rejects.toThrow(/Plugin returned a value that/);
135136
});
136137

138+
test('token expiration is allowed to be null', () => {
139+
expect(credentialsAboutToExpire({
140+
accessKeyId: 'key',
141+
secretAccessKey: 'secret',
142+
// This is not allowed according to the `.d.ts` contract, but it can happen in reality
143+
expiration: null as any,
144+
})).toEqual(false);
145+
});
146+
137147
function mockCredentialFunction(p: CredentialProviderSource['getProvider']) {
138148
mockCredentialPlugin({
139149
name: 'test',

0 commit comments

Comments
 (0)