33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- import { AWSError , HttpRequest , Service } from 'aws-sdk'
6+ import { AWSError , Credentials , 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 userApiConfig = require( './user-service-2.json' )
1716import apiConfig = require( './service-2.json' )
17+ import userApiConfig = require( './user-service-2.json' )
1818import { session } from '../util/codeWhispererSession'
1919import { getLogger } from '../../shared/logger/logger'
2020import { getClientId , getOptOutPreference , getOperatingSystem } from '../../shared/telemetry/util'
@@ -84,13 +84,14 @@ export type Imports = CodeWhispererUserClient.Imports
8484export class DefaultCodeWhispererClient {
8585 private async createSdkClient ( ) : Promise < CodeWhispererClient > {
8686 const isOptedOut = CodeWhispererSettings . instance . isOptoutEnabled ( )
87+ const credential = await AuthUtil . instance . getIamCredential ( )
8788 const cwsprConfig = getCodewhispererConfig ( )
8889 return ( await globals . sdkClientBuilder . createAwsService (
8990 Service ,
9091 {
9192 apiConfig : apiConfig ,
9293 region : cwsprConfig . region ,
93- credentials : undefined ,
94+ credentials : new Credentials ( { accessKeyId : credential . accessKeyId , secretAccessKey : credential . secretAccessKey } ) ,
9495 endpoint : cwsprConfig . endpoint ,
9596 onRequestSetup : [
9697 ( req ) => {
@@ -108,9 +109,9 @@ export class DefaultCodeWhispererClient {
108109 resp . error ?. code === 'AccessDeniedException' &&
109110 resp . error . message . match ( / e x p i r e d / i)
110111 ) {
111- // AuthUtil.instance.reauthenticate().catch((e) => {
112- // getLogger().error('reauthenticate failed: %s', (e as Error).message)
113- // })
112+ AuthUtil . instance . reauthenticate ( ) ? .catch ( ( e ) => {
113+ getLogger ( ) . error ( 'reauthenticate failed: %s' , ( e as Error ) . message )
114+ } )
114115 resp . error . retryable = true
115116 }
116117 } )
@@ -125,7 +126,7 @@ export class DefaultCodeWhispererClient {
125126 async createUserSdkClient ( maxRetries ?: number ) : Promise < CodeWhispererUserClient > {
126127 const isOptedOut = CodeWhispererSettings . instance . isOptoutEnabled ( )
127128 session . setFetchCredentialStart ( )
128- const credential = await AuthUtil . instance . getBearerToken ( )
129+ const bearerToken = await AuthUtil . instance . getToken ( )
129130
130131 session . setSdkApiCallStart ( )
131132 const cwsprConfig = getCodewhispererConfig ( )
@@ -137,9 +138,9 @@ export class DefaultCodeWhispererClient {
137138 endpoint : cwsprConfig . endpoint ,
138139 maxRetries : maxRetries ,
139140 onRequestSetup : [
140- ( req : any ) => {
141- req . on ( 'build' , ( { httpRequest } : { httpRequest : HttpRequest } ) => {
142- httpRequest . headers [ 'Authorization' ] = `Bearer ${ credential } `
141+ ( req ) => {
142+ req . on ( 'build' , ( { httpRequest } ) => {
143+ httpRequest . headers [ 'Authorization' ] = `Bearer ${ bearerToken } `
143144 } )
144145 if ( req . operation === 'generateCompletions' ) {
145146 req . on ( 'build' , ( ) => {
0 commit comments