Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ class AWS_CORE_API CrtCredentialsProvider : public AWSCredentialsProvider {
*/
AWSCredentials GetAWSCredentials() override;

protected:
void Reload() override;
virtual void RefreshIfExpired();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made RefreshIfExpired() virtual, since CrtCredentialsProvider::GetAWSCredentials() calls this, and only this method needs to be changed.


private:
enum class STATE {
INITIALIZED,
NOT_INITIALIZED,
};

static AWSCredentials ExtractCredentialsFromCrt(const Aws::Crt::Auth::Credentials& crtCredentials);
void Reload() override;
void RefreshIfExpired();

std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> m_credentialsProvider;
AWSCredentials m_credentials;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AWS_CORE_API ProfileCredentialsProvider : public AWSCredentialsProvider {
AWSCredentials GetAWSCredentials() override;

/**
* Returns the fullpath of the calculated credentials profile file
* Returns the full path of the calculated credentials profile file.
*/
static Aws::String GetCredentialsProfileFilename();

Expand All @@ -47,9 +47,6 @@ class AWS_CORE_API ProfileCredentialsProvider : public AWSCredentialsProvider {
*/
static Aws::String GetProfileDirectory();

protected:
void Reload() override;
Comment on lines -50 to -51

@grrtrr grrtrr Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was empty (see below), so removed.


private:
class ProfileCredentialsProviderImp;
std::shared_ptr<ProfileCredentialsProviderImp> m_impl;
Expand Down
51 changes: 35 additions & 16 deletions src/aws-cpp-sdk-core/source/auth/ProfileCredentialsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <aws/core/client/UserAgent.h>
#include <aws/core/platform/Environment.h>
#include <aws/core/platform/FileSystem.h>
#include <aws/core/utils/threading/ReaderWriterLock.h>
#include <aws/crt/auth/Credentials.h>
#include <aws/crt/http/HttpConnection.h>

Expand All @@ -16,13 +17,14 @@
using namespace Aws::Auth;
using namespace Aws::Client;
using namespace Aws::FileSystem;
using namespace Aws::Utils::Threading;
using namespace Aws::Crt::Auth;

namespace {
const char PROFILE_AWS_CREDENTIALS_FILE[] = "AWS_SHARED_CREDENTIALS_FILE";
const char PROFILE_DEFAULT_CREDENTIALS_FILE[] = "credentials";
const char PROFILE_PROFILE_DIRECTORY[] = ".aws";
const long DEFAULT_REFRESH_RATE_MS = 300000;
const long PROFILE_FUTURES_TIMEOUT_MS = 60'000;

@grrtrr grrtrr Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constant is only used for the futures timeout.

For parsing profile files, even with network-bound calls, 60 seconds is plenty.


std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> GetProfileCrtProvider(
const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialsConfig) {
Expand Down Expand Up @@ -50,21 +52,41 @@ std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> GetProfileCrtProvider(

class ProfileCredentialsProvider::ProfileCredentialsProviderImp : public CrtCredentialsProvider {
public:
ProfileCredentialsProviderImp()
ProfileCredentialsProviderImp(long refreshRateMs)
: CrtCredentialsProvider(
[]() -> std::shared_ptr<ICredentialsProvider> {
CredentialsProviderProfileConfig config;
return CredentialsProvider::CreateCredentialsProviderProfile(config);
},
std::chrono::milliseconds(DEFAULT_REFRESH_RATE_MS), UserAgentFeature::CREDENTIALS_PROFILE, "ProfileCredentialsProvider") {}
std::chrono::milliseconds(PROFILE_FUTURES_TIMEOUT_MS), UserAgentFeature::CREDENTIALS_PROFILE, "ProfileCredentialsProvider"),
m_loadFrequencyMs(refreshRateMs) {}

ProfileCredentialsProviderImp(const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialsConfig)
ProfileCredentialsProviderImp(const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialsConfig,
long refreshRateMs)
: CrtCredentialsProvider(
[credentialsConfig]() -> std::shared_ptr<ICredentialsProvider> {
return GetProfileCrtProvider(credentialsConfig);
},
std::chrono::milliseconds(DEFAULT_REFRESH_RATE_MS), UserAgentFeature::CREDENTIALS_PROFILE,
"ProfileCredentialsProvider") {}
std::chrono::milliseconds(PROFILE_FUTURES_TIMEOUT_MS), UserAgentFeature::CREDENTIALS_PROFILE,
"ProfileCredentialsProvider"),
m_loadFrequencyMs(refreshRateMs) {}


void RefreshIfExpired() override {
ReaderLockGuard guard(m_reloadLock);
if (!IsTimeToRefresh(m_loadFrequencyMs)) {
return;
}

guard.UpgradeToWriterLock();
if (!IsTimeToRefresh(m_loadFrequencyMs)) { // double-checked lock to avoid refreshing twice
return;
}

Reload();
AWSCredentialsProvider::Reload();

@grrtrr grrtrr Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call the base class Reload() method to reset m_lastLoadedMs.

void AWSCredentialsProvider::Reload()
{
m_lastLoadedMs = DateTime::Now().Millis();
}
bool AWSCredentialsProvider::IsTimeToRefresh(long reloadFrequency)
{
if (DateTime::Now().Millis() - m_lastLoadedMs > reloadFrequency)
{
return true;
}
return false;
}

}


static Aws::String GetCredentialsProfileFilename() {
auto credentialsFileNameFromVar = Aws::Environment::GetEnv(PROFILE_AWS_CREDENTIALS_FILE);
Expand All @@ -84,26 +106,23 @@ class ProfileCredentialsProvider::ProfileCredentialsProviderImp : public CrtCred
return {};
}
}

private:
long m_loadFrequencyMs;
};

ProfileCredentialsProvider::ProfileCredentialsProvider(long refreshRateMs) : m_impl(std::make_shared<ProfileCredentialsProviderImp>()) {
AWS_UNREFERENCED_PARAM(refreshRateMs);
}
ProfileCredentialsProvider::ProfileCredentialsProvider(long refreshRateMs) :
m_impl(std::make_shared<ProfileCredentialsProviderImp>(refreshRateMs)) {}

ProfileCredentialsProvider::ProfileCredentialsProvider(const char* profile, long refreshRateMs) {
Aws::Client::ClientConfiguration::CredentialProviderConfiguration credentialsConfig{};
credentialsConfig.profile = profile;
m_impl = std::make_shared<ProfileCredentialsProviderImp>(credentialsConfig);
AWS_UNREFERENCED_PARAM(refreshRateMs);
m_impl = std::make_shared<ProfileCredentialsProviderImp>(credentialsConfig, refreshRateMs);
}

ProfileCredentialsProvider::ProfileCredentialsProvider(
const Aws::Client::ClientConfiguration::CredentialProviderConfiguration& credentialsConfig, long refreshRateMs)
: m_impl(std::make_shared<ProfileCredentialsProviderImp>(credentialsConfig)) {
AWS_UNREFERENCED_PARAM(refreshRateMs);
}

void ProfileCredentialsProvider::Reload() {}
: m_impl(std::make_shared<ProfileCredentialsProviderImp>(credentialsConfig, refreshRateMs)) {}

Aws::String ProfileCredentialsProvider::GetCredentialsProfileFilename() {
return ProfileCredentialsProviderImp::GetCredentialsProfileFilename();
Expand Down