unit test for EndpointProvider and StsAssumeRoleCrendentialsProvider
This commit is contained in:
@@ -39,7 +39,7 @@ namespace AlibabaCloud
|
||||
ClientConfiguration configuration()const;
|
||||
std::string serviceName()const;
|
||||
protected:
|
||||
HttpClient::HttpResponseOutcome AttemptRequest(const std::string & endpoint, const ServiceRequest &request, HttpRequest::Method method)const;
|
||||
virtual HttpClient::HttpResponseOutcome AttemptRequest(const std::string & endpoint, const ServiceRequest &request, HttpRequest::Method method)const;
|
||||
Error buildCoreError(const HttpResponse &response)const;
|
||||
bool hasResponseError(const HttpResponse &response)const;
|
||||
virtual HttpRequest buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const = 0;
|
||||
|
||||
@@ -27,19 +27,22 @@
|
||||
|
||||
namespace AlibabaCloud
|
||||
{
|
||||
class ALIBABACLOUD_CORE_EXPORT EndpointProvider
|
||||
class ALIBABACLOUD_CORE_EXPORT EndpointProvider : public Location::LocationClient
|
||||
{
|
||||
public:
|
||||
typedef Outcome<Error, std::string> EndpointOutcome;
|
||||
|
||||
EndpointProvider(const std::shared_ptr<Location::LocationClient>& locationClient,
|
||||
const std::string regionId,
|
||||
const std::string product,
|
||||
const std::string serviceCode = std::string(),
|
||||
EndpointProvider(
|
||||
const Credentials &credentials,
|
||||
const ClientConfiguration &configuration,
|
||||
const std::string ®ionId,
|
||||
const std::string &product,
|
||||
const std::string &serviceCode = std::string(),
|
||||
int durationSeconds = 3600);
|
||||
~EndpointProvider();
|
||||
|
||||
EndpointOutcome getEndpoint();
|
||||
using LocationClient::describeEndpoints;
|
||||
private:
|
||||
struct Product
|
||||
{
|
||||
@@ -58,7 +61,6 @@ namespace AlibabaCloud
|
||||
|
||||
std::mutex cachedMutex_;
|
||||
std::string cachedEndpoint_;
|
||||
std::shared_ptr<Location::LocationClient> locationClient_;
|
||||
int durationSeconds_;
|
||||
std::chrono::system_clock::time_point expiry_;
|
||||
std::string regionId_;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
* Copyright 2009-2017 Alibaba Cloud All rights reserved.
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@@ -40,14 +40,16 @@ namespace AlibabaCloud
|
||||
const ClientConfiguration &configuration,
|
||||
const std::shared_ptr<Signer> &signer = std::make_shared<HmacSha1Signer>());
|
||||
virtual ~RpcServiceClient();
|
||||
|
||||
using CoreClient::configuration;
|
||||
using CoreClient::AttemptRequest;
|
||||
|
||||
protected:
|
||||
virtual JsonOutcome makeRequest(const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method = HttpRequest::Method::Get)const;
|
||||
virtual HttpRequest buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const override;
|
||||
HttpRequest buildHttpRequest(const std::string & endpoint, const RpcServiceRequest &msg, HttpRequest::Method method)const;
|
||||
private:
|
||||
std::string canonicalizedQuery(const std::map <std::string, std::string> ¶ms)const;
|
||||
|
||||
|
||||
std::shared_ptr<CredentialsProvider> credentialsProvider_;
|
||||
std::shared_ptr<Signer> signer_;
|
||||
};
|
||||
|
||||
@@ -24,10 +24,12 @@
|
||||
|
||||
namespace AlibabaCloud
|
||||
{
|
||||
class ALIBABACLOUD_CORE_EXPORT StsAssumeRoleCredentialsProvider : public CredentialsProvider
|
||||
class ALIBABACLOUD_CORE_EXPORT StsAssumeRoleCredentialsProvider : public CredentialsProvider, public Sts::StsClient
|
||||
{
|
||||
public:
|
||||
StsAssumeRoleCredentialsProvider(const std::shared_ptr<Sts::StsClient>& stsClient,
|
||||
StsAssumeRoleCredentialsProvider(
|
||||
const Credentials &credentials,
|
||||
const ClientConfiguration &configuration,
|
||||
const std::string &roleArn,
|
||||
const std::string &roleSessionName,
|
||||
const std::string &policy = "",
|
||||
@@ -35,13 +37,13 @@ namespace AlibabaCloud
|
||||
~StsAssumeRoleCredentialsProvider();
|
||||
|
||||
virtual Credentials getCredentials() override;
|
||||
using StsClient::assumeRole;
|
||||
private:
|
||||
void loadCredentials();
|
||||
bool checkExpiry()const;
|
||||
|
||||
std::mutex cachedMutex_;
|
||||
Credentials cachedCredentials_;
|
||||
std::shared_ptr<Sts::StsClient> stsClient_;
|
||||
int durationSeconds_;
|
||||
std::chrono::system_clock::time_point expiry_;
|
||||
std::string policy_;
|
||||
|
||||
@@ -38,11 +38,11 @@ namespace AlibabaCloud
|
||||
LocationClient(const std::shared_ptr<CredentialsProvider> &credentialsProvider, const ClientConfiguration &configuration);
|
||||
LocationClient(const std::string &accessKeyId, const std::string &accessKeySecret, const ClientConfiguration &configuration);
|
||||
~LocationClient();
|
||||
|
||||
DescribeEndpointsOutcome describeEndpoints(const Model::DescribeEndpointsRequest &request)const;
|
||||
virtual DescribeEndpointsOutcome describeEndpoints(const Model::DescribeEndpointsRequest &request)const;
|
||||
void describeEndpointsAsync(const Model::DescribeEndpointsRequest& request, const DescribeEndpointsAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context = nullptr) const;
|
||||
DescribeEndpointsOutcomeCallable describeEndpointsCallable(const Model::DescribeEndpointsRequest& request) const;
|
||||
using RpcServiceClient::makeRequest;
|
||||
using RpcServiceClient::configuration;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,13 +39,13 @@ namespace AlibabaCloud
|
||||
typedef Outcome<Error, Model::GetCallerIdentityResult> GetCallerIdentityOutcome;
|
||||
typedef std::future<GetCallerIdentityOutcome> GetCallerIdentityOutcomeCallable;
|
||||
typedef std::function<void(const StsClient*, const Model::GetCallerIdentityRequest&, const GetCallerIdentityOutcome&, const std::shared_ptr<const AsyncCallerContext>&)> GetCallerIdentityAsyncHandler;
|
||||
|
||||
|
||||
StsClient(const Credentials &credentials, const ClientConfiguration &configuration);
|
||||
StsClient(const std::shared_ptr<CredentialsProvider> &credentialsProvider, const ClientConfiguration &configuration);
|
||||
StsClient(const std::string &accessKeyId, const std::string &accessKeySecret, const ClientConfiguration &configuration);
|
||||
~StsClient();
|
||||
|
||||
AssumeRoleOutcome assumeRole(const Model::AssumeRoleRequest &request)const;
|
||||
virtual AssumeRoleOutcome assumeRole(const Model::AssumeRoleRequest &request)const;
|
||||
void assumeRoleAsync(const Model::AssumeRoleRequest& request, const AssumeRoleAsyncHandler& handler, const std::shared_ptr<const AsyncCallerContext>& context = nullptr) const;
|
||||
AssumeRoleOutcomeCallable assumeRoleCallable(const Model::AssumeRoleRequest& request) const;
|
||||
GetCallerIdentityOutcome getCallerIdentity(const Model::GetCallerIdentityRequest &request)const;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
* Copyright 2009-2017 Alibaba Cloud All rights reserved.
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@@ -58,7 +58,7 @@ void CoreClient::asyncExecute(Runnable * r)const
|
||||
HttpClient::HttpResponseOutcome CoreClient::AttemptRequest(const std::string & endpoint, const ServiceRequest & request, HttpRequest::Method method) const
|
||||
{
|
||||
auto r = buildHttpRequest(endpoint, request, method);
|
||||
auto outcome = httpClient_->makeRequest(r);
|
||||
auto outcome = httpClient_->makeRequest(r);
|
||||
if (!outcome.isSuccess())
|
||||
return outcome;
|
||||
if(hasResponseError(outcome.result()))
|
||||
|
||||
@@ -82,8 +82,14 @@ namespace
|
||||
"]}";
|
||||
}
|
||||
|
||||
EndpointProvider::EndpointProvider(const std::shared_ptr<Location::LocationClient>& locationClient, const std::string regionId, const std::string product, const std::string serviceCode, int durationSeconds) :
|
||||
locationClient_(locationClient),
|
||||
EndpointProvider::EndpointProvider(
|
||||
const Credentials& credentials,
|
||||
const ClientConfiguration &configuration,
|
||||
const std::string ®ionId,
|
||||
const std::string &product,
|
||||
const std::string &serviceCode,
|
||||
int durationSeconds) :
|
||||
LocationClient(credentials, configuration),
|
||||
regionId_(regionId),
|
||||
product_(product),
|
||||
serviceCode_(serviceCode),
|
||||
@@ -157,8 +163,8 @@ std::string EndpointProvider::internalEndpoint(const std::string regionId, const
|
||||
|
||||
EndpointProvider::EndpointOutcome EndpointProvider::getEndpoint()
|
||||
{
|
||||
if (!locationClient_->configuration().endpoint().empty())
|
||||
return EndpointOutcome(locationClient_->configuration().endpoint());
|
||||
if (!configuration().endpoint().empty())
|
||||
return EndpointOutcome(configuration().endpoint());
|
||||
|
||||
if (!serviceCode_.empty())
|
||||
{
|
||||
@@ -188,7 +194,7 @@ EndpointProvider::EndpointOutcome EndpointProvider::loadRemoteEndpoint()
|
||||
request.setId(regionId_);
|
||||
request.setServiceCode(serviceCode_);
|
||||
request.setType("openAPI");
|
||||
auto outcome = locationClient_->describeEndpoints(request);
|
||||
auto outcome = describeEndpoints(request);
|
||||
if (!outcome.isSuccess())
|
||||
return EndpointOutcome(outcome.error());
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
* Copyright 2009-2017 Alibaba Cloud All rights reserved.
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
||||
@@ -21,9 +21,15 @@
|
||||
using namespace AlibabaCloud;
|
||||
using namespace AlibabaCloud::Sts;
|
||||
|
||||
StsAssumeRoleCredentialsProvider::StsAssumeRoleCredentialsProvider(const std::shared_ptr<StsClient>& stsClient, const std::string & roleArn, const std::string & roleSessionName, const std::string & policy, int durationSeconds) :
|
||||
StsAssumeRoleCredentialsProvider::StsAssumeRoleCredentialsProvider(
|
||||
const Credentials &credentials,
|
||||
const ClientConfiguration &configuration,
|
||||
const std::string & roleArn,
|
||||
const std::string & roleSessionName,
|
||||
const std::string & policy,
|
||||
int durationSeconds):
|
||||
CredentialsProvider(),
|
||||
stsClient_(stsClient),
|
||||
StsClient(credentials, configuration),
|
||||
roleArn_(roleArn),
|
||||
roleSessionName_(roleSessionName),
|
||||
policy_(policy),
|
||||
@@ -66,7 +72,7 @@ void StsAssumeRoleCredentialsProvider::loadCredentials()
|
||||
request.setPolicy(policy_);
|
||||
request.setDurationSeconds(durationSeconds_);
|
||||
|
||||
auto assumeRoleOutcome = stsClient_->assumeRole(request);
|
||||
auto assumeRoleOutcome = assumeRole(request);
|
||||
if (assumeRoleOutcome.isSuccess())
|
||||
{
|
||||
const auto stsCredentials = assumeRoleOutcome.result().credentials();
|
||||
|
||||
Reference in New Issue
Block a user