@@ -11,11 +11,9 @@ import { showConfirmationMessage } from '../../shared/utilities/messages'
1111import globals from '../../shared/extensionGlobals'
1212import { once } from '../../shared/utilities/functionUtils'
1313import CodeWhispererUserClient from '../client/codewhispereruserclient'
14- import CodeWhispererClient from '../client/codewhispererclient'
1514import { Credentials , Service } from 'aws-sdk'
1615import { ServiceOptions } from '../../shared/awsClientBuilder'
1716import userApiConfig = require( '../client/user-service-2.json' )
18- import apiConfig = require( '../client/service-2.json' )
1917import { createConstantMap } from '../../shared/utilities/tsUtils'
2018import { getLogger } from '../../shared/logger/logger'
2119import { pageableToCollection } from '../../shared/utilities/collectionUtils'
@@ -146,70 +144,37 @@ export class RegionProfileManager {
146144 async listRegionProfile ( ) : Promise < RegionProfile [ ] > {
147145 this . _profiles = [ ]
148146
149- if ( ! this . authProvider . isConnected ( ) ) {
147+ if ( ! this . authProvider . isConnected ( ) || ! this . authProvider . isSsoSession ( ) ) {
150148 return [ ]
151149 }
152150 const availableProfiles : RegionProfile [ ] = [ ]
153151 const failedRegions : string [ ] = [ ]
154152
155153 for ( const [ region , endpoint ] of endpoints . entries ( ) ) {
154+ const client = await this . _createQClient ( region , endpoint )
155+ const requester = async ( request : CodeWhispererUserClient . ListAvailableProfilesRequest ) =>
156+ client . listAvailableProfiles ( request ) . promise ( )
157+ const request : CodeWhispererUserClient . ListAvailableProfilesRequest = { }
156158 try {
157- // Get region profiles (Q developer profiles) from Q client and authenticate with SSO token
158- if ( this . authProvider . isIdcConnection ( ) ) {
159- const client = await this . _createQUserClient ( region , endpoint )
160- const requester = async ( request : CodeWhispererUserClient . ListAvailableProfilesRequest ) => {
161- return client . listAvailableProfiles ( request ) . promise ( )
159+ const profiles = await pageableToCollection ( requester , request , 'nextToken' , 'profiles' )
160+ . flatten ( )
161+ . promise ( )
162+ const mappedPfs = profiles . map ( ( it ) => {
163+ let accntId = ''
164+ try {
165+ accntId = parse ( it . arn ) . accountId
166+ } catch ( e ) { }
167+
168+ return {
169+ name : it . profileName ,
170+ region : region ,
171+ arn : it . arn ,
172+ description : accntId ,
162173 }
163- const request : CodeWhispererUserClient . ListAvailableProfilesRequest = { }
164- const profiles = await pageableToCollection ( requester , request , 'nextToken' , 'profiles' )
165- . flatten ( )
166- . promise ( )
167- const mappedPfs = profiles . map ( ( it ) => {
168- let accntId = ''
169- try {
170- accntId = parse ( it . arn ) . accountId
171- } catch ( e ) { }
172-
173- return {
174- name : it . profileName ,
175- region : region ,
176- arn : it . arn ,
177- description : accntId ,
178- }
179- } )
180-
181- availableProfiles . push ( ...mappedPfs )
182- RegionProfileManager . logger . debug ( `Found ${ mappedPfs . length } profiles in region ${ region } ` )
183- }
184- // Get region profiles (Q developer profiles) from Q client and authenticate with IAM credentials
185- else if ( this . authProvider . isIamSession ( ) ) {
186- const client = await this . _createQServiceClient ( region , endpoint )
187- const requester = async ( request : CodeWhispererClient . ListProfilesRequest ) => {
188- return client . listProfiles ( request ) . promise ( )
189- }
190- const request : CodeWhispererClient . ListProfilesRequest = { }
191- const profiles = await pageableToCollection ( requester , request , 'nextToken' , 'profiles' )
192- . flatten ( )
193- . promise ( )
194- const mappedPfs = profiles . map ( ( it ) => {
195- let accntId = ''
196- try {
197- accntId = parse ( it . arn ) . accountId
198- } catch ( e ) { }
199-
200- return {
201- name : it . profileName ,
202- region : region ,
203- arn : it . arn ,
204- description : accntId ,
205- }
206- } )
174+ } )
207175
208- availableProfiles . push ( ...mappedPfs )
209- RegionProfileManager . logger . debug ( `Found ${ mappedPfs . length } profiles in region ${ region } ` )
210- } else {
211- throw new ToolkitError ( 'Failed to list profiles when signed out of identity center and IAM credentials' )
212- }
176+ availableProfiles . push ( ...mappedPfs )
177+ RegionProfileManager . logger . debug ( `Found ${ mappedPfs . length } profiles in region ${ region } ` )
213178 } catch ( e ) {
214179 const logMsg = isAwsError ( e ) ? `requestId=${ e . requestId } ; message=${ e . message } ` : ( e as Error ) . message
215180 RegionProfileManager . logger . error ( `Failed to list profiles for region ${ region } : ${ logMsg } ` )
@@ -235,7 +200,7 @@ export class RegionProfileManager {
235200 }
236201
237202 async switchRegionProfile ( regionProfile : RegionProfile | undefined , source : ProfileSwitchIntent ) {
238- if ( ! this . authProvider . isConnected ( ) ) {
203+ if ( ! this . authProvider . isConnected ( ) || ! this . authProvider . isIdcConnection ( ) ) {
239204 return
240205 }
241206
@@ -439,40 +404,27 @@ export class RegionProfileManager {
439404 if ( this . authProvider . isBuilderIdConnection ( ) ) {
440405 return false
441406 }
442- return ( this . authProvider . isIdcConnection ( ) || this . authProvider . isIamSession ( ) ) && this . activeRegionProfile === undefined
407+ return this . authProvider . isIdcConnection ( ) && this . activeRegionProfile === undefined
443408 }
444409
445410 async clearCache ( ) {
446411 await this . cache . clearCache ( )
447412 }
448413
449414 // TODO: Should maintain sdk client in a better way
450- // Create a Q user client compatible with SSO tokens
451- async createQUserClient ( profile : RegionProfile ) : Promise < CodeWhispererUserClient > {
452- if ( ! this . authProvider . isConnected ( ) ) {
453- throw new Error ( 'No valid connection' )
454- }
455- const endpoint = endpoints . get ( profile . region )
456- if ( ! endpoint ) {
457- throw new Error ( `trying to initiatize Q client with unrecognizable region ${ profile . region } ` )
458- }
459- return this . _createQUserClient ( profile . region , endpoint )
460- }
461-
462- // Create a Q service client compatible with IAM credentials
463- async createQServiceClient ( profile : RegionProfile ) : Promise < CodeWhispererClient > {
464- if ( ! this . authProvider . isConnected ( ) ) {
465- throw new Error ( 'No valid connection' )
415+ async createQClient ( profile : RegionProfile ) : Promise < CodeWhispererUserClient > {
416+ if ( ! this . authProvider . isConnected ( ) || ! this . authProvider . isSsoSession ( ) ) {
417+ throw new Error ( 'No valid SSO connection' )
466418 }
467419 const endpoint = endpoints . get ( profile . region )
468420 if ( ! endpoint ) {
469421 throw new Error ( `trying to initiatize Q client with unrecognizable region ${ profile . region } ` )
470422 }
471- return this . _createQServiceClient ( profile . region , endpoint )
423+ return this . _createQClient ( profile . region , endpoint )
472424 }
473425
474- // Visible for testing only, do not use this directly, please use createQUserClient (profile)
475- async _createQUserClient ( region : string , endpoint : string ) : Promise < CodeWhispererUserClient > {
426+ // Visible for testing only, do not use this directly, please use createQClient (profile)
427+ async _createQClient ( region : string , endpoint : string ) : Promise < CodeWhispererUserClient > {
476428 const token = await this . authProvider . getToken ( )
477429 const serviceOption : ServiceOptions = {
478430 apiConfig : userApiConfig ,
@@ -487,32 +439,13 @@ export class RegionProfileManager {
487439 } ,
488440 ] ,
489441 } as ServiceOptions
490-
491- return ( await globals . sdkClientBuilder . createAwsService (
442+
443+ const c = ( await globals . sdkClientBuilder . createAwsService (
492444 Service ,
493445 serviceOption ,
494446 undefined
495447 ) ) as CodeWhispererUserClient
496- }
497-
498- // Visible for testing only, do not use this directly, please use createQServiceClient(profile)
499- async _createQServiceClient ( region : string , endpoint : string ) : Promise < CodeWhispererClient > {
500- const credential = await this . authProvider . getIamCredential ( )
501- const serviceOption : ServiceOptions = {
502- apiConfig : apiConfig ,
503- region : region ,
504- endpoint : endpoint ,
505- credentials : new Credentials ( {
506- accessKeyId : credential . accessKeyId ,
507- secretAccessKey : credential . secretAccessKey ,
508- sessionToken : credential . sessionToken ,
509- } ) ,
510- } as ServiceOptions
511448
512- return ( await globals . sdkClientBuilder . createAwsService (
513- Service ,
514- serviceOption ,
515- undefined
516- ) ) as CodeWhispererClient
449+ return c
517450 }
518451}
0 commit comments