Skip to content

Commit eb3526a

Browse files
committed
undo unnecessary changes
1 parent f61bcee commit eb3526a

File tree

7 files changed

+44
-139
lines changed

7 files changed

+44
-139
lines changed

aws-toolkit-vscode.code-workspace

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
{
1313
"path": "packages/amazonq",
1414
},
15-
{
16-
"path": "../language-server-runtimes",
17-
},
18-
{
19-
"path": "../language-servers",
20-
},
2115
],
2216
"settings": {
2317
"typescript.tsdk": "node_modules/typescript/lib",

packages/amazonq/.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"SSMDOCUMENT_LANGUAGESERVER_PORT": "6010",
1616
"WEBPACK_DEVELOPER_SERVER": "http://localhost:8080",
1717
// Below allows for overrides used during development
18-
"__AMAZONQLSP_PATH": "${workspaceFolder}/../../../language-servers/app/aws-lsp-codewhisperer-runtimes/out/agent-standalone.js",
19-
"__AMAZONQLSP_UI": "${workspaceFolder}/../../../language-servers/chat-client/build/amazonq-ui.js"
18+
// "__AMAZONQLSP_PATH": "${workspaceFolder}/../../../language-servers/app/aws-lsp-codewhisperer-runtimes/out/agent-standalone.js",
19+
// "__AMAZONQLSP_UI": "${workspaceFolder}/../../../language-servers/chat-client/build/amazonq-ui.js"
2020
},
2121
"envFile": "${workspaceFolder}/.local.env",
2222
"outFiles": ["${workspaceFolder}/dist/**/*.js", "${workspaceFolder}/../core/dist/**/*.js"],

