Skip to content

Commit a241b1c

Browse files
committed
Implement updateIamProfile and change STS references to IAM
1 parent eb0be34 commit a241b1c

File tree

2 files changed

+96
-79
lines changed

2 files changed

+96
-79
lines changed

packages/core/src/auth/auth2.ts

Lines changed: 92 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ import {
99
GetSsoTokenParams,
1010
getSsoTokenRequestType,
1111
GetSsoTokenResult,
12-
GetStsCredentialParams,
13-
getStsCredentialRequestType,
14-
GetStsCredentialResult,
12+
GetIamCredentialParams,
13+
getIamCredentialRequestType,
14+
GetIamCredentialResult,
1515
IamIdentityCenterSsoTokenSource,
1616
InvalidateSsoTokenParams,
1717
invalidateSsoTokenRequestType,
1818
ProfileKind,
1919
UpdateProfileParams,
2020
updateProfileRequestType,
2121
SsoTokenChangedParams,
22-
StsCredentialChangedParams,
22+
// StsCredentialChangedParams,
2323
ssoTokenChangedRequestType,
24-
stsCredentialChangedRequestType,
24+
// stsCredentialChangedRequestType,
2525
AwsBuilderIdSsoTokenSource,
2626
UpdateCredentialsParams,
2727
AwsErrorCodes,
@@ -45,10 +45,9 @@ import {
4545
iamCredentialsUpdateRequestType,
4646
Profile,
4747
SsoSession,
48-
IamSession,
49-
invalidateStsCredentialRequestType,
50-
InvalidateStsCredentialParams,
51-
InvalidateStsCredentialResult,
48+
// invalidateStsCredentialRequestType,
49+
// InvalidateStsCredentialParams,
50+
// InvalidateStsCredentialResult,
5251
} from '@aws/language-server-runtimes/protocol'
5352
import { LanguageClient } from 'vscode-languageclient'
5453
import { getLogger } from '../shared/logger/logger'
@@ -122,15 +121,15 @@ export class LanguageClientAuth {
122121
)
123122
}
124123

