4848#include < aws/core/utils/logging/LogMacros.h>
4949#include < aws/sts/STSClient.h>
5050#include < aws/sts/model/AssumeRoleRequest.h>
51+ #include < aws/sts/model/AssumeRoleWithWebIdentityRequest.h>
5152
5253#include < utility>
5354
@@ -77,8 +78,8 @@ STSProfileWithWebIdentityCredentialsProvider::
7778 STSProfileWithWebIdentityCredentialsProvider (
7879 const Aws::String& profileName,
7980 std::chrono::minutes duration,
80- const std::function<Aws::STS::STSClient*( const AWSCredentials&)>&
81- stsClientFactory)
81+ const std::function<std::shared_ptr< Aws::STS::STSClient>(
82+ const AWSCredentials&)>& stsClientFactory)
8283 : m_profileName(profileName)
8384 , m_duration(duration)
8485 , m_reloadFrequency(
@@ -430,27 +431,22 @@ STSProfileWithWebIdentityCredentialsProvider::GetCredentialsFromSTS(
430431 const Aws::String& externalID) {
431432 using namespace Aws ::STS::Model;
432433 if (m_stsClientFactory) {
433- return GetCredentialsFromSTSInternal (
434- roleArn, externalID, m_stsClientFactory (credentials ));
434+ auto client = m_stsClientFactory (credentials);
435+ return GetCredentialsFromSTSInternal ( roleArn, externalID, client. get ( ));
435436 }
436437
437438 Aws::STS::STSClient stsClient{credentials};
438439 return GetCredentialsFromSTSInternal (roleArn, externalID, &stsClient);
439440}
440441
441- AWSCredentials
442- STSProfileWithWebIdentityCredentialsProvider::GetCredentialsFromWebIdentity (
443- const Config::Profile& profile) {
442+ AWSCredentials STSProfileWithWebIdentityCredentialsProvider::
443+ GetCredentialsFromWebIdentityInternal (
444+ const Config::Profile& profile, Aws::STS::STSClient* client) {
445+ using namespace Aws ::STS::Model;
444446 const Aws::String& m_roleArn = profile.GetRoleArn ();
445447 Aws::String m_tokenFile = profile.GetValue (" web_identity_token_file" );
446448 Aws::String m_sessionName = profile.GetValue (" role_session_name" );
447449
448- auto tmpRegion = profile.GetRegion ();
449- if (tmpRegion.empty ()) {
450- // Set same default as STSAssumeRoleWebIdentityCredentialsProvider
451- tmpRegion = Aws::Region::US_EAST_1;
452- }
453-
454450 if (m_sessionName.empty ()) {
455451 m_sessionName = Aws::Utils::UUID::RandomUUID ();
456452 }
@@ -467,30 +463,40 @@ STSProfileWithWebIdentityCredentialsProvider::GetCredentialsFromWebIdentity(
467463 return {};
468464 }
469465
470- Internal::STSCredentialsClient::STSAssumeRoleWithWebIdentityRequest request{
471- m_sessionName, m_roleArn, m_token};
472-
473- Aws::Client::ClientConfiguration config;
474- config.scheme = Aws::Http::Scheme::HTTPS;
475- config.region = tmpRegion;
466+ AssumeRoleWithWebIdentityRequest request;
467+ request.SetRoleArn (m_roleArn);
468+ request.SetRoleSessionName (m_sessionName);
469+ request.SetWebIdentityToken (m_token);
476470
477- Aws::Vector<Aws::String> retryableErrors;
478- retryableErrors.push_back (" IDPCommunicationError" );
479- retryableErrors.push_back (" InvalidIdentityToken" );
480-
481- config.retryStrategy =
482- Aws::MakeShared<Aws::Client::SpecifiedRetryableErrorsRetryStrategy>(
483- CLASS_TAG, retryableErrors, 3 /* maxRetries*/ );
471+ auto outcome = client->AssumeRoleWithWebIdentity (request);
472+ if (outcome.IsSuccess ()) {
473+ const auto & modelCredentials = outcome.GetResult ().GetCredentials ();
474+ AWS_LOGSTREAM_TRACE (
475+ CLASS_TAG,
476+ " Successfully retrieved credentials with AWS_ACCESS_KEY: "
477+ << modelCredentials.GetAccessKeyId ());
478+ return {
479+ modelCredentials.GetAccessKeyId (),
480+ modelCredentials.GetSecretAccessKey (),
481+ modelCredentials.GetSessionToken (),
482+ modelCredentials.GetExpiration ()};
483+ } else {
484+ AWS_LOGSTREAM_ERROR (CLASS_TAG, " failed to assume role" << m_roleArn);
485+ }
486+ return {};
487+ }
484488
485- auto m_client =
486- Aws::MakeUnique<Aws::Internal::STSCredentialsClient>(CLASS_TAG, config);
487- auto result = m_client->GetAssumeRoleWithWebIdentityCredentials (request);
488- AWS_LOGSTREAM_TRACE (
489- CLASS_TAG,
490- " Successfully retrieved credentials with AWS_ACCESS_KEY: "
491- << result.creds .GetAWSAccessKeyId ());
489+ AWSCredentials
490+ STSProfileWithWebIdentityCredentialsProvider::GetCredentialsFromWebIdentity (
491+ const Config::Profile& profile) {
492+ using namespace Aws ::STS::Model;
493+ if (m_stsClientFactory) {
494+ auto client = m_stsClientFactory ({});
495+ return GetCredentialsFromWebIdentityInternal (profile, client.get ());
496+ }
492497
493- return result.creds ;
498+ Aws::STS::STSClient stsClient{AWSCredentials{}};
499+ return GetCredentialsFromWebIdentityInternal (profile, &stsClient);
494500}
495501
496502#endif // HAVE_S3s
0 commit comments