33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- import { AWSError , Credentials , Service } from 'aws-sdk'
6+ import { AWSError , HttpRequest , Service } from 'aws-sdk'
77import globals from '../../shared/extensionGlobals'
88import * as CodeWhispererClient from './codewhispererclient'
99import * as CodeWhispererUserClient from './codewhispereruserclient'
@@ -13,8 +13,8 @@ import { hasVendedIamCredentials } from '../../auth/auth'
1313import { CodeWhispererSettings } from '../util/codewhispererSettings'
1414import { PromiseResult } from 'aws-sdk/lib/request'
1515import { AuthUtil } from '../util/authUtil'
16- import apiConfig = require( './service-2.json' )
1716import userApiConfig = require( './user-service-2.json' )
17+ import apiConfig = require( './service-2.json' )
1818import { session } from '../util/codeWhispererSession'
1919import { getLogger } from '../../shared/logger/logger'
2020import { getClientId , getOptOutPreference , getOperatingSystem } from '../../shared/telemetry/util'
@@ -83,8 +83,6 @@ export type Imports = CodeWhispererUserClient.Imports
8383
8484export class DefaultCodeWhispererClient {
8585 private async createSdkClient ( ) : Promise < CodeWhispererClient > {
86- throw new Error ( 'Do not call this function until IAM is supported by LSP identity server' )
87-
8886 const isOptedOut = CodeWhispererSettings . instance . isOptoutEnabled ( )
8987 const cwsprConfig = getCodewhispererConfig ( )
9088 return ( await globals . sdkClientBuilder . createAwsService (
@@ -127,7 +125,11 @@ export class DefaultCodeWhispererClient {
127125 async createUserSdkClient ( maxRetries ?: number ) : Promise < CodeWhispererUserClient > {
128126 const isOptedOut = CodeWhispererSettings . instance . isOptoutEnabled ( )
129127 session . setFetchCredentialStart ( )
130- const bearerToken = await AuthUtil . instance . getToken ( )
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+ }
132+
131133 session . setSdkApiCallStart ( )
132134 const cwsprConfig = getCodewhispererConfig ( )
133135 return ( await globals . sdkClientBuilder . createAwsService (
@@ -137,11 +139,10 @@ export class DefaultCodeWhispererClient {
137139 region : cwsprConfig . region ,
138140 endpoint : cwsprConfig . endpoint ,
139141 maxRetries : maxRetries ,
140- credentials : new Credentials ( { accessKeyId : 'xxx' , secretAccessKey : 'xxx' } ) ,
141142 onRequestSetup : [
142- ( req ) => {
143- req . on ( 'build' , ( { httpRequest } ) => {
144- httpRequest . headers [ 'Authorization' ] = `Bearer ${ bearerToken } `
143+ ( req : any ) => {
144+ req . on ( 'build' , ( { httpRequest } : { httpRequest : HttpRequest } ) => {
145+ httpRequest . headers [ 'Authorization' ] = `Bearer ${ credential } `
145146 } )
146147 if ( req . operation === 'generateCompletions' ) {
147148 req . on ( 'build' , ( ) => {
@@ -160,6 +161,88 @@ export class DefaultCodeWhispererClient {
160161 return AuthUtil . instance . isConnected ( ) // TODO: Handle IAM credentials
161162 }
162163
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+
163246 public async generateRecommendations (
164247 request : GenerateRecommendationsRequest
165248 ) : Promise < GenerateRecommendationsResponse > {
0 commit comments