Skip to content

Commit

Permalink
Fix build on smithy client with test actually allocating a client
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyRyabinin committed Aug 1, 2024
1 parent f691b28 commit ef44297
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 41 deletions.
29 changes: 15 additions & 14 deletions src/aws-cpp-sdk-core/include/smithy/client/AwsSmithyClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,19 @@ namespace client
class AwsSmithyClientT : public AwsSmithyClientBase
{
public:
AwsSmithyClientT(Aws::Client::ClientConfiguration& clientConfig, const Aws::String& serviceName,
AwsSmithyClientT(const ServiceClientConfigurationT& clientConfig, const Aws::String& serviceName,
const std::shared_ptr<Aws::Http::HttpClient>& httpClient,
const std::shared_ptr<Aws::Client::AWSErrorMarshaller>& errorMarshaller,
const ServiceClientConfigurationT& m_client_config,
const std::shared_ptr<EndpointProviderT> endpointProvider,
const std::shared_ptr<ServiceAuthSchemeResolverT>& m_auth_scheme_resolver,
const Aws::UnorderedMap<Aws::String, AuthSchemesVariantT>& m_auth_schemes)
const std::shared_ptr<ServiceAuthSchemeResolverT>& authSchemeResolver,
const Aws::UnorderedMap<Aws::String, AuthSchemesVariantT>& authSchemes)
: AwsSmithyClientBase(clientConfig, serviceName, httpClient, errorMarshaller),
m_clientConfig(m_client_config),
m_clientConfig(clientConfig),
m_endpointProvider(endpointProvider),
m_authSchemeResolver(m_auth_scheme_resolver),
m_authSchemes(m_auth_schemes)
m_authSchemeResolver(authSchemeResolver),
m_authSchemes(authSchemes)
{
if (ServiceNameT) {
m_serviceName = ServiceNameT;
}
m_serviceName = ServiceNameT;
}

virtual ~AwsSmithyClientT() = default;
Expand All @@ -70,28 +67,32 @@ namespace client
assert(m_authSchemeResolver);
typename ServiceAuthSchemeResolverT::ServiceAuthSchemeParameters identityParams;

identityParams.serviceName = m_serviceName;
identityParams.operation = ctx.m_requestName;
identityParams.region = m_clientConfig.region;

if (ctx.m_pRequest) {
// refactor once auth scheme resolver will use it's own rule set
const auto& epParams = ctx.m_pRequest->GetEndpointContextParams();
for (const auto& epParam : epParams) {
using ParameterType = Aws::Endpoint::EndpointParameter::ParameterType;
if(epParam.GetStoredType() == ParameterType::STRING)
identityParams.insert({epParam.GetName(), epParam.GetStrValueNoCheck()});
identityParams.additionalProperties.insert({epParam.GetName(), epParam.GetStrValueNoCheck()});
else if (epParam.GetStoredType() == ParameterType::BOOLEAN)
identityParams.insert({epParam.GetName(), epParam.GetBoolValueNoCheck()});
identityParams.additionalProperties.insert({epParam.GetName(), epParam.GetBoolValueNoCheck()});
else
assert(!"Unknown endpoint parameter!");
}
const auto& serviceParams = ctx.m_pRequest->GetServiceSpecificParameters();
if (serviceParams) {
for (const auto& serviceParam : serviceParams->parameterMap) {
identityParams.insert({serviceParam.first, serviceParam.second});
identityParams.additionalProperties.insert({serviceParam.first, serviceParam.second});
}
}
}
Aws::Vector<AuthSchemeOption> authSchemeOptions = m_authSchemeResolver->resolveAuthScheme(identityParams);

auto authSchemeOptionIt = std::find_first_of(authSchemeOptions.begin(), authSchemeOptions.end(),
auto authSchemeOptionIt = std::find_if(authSchemeOptions.begin(), authSchemeOptions.end(),
[this](const AuthSchemeOption& opt)
{
return m_authSchemes.find(opt.schemeId) != m_authSchemes.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace client
using SelectAuthSchemeOptionOutcome = Aws::Utils::Outcome<AuthSchemeOption, AWSError>;
using ResolveEndpointOutcome = Aws::Utils::Outcome<Aws::Endpoint::AWSEndpoint, AWSError>;

AwsSmithyClientBase(Aws::Client::ClientConfiguration& clientConfig,
AwsSmithyClientBase(const Aws::Client::ClientConfiguration& clientConfig,
Aws::String serviceName,
std::shared_ptr<Aws::Http::HttpClient> httpClient,
std::shared_ptr<Aws::Client::AWSErrorMarshaller> errorMarshaller) :
Expand Down Expand Up @@ -127,7 +127,7 @@ namespace client
virtual bool AdjustClockSkew(HttpResponseOutcome& outcome, const AuthSchemeOption& authSchemeOption) const = 0;

protected:
Aws::Client::ClientConfiguration& m_clientConfig;
Aws::Client::ClientConfiguration m_clientConfig;
Aws::String m_serviceName;
Aws::String m_userAgent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace smithy
false/*retryable*/));
}

AuthSchemesVariantT authScheme = *authSchemeIt;
const AuthSchemesVariantT& authScheme = authSchemeIt->second;

return SignWithAuthScheme(std::move(HTTPRequest), authScheme, authSchemeOption);
}
Expand All @@ -71,10 +71,10 @@ namespace smithy
assert(!"Auth scheme has not been found for a given auth option!");
return false;
}
AuthSchemesVariantT authScheme = *authSchemeIt;
AuthSchemesVariantT authScheme = authSchemeIt->second;

