Skip to content

Commit 9e8032f

Browse files
committed
Add User-Agent credential tracking for profile HTTP
- Add CREDENTIALS_HTTP (z), user agent features - Implement credential tracking tests for HTTP providers - Add business metrics to track credential provider usage in User-Agent header
1 parent 9a537a0 commit 9e8032f

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum class UserAgentFeature {
3737
CREDENTIALS_PROFILE_PROCESS,
3838
CREDENTIALS_IMDS,
3939
CREDENTIALS_STS_ASSUME_ROLE,
40+
CREDENTIALS_HTTP,
4041
};
4142

4243
class AWS_CORE_API UserAgent {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ void GeneralHTTPCredentialsProvider::Reload()
245245
m_credentials.SetSessionToken(token);
246246
m_credentials.SetExpiration(Aws::Utils::DateTime(credentialsView.GetString("Expiration"), Aws::Utils::DateFormat::ISO_8601));
247247
m_credentials.SetAccountId(accountId);
248+
if (!m_credentials.IsEmpty()) {
249+
m_credentials.AddUserAgentFeature(Aws::Client::UserAgentFeature::CREDENTIALS_HTTP);
250+
}
248251
AWSCredentialsProvider::Reload();
249252
}
250253

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const std::pair<UserAgentFeature, const char*> BUSINESS_METRIC_MAPPING[] = {
4747
{UserAgentFeature::CREDENTIALS_PROFILE_PROCESS, "v"},
4848
{UserAgentFeature::CREDENTIALS_IMDS, "0"},
4949
{UserAgentFeature::CREDENTIALS_STS_ASSUME_ROLE, "i"},
50+
{UserAgentFeature::CREDENTIALS_HTTP, "z"},
5051
};
5152

5253
const std::pair<const char*, UserAgentFeature> RETRY_FEATURE_MAPPING[] = {

tests/aws-cpp-sdk-core-tests/aws/auth/CredentialTrackingTest.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <aws/testing/platform/PlatformTesting.h>
1212
#include <aws/core/auth/AWSCredentialsProvider.h>
1313
#include <aws/core/auth/AWSCredentialsProviderChain.h>
14+
#include <aws/core/auth/GeneralHTTPCredentialsProvider.h>
1415
#include <aws/core/client/AWSClient.h>
1516
#include <aws/core/utils/StringUtils.h>
1617
#include <aws/core/utils/HashingUtils.h>
@@ -22,6 +23,7 @@
2223
using namespace Aws::Client;
2324
using namespace Aws::Auth;
2425
using namespace Aws::Http;
26+
using namespace Aws::Http::Standard;
2527

2628
namespace {
2729
const char* TEST_LOG_TAG = "CredentialTrackingTest";
@@ -213,4 +215,30 @@ TEST_F(CredentialTrackingTest, TestInstanceProfileCredentialsTracking)
213215
Aws::MakeShared<Aws::Config::EC2InstanceProfileConfigLoader>(TEST_LOG_TAG, mockClient), 1000);
214216

215217
RunTestWithCredentialsProvider(std::move(imdsProvider), "0");
218+
}
219+
220+
TEST_F(CredentialTrackingTest, TestHTTPCredentialsTracking)
221+
{
222+
// First mock: Response for credentials retrieval
223+
std::shared_ptr<HttpRequest> requestTmp =
224+
CreateHttpRequest(Aws::Http::URI("dummy"), Aws::Http::HttpMethod::HTTP_GET,
225+
Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
226+
auto successResponse = Aws::MakeShared<Standard::StandardHttpResponse>(TEST_LOG_TAG, requestTmp);
227+
successResponse->SetResponseCode(HttpResponseCode::OK);
228+
successResponse->GetResponseBody() << R"({
229+
"AccessKeyId": "test-http-access-key",
230+
"SecretAccessKey": "test-http-secret-key",
231+
"Token": "test-http-token",
232+
"Expiration": "2037-04-19T00:00:00Z"
233+
})";
234+
mockHttpClient->AddResponseToReturn(successResponse);
235+
236+
// Set environment for HTTP credentials provider
237+
Aws::Environment::EnvironmentRAII testEnvironment{{
238+
{"AWS_CONTAINER_CREDENTIALS_FULL_URI", "http://127.0.0.1/credentials"},
239+
}};
240+
241+
auto credsProvider = Aws::MakeShared<Aws::Auth::GeneralHTTPCredentialsProvider>(TEST_LOG_TAG,
242+
"", "http://127.0.0.1/credentials", "", "");
243+
RunTestWithCredentialsProvider(std::move(credsProvider), "z");
216244
}

0 commit comments

Comments
 (0)