Skip to content

Commit ed79310

Browse files
author
Keegan Irby
committed
Pass in active credentials when created CWL client for LiveTail
1 parent 7b4477e commit ed79310

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-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)

0 commit comments

Comments
 (0)