Skip to content

Commit 9ba0d7d

Browse files
committed
Support assume role external ID in STSProfileCredentialsProvider.
1 parent 592b3df commit 9ba0d7d

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,13 @@ namespace Aws
106106
* Returns the assumed role credentials or empty credentials on error.
107107
*/
108108
AWSCredentials GetCredentialsFromSTS(const AWSCredentials& credentials, const Aws::String& roleARN);
109+
/**
110+
* Assumes a role given its ARN. Communication with STS is done through the provided credentials.
111+
* Returns the assumed role credentials or empty credentials on error.
112+
*/
113+
AWSCredentials GetCredentialsFromSTS(const AWSCredentials& credentials, const Aws::String& roleARN, const Aws::String& externalId);
109114
private:
110-
AWSCredentials GetCredentialsFromSTSInternal(const Aws::String& roleArn, Aws::STS::STSClient* client);
115+
AWSCredentials GetCredentialsFromSTSInternal(const Aws::String& roleArn, const Aws::String& externalId, Aws::STS::STSClient* client);
111116

112117
Aws::String m_profileName;
113118
AWSCredentials m_credentials;

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,9 @@ void STSProfileCredentialsProvider::Reload()
316316
}
317317

318318
// get the role arn from the profile at the top of the stack (which hasn't been popped out yet)
319-
const auto arn = sourceProfiles.back()->second.GetRoleArn();
320-
const auto& assumedCreds = GetCredentialsFromSTS(stsCreds, arn);
319+
const auto& arn = sourceProfiles.back()->second.GetRoleArn();
320+
const auto& externalId = sourceProfiles.back()->second.GetExternalId();
321+
const auto& assumedCreds = GetCredentialsFromSTS(stsCreds, arn, externalId);
321322
sourceProfiles.back()->second.SetCredentials(assumedCreds);
322323
}
323324

@@ -331,14 +332,18 @@ void STSProfileCredentialsProvider::Reload()
331332
AWSCredentialsProvider::Reload();
332333
}
333334

334-
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTSInternal(const Aws::String& roleArn, Aws::STS::STSClient* client)
335+
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTSInternal(const Aws::String& roleArn, const Aws::String& externalId, Aws::STS::STSClient* client)
335336
{
336337
using namespace Aws::STS::Model;
337338
AssumeRoleRequest assumeRoleRequest;
338339
assumeRoleRequest
339340
.WithRoleArn(roleArn)
340341
.WithRoleSessionName(Aws::Utils::UUID::PseudoRandomUUID())
341342
.WithDurationSeconds(static_cast<int>(std::chrono::seconds(m_duration).count()));
343+
if (!externalId.empty())
344+
{
345+
assumeRoleRequest.SetExternalId(externalId);
346+
}
342347
auto outcome = client->AssumeRole(assumeRoleRequest);
343348
if (outcome.IsSuccess())
344349
{
@@ -356,13 +361,18 @@ AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTSInternal(cons
356361
}
357362

358363
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTS(const AWSCredentials& credentials, const Aws::String& roleArn)
364+
{
365+
return GetCredentialsFromSTS(credentials, roleArn, "");
366+
}
367+
368+
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTS(const AWSCredentials& credentials, const Aws::String& roleArn, const Aws::String& externalId)
359369
{
360370
using namespace Aws::STS::Model;
361371
if (m_stsClientFactory) {
362372
auto client = m_stsClientFactory(credentials);
363-
return GetCredentialsFromSTSInternal(roleArn, client.get());
373+
return GetCredentialsFromSTSInternal(roleArn, externalId, client.get());
364374
}
365375

366376
Aws::STS::STSClient stsClient {credentials};
367-
return GetCredentialsFromSTSInternal(roleArn, &stsClient);
377+
return GetCredentialsFromSTSInternal(roleArn, externalId, &stsClient);
368378
}

0 commit comments

Comments
 (0)