Skip to content

Commit 58361bd

Browse files
committed
updated UA tracking to be simpler
1 parent c534be6 commit 58361bd

File tree

5 files changed

+42
-135
lines changed

5 files changed

+42
-135
lines changed

src/aws-cpp-sdk-core/include/aws/core/client/ClientConfiguration.h

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -441,37 +441,12 @@ namespace Aws
441441
ResponseChecksumValidation responseChecksumValidation = ResponseChecksumValidation::WHEN_SUPPORTED;
442442
} checksumConfig;
443443

444-
/**
445-
* Configuration source types for tracking where values come from
446-
*/
447-
enum class ConfigSourceType {
448-
ENVIRONMENT,
449-
PROFILE,
450-
DEFAULT_VALUE
451-
};
452-
453-
/**
454-
* Structure to hold both config value and its source
455-
*/
456-
struct ConfigSource {
457-
Aws::String value;
458-
ConfigSourceType source;
459-
ConfigSource(const Aws::String& val, ConfigSourceType src) : value(val), source(src) {}
460-
};
461-
462444
/**
463445
* A helper function to read config value from env variable or aws profile config
464446
*/
465447
static Aws::String LoadConfigFromEnvOrProfile(const Aws::String& envKey, const Aws::String& profile,
466448
const Aws::String& profileProperty, const Aws::Vector<Aws::String>& allowedValues,
467449
const Aws::String& defaultValue);
468-
469-
/**
470-
* A helper function to read config value and track its source
471-
*/
472-
static ConfigSource LoadConfigFromEnvOrProfileWithSource(const Aws::String& envKey, const Aws::String& profile,
473-
const Aws::String& profileProperty, const Aws::Vector<Aws::String>& allowedValues,
474-
const Aws::String& defaultValue);
475450

476451
/**
477452
* A wrapper for interfacing with telemetry functionality. Defaults to Noop provider.
@@ -564,11 +539,6 @@ namespace Aws
564539
* The OAuth 2.0 access token or OpenID Connect ID token
565540
*/
566541
Aws::String tokenFilePath;
567-
568-
/**
569-
* Credential source type for user agent tracking
570-
*/
571-
Aws::String credentialSource;
572542

