Skip to content

Commit 6fc04c7

Browse files
authored
test(sdkv3): add tests for expiring credentials (#6442)
## Problem The client should not be making requests with stale credentials so we want test coverage to ensure this ## Solution - Create a mock credentials shim and insert it into the global context. - The client builder will then use that shim, and we can manually expire it. - Ensure client does not cache potentially stale credentials. --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 896835c commit 6fc04c7

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

packages/core/src/test/shared/awsClientBuilderV3.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { Client } from '@aws-sdk/smithy-client'
1414
import { extensionVersion } from '../../shared'
1515
import { assertTelemetry } from '../testUtil'
1616
import { telemetry } from '../../shared/telemetry'
17+
import { CredentialsShim } from '../../auth/deprecated/loginManager'
18+
import { Credentials } from '@aws-sdk/types'
19+
import { oneDay } from '../../shared/datetime'
1720

1821
describe('AwsClientBuilderV3', function () {
1922
let builder: AWSClientBuilderV3
@@ -57,6 +60,52 @@ describe('AwsClientBuilderV3', function () {
5760
assert.strictEqual(service.config.userAgent[0][0], 'CUSTOM USER AGENT')
5861
})
5962
})
63+
64+
describe('clientCredentials', function () {
65+
let fakeContext: FakeAwsContext
66+
let mockCredsShim: MockCredentialsShim
67+
let oldCreds: Credentials
68+
let newCreds: Credentials
69+
70+
beforeEach(function () {
71+
fakeContext = new FakeAwsContext()
72+
oldCreds = {
73+
accessKeyId: 'old',
74+
secretAccessKey: 'old',
75+
sessionToken: 'old',
76+
expiration: new Date(Date.now() + oneDay),
77+
}
78+
newCreds = {
79+
accessKeyId: 'new',
80+
secretAccessKey: 'new',
81+
sessionToken: 'new',
82+
expiration: new Date(Date.now() + oneDay),
83+
}
84+
mockCredsShim = new MockCredentialsShim(oldCreds, newCreds)
85+
fakeContext.credentialsShim = mockCredsShim
86+
builder = new AWSClientBuilderV3(fakeContext)
87+
})
88+
89+
it('refreshes credentials when they expire', async function () {
90+
const service = await builder.createAwsService(Client)
91+
assert.strictEqual(await service.config.credentials(), oldCreds)
92+
mockCredsShim.expire()
93+
assert.strictEqual(await service.config.credentials(), newCreds)
94+
})
95+
96+
it('does not cache stale credentials', async function () {
97+
const service = await builder.createAwsService(Client)
98+
assert.strictEqual(await service.config.credentials(), oldCreds)
99+
const newerCreds = {
100+
accessKeyId: 'old2',
101+
secretAccessKey: 'old2',
102+
sessionToken: 'old2',
103+
expiration: new Date(Date.now() + oneDay),
104+
}
105+
mockCredsShim.update(newerCreds)
106+
assert.strictEqual(await service.config.credentials(), newerCreds)
107+
})
108+
})
60109
})
61110

62111
describe('getServiceId', function () {
@@ -77,3 +126,29 @@ describe('recordErrorTelemetry', function () {
77126
assertTelemetry('vscode_executeCommand', { requestServiceType: 'aws-service' })
78127
})
79128
})
129+
130+
class MockCredentialsShim implements CredentialsShim {
131+
public constructor(
132+
public credentials: Credentials,
133+
public readonly refreshedCredentials: Credentials
134+
) {}
135+
136+
public expire(): void {
137+
this.credentials = {
138+
...this.credentials,
139+
expiration: new Date(Date.now() - oneDay),
140+
}
141+
}
142+
143+
public update(newCreds: Credentials): void {
144+
this.credentials = newCreds
145+
}
146+
147+
public async get(): Promise<Credentials> {
148+
return this.credentials
149+
}
150+
151+
public async refresh(): Promise<Credentials> {
152+
return this.refreshedCredentials
153+
}
154+
}

0 commit comments

Comments
 (0)