-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathAwsSmithyClient.h
366 lines (330 loc) · 18.5 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* 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/http/HttpResponse.h>
#include <aws/core/http/HttpClientFactory.h>
#include <smithy/identity/signer/built-in/SignerProperties.h>
#include <smithy/client/AwsLegacyClient.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 AwsLegacyClientT<ServiceNameT, ResponseT,
AwsSmithyClientT<ServiceNameT,
ServiceClientConfigurationT,
ServiceAuthSchemeResolverT,
AuthSchemesVariantT,
EndpointProviderT,
SerializerT,
ResponseT,
ErrorMarshallerT>>
{
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 AwsSmithyClientAsyncRequestContext& ctx) const override
{
return AwsClientRequestSigning<AuthSchemesVariantT>::SignRequest(httpRequest, ctx, m_authSchemes);
}
bool AdjustClockSkew(HttpResponseOutcome& outcome, const AuthSchemeOption& authSchemeOption) const override
{
return AwsClientRequestSigning<AuthSchemesVariantT>::AdjustClockSkew(outcome, authSchemeOption, m_authSchemes);
}
IdentityOutcome ResolveIdentity(const AwsSmithyClientAsyncRequestContext& ctx) const override {
return AwsClientRequestSigning<AuthSchemesVariantT>::ResolveIdentity(ctx, m_authSchemes);
}
GetContextEndpointParametersOutcome GetContextEndpointParameters(const AwsSmithyClientAsyncRequestContext& ctx) const override {
return GetContextEndpointParametersImpl(ctx);
}
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(
EndpointUpdateCallback&& endpointCallback,
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
{
ExtractUriCallback getUriCallback = [&](Aws::Http::URI& uri, Aws::String& signerRegionOverride,
Aws::String& signerServiceNameOverride, const AuthSchemeOption& authSchemeOption) -> bool{
Aws::Endpoint::EndpointParameters epParams = Aws::Endpoint::EndpointParameters();
const auto authSchemeEpParams = authSchemeOption.endpointParameters();
epParams.insert(epParams.end(), authSchemeEpParams.begin(), authSchemeEpParams.end());
if(serviceSpecificParameters)
{
auto bucketIt = serviceSpecificParameters->parameterMap.find("bucketName");
if(bucketIt != serviceSpecificParameters->parameterMap.end())
{
auto bucket = bucketIt->second;
epParams.emplace_back(Aws::String("Bucket"), bucket);
}
}
auto epResolutionOutcome = this->ResolveEndpoint(std::move(epParams), std::move(endpointCallback));
if (!epResolutionOutcome.IsSuccess())
{
AWS_LOGSTREAM_ERROR(ServiceNameT, "Presigned URL generating failed. Encountered error: " << epResolutionOutcome.GetError().GetMessage());
return false;
}
auto endpoint = std::move(epResolutionOutcome.GetResultWithOwnership());
uri = endpoint.GetURI();
signerRegionOverride = region;
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 true;
};
CreateHttpRequestCallback createHttpRequestCallback = [&customizedHeaders, &serviceSpecificParameters](const Aws::Http::URI& uri, const Aws::Http::HttpMethod& method) -> std::shared_ptr<HttpRequest> {
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);
}
return request;
};
return GeneratePresignedUrl(
std::move(getUriCallback),
std::move(createHttpRequestCallback),
method,
region,
serviceName,
expirationInSeconds);
}
/* 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{};
private:
using ExtractUriCallback = std::function<bool (Aws::Http::URI&, Aws::String& region, Aws::String& serviceName,const AuthSchemeOption&)>;
using CreateHttpRequestCallback = std::function<std::shared_ptr<HttpRequest> (const Aws::Http::URI&, const Aws::Http::HttpMethod&)>;
Aws::String GeneratePresignedUrl(
ExtractUriCallback&& getUriCallback,
CreateHttpRequestCallback&& createHttpRequestCallback,
Aws::Http::HttpMethod method,
const Aws::String& region,
const Aws::String& serviceName,
long long expirationInSeconds) const
{
AwsSmithyClientAsyncRequestContext ctx;
auto authSchemeOptionOutcome = SelectAuthSchemeOption( ctx);
auto authSchemeOption = std::move(authSchemeOptionOutcome.GetResultWithOwnership());
Aws::Http::URI uri;
Aws::String signerRegionOverride = region;
Aws::String signerServiceNameOverride = serviceName;
if(!getUriCallback(uri , signerRegionOverride, signerServiceNameOverride, authSchemeOption))
{
return {};
}
std::shared_ptr<HttpRequest> request = createHttpRequestCallback(uri, method);
if (AwsClientRequestSigning<AuthSchemesVariantT>::PreSignRequest(request, authSchemeOption, m_authSchemes, signerRegionOverride, signerServiceNameOverride, expirationInSeconds).IsSuccess())
{
return request->GetURIString();
}
return {};
}
friend class AwsLegacyClientT<ServiceNameT, ResponseT, AwsSmithyClientT<ServiceNameT,
ServiceClientConfigurationT,
ServiceAuthSchemeResolverT,
AuthSchemesVariantT,
EndpointProviderT,
SerializerT,
ResponseT,
ErrorMarshallerT>>;
GetContextEndpointParametersOutcome GetContextEndpointParametersImpl(const AwsSmithyClientAsyncRequestContext& ctx) const {
Aws::Vector<Aws::Endpoint::EndpointParameter> endpointParameters;
const auto resolvedAccountId = ctx.m_awsIdentity->accountId();
const auto resolvedNonEmptyAccountId = resolvedAccountId.has_value() && !resolvedAccountId.value().empty();
// Set user agent if account ID was resolved in identity provider
if (resolvedNonEmptyAccountId) {
ctx.m_pRequest->AddUserAgentFeature(Aws::Client::UserAgentFeature::RESOLVED_ACCOUNT_ID);
}
// Only set EP param if client configuration does not have a configured account ID and we resolved a account id
if (resolvedNonEmptyAccountId && m_clientConfiguration.accountId.empty()) {
endpointParameters.emplace_back("AccountId", resolvedAccountId.value(), Aws::Endpoint::EndpointParameter::ParameterOrigin::OPERATION_CONTEXT);
}
return endpointParameters;
}
};
} // namespace client
} // namespace smithy