573543
/**
574544
* Time out for the credentials future call.

src/aws-cpp-sdk-core/include/aws/core/client/UserAgent.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ enum class UserAgentFeature {
3333
RESOLVED_ACCOUNT_ID,
3434
GZIP_REQUEST_COMPRESSION,
3535
CREDENTIALS_ENV_VARS,
36-
CREDENTIALS_ENV_VARS_STS_WEB_ID_TOKEN,
3736
CREDENTIALS_PROFILE,
3837
CREDENTIALS_PROFILE_PROCESS,
3938
CREDENTIALS_IMDS,

src/aws-cpp-sdk-core/source/auth/STSCredentialsProvider.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <aws/core/Globals.h>
66
#include <aws/core/auth/STSCredentialsProvider.h>
77
#include <aws/core/client/ClientConfiguration.h>
8+
#include <aws/core/client/UserAgent.h>
89
#include <aws/core/platform/Environment.h>
910
#include <aws/crt/auth/Credentials.h>
1011

@@ -96,6 +97,9 @@ AWSCredentials STSAssumeRoleWebIdentityCredentialsProvider::GetAWSCredentials()
9697
credentials.SetExpiration(DateTime{static_cast<double>(expiration)});
9798
const auto sessionTokenCursor = crtCredentials->GetSessionToken();
9899
credentials.SetSessionToken({reinterpret_cast<char*>(sessionTokenCursor.ptr), sessionTokenCursor.len});
100+
if (!credentials.IsEmpty()) {
101+
credentials.AddUserAgentFeature(Aws::Client::UserAgentFeature::CREDENTIALS_STS_WEB_IDENTITY_TOKEN);
102+
}
99103
}
100104
refreshDone = true;
101105
}

src/aws-cpp-sdk-core/source/client/ClientConfiguration.cpp

Lines changed: 38 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,6 @@ static T LoadEnumFromString(const std::array<std::pair<const char*, T>, N>& mapp
7373
return mapping->second;
7474
}
7575

76-
// Helper to load raw values without lowercasing (for file paths, ARNs, etc.)
77-
static Aws::String LoadRawFromEnvOrProfile(const Aws::String& envKey,
78-
const Aws::String& profile,
79-
const Aws::String& profileProperty,
80-
const Aws::String& defaultValue) {
81-
Aws::String option = Aws::Environment::GetEnv(envKey.c_str());
82-
if (option.empty()) {
83-
option = Aws::Config::GetCachedConfigValue(profile, profileProperty);
84-
}
85-
return option.empty() ? defaultValue : Aws::Utils::StringUtils::Trim(option.c_str());
86-
}
87-
8876
ClientConfiguration::ProviderFactories ClientConfiguration::ProviderFactories::defaultFactories = []()
8977
{
9078
ProviderFactories factories;
@@ -339,25 +327,23 @@ void setConfigFromEnvOrProfile(ClientConfiguration &config)
339327
// Uses default retry mode with the specified max attempts from metadata_service_num_attempts
340328
config.credentialProviderConfig.imdsConfig.imdsRetryStrategy = InitRetryStrategy(attempts, "");
341329

342-
auto roleArnSrc = ClientConfiguration::LoadConfigFromEnvOrProfileWithSource(
343-
AWS_IAM_ROLE_ARN_ENV_VAR, config.profileName, AWS_IAM_ROLE_ARN_CONFIG_FILE_OPTION, {}, "");
344-
config.credentialProviderConfig.stsCredentialsProviderConfig.roleArn = roleArnSrc.value;
345-
346-
config.credentialProviderConfig.stsCredentialsProviderConfig.sessionName = LoadRawFromEnvOrProfile(
347-
AWS_IAM_ROLE_SESSION_NAME_ENV_VAR, config.profileName, AWS_IAM_ROLE_SESSION_NAME_CONFIG_FILE_OPTION, "");
348-
349-
auto tokenFileSrc = ClientConfiguration::LoadConfigFromEnvOrProfileWithSource(
350-
AWS_WEB_IDENTITY_TOKEN_FILE_ENV_VAR, config.profileName, AWS_WEB_IDENTITY_TOKEN_FILE_CONFIG_FILE_OPTION, {}, "");
351-
config.credentialProviderConfig.stsCredentialsProviderConfig.tokenFilePath = tokenFileSrc.value;
352-
353-
if (!roleArnSrc.value.empty() && !tokenFileSrc.value.empty()) {
354-
using Src = ClientConfiguration::ConfigSourceType;
355-
const bool fromEnv = (roleArnSrc.source == Src::ENVIRONMENT) || (tokenFileSrc.source == Src::ENVIRONMENT);
356-
config.credentialProviderConfig.stsCredentialsProviderConfig.credentialSource =
357-
fromEnv ? "env_web_identity" : "web_identity";
358-
} else {
359-
config.credentialProviderConfig.stsCredentialsProviderConfig.credentialSource.clear();
360-
}
330+
config.credentialProviderConfig.stsCredentialsProviderConfig.roleArn = ClientConfiguration::LoadConfigFromEnvOrProfile(AWS_IAM_ROLE_ARN_ENV_VAR,
331+
config.profileName,
332+
AWS_IAM_ROLE_ARN_CONFIG_FILE_OPTION,
333+
{}, /* allowed values */
334+
"" /* default value */);
335+
336+
config.credentialProviderConfig.stsCredentialsProviderConfig.sessionName = ClientConfiguration::LoadConfigFromEnvOrProfile(AWS_IAM_ROLE_SESSION_NAME_ENV_VAR,
337+
config.profileName,
338+
AWS_IAM_ROLE_SESSION_NAME_CONFIG_FILE_OPTION,
339+
{}, /* allowed values */
340+
"" /* default value */);
341+
342+
config.credentialProviderConfig.stsCredentialsProviderConfig.tokenFilePath = ClientConfiguration::LoadConfigFromEnvOrProfile(AWS_WEB_IDENTITY_TOKEN_FILE_ENV_VAR,
343+
config.profileName,
344+
AWS_WEB_IDENTITY_TOKEN_FILE_CONFIG_FILE_OPTION,
345+
{}, /* allowed values */
346+
"" /* default value */);
361347
}
362348

363349
ClientConfiguration::ClientConfiguration()
@@ -566,79 +552,35 @@ std::shared_ptr<RetryStrategy> InitRetryStrategy(Aws::String retryMode)
566552
return InitRetryStrategy(maxAttempts, retryMode);
567553
}
568554

