1- import { AwsCredentialIdentity } from '@aws-sdk/types' ;
21import { DeepReadonly } from 'ts-essentials' ;
32import { LspAuthHandlers } from '../protocol/LspAuthHandlers' ;
43import { DefaultSettings } from '../settings/Settings' ;
54import { SettingsManager } from '../settings/SettingsManager' ;
6- import { parseProfile } from '../settings/SettingsParser' ;
7- import { ClientMessage } from '../telemetry/ClientMessage' ;
85import { LoggerFactory } from '../telemetry/LoggerFactory' ;
96import { extractErrorMessage } from '../utils/Errors' ;
7+ import { getRegion } from '../utils/Region' ;
108import { parseWithPrettyError } from '../utils/ZodErrorWrapper' ;
119import {
1210 parseListProfilesResult ,
@@ -30,31 +28,33 @@ import {
3028 InvalidateSsoTokenParams ,
3129 InvalidateSsoTokenResult ,
3230 SsoTokenChangedParams ,
31+ IamCredentials ,
3332} from './AwsLspAuthTypes' ;
34- import { sdkIAMCredentials } from './AwsSdkCredentialsProvider' ;
3533
3634export class AwsCredentials {
3735 private readonly logger = LoggerFactory . getLogger ( AwsCredentials ) ;
38- private profileName = DefaultSettings . profile . profile ;
3936
37+ private iamCredentials ?: IamCredentials ;
4038 private bearerCredentials ?: BearerCredentials ;
4139 private connectionMetadata ?: ConnectionMetadata ;
4240
4341 constructor (
4442 private readonly awsHandlers : LspAuthHandlers ,
4543 private readonly settingsManager : SettingsManager ,
46- private readonly clientMessage : ClientMessage ,
47- private readonly getIAMFromSdk : (
48- profile : string ,
49- ) => Promise < DeepReadonly < AwsCredentialIdentity > > = sdkIAMCredentials ,
5044 ) { }
5145
52- getIAM ( ) : Promise < DeepReadonly < AwsCredentialIdentity > > {
53- return this . getIAMFromSdk ( this . profileName ) ;
46+ getIAM ( ) : DeepReadonly < IamCredentials > {
47+ if ( ! this . iamCredentials ) {
48+ throw new Error ( 'IAM credentials not configured' ) ;
49+ }
50+ return structuredClone ( this . iamCredentials ) ;
5451 }
5552
56- getBearer ( ) : DeepReadonly < BearerCredentials | undefined > {
57- return this . bearerCredentials ;
53+ getBearer ( ) : DeepReadonly < BearerCredentials > {
54+ if ( ! this . bearerCredentials ) {
55+ throw new Error ( 'Bearer credentials not configured' ) ;
56+ }
57+ return structuredClone ( this . bearerCredentials ) ;
5858 }
5959
6060 getConnectionMetadata ( ) : ConnectionMetadata | undefined {
@@ -137,29 +137,28 @@ export class AwsCredentials {
137137 }
138138 }
139139
140- handleIamCredentialsUpdate ( params : UpdateCredentialsParams ) {
141- let newProfileName = DefaultSettings . profile . profile ;
142- let newRegion = DefaultSettings . profile . region ;
140+ handleIamCredentialsUpdate ( params : UpdateCredentialsParams ) : boolean {
143141 try {
144142 const { data } = parseWithPrettyError ( parseUpdateCredentialsParams , params ) ;
145143 if ( 'accessKeyId' in data ) {
146- const profile = parseWithPrettyError (
147- parseProfile ,
148- {
149- profile : data . profile ,
150- region : data . region ,
151- } ,
152- DefaultSettings . profile ,
153- ) ;
154-
155- newProfileName = profile . profile ;
156- newRegion = profile . region ;
144+ const region = getRegion ( data . region ) ;
145+
146+ this . iamCredentials = {
147+ ...data ,
148+ region,
149+ } ;
150+
151+ this . settingsManager . updateProfileSettings ( data . profile , region ) ;
152+ return true ;
157153 }
154+
155+ throw new Error ( 'Not an IAM credential' ) ;
158156 } catch ( error ) {
159- this . logger . error ( `Failed to update IAM profile: ${ extractErrorMessage ( error ) } ` ) ;
160- } finally {
161- this . profileName = newProfileName ;
162- this . settingsManager . updateProfileSettings ( newProfileName , newRegion ) ;
157+ this . iamCredentials = undefined ;
158+
159+ this . logger . error ( `Failed to update IAM credentials: ${ extractErrorMessage ( error ) } ` ) ;
160+ this . settingsManager . updateProfileSettings ( DefaultSettings . profile . profile , DefaultSettings . profile . region ) ;
161+ return false ;
163162 }
164163 }
165164
@@ -183,7 +182,7 @@ export class AwsCredentials {
183182
184183 handleIamCredentialsDelete ( ) {
185184 this . logger . info ( 'IAM credentials deleted' ) ;
186- this . profileName = DefaultSettings . profile . profile ;
185+ this . iamCredentials = undefined ;
187186 }
188187
189188 handleBearerCredentialsDelete ( ) {
0 commit comments