ClockSkewVisitor visitor(outcome, serverTime, authSchemeOption);
visitor.Visit(authScheme);
authScheme.Visit(visitor);

return visitor.m_resultShouldWait;
}
Expand All @@ -99,7 +99,7 @@ namespace smithy
// Auth Scheme Variant alternative contains the requested auth option
assert(strcmp(authScheme.schemeId, m_targetAuthSchemeOption.schemeId) == 0);

using IdentityT = typename decltype(authScheme)::IdentityT;
using IdentityT = typename std::remove_reference<decltype(authScheme)>::type::IdentityT;
using IdentityResolver = IdentityResolverBase<IdentityT>;
using Signer = AwsSignerBase<IdentityT>;

Expand All @@ -113,12 +113,14 @@ namespace smithy
return;
}

static_assert(
std::is_same<IdentityResolverBase<IdentityT>, typename decltype(identityResolver
)::IdentityT>::value, "Must be the same type");
static_assert(std::is_base_of<IdentityResolverBase<IdentityT>, decltype(identityResolver)>::value, "Must be the same type");
auto identityResult = identityResolver->getIdentity(m_targetAuthSchemeOption.identityProperties, m_targetAuthSchemeOption.identityProperties);

IdentityT identity = identityResolver->getIdentity(m_targetAuthSchemeOption.identityProperties);
if (!identityResult.IsSuccess())
{
result.emplace(identityResult.GetError());
return;
}
auto identity = std::move(identityResult.GetResultWithOwnership());

std::shared_ptr<Signer> signer = authScheme.signer();
if (!signer)
Expand All @@ -130,11 +132,7 @@ namespace smithy
return;
}


static_assert(std::is_same<AwsSignerBase<IdentityT>, typename decltype(signer)::IdentityT>::value, "Must be the same type");
static_assert(std::is_base_of<AwsSignerBase<IdentityT>, decltype(signer)>::value, "Must be the same type");

result.emplace(signer->sign(m_httpRequest, identity, m_targetAuthSchemeOption.signerProperties));
result.emplace(signer->sign(m_httpRequest, *identity, m_targetAuthSchemeOption.signerProperties));
}
};

