@@ -94,6 +94,22 @@ export type Login = SsoLogin | IamLogin
9494
9595export type TokenSource = IamIdentityCenterSsoTokenSource | AwsBuilderIdSsoTokenSource
9696
97+ export type IamProfileOptions = {
98+ accessKey ?: string
99+ secretKey ?: string
100+ sessionToken ?: string
101+ roleArn ?: string
102+ sourceProfile ?: string
103+ }
104+
105+ const IamProfileOptionsDefaults = {
106+ accessKey : '' ,
107+ secretKey : '' ,
108+ sessionToken : '' ,
109+ roleArn : '' ,
110+ sourceProfile : '' ,
111+ } satisfies IamProfileOptions
112+
97113/**
98114 * Handles auth requests to the Identity Server in the Amazon Q LSP.
99115 */
@@ -216,56 +232,32 @@ export class LanguageClientAuth {
216232 return this . client . sendRequest ( updateProfileRequestType . method , params )
217233 }
218234
219- async updateIamProfile (
220- profileName : string ,
221- accessKey : string ,
222- secretKey : string ,
223- sessionToken ?: string ,
224- roleArn ?: string ,
225- sourceProfile ?: string
226- ) : Promise < UpdateProfileResult > {
227- // Add credentials and delete SSO settings from profile
228- let profile : Profile
229- if ( roleArn && sourceProfile ) {
230- profile = {
231- kinds : [ ProfileKind . IamSourceProfileProfile ] ,
232- name : profileName ,
233- settings : {
234- sso_session : '' ,
235- aws_access_key_id : '' ,
236- aws_secret_access_key : '' ,
237- aws_session_token : '' ,
238- role_arn : roleArn ,
239- source_profile : sourceProfile ,
240- } ,
241- }
242- } else if ( accessKey && secretKey ) {
243- profile = {
244- kinds : [ ProfileKind . IamCredentialsProfile ] ,
245- name : profileName ,
246- settings : {
247- sso_session : '' ,
248- aws_access_key_id : accessKey ,
249- aws_secret_access_key : secretKey ,
250- aws_session_token : sessionToken ,
251- role_arn : '' ,
252- source_profile : '' ,
253- } ,
254- }
235+ async updateIamProfile ( profileName : string , opts : IamProfileOptions ) : Promise < UpdateProfileResult > {
236+ // Substitute missing fields for defaults
237+ const fields = { ...IamProfileOptionsDefaults , ...opts }
238+ // Get the profile kind matching the provided fields
239+ let kind : ProfileKind
240+ if ( fields . roleArn && fields . sourceProfile ) {
241+ kind = ProfileKind . IamSourceProfileProfile
242+ } else if ( fields . accessKey && fields . secretKey ) {
243+ kind = ProfileKind . IamCredentialsProfile
255244 } else {
256- profile = {
257- kinds : [ ProfileKind . Unknown ] ,
245+ kind = ProfileKind . Unknown
246+ }
247+
248+ const params = await this . encrypt ( {
249+ profile : {
250+ kinds : [ kind ] ,
258251 name : profileName ,
259252 settings : {
260- aws_access_key_id : '' ,
261- aws_secret_access_key : '' ,
262- aws_session_token : '' ,
263- role_arn : '' ,
264- source_profile : '' ,
253+ aws_access_key_id : fields . accessKey ,
254+ aws_secret_access_key : fields . secretKey ,
255+ aws_session_token : fields . sessionToken ,
256+ role_arn : fields . roleArn ,
257+ source_profile : fields . sourceProfile ,
265258 } ,
266- }
267- }
268- const params = await this . encrypt ( { profile : profile } )
259+ } ,
260+ } )
269261 return this . client . sendRequest ( updateProfileRequestType . method , params )
270262 }
271263
@@ -567,7 +559,7 @@ export class IamLogin extends BaseLogin {
567559 lspAuth . registerGetMfaCodeHandler ( ( params : GetMfaCodeParams ) => this . getMfaCodeHandler ( params ) )
568560 }
569561
570- async login ( opts : { accessKey : string ; secretKey : string ; sessionToken ?: string ; roleArn ?: string } ) {
562+ async login ( opts : IamProfileOptions ) {
571563 await this . updateProfile ( opts )
572564 return this . _getIamCredential ( true )
573565 }
@@ -583,34 +575,33 @@ export class IamLogin extends BaseLogin {
583575 if ( this . iamCredentialId ) {
584576 await this . lspAuth . invalidateStsCredential ( this . iamCredentialId )
585577 }
586- await this . lspAuth . updateIamProfile ( this . profileName , '' , '' , '' , '' , '' )
587- await this . lspAuth . updateIamProfile ( this . profileName + '-source' , '' , '' , '' , '' , '' )
578+ await this . lspAuth . updateIamProfile ( this . profileName , { } )
579+ await this . lspAuth . updateIamProfile ( this . profileName + '-source' , { } )
588580 this . updateConnectionState ( 'notConnected' )
589581 this . _data = undefined
590582 // TODO: DeleteProfile api in Identity Service (this doesn't exist yet)
591583 }
592584
593- async updateProfile ( opts : { accessKey : string ; secretKey : string ; sessionToken ?: string ; roleArn ?: string } ) {
585+ async updateProfile ( opts : IamProfileOptions ) {
594586 if ( opts . roleArn ) {
587+ // Create the source and target profiles
595588 const sourceProfile = this . profileName + '-source'
596- await this . lspAuth . updateIamProfile (
597- sourceProfile ,
598- opts . accessKey ,
599- opts . secretKey ,
600- opts . sessionToken ,
601- '' ,
602- ''
603- )
604- await this . lspAuth . updateIamProfile ( this . profileName , '' , '' , '' , opts . roleArn , sourceProfile )
589+ await this . lspAuth . updateIamProfile ( sourceProfile , {
590+ accessKey : opts . accessKey ,
591+ secretKey : opts . secretKey ,
592+ sessionToken : opts . sessionToken ,
593+ } )
594+ await this . lspAuth . updateIamProfile ( this . profileName , {
595+ roleArn : opts . roleArn ,
596+ sourceProfile : sourceProfile ,
597+ } )
605598 } else {
606- await this . lspAuth . updateIamProfile (
607- this . profileName ,
608- opts . accessKey ,
609- opts . secretKey ,
610- opts . sessionToken ,
611- '' ,
612- ''
613- )
599+ // Create the target profile
600+ await this . lspAuth . updateIamProfile ( this . profileName , {
601+ accessKey : opts . accessKey ,
602+ secretKey : opts . secretKey ,
603+ sessionToken : opts . sessionToken ,
604+ } )
614605 }
615606 }
616607
0 commit comments