11const { fromTemporaryCredentials } = require ( '@aws-sdk/credential-providers' ) ;
2- const { errorUtils } = require ( 'arsenal ' ) ;
2+ const { GetCallerIdentityCommand } = require ( '@aws-sdk/client-sts ' ) ;
33
44const { authTypeAssumeRole, authTypeNone } = require ( '../../lib/constants' ) ;
55const VaultClientCache = require ( '../../lib/clients/VaultClientCache' ) ;
@@ -44,10 +44,10 @@ class VaultClientWrapper {
4444 }
4545
4646 const stsWithCreds = CredentialsManager . resolveExternalFileSync ( sts , this . logger ) ;
47+ const endpoint = `${ sts . transport || 'https' } ://${ sts . host } :${ sts . port } ` ;
4748
48- // FIXME: works with vault 7.10 but not 8.3 (return 501)
49- // https://scality.atlassian.net/browse/VAULT-238
50- this . _tempCredsPromise = Promise . resolve ( { Account : '000000000000' } )
49+ const getCallerIdentity = new GetCallerIdentityCommand ( { } ) ;
50+ this . _tempCredsPromise = stsWithCreds . send ( getCallerIdentity )
5151 . then ( res => {
5252 const roleArn = `arn:aws:iam::${ res . Account } :role/${ roleName } ` ;
5353 const roleSessionName = `${ this . _clientId } ` ;
@@ -57,50 +57,43 @@ class VaultClientWrapper {
5757 secretAccessKey : stsWithCreds . secretKey ,
5858 } ;
5959
60- // Create a credential provider that assumes the role
61- return fromTemporaryCredentials ( {
62- masterCredentials,
60+ const creds = fromTemporaryCredentials ( {
6361 params : {
6462 RoleArn : roleArn ,
6563 RoleSessionName : roleSessionName ,
66- // default expiration: 1 hour
6764 } ,
6865 clientConfig : {
69- endpoint : `${ this . _transport } ://${ sts . host } :${ sts . port } ` ,
70- region : 'us-east-1' ,
71- tls : this . _transport === 'https' ,
72- maxAttempts : 1 ,
73- requestHandler : {
74- httpAgent : this . _transport === 'http' ? this . stsAgent : undefined ,
75- httpsAgent : this . _transport === 'https' ? this . stsAgent : undefined ,
76- connectionTimeout : 0 ,
77- socketTimeout : 0 ,
78- } ,
66+ endpoint,
67+ region : sts . region ,
68+ credentials : masterCredentials ,
69+ requestHandler : this . stsAgent ,
7970 } ,
8071 } ) ;
72+ return creds ( ) ;
8173 } )
82- . then ( creds => {
83- this . _tempCredsPromiseResolved = true ;
84- return creds ;
74+ . then ( res => {
75+ this . _tempCreds = {
76+ accessKey : res . accessKeyId ,
77+ secretKey : res . secretAccessKey ,
78+ sessionToken : res . sessionToken ,
79+ } ;
8580 } )
8681 . catch ( err => {
87- if ( err . retryable ) {
88- const retryDelayMs = 5000 ;
89-
90- this . logger . error ( 'could not set up temporary credentials, retrying' , {
91- retryDelayMs,
92- error : errorUtils . reshapeExceptionError ( err ) ,
93- } ) ;
94-
95- setTimeout ( ( ) => this . _storeAWSCredentialsPromise ( ) , retryDelayMs ) ;
96- } else {
97- this . logger . error ( 'could not set up temporary credentials' , {
98- error : errorUtils . reshapeExceptionError ( err ) ,
99- } ) ;
100- }
82+ this . logger . error ( 'failed to get temporary credentials' , {
83+ error : errorUtils . reshapeExceptionError ( err ) ,
84+ } ) ;
85+ throw err ;
10186 } ) ;
10287 }
10388
89+ getSTSCredentials ( ) {
90+ if ( this . _authConfig . type !== authTypeAssumeRole ) {
91+ return null ;
92+ }
93+
94+ return this . _tempCreds ;
95+ }
96+
10497 getAccountId ( canonicalId , cb ) {
10598 this . getAccountIds ( [ canonicalId ] , ( err , res ) => {
10699 if ( err ) {
0 commit comments