packages/core/src/amazonqFeatureDev/client/featureDev.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,18 @@ const writeAPIRetryOptions = {
5151

5252
// Create a client for featureDev proxy client based off of aws sdk v2
5353
export async function createFeatureDevProxyClient(options?: Partial<ServiceOptions>): Promise<FeatureDevProxyClient> {
54-
const credential = await AuthUtil.instance.getCredential()
55-
// TODO: handle IAM credentials when IAM version of API JSON file is generated
56-
if (credential !== 'string') {
57-
throw new Error('Feature dev does not support IAM credentials')
58-
}
54+
const bearerToken = await AuthUtil.instance.getBearerToken()
5955
const cwsprConfig = getCodewhispererConfig()
6056
return (await globals.sdkClientBuilder.createAwsService(
6157
Service,
6258
{
6359
apiConfig: apiConfig,
6460
region: cwsprConfig.region,
6561
endpoint: cwsprConfig.endpoint,
62+
token: new Token({ token: bearerToken }),
6663
httpOptions: {
6764
connectTimeout: 10000, // 10 seconds, 3 times P99 API latency
6865
},
69-
token: new Token({ token: credential }),
7066
...options,
7167
} as ServiceOptions,
7268
undefined

packages/core/src/codewhisperer/client/codewhisperer.ts

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ export class DefaultCodeWhispererClient {
125125
async createUserSdkClient(maxRetries?: number): Promise<CodeWhispererUserClient> {
126126
const isOptedOut = CodeWhispererSettings.instance.isOptoutEnabled()
127127
session.setFetchCredentialStart()
128-
const credential = await AuthUtil.instance.getCredential()
129-
if (typeof credential !== 'string') {
130-
throw new TypeError('Cannot create user SDK client from IAM credentials')
131-
}
128+
const credential = await AuthUtil.instance.getBearerToken()
132129

133130
session.setSdkApiCallStart()
134131
const cwsprConfig = getCodewhispererConfig()
@@ -161,88 +158,6 @@ export class DefaultCodeWhispererClient {
161158
return AuthUtil.instance.isConnected() // TODO: Handle IAM credentials
162159
}
163160

164-
// private async createServiceSdkClient(credential: IamCredentials): Promise<CodeWhispererClient> {
165-
// const isOptedOut = CodeWhispererSettings.instance.isOptoutEnabled()
166-
// const cwsprConfig = getCodewhispererConfig()
167-
// return (await globals.sdkClientBuilder.createAwsService(
168-
// Service,
169-
// {
170-
// apiConfig: apiConfig,
171-
// region: cwsprConfig.region,
172-
// credentials: undefined,
173-
// endpoint: cwsprConfig.endpoint,
174-
// onRequestSetup: [
175-
// (req) => {
176-
// if (req.operation === 'listRecommendations') {
177-
// req.on('build', () => {
178-
// req.httpRequest.headers['x-amzn-codewhisperer-optout'] = `${isOptedOut}`
179-
// })
180-
// }
181-
// // This logic is for backward compatability with legacy SDK v2 behavior for refreshing
182-
// // credentials. Once the Toolkit adds a file watcher for credentials it won't be needed.
183-
184-
// if (hasVendedIamCredentials()) {
185-
// req.on('retry', (resp) => {
186-
// if (
187-
// resp.error?.code === 'AccessDeniedException' &&
188-
// resp.error.message.match(/expired/i)
189-
// ) {
190-
// // AuthUtil.instance.reauthenticate().catch((e) => {
191-
// // getLogger().error('reauthenticate failed: %s', (e as Error).message)
192-
// // })
193-
// resp.error.retryable = true
194-
// }
195-
// })
196-
// }
197-
// },
198-
// ],
199-
// } as ServiceOptions,
200-
// undefined
201-
// )) as CodeWhispererClient
202-
// }
203-
204-
// private async createUserServiceSdkClient(
205-
// credential: string,
206-
// maxRetries?: number
207-
// ): Promise<CodeWhispererUserClient> {
208-
// const isOptedOut = CodeWhispererSettings.instance.isOptoutEnabled()
209-
// session.setFetchCredentialStart()
210-
// session.setSdkApiCallStart()
211-
// const cwsprConfig = getCodewhispererConfig()
212-
// return (await globals.sdkClientBuilder.createAwsService(
213-
// Service,
214-
// {
215-
// apiConfig: userApiConfig,
216-
// region: cwsprConfig.region,
217-
// endpoint: cwsprConfig.endpoint,
218-
// maxRetries: maxRetries,
219-
// onRequestSetup: [
220-
// (req: any) => {
221-
// req.on('build', ({ httpRequest }: { httpRequest: HttpRequest }) => {
222-
// httpRequest.headers['Authorization'] = `Bearer ${credential}`
223-
// })
224-
// if (req.operation === 'generateCompletions') {
225-
// req.on('build', () => {
226-
// req.httpRequest.headers['x-amzn-codewhisperer-optout'] = `${isOptedOut}`
227-
// req.httpRequest.headers['Connection'] = keepAliveHeader
228-
// })
229-
// }
230-
// },
231-
// ],
232-
// } as ServiceOptions,
233-
// undefined
234-
// )) as CodeWhispererUserClient
235-
// }
236-
237-
// async createSdkClient(maxRetries?: number): Promise<CodeWhispererUserClient | CodeWhispererClient> {
238-
// const credential = await AuthUtil.instance.getCredential()
239-
// if (typeof credential === 'string') {
240-
// return this.createUserServiceSdkClient(credential, maxRetries)
241-
// } else {
242-
// return this.createServiceSdkClient(credential)
243-
// }
244-
// }
245-
246161
public async generateRecommendations(
247162
request: GenerateRecommendationsRequest
248163
): Promise<GenerateRecommendationsResponse> {

packages/core/src/codewhisperer/region/regionProfileManager.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { once } from '../../shared/utilities/functionUtils'
1313
import CodeWhispererUserClient from '../client/codewhispereruserclient'
1414
import { Credentials, HttpRequest, Service } from 'aws-sdk'
1515
import { ServiceOptions } from '../../shared/awsClientBuilder'
16-
import tokenApiConfig = require('../client/user-service-2.json')
17-
import iamApiConfig = require('../client/service-2.json')
16+
import userApiConfig = require('../client/user-service-2.json')
17+
import apiConfig = require('../client/service-2.json')
1818
import { createConstantMap } from '../../shared/utilities/tsUtils'
1919
import { getLogger } from '../../shared/logger/logger'
2020
import { pageableToCollection } from '../../shared/utilities/collectionUtils'
@@ -426,28 +426,30 @@ export class RegionProfileManager {
426426

427427
// Visible for testing only, do not use this directly, please use createQClient(profile)
428428
async _createQClient(region: string, endpoint: string): Promise<CodeWhispererUserClient> {
429-
const credential = await this.authProvider.getCredential()
430-
const authConfig: ServiceOptions =
431-
typeof credential === 'string'
432-
? {
433-
onRequestSetup: [
434-
(req: any) => {
435-
req.on('build', ({ httpRequest }: { httpRequest: HttpRequest }) => {
436-
httpRequest.headers['Authorization'] = `Bearer ${credential}`
437-
})
438-
},
439-
],
440-
}
441-
: {
442-
credentials: new Credentials({
443-
accessKeyId: credential.accessKeyId,
444-
secretAccessKey: credential.secretAccessKey,
445-
sessionToken: credential.sessionToken,
446-
}),
447-
}
448-
const apiConfig = typeof credential === 'string' ? tokenApiConfig : iamApiConfig
429+
let authConfig: ServiceOptions = {}
430+
if (this.authProvider.isSsoSession()) {
431+
const credential = await this.authProvider.getBearerToken()
432+
authConfig = {
433+
onRequestSetup: [
434+
(req: any) => {
435+
req.on('build', ({ httpRequest }: { httpRequest: HttpRequest }) => {
436+
httpRequest.headers['Authorization'] = `Bearer ${credential}`
437+
})
438+
},
439+
],
440+
}
441+
} else if (this.authProvider.isIamSession()) {
442+
const credential = await this.authProvider.getIamCredential()
443+
authConfig = {
444+
credentials: new Credentials({
445+
accessKeyId: credential.accessKeyId,
446+
secretAccessKey: credential.secretAccessKey,
447+
sessionToken: credential.sessionToken,
448+
}),
449+
}
450+
}
449451
const serviceOption: ServiceOptions = {
450-
apiConfig: apiConfig,
452+
apiConfig: this.authProvider.isSsoSession() ? userApiConfig : apiConfig,
451453
region: region,
452454
endpoint: endpoint,
453455
...authConfig,

packages/core/src/codewhisperer/util/authUtil.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export interface IAuthProvider {
6161
isIdcConnection(): boolean
6262
isSsoSession(): boolean
6363
isIamSession(): boolean
64-
getCredential(): Promise<string | IamCredentials>
64+
getBearerToken(): Promise<string>
65+
getIamCredential(): Promise<IamCredentials>
6566
readonly profileName: string
6667
readonly connection?: { startUrl?: string; region?: string; accessKey?: string; secretKey?: string }
6768
}
@@ -201,9 +202,17 @@ export class AuthUtil implements IAuthProvider {
201202
return response
202203
}
203204

204-
async getCredential() {
205+
async getBearerToken() {
206+
if (this.isSsoSession()) {
207+
return (await (this.session as SsoLogin).getCredential()).credential
208+
} else {
209+
throw new ToolkitError('Cannot get credential without logging in.')
210+
}
211+
}
212+
213+
async getIamCredential() {
205214
if (this.session) {
206-
return (await this.session.getCredential()).credential
215+
return (await (this.session as IamLogin).getCredential()).credential
207216
} else {
208217
throw new ToolkitError('Cannot get credential without logging in.')
209218
}

packages/core/src/shared/clients/codewhispererChatClient.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,17 @@ import { ConfiguredRetryStrategy } from '@smithy/util-retry'
77
import { getCodewhispererConfig } from '../../codewhisperer/client/codewhisperer'
88
import { AuthUtil } from '../../codewhisperer/util/authUtil'
99
import { getUserAgent } from '../telemetry/util'
10-
import { Credentials, Token } from 'aws-sdk'
1110

1211
// Create a client for featureDev streaming based off of aws sdk v3
1312
export async function createCodeWhispererChatStreamingClient(): Promise<CodeWhispererStreaming> {
14-
const credential = await AuthUtil.instance.getCredential()
15-
const authConfig =
16-
typeof credential === 'string'
17-
? { token: new Token({ token: credential }) }
18-
: {
19-
credentials: new Credentials({
20-
accessKeyId: credential.accessKeyId,
21-
secretAccessKey: credential.secretAccessKey,
22-
sessionToken: credential.sessionToken,
23-
}),
24-
}
13+
const bearerToken = await AuthUtil.instance.getBearerToken()
2514
const cwsprConfig = getCodewhispererConfig()
2615
const streamingClient = new CodeWhispererStreaming({
2716
region: cwsprConfig.region,
2817
endpoint: cwsprConfig.endpoint,
18+
token: { token: bearerToken },
2919
customUserAgent: getUserAgent(),
3020
retryStrategy: new ConfiguredRetryStrategy(1, (attempt: number) => 500 + attempt ** 10),
31-
...authConfig,
3221
})
3322
return streamingClient
3423
}

0 commit comments

Comments
 (0)