@@ -161,27 +161,43 @@ export async function getAssumedRoleCredentials(
161
161
c : Context ,
162
162
awsRoleArn : string ,
163
163
awsExternalId : string ,
164
- awsRegion : string
164
+ awsRegion : string ,
165
+ creds ?: {
166
+ accessKeyId : string ;
167
+ secretAccessKey : string ;
168
+ sessionToken ?: string ;
169
+ }
165
170
) {
166
171
const cacheKey = `${ awsRoleArn } /${ awsExternalId } /${ awsRegion } ` ;
167
172
const getFromCacheByKey = c . get ( 'getFromCacheByKey' ) ;
168
173
const putInCacheWithValue = c . get ( 'putInCacheWithValue' ) ;
169
174
170
- const {
171
- AWS_ASSUME_ROLE_ACCESS_KEY_ID ,
172
- AWS_ASSUME_ROLE_SECRET_ACCESS_KEY ,
173
- AWS_ASSUME_ROLE_REGION ,
174
- } = env ( c ) ;
175
175
const resp = getFromCacheByKey
176
176
? await getFromCacheByKey ( env ( c ) , cacheKey )
177
177
: null ;
178
178
if ( resp ) {
179
179
return resp ;
180
180
}
181
- // Long-term credentials to assume role, static values from ENV
182
- const accessKeyId : string = AWS_ASSUME_ROLE_ACCESS_KEY_ID || '' ;
183
- const secretAccessKey : string = AWS_ASSUME_ROLE_SECRET_ACCESS_KEY || '' ;
184
- const region = awsRegion || AWS_ASSUME_ROLE_REGION || 'us-east-1' ;
181
+
182
+ // Determine which credentials to use
183
+ let accessKeyId : string ;
184
+ let secretAccessKey : string ;
185
+ let sessionToken : string | undefined ;
186
+
187
+ if ( creds ) {
188
+ // Use provided credentials
189
+ accessKeyId = creds . accessKeyId ;
190
+ secretAccessKey = creds . secretAccessKey ;
191
+ sessionToken = creds . sessionToken ;
192
+ } else {
193
+ // Use environment credentials
194
+ const { AWS_ASSUME_ROLE_ACCESS_KEY_ID , AWS_ASSUME_ROLE_SECRET_ACCESS_KEY } =
195
+ env ( c ) ;
196
+ accessKeyId = AWS_ASSUME_ROLE_ACCESS_KEY_ID || '' ;
197
+ secretAccessKey = AWS_ASSUME_ROLE_SECRET_ACCESS_KEY || '' ;
198
+ }
199
+
200
+ const region = awsRegion || 'us-east-1' ;
185
201
const service = 'sts' ;
186
202
const hostname = `sts.${ region } .amazonaws.com` ;
187
203
const signer = new SignatureV4 ( {
@@ -190,10 +206,13 @@ export async function getAssumedRoleCredentials(
190
206
credentials : {
191
207
accessKeyId,
192
208
secretAccessKey,
209
+ sessionToken,
193
210
} ,
194
211
sha256 : Sha256 ,
195
212
} ) ;
196
- const url = `https://${ hostname } ?Action=AssumeRole&Version=2011-06-15&RoleArn=${ awsRoleArn } &ExternalId=${ awsExternalId } &RoleSessionName=random` ;
213
+ const date = new Date ( ) ;
214
+ const sessionName = `${ date . getFullYear ( ) } ${ date . getMonth ( ) } ${ date . getDay ( ) } ` ;
215
+ const url = `https://${ hostname } ?Action=AssumeRole&Version=2011-06-15&RoleArn=${ awsRoleArn } &RoleSessionName=${ sessionName } ${ awsExternalId ? `&ExternalId=${ awsExternalId } ` : '' } ` ;
197
216
const urlObj = new URL ( url ) ;
198
217
const requestHeaders = { host : hostname } ;
199
218
const options = {
@@ -227,7 +246,6 @@ export async function getAssumedRoleCredentials(
227
246
} catch ( error ) {
228
247
console . error ( { message : `Error assuming role:, ${ error } ` } ) ;
229
248
}
230
-
231
249
return credentials ;
232
250
}
233
251
0 commit comments