569-
ClientConfiguration::ConfigSource ClientConfiguration::LoadConfigFromEnvOrProfileWithSource(const Aws::String& envKey,
570-
const Aws::String& profile,
571-
const Aws::String& profileProperty,
572-
const Aws::Vector<Aws::String>& allowedValues,
573-
const Aws::String& defaultValue)
555+
Aws::String ClientConfiguration::LoadConfigFromEnvOrProfile(const Aws::String& envKey,
556+
const Aws::String& profile,
557+
const Aws::String& profileProperty,
558+
const Aws::Vector<Aws::String>& allowedValues,
559+
const Aws::String& defaultValue)
574560
{
575-
Aws::String option;
576-
ConfigSourceType sourceType = ConfigSourceType::DEFAULT_VALUE;
577-
578-
if (!envKey.empty()) {
579-
option = Aws::Environment::GetEnv(envKey.c_str());
580-
if (!option.empty()) {
581-
sourceType = ConfigSourceType::ENVIRONMENT;
582-
}
583-
}
584-
585-
if (option.empty() && !profileProperty.empty()) {
561+
Aws::String option = Aws::Environment::GetEnv(envKey.c_str());
562+
if (option.empty()) {
586563
option = Aws::Config::GetCachedConfigValue(profile, profileProperty);
587-
if (!option.empty()) {
588-
sourceType = ConfigSourceType::PROFILE;
589-
}
590564
}
591-
592-
option = Aws::Utils::StringUtils::Trim(option.c_str());
593-
565+
option = Aws::Utils::StringUtils::ToLower(option.c_str());
594566
if (option.empty()) {
595-
return ConfigSource(defaultValue, ConfigSourceType::DEFAULT_VALUE);
567+
return defaultValue;
596568
}
597569

598-
// Validate only if we have an allowed list (enum-like). Do NOT mutate case of the returned value.
599-
if (!allowedValues.empty()) {
600-
const Aws::String optionLower = Aws::Utils::StringUtils::ToLower(option.c_str());
601-
602-
// Build a lowercased view of the allowed set once
603-
bool allowed = std::any_of(allowedValues.cbegin(), allowedValues.cend(),
604-
[&](const Aws::String& v){ return optionLower == Aws::Utils::StringUtils::ToLower(v.c_str()); });
605-
606-
if (!allowed) {
607-
Aws::OStringStream expectedStr;
608-
expectedStr << "[";
609-
for (size_t i = 0; i < allowedValues.size(); ++i) {
610-
expectedStr << allowedValues[i];
611-
if ( i + 1 < allowedValues.size() ) expectedStr << ";";
612-
}
613-
expectedStr << "]";
614-
615-
const char* src = (sourceType == ConfigSourceType::ENVIRONMENT) ? "environment" :
616-
(sourceType == ConfigSourceType::PROFILE) ? "profile" : "default";
617-
618-
AWS_LOGSTREAM_WARN(
619-
CLIENT_CONFIG_TAG,
620-
"Unrecognized value from " << src
621-
<< (sourceType == ConfigSourceType::ENVIRONMENT ? (Aws::String(" (") + envKey + ")") :
622-
sourceType == ConfigSourceType::PROFILE ? (Aws::String(" (") + profile + ":" + profileProperty + ")") : "")
623-
<< ": \"" << option << "\". Using default: \"" << defaultValue
624-
<< "\". Expected one of: " << expectedStr.str()
625-
);
626-
627-
return ConfigSource(defaultValue, ConfigSourceType::DEFAULT_VALUE);
570+
if (!allowedValues.empty() && std::find(allowedValues.cbegin(), allowedValues.cend(), option) == allowedValues.cend()) {
571+
Aws::OStringStream expectedStr;
572+
expectedStr << "[";
573+
for(const auto& allowed : allowedValues) {
574+
expectedStr << allowed << ";";
628575
}
629-
}
576+
expectedStr << "]";
630577

631-
// Return original (un-lowercased) token and the actual source
632-
return ConfigSource(option, sourceType);
633-
}
634-
635-
Aws::String ClientConfiguration::LoadConfigFromEnvOrProfile(const Aws::String& envKey,
636-
const Aws::String& profile,
637-
const Aws::String& profileProperty,
638-
const Aws::Vector<Aws::String>& allowedValues,
639-
const Aws::String& defaultValue)
640-
{
641-
return LoadConfigFromEnvOrProfileWithSource(envKey, profile, profileProperty, allowedValues, defaultValue).value;
578+
AWS_LOGSTREAM_WARN(CLIENT_CONFIG_TAG, "Unrecognised value for " << envKey << ": " << option <<
579+
". Using default instead: " << defaultValue <<
580+
". Expected empty or one of: " << expectedStr.str());
581+
option = defaultValue;
582+
}
583+
return option;
642584
}
643585

644586
} // namespace Client

src/aws-cpp-sdk-core/source/client/UserAgent.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const std::pair<UserAgentFeature, const char*> BUSINESS_METRIC_MAPPING[] = {
4343
{UserAgentFeature::RESOLVED_ACCOUNT_ID, "T"},
4444
{UserAgentFeature::GZIP_REQUEST_COMPRESSION, "L"},
4545
{UserAgentFeature::CREDENTIALS_ENV_VARS, "g"},
46-
{UserAgentFeature::CREDENTIALS_ENV_VARS_STS_WEB_ID_TOKEN, "h"},
4746
{UserAgentFeature::CREDENTIALS_PROFILE, "n"},
4847
{UserAgentFeature::CREDENTIALS_PROFILE_PROCESS, "v"},
4948
{UserAgentFeature::CREDENTIALS_IMDS, "0"},
@@ -132,13 +131,6 @@ UserAgent::UserAgent(const ClientConfiguration& clientConfiguration,
132131
if (accountIdMode.has_value()) {
133132
m_features.emplace(accountIdMode.value());
134133
}
135-
136-
const auto& sts = clientConfiguration.credentialProviderConfig.stsCredentialsProviderConfig;
137-
if (sts.credentialSource == "env_web_identity") {
138-
m_features.emplace(UserAgentFeature::CREDENTIALS_ENV_VARS_STS_WEB_ID_TOKEN);
139-
} else if (sts.credentialSource == "web_identity") {
140-
m_features.emplace(UserAgentFeature::CREDENTIALS_STS_WEB_IDENTITY_TOKEN);
141-
}
142134
}
143135

144136
Aws::String UserAgent::SerializeWithFeatures(const Aws::Set<UserAgentFeature>& features) const {

0 commit comments

Comments
 (0)