Skip to content

Commit 65ecdfd

Browse files
committed
Create another function to manipulate the header in the signer
then don’t need to change awsclient
1 parent 116e055 commit 65ecdfd

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

src/aws-cpp-sdk-core/include/aws/core/auth/signer/AWSAuthV4Signer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ namespace Aws
202202

203203
protected:
204204
virtual bool ServiceRequireUnsignedPayload(const Aws::String& serviceName) const;
205+
void UpdateUserAgentWithCredentialFeatures(Aws::Http::HttpRequest& request, const Aws::AmazonWebServiceRequest& awsRequest) const;
205206
bool m_includeSha256HashHeader;
206207

207208
private:

src/aws-cpp-sdk-core/source/auth/signer/AWSAuthV4Signer.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <aws/core/auth/signer/AWSAuthSignerCommon.h>
88
#include <aws/core/auth/signer/AWSAuthSignerHelper.h>
99

10+
#include <aws/core/AmazonWebServiceRequest.h>
1011
#include <aws/core/auth/AWSCredentialsProvider.h>
12+
#include <aws/core/client/UserAgent.h>
1113
#include <aws/core/http/HttpRequest.h>
1214
#include <aws/core/http/URI.h>
1315
#include <aws/core/utils/DateTime.h>
@@ -342,6 +344,10 @@ bool AWSAuthV4Signer::SignRequest(Aws::Http::HttpRequest& request, const char* r
342344
bool AWSAuthV4Signer::SignRequest(Aws::Http::HttpRequest& request, Aws::AmazonWebServiceRequest& awsRequest, const char* region, const char* serviceName, bool signBody) const
343345
{
344346
AWSCredentials credentials = GetCredentials(awsRequest, request.GetServiceSpecificParameters());
347+
348+
// Update User-Agent with credential tracking features added during credential resolution
349+
UpdateUserAgentWithCredentialFeatures(request, awsRequest);
350+
345351
return SignRequestWithCreds(request, credentials, region, serviceName, signBody);
346352
}
347353

@@ -606,3 +612,48 @@ Aws::Auth::AWSCredentials AWSAuthV4Signer::GetCredentials(Aws::AmazonWebServiceR
606612
AWS_UNREFERENCED_PARAM(serviceSpecificParameters);
607613
return m_credentialsProvider->GetAWSCredentials(awsRequest);
608614
}
615+
616+
void AWSAuthV4Signer::UpdateUserAgentWithCredentialFeatures(Aws::Http::HttpRequest& request, const Aws::AmazonWebServiceRequest& awsRequest) const {
617+
if (request.HasHeader(USER_AGENT)) {
618+
auto existingUA = request.GetHeaderValue(USER_AGENT);
619+
auto features = awsRequest.GetUserAgentFeatures();
620+
621+
if (!features.empty()) {
622+
// Build business metrics string from features
623+
Aws::String businessMetrics = "m/";
624+
bool first = true;
625+
for (const auto& feature : features) {
626+
if (!first) businessMetrics += ",";
627+
first = false;
628+
629+
// Map features to their character codes
630+
switch (feature) {
631+
case Aws::Client::UserAgentFeature::CREDENTIALS_ENV_VARS:
632+
businessMetrics += "g";
633+
break;
634+
// Add other feature mappings as needed
635+
default:
636+
// Skip unknown features
637+
break;
638+
}
639+
}
640+
641+
// Parse existing User-Agent and replace business metrics section
642+
auto parts = Aws::Utils::StringUtils::Split(existingUA, ' ');
643+
for (auto& part : parts) {
644+
if (part.find("m/") == 0) {
645+
part = businessMetrics;
646+
break;
647+
}
648+
}
649+
650+
// Rejoin and update
651+
Aws::String updatedUA;
652+
for (size_t i = 0; i < parts.size(); ++i) {
653+
if (i > 0) updatedUA += " ";
654+
updatedUA += parts[i];
655+
}
656+
request.SetUserAgent(updatedUA);
657+
}
658+
}
659+
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include <aws/core/Version.h>
4545
#include <aws/core/platform/Environment.h>
4646
#include <aws/core/platform/OSVersionInfo.h>
47-
#include <aws/core/auth/AWSCredentialsProviderChain.h>
4847

4948
#include <smithy/tracing/TracingUtils.h>
5049
#include <smithy/client/features/ChecksumInterceptor.h>
@@ -592,11 +591,6 @@ HttpResponseOutcome AWSClient::AttemptOneRequest(const std::shared_ptr<Aws::Http
592591
return HttpResponseOutcome(AWSError<CoreErrors>(CoreErrors::CLIENT_SIGNING_FAILURE, "", "SDK failed to sign the request", false/*retryable*/));
593592
}
594593

595-
// Update User-Agent with credential tracking features added during signing
596-
if (m_userAgentInterceptor) {
597-
m_userAgentInterceptor->UpdateUserAgentWithRequestFeatures(request, httpRequest);
598-
}
599-
600594
if (request.GetRequestSignedHandler())
601595
{
602596
request.GetRequestSignedHandler()(*httpRequest);

0 commit comments

Comments
 (0)