Skip to content

Commit 3bf05b6

Browse files
committed
Support assume role external ID in STSProfileCredentialsProvider.
1 parent bb7d5a0 commit 3bf05b6

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/aws-cpp-sdk-identity-management/include/aws/identity-management/auth/STSProfileCredentialsProvider.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ namespace Aws
6767
* Returns the assumed role credentials or empty credentials on error.
6868
*/
6969
AWSCredentials GetCredentialsFromSTS(const AWSCredentials& credentials, const Aws::String& roleARN);
70+
/**
71+
* Assumes a role given its ARN. Communication with STS is done through the provided credentials.
72+
* Returns the assumed role credentials or empty credentials on error.
73+
*/
74+
AWSCredentials GetCredentialsFromSTS(const AWSCredentials& credentials, const Aws::String& roleARN, const Aws::String& externalId);
7075
private:
71-
AWSCredentials GetCredentialsFromSTSInternal(const Aws::String& roleArn, Aws::STS::STSClient* client);
76+
AWSCredentials GetCredentialsFromSTSInternal(const Aws::String& roleArn, const Aws::String& externalId, Aws::STS::STSClient* client);
7277

7378
Aws::String m_profileName;
7479
AWSCredentials m_credentials;

src/aws-cpp-sdk-identity-management/source/auth/STSProfileCredentialsProvider.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,9 @@ void STSProfileCredentialsProvider::Reload()
294294
}
295295

296296
// get the role arn from the profile at the top of the stack (which hasn't been popped out yet)
297-
const auto arn = sourceProfiles.back()->second.GetRoleArn();
298-
const auto& assumedCreds = GetCredentialsFromSTS(stsCreds, arn);
297+
const auto& arn = sourceProfiles.back()->second.GetRoleArn();
298+
const auto& externalId = sourceProfiles.back()->second.GetExternalId();
299+
const auto& assumedCreds = GetCredentialsFromSTS(stsCreds, arn, externalId);
299300
sourceProfiles.back()->second.SetCredentials(assumedCreds);
300301
}
301302

@@ -309,14 +310,18 @@ void STSProfileCredentialsProvider::Reload()
309310
AWSCredentialsProvider::Reload();
310311
}
311312

312-
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTSInternal(const Aws::String& roleArn, Aws::STS::STSClient* client)
313+
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTSInternal(const Aws::String& roleArn, const Aws::String& externalId, Aws::STS::STSClient* client)
313314
{
314315
using namespace Aws::STS::Model;
315316
AssumeRoleRequest assumeRoleRequest;
316317
assumeRoleRequest
317318
.WithRoleArn(roleArn)
318319
.WithRoleSessionName(Aws::Utils::UUID::PseudoRandomUUID())
319320
.WithDurationSeconds(static_cast<int>(std::chrono::seconds(m_duration).count()));
321+
if (!externalId.empty())
322+
{
323+
assumeRoleRequest.SetExternalId(externalId);
324+
}
320325
auto outcome = client->AssumeRole(assumeRoleRequest);
321326
if (outcome.IsSuccess())
322327
{
@@ -334,12 +339,17 @@ AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTSInternal(cons
334339
}
335340

336341
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTS(const AWSCredentials& credentials, const Aws::String& roleArn)
342+
{
343+
return GetCredentialsFromSTS(credentials, roleArn, "");
344+
}
345+
346+
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTS(const AWSCredentials& credentials, const Aws::String& roleArn, const Aws::String& externalId)
337347
{
338348
using namespace Aws::STS::Model;
339349
if (m_stsClientFactory) {
340-
return GetCredentialsFromSTSInternal(roleArn, m_stsClientFactory(credentials));
350+
return GetCredentialsFromSTSInternal(roleArn, externalId m_stsClientFactory(credentials));
341351
}
342352

343353
Aws::STS::STSClient stsClient {credentials};
344-
return GetCredentialsFromSTSInternal(roleArn, &stsClient);
354+
return GetCredentialsFromSTSInternal(roleArn, externalId, &stsClient);
345355
}

0 commit comments

Comments
 (0)