125-
getStsCredential(login: boolean = false, cancellationToken?: CancellationToken): Promise<GetStsCredentialResult> {
124+
getIamCredential(login: boolean = false, cancellationToken?: CancellationToken): Promise<GetIamCredentialResult> {
126125
return this.client.sendRequest(
127-
getStsCredentialRequestType.method,
126+
getIamCredentialRequestType.method,
128127
{
129128
clientName: this.clientName,
130129
options: {
131130
loginOnInvalidToken: login,
132131
},
133-
} satisfies GetStsCredentialParams,
132+
} satisfies GetIamCredentialParams,
134133
cancellationToken
135134
)
136135
}
@@ -141,13 +140,16 @@ export class LanguageClientAuth {
141140
region: string,
142141
scopes: string[]
143142
): Promise<UpdateProfileResult> {
143+
// Add SSO settings and delete credentials from profile
144144
return this.client.sendRequest(updateProfileRequestType.method, {
145145
profile: {
146146
kinds: [ProfileKind.SsoTokenProfile],
147147
name: profileName,
148148
settings: {
149-
region,
149+
region: region,
150150
sso_session: profileName,
151+
aws_access_key_id: '',
152+
aws_secret_access_key: '',
151153
},
152154
},
153155
ssoSession: {
@@ -162,18 +164,22 @@ export class LanguageClientAuth {
162164
}
163165

164166
updateIamProfile(profileName: string, accessKey: string, secretKey: string): Promise<UpdateProfileResult> {
167+
// Add credentials and delete SSO settings from profile
165168
return this.client.sendRequest(updateProfileRequestType.method, {
166169
profile: {
167-
kinds: [ProfileKind.SsoTokenProfile],
168-
name: profileName,
169-
},
170-
iamSession: {
170+
kinds: [ProfileKind.IamCredentialProfile],
171171
name: profileName,
172-
credentials: {
173-
accessKeyId: accessKey,
174-
secretAccessKey: secretKey,
172+
settings: {
173+
region: '',
174+
sso_session: '',
175+
aws_access_key_id: accessKey,
176+
aws_secret_access_key: secretKey,
175177
},
176178
},
179+
ssoSession: {
180+
name: profileName,
181+
settings: undefined,
182+
}
177183
} satisfies UpdateProfileParams)
178184
}
179185

@@ -191,12 +197,8 @@ export class LanguageClientAuth {
191197
const ssoSession = profile?.settings?.sso_session
192198
? response.ssoSessions.find((session) => session.name === profile!.settings!.sso_session)
193199
: undefined
194-
const iamSession = undefined
195-
// const iamSession = profile?.settings?.sso_session
196-
// ? response.iamSessions?.find((session) => session.name === profile!.settings!.sso_session)
197-
// : undefined
198200

199-
return { profile, ssoSession, iamSession }
201+
return { profile, ssoSession }
200202
}
201203

202204
updateBearerToken(request: UpdateCredentialsParams) {
@@ -207,11 +209,11 @@ export class LanguageClientAuth {
207209
return this.client.sendNotification(bearerCredentialsDeleteNotificationType.method)
208210
}
209211

210-
updateStsCredential(request: UpdateCredentialsParams) {
212+
updateIamCredential(request: UpdateCredentialsParams) {
211213
return this.client.sendRequest(iamCredentialsUpdateRequestType.method, request)
212214
}
213215

214-
deleteStsCredential() {
216+
deleteIamCredential() {
215217
return this.client.sendNotification(iamCredentialsDeleteNotificationType.method)
216218
}
217219

@@ -221,19 +223,19 @@ export class LanguageClientAuth {
221223
} satisfies InvalidateSsoTokenParams) as Promise<InvalidateSsoTokenResult>
222224
}
223225

224-
invalidateStsCredential(tokenId: string) {
225-
return this.client.sendRequest(invalidateStsCredentialRequestType.method, {
226-
stsCredentialId: tokenId,
227-
} satisfies InvalidateStsCredentialParams) as Promise<InvalidateStsCredentialResult>
228-
}
226+
// invalidateStsCredential(tokenId: string) {
227+
// return this.client.sendRequest(invalidateStsCredentialRequestType.method, {
228+
// stsCredentialId: tokenId,
229+
// } satisfies InvalidateStsCredentialParams) as Promise<InvalidateStsCredentialResult>
230+
// }
229231

230232
registerSsoTokenChangedHandler(ssoTokenChangedHandler: (params: SsoTokenChangedParams) => any) {
231233
this.client.onNotification(ssoTokenChangedRequestType.method, ssoTokenChangedHandler)
232234
}
233235

234-
registerStsCredentialChangedHandler(stsCredentialChangedHandler: (params: StsCredentialChangedParams) => any) {
235-
this.client.onNotification(stsCredentialChangedRequestType.method, stsCredentialChangedHandler)
236-
}
236+
// registerStsCredentialChangedHandler(stsCredentialChangedHandler: (params: StsCredentialChangedParams) => any) {
237+
// this.client.onNotification(stsCredentialChangedRequestType.method, stsCredentialChangedHandler)
238+
// }
237239

238240
registerCacheWatcher(cacheChangedHandler: (event: cacheChangedEvent) => any) {
239241
this.cacheWatcher.onDidCreate(() => cacheChangedHandler('create'))
@@ -255,8 +257,8 @@ export abstract class BaseLogin {
255257
protected readonly eventEmitter: vscode.EventEmitter<AuthStateEvent>
256258
) {}
257259

258-
abstract login(opts: any): Promise<GetSsoTokenResult | GetStsCredentialResult | undefined>
259-
abstract reauthenticate(): Promise<GetSsoTokenResult | GetStsCredentialResult | undefined>
260+
abstract login(opts: any): Promise<GetSsoTokenResult | GetIamCredentialResult | undefined>
261+
abstract reauthenticate(): Promise<GetSsoTokenResult | GetIamCredentialResult | undefined>
260262
abstract logout(): void
261263
abstract restore(): void
262264
abstract getToken(): Promise<{ token: string; updateCredentialsParams: UpdateCredentialsParams }>
@@ -280,7 +282,6 @@ export abstract class BaseLogin {
280282
async getProfile(): Promise<{
281283
profile: Profile | undefined
282284
ssoSession: SsoSession | undefined
283-
iamSession: IamSession | undefined
284285
}> {
285286
return await this.lspAuth.getProfile(this.profileName)
286287
}
@@ -305,6 +306,14 @@ export abstract class BaseLogin {
305306
this.eventEmitter.fire({ id: this.profileName, state: this.connectionState })
306307
}
307308
}
309+
310+
/**
311+
* Decrypts an encrypted string, removes its quotes, and returns the resulting string
312+
*/
313+
protected async decrypt(encrypted: string): Promise<string> {
314+
const decrypted = await jose.compactDecrypt(encrypted, this.lspAuth.encryptionKey)
315+
return decrypted.plaintext.toString().replaceAll('"', '')
316+
}
308317
}
309318

310319
/**
@@ -374,9 +383,9 @@ export class SsoLogin extends BaseLogin {
374383
*/
375384
async getToken() {
376385
const response = await this._getSsoToken(false)
377-
const decryptedKey = await jose.compactDecrypt(response.ssoToken.accessToken, this.lspAuth.encryptionKey)
386+
const accessToken = await this.decrypt(response.ssoToken.accessToken)
378387
return {
379-
token: decryptedKey.plaintext.toString().replaceAll('"', ''),
388+
token: accessToken,
380389
updateCredentialsParams: response.updateCredentialsParams,
381390
}
382391
}
@@ -452,31 +461,31 @@ export class SsoLogin extends BaseLogin {
452461
*/
453462
export class IamLogin extends BaseLogin {
454463
// Cached information from the identity server for easy reference
455-
private stsCredentialId: string | undefined
464+
// private iamCredentialId: string | undefined
456465

457466
constructor(profileName: string, lspAuth: LanguageClientAuth, eventEmitter: vscode.EventEmitter<AuthStateEvent>) {
458467
super(profileName, lspAuth, eventEmitter)
459-
lspAuth.registerStsCredentialChangedHandler((params: StsCredentialChangedParams) =>
460-
this.stsCredentialChangedHandler(params)
461-
)
468+
// lspAuth.registerStsCredentialChangedHandler((params: StsCredentialChangedParams) =>
469+
// this.stsCredentialChangedHandler(params)
470+
// )
462471
}
463472

464473
async login(opts: { accessKey: string; secretKey: string }) {
465474
await this.updateProfile(opts)
466-
return this._getStsCredential(true)
475+
return this._getIamCredential(true)
467476
}
468477

469478
async reauthenticate() {
470479
if (this.connectionState === 'notConnected') {
471480
throw new ToolkitError('Cannot reauthenticate when not connected.')
472481
}
473-
return this._getStsCredential(true)
482+
return this._getIamCredential(true)
474483
}
475484

476485
async logout() {
477-
if (this.stsCredentialId) {
478-
await this.lspAuth.invalidateStsCredential(this.stsCredentialId)
479-
}
486+
// if (this.stsCredentialId) {
487+
// await this.lspAuth.invalidateStsCredential(this.iamCredentialId)
488+
// }
480489
this.updateConnectionState('notConnected')
481490
this._data = undefined
482491
// TODO: DeleteProfile api in Identity Service (this doesn't exist yet)
@@ -494,16 +503,16 @@ export class IamLogin extends BaseLogin {
494503
* Restore the connection state and connection details to memory, if they exist.
495504
*/
496505
async restore() {
497-
const sessionData = await this.getProfile()
498-
const credentials = sessionData?.iamSession?.credentials
499-
if (credentials?.accessKeyId && credentials?.secretAccessKey) {
500-
this._data = {
501-
accessKey: credentials.accessKeyId,
502-
secretKey: credentials.secretAccessKey,
503-
}
504-
}
506+
// const sessionData = await this.getProfile()
507+
// const credentials = sessionData?.iamSession?.credentials
508+
// if (credentials?.accessKeyId && credentials?.secretAccessKey) {
509+
// this._data = {
510+
// accessKey: credentials.accessKeyId,
511+
// secretKey: credentials.secretAccessKey,
512+
// }
513+
// }
505514
try {
506-
await this._getStsCredential(false)
515+
await this._getIamCredential(false)
507516
} catch (err) {
508517
getLogger().error('Restoring connection failed: %s', err)
509518
}
@@ -515,10 +524,18 @@ export class IamLogin extends BaseLogin {
515524
*/
516525
async getToken() {
517526
// TODO: fix STS credential decryption
518-
const response = await this._getStsCredential(false)
519-
const decryptedKey = await jose.compactDecrypt(response.stsCredential.id, this.lspAuth.encryptionKey)
527+
const response = await this._getIamCredential(false)
528+
const accessKey = await this.decrypt(response.credentials.accessKeyId)
529+
// const secretKey = await this.decrypt(response.credentials.secretAccessKey)
530+
// let sessionToken: string | undefined
531+
// if (response.credentials.sessionToken) {
532+
// sessionToken = await this.decrypt(response.credentials.sessionToken)
533+
// }
520534
return {
521-
token: decryptedKey.plaintext.toString().replaceAll('"', ''),
535+
// accessKey: accessKey,
536+
// secretKey: secretKey,
537+
// sessionToken: sessionToken,
538+
token: accessKey,
522539
updateCredentialsParams: response.updateCredentialsParams,
523540
}
524541
}
@@ -527,12 +544,12 @@ export class IamLogin extends BaseLogin {
527544
* Returns the response from `getSsoToken` LSP API and sets the connection state based on the errors/result
528545
* of the call.
529546
*/
530-
private async _getStsCredential(login: boolean) {
531-
let response: GetStsCredentialResult
547+
private async _getIamCredential(login: boolean) {
548+
let response: GetIamCredentialResult
532549
this.cancellationToken = new CancellationTokenSource()
533550

534551
try {
535-
response = await this.lspAuth.getStsCredential(login, this.cancellationToken.token)
552+
response = await this.lspAuth.getIamCredential(login, this.cancellationToken.token)
536553
} catch (err: any) {
537554
switch (err.data?.awsErrorCode) {
538555
case AwsErrorCodes.E_CANCELLED:
@@ -559,19 +576,19 @@ export class IamLogin extends BaseLogin {
559576
this.cancellationToken = undefined
560577
}
561578

562-
this.stsCredentialId = response.stsCredential.id
579+
// this.iamCredentialId = response.id
563580
this.updateConnectionState('connected')
564581
return response
565582
}
566583

567-
private stsCredentialChangedHandler(params: StsCredentialChangedParams) {
568-
if (params.stsCredentialId === this.stsCredentialId) {
569-
if (params.kind === CredentialChangedKind.Expired) {
570-
this.updateConnectionState('expired')
571-
return
572-
} else if (params.kind === CredentialChangedKind.Refreshed) {
573-
this.eventEmitter.fire({ id: this.profileName, state: 'refreshed' })
574-
}
575-
}
576-
}
584+
// private stsCredentialChangedHandler(params: StsCredentialChangedParams) {
585+
// if (params.stsCredentialId === this.iamCredentialId) {
586+
// if (params.kind === CredentialChangedKind.Expired) {
587+
// this.updateConnectionState('expired')
588+
// return
589+
// } else if (params.kind === CredentialChangedKind.Refreshed) {
590+
// this.eventEmitter.fire({ id: this.profileName, state: 'refreshed' })
591+
// }
592+
// }
593+
// }
577594
}

packages/core/src/codewhisperer/util/authUtil.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { once } from '../../shared/utilities/functionUtils'
4242
import {
4343
CancellationTokenSource,
4444
GetSsoTokenResult,
45-
GetStsCredentialResult,
45+
GetIamCredentialResult,
4646
SsoTokenSourceKind,
4747
} from '@aws/language-server-runtimes/server-interface'
4848

@@ -158,14 +158,14 @@ export class AuthUtil implements IAuthProvider {
158158
}
159159

160160
// Log into the desired session type using the authentication parameters
161-
async login(accessKey: string, secretKey: string, loginType: 'iam'): Promise<GetStsCredentialResult | undefined>
161+
async login(accessKey: string, secretKey: string, loginType: 'iam'): Promise<GetIamCredentialResult | undefined>
162162
async login(startUrl: string, region: string, loginType: 'sso'): Promise<GetSsoTokenResult | undefined>
163163
async login(
164164
first: string,
165165
second: string,
166166
loginType: 'iam' | 'sso'
167-
): Promise<GetSsoTokenResult | GetStsCredentialResult | undefined> {
168-
let response: GetSsoTokenResult | GetStsCredentialResult | undefined
167+
): Promise<GetSsoTokenResult | GetIamCredentialResult | undefined> {
168+
let response: GetSsoTokenResult | GetIamCredentialResult | undefined
169169

170170
// Start session if the current session type does not match the desired type
171171
if (loginType === 'sso' && !this.isSsoSession()) {

0 commit comments

Comments
 (0)