@@ -95,6 +95,22 @@ export type Login = SsoLogin | IamLogin
9595
9696export type TokenSource = IamIdentityCenterSsoTokenSource | AwsBuilderIdSsoTokenSource
9797
98+ export type IamProfileOptions = {
99+ accessKey ?: string
100+ secretKey ?: string
101+ sessionToken ?: string
102+ roleArn ?: string
103+ sourceProfile ?: string
104+ }
105+
106+ const IamProfileOptionsDefaults = {
107+ accessKey : '' ,
108+ secretKey : '' ,
109+ sessionToken : '' ,
110+ roleArn : '' ,
111+ sourceProfile : '' ,
112+ } satisfies IamProfileOptions
113+
98114/**
99115 * Handles auth requests to the Identity Server in the Amazon Q LSP.
100116 */
@@ -185,58 +201,32 @@ export class LanguageClientAuth {
185201 } satisfies UpdateProfileParams )
186202 }
187203
188- updateIamProfile (
189- profileName : string ,
190- accessKey : string ,
191- secretKey : string ,
192- sessionToken ?: string ,
193- roleArn ?: string ,
194- sourceProfile ?: string
195- ) : Promise < UpdateProfileResult > {
196- // Add credentials and delete SSO settings from profile
197- let profile : Profile
198- if ( roleArn && sourceProfile ) {
199- profile = {
200- kinds : [ ProfileKind . IamSourceProfileProfile ] ,
201- name : profileName ,
202- settings : {
203- sso_session : '' ,
204- aws_access_key_id : '' ,
205- aws_secret_access_key : '' ,
206- aws_session_token : '' ,
207- role_arn : roleArn ,
208- source_profile : sourceProfile ,
209- } ,
210- }
211- } else if ( accessKey && secretKey ) {
212- profile = {
213- kinds : [ ProfileKind . IamCredentialsProfile ] ,
214- name : profileName ,
215- settings : {
216- sso_session : '' ,
217- aws_access_key_id : accessKey ,
218- aws_secret_access_key : secretKey ,
219- aws_session_token : sessionToken ,
220- role_arn : '' ,
221- source_profile : '' ,
222- } ,
223- }
204+ updateIamProfile ( profileName : string , opts : IamProfileOptions ) : Promise < UpdateProfileResult > {
205+ // Substitute missing fields for defaults
206+ const fields = { ...IamProfileOptionsDefaults , ...opts }
207+ // Get the profile kind matching the provided fields
208+ let kind : ProfileKind
209+ if ( fields . roleArn && fields . sourceProfile ) {
210+ kind = ProfileKind . IamSourceProfileProfile
211+ } else if ( fields . accessKey && fields . secretKey ) {
212+ kind = ProfileKind . IamCredentialsProfile
224213 } else {
225- profile = {
226- kinds : [ ProfileKind . Unknown ] ,
214+ kind = ProfileKind . Unknown
215+ }
216+
217+ return this . client . sendRequest ( updateProfileRequestType . method , {
218+ profile : {
219+ kinds : [ kind ] ,
227220 name : profileName ,
228221 settings : {
229- aws_access_key_id : '' ,
230- aws_secret_access_key : '' ,
231- aws_session_token : '' ,
232- role_arn : '' ,
233- source_profile : '' ,
222+ aws_access_key_id : fields . accessKey ,
223+ aws_secret_access_key : fields . secretKey ,
224+ aws_session_token : fields . sessionToken ,
225+ role_arn : fields . roleArn ,
226+ source_profile : fields . sourceProfile ,
234227 } ,
235- }
236- }
237- return this . client . sendRequest ( updateProfileRequestType . method , {
238- profile : profile ,
239- } satisfies UpdateProfileParams )
228+ } ,
229+ } )
240230 }
241231
242232 listProfiles ( ) {
@@ -550,7 +540,7 @@ export class IamLogin extends BaseLogin {
550540 lspAuth . registerGetMfaCodeHandler ( ( params : GetMfaCodeParams ) => this . getMfaCodeHandler ( params ) )
551541 }
552542
553- async login ( opts : { accessKey : string ; secretKey : string ; sessionToken ?: string ; roleArn ?: string } ) {
543+ async login ( opts : IamProfileOptions ) {
554544 await this . updateProfile ( opts )
555545 return this . _getIamCredential ( true )
556546 }
@@ -566,34 +556,33 @@ export class IamLogin extends BaseLogin {
566556 if ( this . iamCredentialId ) {
567557 await this . lspAuth . invalidateStsCredential ( this . iamCredentialId )
568558 }
569- await this . lspAuth . updateIamProfile ( this . profileName , '' , '' , '' , '' , '' )
570- await this . lspAuth . updateIamProfile ( this . profileName + '-source' , '' , '' , '' , '' , '' )
559+ await this . lspAuth . updateIamProfile ( this . profileName , { } )
560+ await this . lspAuth . updateIamProfile ( this . profileName + '-source' , { } )
571561 this . updateConnectionState ( 'notConnected' )
572562 this . _data = undefined
573563 // TODO: DeleteProfile api in Identity Service (this doesn't exist yet)
574564 }
575565
576- async updateProfile ( opts : { accessKey : string ; secretKey : string ; sessionToken ?: string ; roleArn ?: string } ) {
566+ async updateProfile ( opts : IamProfileOptions ) {
577567 if ( opts . roleArn ) {
568+ // Create the source and target profiles
578569 const sourceProfile = this . profileName + '-source'
579- await this . lspAuth . updateIamProfile (
580- sourceProfile ,
581- opts . accessKey ,
582- opts . secretKey ,
583- opts . sessionToken ,
584- '' ,
585- ''
586- )
587- await this . lspAuth . updateIamProfile ( this . profileName , '' , '' , '' , opts . roleArn , sourceProfile )
570+ await this . lspAuth . updateIamProfile ( sourceProfile , {
571+ accessKey : opts . accessKey ,
572+ secretKey : opts . secretKey ,
573+ sessionToken : opts . sessionToken ,
574+ } )
575+ await this . lspAuth . updateIamProfile ( this . profileName , {
576+ roleArn : opts . roleArn ,
577+ sourceProfile : sourceProfile ,
578+ } )
588579 } else {
589- await this . lspAuth . updateIamProfile (
590- this . profileName ,
591- opts . accessKey ,
592- opts . secretKey ,
593- opts . sessionToken ,
594- '' ,
595- ''
596- )
580+ // Create the target profile
581+ await this . lspAuth . updateIamProfile ( this . profileName , {
582+ accessKey : opts . accessKey ,
583+ secretKey : opts . secretKey ,
584+ sessionToken : opts . sessionToken ,
585+ } )
597586 }
598587 }
599588
0 commit comments