-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathAwsSmithyClient.h
280 lines (251 loc) · 14.4 KB
/
AwsSmithyClient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#pragma once
#include <smithy/client/AwsSmithyClientBase.h>
#include <smithy/client/AwsSmithyClientAsyncRequestContext.h>
#include <smithy/client/common/AwsSmithyRequestSigning.h>
#include <smithy/identity/identity/AwsIdentity.h>
#include <smithy/identity/auth/AuthSchemeOption.h>
#include <smithy/identity/auth/AuthSchemeResolverBase.h>
#include <smithy/tracing/TelemetryProvider.h>
#include <aws/crt/Variant.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/http/HttpResponse.h>
#include <aws/core/utils/memory/stl/AWSMap.h>
#include <aws/core/utils/FutureOutcome.h>
#include <aws/core/utils/Outcome.h>
#include <aws/core/utils/threading/Executor.h>
#include <aws/core/utils/threading/SameThreadExecutor.h>
#include <aws/core/http/HttpResponse.h>
#include <aws/core/http/HttpClientFactory.h>
#include <smithy/identity/signer/built-in/SignerProperties.h>
namespace smithy {
namespace client
{
template<const char* ServiceNameT,
typename ServiceClientConfigurationT,
typename ServiceAuthSchemeResolverT,
typename AuthSchemesVariantT,
typename EndpointProviderT,
typename SerializerT,
typename ResponseT,
typename ErrorMarshallerT>
class AwsSmithyClientT : public AwsSmithyClientBase
{
public:
static_assert(std::is_base_of<Aws::Client::AWSErrorMarshaller, ErrorMarshallerT>::value, "MarshallerT must be derived from class Aws::Client::AWSErrorMarshaller");
explicit AwsSmithyClientT(const ServiceClientConfigurationT& clientConfig,
const Aws::String& serviceName,
const Aws::String& serviceUserAgentName,
const std::shared_ptr<Aws::Http::HttpClient>& httpClient,
const std::shared_ptr<Aws::Client::AWSErrorMarshaller>& errorMarshaller,
const std::shared_ptr<EndpointProviderT> endpointProvider,
const std::shared_ptr<ServiceAuthSchemeResolverT>& authSchemeResolver,
const Aws::UnorderedMap<Aws::String, AuthSchemesVariantT>& authSchemes)
: AwsSmithyClientBase(Aws::MakeUnique<ServiceClientConfigurationT>(ServiceNameT, clientConfig),
serviceName,
serviceUserAgentName,
httpClient,
errorMarshaller),
m_clientConfiguration(*static_cast<ServiceClientConfigurationT*>(AwsSmithyClientBase::m_clientConfig.get())),
m_endpointProvider(endpointProvider),
m_authSchemeResolver(authSchemeResolver),
m_authSchemes(authSchemes),
m_serializer(Aws::MakeShared<SerializerT>(ServiceNameT, m_clientConfiguration.telemetryProvider))
{
initClient();
}
AwsSmithyClientT(const AwsSmithyClientT& other)
: AwsSmithyClientBase(other,
Aws::MakeUnique<ServiceClientConfigurationT>(ServiceNameT, other.m_clientConfiguration),
Aws::Http::CreateHttpClient(other.m_clientConfiguration),
Aws::MakeShared<ErrorMarshallerT>(ServiceNameT)),
m_clientConfiguration(*static_cast<ServiceClientConfigurationT*>(m_clientConfig.get()))
{
m_endpointProvider = other.m_endpointProvider; /* shallow copy */
m_authSchemeResolver = other.m_authSchemeResolver; /* shallow copy */
m_authSchemes = other.m_authSchemes;
m_serializer = Aws::MakeShared<SerializerT>(ServiceNameT, m_clientConfiguration.telemetryProvider);
initClient();
}
AwsSmithyClientT& operator=(const AwsSmithyClientT& other)
{
if(this != &other)
{
m_clientConfiguration = other.m_clientConfiguration;
AwsSmithyClientBase::baseCopyAssign(other,
Aws::Http::CreateHttpClient(m_clientConfiguration),
Aws::MakeShared<ErrorMarshallerT>(ServiceNameT));
m_endpointProvider = other.m_endpointProvider; /* shallow copy */
m_authSchemeResolver = other.m_authSchemeResolver; /* shallow copy */
m_authSchemes = other.m_authSchemes;
m_serializer = Aws::MakeShared<SerializerT>(ServiceNameT, m_clientConfiguration.telemetryProvider);
initClient();
}
return *this;
}
AwsSmithyClientT(AwsSmithyClientT&& other) :
AwsSmithyClientBase(std::move(static_cast<AwsSmithyClientBase&&>(other)),
Aws::MakeUnique<ServiceClientConfigurationT>(ServiceNameT, std::move(other.m_clientConfiguration))),
m_clientConfiguration{*static_cast<ServiceClientConfigurationT*>(m_clientConfig.get())},
m_endpointProvider(std::move(other.m_endpointProvider)),
m_authSchemeResolver(std::move(other.m_authSchemeResolver)),
m_authSchemes(std::move(other.m_authSchemes)),
m_serializer(std::move(other.m_serializer))
{
}
AwsSmithyClientT& operator=(AwsSmithyClientT&& other)
{
if(this != &other)
{
m_clientConfiguration = std::move(other.m_clientConfiguration);
AwsSmithyClientBase::baseMoveAssign(std::move(static_cast<AwsSmithyClientBase&&>(other)));
m_endpointProvider = std::move(other.m_endpointProvider);
m_authSchemeResolver = std::move(other.m_authSchemeResolver);
m_authSchemes = std::move(other.m_authSchemes);
m_serializer = std::move(other.m_serializer);
}
return *this;
}
virtual ~AwsSmithyClientT() = default;
protected:
void initClient() {
if (m_endpointProvider && m_authSchemeResolver) {
m_endpointProvider->InitBuiltInParameters(m_clientConfiguration);
m_authSchemeResolver->Init(m_clientConfiguration);
} else {
AWS_LOGSTREAM_FATAL(ServiceNameT, "Unable to init client: endpoint provider=" << m_endpointProvider
<< " or " << "authSchemeResolver=" << m_authSchemeResolver << " are null!");
}
}
inline const char* GetServiceClientName() const override { return m_serviceName.c_str(); }
ResolveEndpointOutcome ResolveEndpoint(const Aws::Endpoint::EndpointParameters& endpointParameters, EndpointUpdateCallback&& epCallback) const override
{
assert(m_endpointProvider);
ResolveEndpointOutcome resolveEndpointOutcome = m_endpointProvider->ResolveEndpoint(endpointParameters);
if (resolveEndpointOutcome.IsSuccess())
{
epCallback(resolveEndpointOutcome.GetResult());
}
return resolveEndpointOutcome;
}
SelectAuthSchemeOptionOutcome SelectAuthSchemeOption(const AwsSmithyClientAsyncRequestContext& ctx) const override
{
assert(m_authSchemeResolver);
typename ServiceAuthSchemeResolverT::ServiceAuthSchemeParameters identityParams;
identityParams.serviceName = m_serviceName;
identityParams.operation = ctx.m_requestName;
identityParams.region = m_clientConfiguration.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.additionalProperties.insert({epParam.GetName(), epParam.GetStrValueNoCheck()});
else if (epParam.GetStoredType() == ParameterType::BOOLEAN)
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.additionalProperties.insert({serviceParam.first, serviceParam.second});
}
}
}
Aws::Vector<AuthSchemeOption> authSchemeOptions = m_authSchemeResolver->resolveAuthScheme(identityParams);
auto authSchemeOptionIt = std::find_if(authSchemeOptions.begin(), authSchemeOptions.end(),
[this](const AuthSchemeOption& opt)
{
return m_authSchemes.find(opt.schemeId) != m_authSchemes.end();
});
assert(authSchemeOptionIt != authSchemeOptions.end());
if (authSchemeOptionIt != authSchemeOptions.end()) {
return SelectAuthSchemeOptionOutcome(*authSchemeOptionIt);
}
return Aws::Client::AWSError<CoreErrors>(Aws::Client::CoreErrors::CLIENT_SIGNING_FAILURE,
"",
"Failed to select an auth scheme",
false/*retryable*/);
}
SigningOutcome SignHttpRequest(std::shared_ptr<HttpRequest> httpRequest, const AuthSchemeOption& targetAuthSchemeOption) const override
{
return AwsClientRequestSigning<AuthSchemesVariantT>::SignRequest(httpRequest, targetAuthSchemeOption, m_authSchemes);
}
bool AdjustClockSkew(HttpResponseOutcome& outcome, const AuthSchemeOption& authSchemeOption) const override
{
return AwsClientRequestSigning<AuthSchemesVariantT>::AdjustClockSkew(outcome, authSchemeOption, m_authSchemes);
}
ResponseT MakeRequestDeserialize(Aws::AmazonWebServiceRequest const * const request,
const char* requestName,
Aws::Http::HttpMethod method,
EndpointUpdateCallback&& endpointCallback) const
{
auto httpResponseOutcome = MakeRequestSync(request, requestName, method, std::move(endpointCallback));
return m_serializer->Deserialize(std::move(httpResponseOutcome), GetServiceClientName(), requestName);
}
Aws::String GeneratePresignedUrl(const Aws::Http::URI& uri,
Aws::Http::HttpMethod method,
const Aws::String& region,
const Aws::String& serviceName,
long long expirationInSeconds,
const Aws::Http::HeaderValueCollection& customizedHeaders,
const std::shared_ptr<Aws::Http::ServiceSpecificParameters> serviceSpecificParameters) const
{
std::shared_ptr<HttpRequest> request = CreateHttpRequest(uri, method, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
request->SetServiceSpecificParameters(serviceSpecificParameters);
for (const auto& it: customizedHeaders)
{
request->SetHeaderValue(it.first.c_str(), it.second);
}
AwsSmithyClientAsyncRequestContext ctx;
auto authSchemeOptionOutcome = SelectAuthSchemeOption( ctx);
auto authSchemeOption = std::move(authSchemeOptionOutcome.GetResultWithOwnership());
if (AwsClientRequestSigning<AuthSchemesVariantT>::PreSignRequest(request, authSchemeOption, m_authSchemes, region, serviceName, expirationInSeconds).IsSuccess())
{
return request->GetURIString();
}
return {};
}
Aws::String GeneratePresignedUrl(const Aws::Endpoint::AWSEndpoint& endpoint,
Aws::Http::HttpMethod method,
const Aws::String& region,
const Aws::String& serviceName,
long long expirationInSeconds,
const Aws::Http::HeaderValueCollection& customizedHeaders,
const std::shared_ptr<Aws::Http::ServiceSpecificParameters> serviceSpecificParameters) const
{
const Aws::Http::URI& uri = endpoint.GetURI();
auto signerRegionOverride = region;
auto signerServiceNameOverride = serviceName;
// signer name is needed for some identity resolvers
if (endpoint.GetAttributes()) {
if (endpoint.GetAttributes()->authScheme.GetSigningRegion()) {
signerRegionOverride = endpoint.GetAttributes()->authScheme.GetSigningRegion()->c_str();
}
if (endpoint.GetAttributes()->authScheme.GetSigningRegionSet()) {
signerRegionOverride = endpoint.GetAttributes()->authScheme.GetSigningRegionSet()->c_str();
}
if (endpoint.GetAttributes()->authScheme.GetSigningName()) {
signerServiceNameOverride = endpoint.GetAttributes()->authScheme.GetSigningName()->c_str();
}
}
return GeneratePresignedUrl(uri, method, signerRegionOverride, signerServiceNameOverride, expirationInSeconds, customizedHeaders, serviceSpecificParameters);
}
protected:
/* Service client specific config, the actual object is stored in AwsSmithyClientBase by pointer
* In order to avoid config object duplication, smithy template client access it by a reference.
* So that base client has it by base config pointer, child smithy client has it by child config reference.
*/
ServiceClientConfigurationT& m_clientConfiguration;
std::shared_ptr<EndpointProviderT> m_endpointProvider{};
std::shared_ptr<ServiceAuthSchemeResolverT> m_authSchemeResolver{};
Aws::UnorderedMap<Aws::String, AuthSchemesVariantT> m_authSchemes{};
std::shared_ptr<SerializerT> m_serializer{};
};
} // namespace client
} // namespace smithy