Skip to content

Commit 30d9217

Browse files
authored
fix(cwl): pass credentials to LiveTail client #5993
## Problem When creating the CloudWatchClient for a LiveTail session, we are not specifying which credentials to use. This is causing the client to always use the `Default` credential profile, even if a different AWS Credential profile is selected within AWSToolkit. ## Solution Resolve the active AWS credential from `globals.awsContext`, and supply that to the LiveTailSession constructor. These credentials are then use to construct the CWL client.
1 parent 22eefef commit 30d9217

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

packages/core/src/awsService/cloudWatchLogs/commands/tailLogGroup.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
LiveTailSessionUpdate,
1414
StartLiveTailResponseStream,
1515
} from '@aws-sdk/client-cloudwatch-logs'
16-
import { getLogger, ToolkitError } from '../../../shared'
16+
import { getLogger, globals, ToolkitError } from '../../../shared'
1717
import { uriToKey } from '../cloudWatchLogsUtils'
1818

1919
export async function tailLogGroup(
@@ -25,12 +25,16 @@ export async function tailLogGroup(
2525
if (!wizardResponse) {
2626
throw new CancellationError('user')
2727
}
28-
28+
const awsCredentials = await globals.awsContext.getCredentials()
29+
if (awsCredentials === undefined) {
30+
throw new ToolkitError('Failed to start LiveTail session: credentials are undefined.')
31+
}
2932
const liveTailSessionConfig: LiveTailSessionConfiguration = {
3033
logGroupArn: wizardResponse.regionLogGroupSubmenuResponse.data,
3134
logStreamFilter: wizardResponse.logStreamFilter,
3235
logEventFilterPattern: wizardResponse.filterPattern,
3336
region: wizardResponse.regionLogGroupSubmenuResponse.region,
37+
awsCredentials: awsCredentials,
3438
}
3539
const session = new LiveTailSession(liveTailSessionConfig)
3640
if (registry.has(uriToKey(session.uri))) {

packages/core/src/awsService/cloudWatchLogs/registry/liveTailSession.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
import * as vscode from 'vscode'
6+
import * as AWS from '@aws-sdk/types'
67
import {
78
CloudWatchLogsClient,
89
StartLiveTailCommand,
@@ -19,6 +20,7 @@ export type LiveTailSessionConfiguration = {
1920
logStreamFilter?: LogStreamFilterResponse
2021
logEventFilterPattern?: string
2122
region: string
23+
awsCredentials: AWS.Credentials
2224
}
2325

2426
export type LiveTailSessionClient = {
@@ -49,6 +51,7 @@ export class LiveTailSession {
4951
this.logStreamFilter = configuration.logStreamFilter
5052
this.liveTailClient = {
5153
cwlClient: new CloudWatchLogsClient({
54+
credentials: configuration.awsCredentials,
5255
region: configuration.region,
5356
customUserAgent: getUserAgent(),
5457
}),

packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import {
2020
import { getTestWindow } from '../../../shared/vscode/window'
2121
import { CloudWatchLogsSettings, uriToKey } from '../../../../awsService/cloudWatchLogs/cloudWatchLogsUtils'
2222
import { installFakeClock } from '../../../testUtil'
23-
import { DefaultAwsContext } from '../../../../shared'
23+
import { DefaultAwsContext, ToolkitError } from '../../../../shared'
2424

2525
describe('TailLogGroup', function () {
2626
const testLogGroup = 'test-log-group'
2727
const testRegion = 'test-region'
2828
const testMessage = 'test-message'
2929
const testAwsAccountId = '1234'
30+
const testAwsCredentials = {} as any as AWS.Credentials
3031

3132
let sandbox: sinon.SinonSandbox
3233
let registry: LiveTailSessionRegistry
@@ -57,6 +58,8 @@ describe('TailLogGroup', function () {
5758

5859
it('starts LiveTailSession and writes to document. Closes tab and asserts session gets closed.', async function () {
5960
sandbox.stub(DefaultAwsContext.prototype, 'getCredentialAccountId').returns(testAwsAccountId)
61+
sandbox.stub(DefaultAwsContext.prototype, 'getCredentials').returns(Promise.resolve(testAwsCredentials))
62+
6063
wizardSpy = sandbox.stub(TailLogGroupWizard.prototype, 'run').callsFake(async function () {
6164
return getTestWizardResponse()
6265
})
@@ -122,6 +125,19 @@ describe('TailLogGroup', function () {
122125
assert.strictEqual(stopLiveTailSessionSpy.calledOnce, true)
123126
})
124127

128+
it('throws if crendentials are undefined', async function () {
129+
sandbox.stub(DefaultAwsContext.prototype, 'getCredentials').returns(Promise.resolve(undefined))
130+
wizardSpy = sandbox.stub(TailLogGroupWizard.prototype, 'run').callsFake(async function () {
131+
return getTestWizardResponse()
132+
})
133+
await assert.rejects(async () => {
134+
await tailLogGroup(registry, {
135+
groupName: testLogGroup,
136+
regionName: testRegion,
137+
})
138+
}, ToolkitError)
139+
})
140+
125141
it('closeSession removes session from registry and calls underlying stopLiveTailSession function.', function () {
126142
stopLiveTailSessionSpy = sandbox
127143
.stub(LiveTailSession.prototype, 'stopLiveTailSession')
@@ -132,6 +148,7 @@ describe('TailLogGroup', function () {
132148
const session = new LiveTailSession({
133149
logGroupArn: testLogGroup,
134150
region: testRegion,
151+
awsCredentials: testAwsCredentials,
135152
})
136153
registry.set(uriToKey(session.uri), session)
137154

@@ -145,6 +162,7 @@ describe('TailLogGroup', function () {
145162
const session = new LiveTailSession({
146163
logGroupArn: testLogGroup,
147164
region: testRegion,
165+
awsCredentials: testAwsCredentials,
148166
})
149167
const testData = 'blah blah blah'
150168
const document = await vscode.workspace.openTextDocument(session.uri)

packages/core/src/test/awsService/cloudWatchLogs/registry/liveTailRegistry.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import { cloudwatchLogsLiveTailScheme } from '../../../../shared/constants'
1111
describe('LiveTailSession URI', async function () {
1212
const testLogGroupName = 'test-log-group'
1313
const testRegion = 'test-region'
14+
const testAwsCredentials = {} as any as AWS.Credentials
1415
const expectedUriBase = `${cloudwatchLogsLiveTailScheme}:${testRegion}:${testLogGroupName}`
1516

1617
it('is correct with no logStream filter, no filter pattern', function () {
1718
const config: LiveTailSessionConfiguration = {
1819
logGroupArn: testLogGroupName,
1920
region: testRegion,
21+
awsCredentials: testAwsCredentials,
2022
}
2123
const expectedUri = vscode.Uri.parse(expectedUriBase)
2224
const uri = createLiveTailURIFromArgs(config)
@@ -28,6 +30,7 @@ describe('LiveTailSession URI', async function () {
2830
logGroupArn: testLogGroupName,
2931
region: testRegion,
3032
logEventFilterPattern: 'test-filter',
33+
awsCredentials: testAwsCredentials,
3134
}
3235
const expectedUri = vscode.Uri.parse(`${expectedUriBase}:test-filter`)
3336
const uri = createLiveTailURIFromArgs(config)
@@ -41,6 +44,7 @@ describe('LiveTailSession URI', async function () {
4144
logStreamFilter: {
4245
type: 'all',
4346
},
47+
awsCredentials: testAwsCredentials,
4448
}
4549
const expectedUri = vscode.Uri.parse(`${expectedUriBase}:all`)
4650
const uri = createLiveTailURIFromArgs(config)
@@ -55,6 +59,7 @@ describe('LiveTailSession URI', async function () {
5559
type: 'prefix',
5660
filter: 'test-prefix',
5761
},
62+
awsCredentials: testAwsCredentials,
5863
}
5964
const expectedUri = vscode.Uri.parse(`${expectedUriBase}:prefix:test-prefix`)
6065
const uri = createLiveTailURIFromArgs(config)
@@ -69,6 +74,7 @@ describe('LiveTailSession URI', async function () {
6974
type: 'specific',
7075
filter: 'test-stream',
7176
},
77+
awsCredentials: testAwsCredentials,
7278
}
7379
const expectedUri = vscode.Uri.parse(`${expectedUriBase}:specific:test-stream`)
7480
const uri = createLiveTailURIFromArgs(config)
@@ -84,6 +90,7 @@ describe('LiveTailSession URI', async function () {
8490
filter: 'test-stream',
8591
},
8692
logEventFilterPattern: 'test-filter',
93+
awsCredentials: testAwsCredentials,
8794
}
8895
const expectedUri = vscode.Uri.parse(`${expectedUriBase}:specific:test-stream:test-filter`)
8996
const uri = createLiveTailURIFromArgs(config)

0 commit comments

Comments
 (0)