Expand All @@ -143,7 +141,8 @@ namespace smithy
const AuthSchemeOption& targetAuthSchemeOption)
{
SignerVisitor visitor(httpRequest, targetAuthSchemeOption);
visitor.Visit(authSchemesVariant);
AuthSchemesVariantT authSchemesVariantCopy(authSchemesVariant); // TODO: allow const visiting
authSchemesVariantCopy.Visit(visitor);

if (!visitor.result)
{
Expand Down Expand Up @@ -177,7 +176,7 @@ namespace smithy
// Auth Scheme Variant alternative contains the requested auth option
assert(strcmp(authScheme.schemeId, m_targetAuthSchemeOption.schemeId) == 0);

using IdentityT = typename decltype(authScheme)::IdentityT;
using IdentityT = typename std::remove_reference<decltype(authScheme)>::type::IdentityT;
using Signer = AwsSignerBase<IdentityT>;

std::shared_ptr<Signer> signer = authScheme.signer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ namespace smithy {
public:
using IdentityT = IDENTITY_T;
static_assert(std::is_base_of<AwsIdentity, IDENTITY_T>::value, "Identity type should inherit AwsIdentity");
using SigningProperties = Aws::UnorderedMap<Aws::String, Aws::String>;
using SigningProperties = Aws::UnorderedMap<Aws::String, Aws::Crt::Variant<Aws::String, bool>>;
using AdditionalParameters = Aws::UnorderedMap<Aws::String, Aws::Crt::Variant<Aws::String, bool>>;
using HttpRequest = Aws::Http::HttpRequest;
using SigningError = Aws::Client::AWSError<Aws::Client::CoreErrors>;
using SigningFutureOutcome = Aws::Utils::FutureOutcome<std::shared_ptr<HttpRequest>, SigningError>;

// signer may copy the original httpRequest or create a new one
virtual SigningFutureOutcome sign(std::shared_ptr<HttpRequest> httpRequest, const IdentityT& identity, SigningProperties properties, const AdditionalParameters& additionalParameters) = 0;
virtual SigningFutureOutcome sign(std::shared_ptr<HttpRequest> httpRequest, const IdentityT& identity, SigningProperties properties) = 0;

virtual ~AwsSignerBase() {};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ namespace smithy {
{
}

SigningFutureOutcome sign(std::shared_ptr<HttpRequest> httpRequest, const AwsCredentialIdentityBase& identity, SigningProperties properties, const AdditionalParameters& additionalParameters) override
SigningFutureOutcome sign(std::shared_ptr<HttpRequest> httpRequest, const AwsCredentialIdentityBase& identity, SigningProperties properties) override
{
AWS_UNREFERENCED_PARAM(additionalParameters);

Aws::Auth::AWSCredentials legacyCreds(identity.accessKeyId(), identity.secretAccessKey(), *identity.sessionToken(), *identity.expiration());

auto signPayloadIt = properties.find("SignPayload");
bool signPayload = signPayloadIt != properties.end() ? signPayloadIt->second == "true" : false;
bool signPayload = signPayloadIt != properties.end() ? signPayloadIt->second.get<Aws::String>() == "true" : false;

assert(httpRequest);
bool success = legacySigner.SignRequestWithCreds(*httpRequest, legacyCreds, parameters.region->c_str(), parameters.serviceName.c_str(), signPayload);
Expand Down
33 changes: 30 additions & 3 deletions tests/aws-cpp-sdk-core-tests/smithy/client/SmithyClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <aws/core/client/ClientConfiguration.h>

#include "aws/core/endpoint/EndpointProviderBase.h"
#include <aws/core/utils/memory/stl/AWSAllocator.h>
#include <aws/core/http/HttpClientFactory.h>


class SmithyClientTest : public Aws::Testing::AwsCppSdkGTestSuite {
Expand All @@ -24,11 +26,36 @@ static constexpr char MyServiceName[] = "MySuperService";

TEST_F(SmithyClientTest, TestCompiles) {

const char ALLOCATION_TAG[] = "SmithyClientTest";

using MySmithyClientConfig = Aws::Client::ClientConfiguration;
using MyServiceAuthSchemeResolver = smithy::SigV4AuthSchemeResolver<>;

using MySmithyClient = smithy::client::AwsSmithyClientT<MyServiceName, MySmithyClientConfig, MyServiceAuthSchemeResolver, smithy::SigV4AuthScheme, MyTestEndpointProvider>;
Aws::Client::ClientConfiguration clientConfig;
std::shared_ptr<Aws::Http::HttpClient> httpClient = Aws::Http::CreateHttpClient(clientConfig);
std::shared_ptr<Aws::Client::AWSErrorMarshaller> errorMarshaller = Aws::MakeShared<Aws::Client::XmlErrorMarshaller>(ALLOCATION_TAG);
std::shared_ptr<MyTestEndpointProvider> endPointProvider;

std::shared_ptr<MyServiceAuthSchemeResolver> authSchemeResolver = Aws::MakeShared<MyServiceAuthSchemeResolver>(ALLOCATION_TAG);


Aws::UnorderedMap<Aws::String, Aws::Crt::Variant<smithy::SigV4AuthScheme>> authSchemesMap;

using MySmithyClient = smithy::client::AwsSmithyClientT<MyServiceName,
MySmithyClientConfig,
MyServiceAuthSchemeResolver,
Aws::Crt::Variant<smithy::SigV4AuthScheme>,
MyTestEndpointProvider>;

MySmithyClient* ptr = nullptr;
std::shared_ptr<MySmithyClient> ptr = Aws::MakeShared<MySmithyClient>(
ALLOCATION_TAG,
clientConfig,
"MyService",
httpClient,
errorMarshaller,
endPointProvider,
authSchemeResolver,
authSchemesMap
);
AWS_UNREFERENCED_PARAM(ptr);
}
}

0 comments on commit ef44297

Please sign in to comment.