Skip to content

Commit 654d96a

Browse files
committed
Split IamCredentialProfile into subprofiles
1 parent 08b5856 commit 654d96a

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

packages/core/src/auth/auth2.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,49 @@ export class LanguageClientAuth {
177177
} satisfies UpdateProfileParams)
178178
}
179179

180-
updateIamProfile(profileName: string, accessKey: string, secretKey: string, sessionToken?: string, roleArn?: string): Promise<UpdateProfileResult> {
180+
updateIamProfile(profileName: string, accessKey: string, secretKey: string, sessionToken?: string, roleArn?: string, sourceProfile?: string): Promise<UpdateProfileResult> {
181181
// Add credentials and delete SSO settings from profile
182-
return this.client.sendRequest(updateProfileRequestType.method, {
183-
profile: {
184-
kinds: [ProfileKind.IamCredentialProfile],
182+
let profile: Profile
183+
if (roleArn) {
184+
profile = {
185+
kinds: [ProfileKind.RoleSourceProfile],
186+
name: profileName,
187+
settings: {
188+
region: '',
189+
sso_session: '',
190+
role_arn: roleArn,
191+
source_profile: sourceProfile,
192+
},
193+
}
194+
} else if (accessKey && secretKey) {
195+
profile = {
196+
kinds: [ProfileKind.IamUserProfile],
185197
name: profileName,
186198
settings: {
187199
region: '',
188200
sso_session: '',
189201
aws_access_key_id: accessKey,
190202
aws_secret_access_key: secretKey,
191203
aws_session_token: sessionToken,
192-
role_arn: roleArn,
193204
},
194-
},
205+
}
206+
} else {
207+
profile = {
208+
kinds: [ProfileKind.Unknown],
209+
name: profileName,
210+
settings: {
211+
region: '',
212+
sso_session: '',
213+
aws_access_key_id: '',
214+
aws_secret_access_key: '',
215+
aws_session_token: '',
216+
role_arn: '',
217+
source_profile: '',
218+
},
219+
}
220+
}
221+
return this.client.sendRequest(updateProfileRequestType.method, {
222+
profile: profile,
195223
ssoSession: {
196224
name: profileName,
197225
settings: undefined,
@@ -514,34 +542,27 @@ export class IamLogin extends BaseLogin {
514542
if (this.iamCredentialId) {
515543
await this.lspAuth.invalidateStsCredential(this.iamCredentialId)
516544
}
517-
await this.lspAuth.updateIamProfile(this.profileName, '', '', '', '')
545+
await this.lspAuth.updateIamProfile(this.profileName, '', '', '', '', '')
546+
await this.lspAuth.updateIamProfile(this.profileName + '-source', '', '', '', '', '')
518547
this.updateConnectionState('notConnected')
519548
this._data = undefined
520549
// TODO: DeleteProfile api in Identity Service (this doesn't exist yet)
521550
}
522551

523552
async updateProfile(opts: { accessKey: string; secretKey: string, sessionToken?: string, roleArn?: string }) {
524-
await this.lspAuth.updateIamProfile(this.profileName, opts.accessKey, opts.secretKey, opts.sessionToken, opts.roleArn)
525-
this._data = {
526-
accessKey: opts.accessKey,
527-
secretKey: opts.secretKey,
528-
sessionToken: opts.sessionToken,
553+
if (opts.roleArn) {
554+
const sourceProfile = this.profileName + '-source'
555+
await this.lspAuth.updateIamProfile(sourceProfile, opts.accessKey, opts.secretKey, opts.sessionToken, '', '')
556+
await this.lspAuth.updateIamProfile(this.profileName, '', '', '', opts.roleArn, sourceProfile)
557+
} else {
558+
await this.lspAuth.updateIamProfile(this.profileName, opts.accessKey, opts.secretKey, opts.sessionToken, '', '')
529559
}
530560
}
531561

532562
/**
533563
* Restore the connection state and connection details to memory, if they exist.
534564
*/
535565
async restore() {
536-
const sessionData = await this.getProfile()
537-
const credentials = sessionData?.profile?.settings
538-
if (credentials?.aws_access_key_id && credentials?.aws_secret_access_key) {
539-
this._data = {
540-
accessKey: credentials.aws_access_key_id,
541-
secretKey: credentials.aws_secret_access_key,
542-
sessionToken: credentials.aws_session_token
543-
}
544-
}
545566
try {
546567
await this._getIamCredential(false)
547568
} catch (err) {
@@ -597,7 +618,13 @@ export class IamLogin extends BaseLogin {
597618
this.cancellationToken = undefined
598619
}
599620

600-
if (response.credentials.sessionToken) {
621+
// Update cached credentials and credential ID
622+
if (response.credentials.accessKeyId && response.credentials.secretAccessKey) {
623+
this._data = {
624+
accessKey: response.credentials.accessKeyId,
625+
secretKey: response.credentials.secretAccessKey,
626+
sessionToken: response.credentials.sessionToken,
627+
}
601628
this.iamCredentialId = response.id
602629
}
603630
this.updateConnectionState('connected')

0 commit comments

Comments
 (0)