由丰鲁成发起的CORE SDK自动发布, BUILD_ID=147, 版本号:1.0.9
发布日志: 1, Update InstanceProfileCredentialsProvider 2, Update common api
This commit is contained in:
@@ -28,7 +28,6 @@ set(core_public_header
|
||||
include/alibabacloud/core/CoreExport.h
|
||||
include/alibabacloud/core/Credentials.h
|
||||
include/alibabacloud/core/CredentialsProvider.h
|
||||
include/alibabacloud/core/EcsInstanceProfileConfigLoader.h
|
||||
include/alibabacloud/core/EndpointProvider.h
|
||||
include/alibabacloud/core/Error.h
|
||||
include/alibabacloud/core/Global.h
|
||||
@@ -37,11 +36,9 @@ set(core_public_header
|
||||
include/alibabacloud/core/HttpMessage.h
|
||||
include/alibabacloud/core/HttpResponse.h
|
||||
include/alibabacloud/core/HttpRequest.h
|
||||
include/alibabacloud/core/InstanceProfileConfigLoader.h
|
||||
include/alibabacloud/core/InstanceProfileCredentialsProvider.h
|
||||
include/alibabacloud/core/NetworkProxy.h
|
||||
include/alibabacloud/core/Outcome.h
|
||||
include/alibabacloud/core/Profile.h
|
||||
include/alibabacloud/core/Runnable.h
|
||||
include/alibabacloud/core/RoaServiceClient.h
|
||||
include/alibabacloud/core/RoaServiceRequest.h
|
||||
@@ -77,9 +74,8 @@ set(core_src
|
||||
src/CredentialsProvider.cc
|
||||
src/CurlHttpClient.h
|
||||
src/CurlHttpClient.cc
|
||||
src/EcsInstanceProfileConfigLoader.cc
|
||||
src/EcsMetadataClient.h
|
||||
src/EcsMetadataClient.cc
|
||||
src/EcsMetadataFetcher.cc
|
||||
src/EcsMetadataFetcher.h
|
||||
src/EndpointProvider.cc
|
||||
src/Error.cc
|
||||
src/Executor.cc
|
||||
@@ -89,11 +85,9 @@ set(core_src
|
||||
src/HttpMessage.cc
|
||||
src/HttpResponse.cc
|
||||
src/HttpRequest.cc
|
||||
src/InstanceProfileConfigLoader.cc
|
||||
src/InstanceProfileCredentialsProvider.cc
|
||||
src/NetworkProxy.cc
|
||||
src/Outcome.cc
|
||||
src/Profile.cc
|
||||
src/Runnable.cc
|
||||
src/RoaServiceClient.cc
|
||||
src/RoaServiceRequest.cc
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
2018-01-26 Version: 1.0.9
|
||||
1, Update InstanceProfileCredentialsProvider
|
||||
2, Update common api
|
||||
|
||||
2017-12-27 Version: 1.0.0
|
||||
1, Fix build failed on linux
|
||||
|
||||
|
||||
@@ -27,7 +27,13 @@ namespace AlibabaCloud
|
||||
class ALIBABACLOUD_CORE_EXPORT CommonRequest : public ServiceRequest
|
||||
{
|
||||
public:
|
||||
CommonRequest();
|
||||
enum RequestPattern
|
||||
{
|
||||
RpcPattern,
|
||||
RoaPattern
|
||||
};
|
||||
|
||||
explicit CommonRequest(RequestPattern pattern = RpcPattern);
|
||||
~CommonRequest();
|
||||
|
||||
std::string domain()const;
|
||||
@@ -42,15 +48,15 @@ namespace AlibabaCloud
|
||||
void setHttpMethod(HttpRequest::Method method);
|
||||
void setQueryParameter(const ParameterNameType &name, const ParameterValueType &value);
|
||||
using ServiceRequest::setResourcePath;
|
||||
void setUriPattern(const std::string &uriPattern);
|
||||
void setRequestPattern(RequestPattern pattern);
|
||||
using ServiceRequest::setVersion;
|
||||
std::string uriPattern()const;
|
||||
RequestPattern requestPattern()const;
|
||||
protected:
|
||||
using ServiceRequest::product;
|
||||
|
||||
private:
|
||||
std::string domain_;
|
||||
std::string uriPattern_;
|
||||
RequestPattern requestPattern_;
|
||||
ParameterCollection queryParams_;
|
||||
ParameterCollection headerParams_;
|
||||
HttpRequest::Method httpMethod_;
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ALIBABACLOUD_CORE_ECSINSTANCEPROFILECONFIGLOADER_H_
|
||||
#define ALIBABACLOUD_CORE_ECSINSTANCEPROFILECONFIGLOADER_H_
|
||||
|
||||
#include <memory>
|
||||
#include "InstanceProfileConfigLoader.h"
|
||||
|
||||
namespace AlibabaCloud
|
||||
{
|
||||
class EcsMetadataClient;
|
||||
class EcsInstanceProfileConfigLoader : public InstanceProfileConfigLoader
|
||||
{
|
||||
public:
|
||||
EcsInstanceProfileConfigLoader();
|
||||
EcsInstanceProfileConfigLoader(const std::shared_ptr<EcsMetadataClient>& metadataClient);
|
||||
~EcsInstanceProfileConfigLoader();
|
||||
|
||||
protected:
|
||||
virtual bool loadInternal() override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<EcsMetadataClient> metadataClient_;
|
||||
};
|
||||
}
|
||||
#endif // !ALIBABACLOUD_CORE_ECSINSTANCEPROFILECONFIGLOADER_H_
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ALIBABACLOUD_CORE_INSTANCEPROFILECONFIGLOADER_H_
|
||||
#define ALIBABACLOUD_CORE_INSTANCEPROFILECONFIGLOADER_H_
|
||||
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "Profile.h"
|
||||
|
||||
namespace AlibabaCloud
|
||||
{
|
||||
class InstanceProfileConfigLoader
|
||||
{
|
||||
public:
|
||||
InstanceProfileConfigLoader();
|
||||
virtual ~InstanceProfileConfigLoader();
|
||||
|
||||
bool load();
|
||||
bool persistProfiles(const std::map<std::string, Profile>& profiles);
|
||||
|
||||
std::map<std::string, Profile> allProfiles() const;
|
||||
std::chrono::system_clock::time_point lastLoadTime() const;
|
||||
|
||||
protected:
|
||||
virtual bool loadInternal() = 0;
|
||||
virtual bool persistInternal(const std::map<std::string, Profile>&);
|
||||
|
||||
std::map<std::string, Profile> profiles_;
|
||||
std::chrono::system_clock::time_point lastLoadTime_;
|
||||
};
|
||||
}
|
||||
#endif // !ALIBABACLOUD_CORE_INSTANCEPROFILECONFIGLOADER_H_
|
||||
@@ -20,28 +20,31 @@
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "CredentialsProvider.h"
|
||||
#include "Credentials.h"
|
||||
|
||||
namespace AlibabaCloud
|
||||
{
|
||||
class EcsInstanceProfileConfigLoader;
|
||||
class InstanceProfileConfigLoader;
|
||||
{
|
||||
class EcsMetadataFetcher;
|
||||
class ALIBABACLOUD_CORE_EXPORT InstanceProfileCredentialsProvider : public CredentialsProvider
|
||||
{
|
||||
public:
|
||||
InstanceProfileCredentialsProvider(size_t refreshRateMs = (1000 * 60 * 10));
|
||||
InstanceProfileCredentialsProvider(const std::shared_ptr<EcsInstanceProfileConfigLoader>&, size_t refreshRateMs = (1000 * 60 * 10));
|
||||
InstanceProfileCredentialsProvider(const std::string &roleName, int durationSeconds = 3600);
|
||||
~InstanceProfileCredentialsProvider();
|
||||
|
||||
std::string roleName()const;
|
||||
virtual Credentials getCredentials() override;
|
||||
|
||||
private:
|
||||
void refreshIfExpired();
|
||||
virtual bool isTimeToRefresh(long reloadFrequency);
|
||||
|
||||
std::chrono::system_clock::time_point lastLoaded_;
|
||||
std::shared_ptr<InstanceProfileConfigLoader> metadataConfigLoader_;
|
||||
size_t loadFrequencyMs_;
|
||||
mutable std::mutex m_reloadMutex;
|
||||
void loadCredentials();
|
||||
bool checkExpiry()const;
|
||||
|
||||
std::mutex cachedMutex_;
|
||||
Credentials cachedCredentials_;
|
||||
int durationSeconds_;
|
||||
std::chrono::system_clock::time_point expiry_;
|
||||
EcsMetadataFetcher *fetcher_;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ALIBABACLOUD_CORE_PROFILE_H_
|
||||
#define ALIBABACLOUD_CORE_PROFILE_H_
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <alibabacloud/core/Credentials.h>
|
||||
|
||||
namespace AlibabaCloud
|
||||
{
|
||||
class Profile
|
||||
{
|
||||
public:
|
||||
std::string name() const;
|
||||
void setName(const std::string& value);
|
||||
Credentials credentials() const;
|
||||
void setCredentials(const Credentials& value);
|
||||
std::string region() const;
|
||||
void setRegion(const std::string& value);
|
||||
std::string roleArn() const;
|
||||
void setRoleArn(const std::string& value);
|
||||
std::string sourceProfile() const;
|
||||
void setSourceProfile(const std::string& value);
|
||||
void setAllKeyValPairs(const std::map<std::string, std::string>& map);
|
||||
std::string value(const std::string& key);
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::string region_;
|
||||
Credentials credentials_;
|
||||
std::string roleArn_;
|
||||
std::string sourceProfile_;
|
||||
std::map<std::string, std::string> allKeyValPairs_;
|
||||
};
|
||||
|
||||
}
|
||||
#endif // !ALIBABACLOUD_CORE_PROFILE_H_
|
||||
@@ -121,8 +121,7 @@ HttpRequest CommonClient::buildHttpRequest(const std::string & endpoint, const S
|
||||
|
||||
HttpRequest CommonClient::buildHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const
|
||||
{
|
||||
if (msg.uriPattern().empty() ||
|
||||
(strcasecmp(msg.uriPattern().c_str(),"rpc") == 0))
|
||||
if (msg.requestPattern() == CommonRequest::RpcPattern)
|
||||
return buildRpcHttpRequest(endpoint, msg, method);
|
||||
else
|
||||
return buildRoaHttpRequest(endpoint, msg, method);
|
||||
@@ -207,7 +206,8 @@ HttpRequest CommonClient::buildRoaHttpRequest(const std::string & endpoint, cons
|
||||
request.setHeader("Host", url.host());
|
||||
request.setHeader("x-sdk-client", std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
|
||||
request.setHeader("x-acs-region-id", configuration().regionId());
|
||||
request.setHeader("x-acs-security-token", credentials.sessionToken());
|
||||
if (!credentials.sessionToken().empty())
|
||||
request.setHeader("x-acs-security-token", credentials.sessionToken());
|
||||
request.setHeader("x-acs-signature-method", signer_->name());
|
||||
request.setHeader("x-acs-signature-nonce", GenerateUuid());
|
||||
request.setHeader("x-acs-signature-version", signer_->version());
|
||||
|
||||
@@ -18,12 +18,12 @@
|
||||
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
CommonRequest::CommonRequest():
|
||||
CommonRequest::CommonRequest(RequestPattern pattern):
|
||||
ServiceRequest("",""),
|
||||
domain_(),
|
||||
queryParams_(),
|
||||
httpMethod_(HttpRequest::Get),
|
||||
uriPattern_("rpc")
|
||||
requestPattern_(pattern)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -40,14 +40,14 @@ void CommonRequest::setDomain(const std::string &domain)
|
||||
domain_ = domain;
|
||||
}
|
||||
|
||||
std::string CommonRequest::uriPattern() const
|
||||
CommonRequest::RequestPattern CommonRequest::requestPattern() const
|
||||
{
|
||||
return uriPattern_;
|
||||
return requestPattern_;
|
||||
}
|
||||
|
||||
void CommonRequest::setUriPattern(const std::string & uriPattern)
|
||||
void CommonRequest::setRequestPattern(RequestPattern pattern)
|
||||
{
|
||||
uriPattern_ = uriPattern;
|
||||
requestPattern_ = pattern;
|
||||
}
|
||||
|
||||
void CommonRequest::setHttpMethod(HttpRequest::Method method)
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <alibabacloud/core/EcsInstanceProfileConfigLoader.h>
|
||||
#include "EcsMetadataClient.h"
|
||||
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
EcsInstanceProfileConfigLoader::EcsInstanceProfileConfigLoader() :
|
||||
metadataClient_(std::make_shared<EcsMetadataClient>())
|
||||
{
|
||||
}
|
||||
|
||||
EcsInstanceProfileConfigLoader::EcsInstanceProfileConfigLoader(const std::shared_ptr<EcsMetadataClient>& client) :
|
||||
metadataClient_(client)
|
||||
{
|
||||
}
|
||||
|
||||
EcsInstanceProfileConfigLoader::~EcsInstanceProfileConfigLoader()
|
||||
{
|
||||
}
|
||||
|
||||
bool EcsInstanceProfileConfigLoader::loadInternal()
|
||||
{
|
||||
//TODO(fenglc): load form remote server
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "EcsMetadataClient.h"
|
||||
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
EcsMetadataClient::EcsMetadataClient()
|
||||
{
|
||||
}
|
||||
|
||||
EcsMetadataClient::~EcsMetadataClient()
|
||||
{
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ALIBABACLOUD_CORE_ECSMETADATACLIENT_H_
|
||||
#define ALIBABACLOUD_CORE_ECSMETADATACLIENT_H_
|
||||
|
||||
namespace AlibabaCloud
|
||||
{
|
||||
class EcsMetadataClient
|
||||
{
|
||||
public:
|
||||
EcsMetadataClient();
|
||||
~EcsMetadataClient();
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
}
|
||||
#endif // !ALIBABACLOUD_CORE_ECSMETADATACLIENT_H_
|
||||
69
core/src/EcsMetadataFetcher.cc
Normal file
69
core/src/EcsMetadataFetcher.cc
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "EcsMetadataFetcher.h"
|
||||
#include <sstream>
|
||||
#include <memory>
|
||||
#include <alibabacloud/core/Url.h>
|
||||
#include "CurlHttpClient.h"
|
||||
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
namespace
|
||||
{
|
||||
const int DEFAULT_TIMEOUT_IN_MILLISECONDS = 5000;
|
||||
const char* const METADATA_SERVICE_HOST = "100.100.100.200";
|
||||
const char* const URL_IN_ECS_METADATA = "/latest/meta-data/ram/security-credentials/";
|
||||
}
|
||||
|
||||
EcsMetadataFetcher::EcsMetadataFetcher()
|
||||
{
|
||||
}
|
||||
|
||||
EcsMetadataFetcher::~EcsMetadataFetcher()
|
||||
{
|
||||
}
|
||||
|
||||
std::string EcsMetadataFetcher::roleName()const
|
||||
{
|
||||
return roleName_;
|
||||
}
|
||||
|
||||
void EcsMetadataFetcher::setRoleName(const std::string & roleName)
|
||||
{
|
||||
roleName_ = roleName;
|
||||
}
|
||||
|
||||
std::string EcsMetadataFetcher::getMetadata()
|
||||
{
|
||||
std::stringstream path;
|
||||
path << URL_IN_ECS_METADATA << roleName_;
|
||||
|
||||
Url credentialUrl;
|
||||
credentialUrl.setHost(METADATA_SERVICE_HOST);
|
||||
credentialUrl.setPath(path.str());
|
||||
|
||||
auto client = std::make_shared<CurlHttpClient>();
|
||||
|
||||
HttpRequest request;
|
||||
request.setUrl(credentialUrl);
|
||||
auto outcome = client->makeRequest(request);
|
||||
|
||||
if (outcome.isSuccess())
|
||||
return std::string(outcome.result().body(), outcome.result().bodySize());
|
||||
else
|
||||
return outcome.error().errorCode();
|
||||
}
|
||||
39
core/src/EcsMetadataFetcher.h
Normal file
39
core/src/EcsMetadataFetcher.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ALIBABACLOUD_CORE_ECSMETADATASERVICECREDENTIALSFETCHER_H_
|
||||
#define ALIBABACLOUD_CORE_ECSMETADATASERVICECREDENTIALSFETCHER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace AlibabaCloud
|
||||
{
|
||||
class EcsMetadataFetcher
|
||||
{
|
||||
public:
|
||||
EcsMetadataFetcher();
|
||||
~EcsMetadataFetcher();
|
||||
|
||||
std::string roleName()const;
|
||||
void setRoleName(const std::string & roleName);
|
||||
|
||||
std::string getMetadata();
|
||||
private:
|
||||
std::string roleName_;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // !ALIBABACLOUD_CORE_ECSMETADATASERVICECREDENTIALSFETCHER_H_
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <alibabacloud/core/InstanceProfileConfigLoader.h>
|
||||
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
InstanceProfileConfigLoader::InstanceProfileConfigLoader()
|
||||
{
|
||||
}
|
||||
|
||||
InstanceProfileConfigLoader::~InstanceProfileConfigLoader()
|
||||
{
|
||||
}
|
||||
|
||||
std::map<std::string, Profile> InstanceProfileConfigLoader::allProfiles() const
|
||||
{
|
||||
return profiles_;
|
||||
}
|
||||
|
||||
std::chrono::system_clock::time_point InstanceProfileConfigLoader::lastLoadTime() const
|
||||
{
|
||||
return lastLoadTime_;
|
||||
}
|
||||
|
||||
bool InstanceProfileConfigLoader::persistInternal(const std::map<std::string, Profile>&)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool InstanceProfileConfigLoader::load()
|
||||
{
|
||||
if (loadInternal())
|
||||
{
|
||||
lastLoadTime_ = std::chrono::system_clock::now();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool InstanceProfileConfigLoader::persistProfiles(const std::map<std::string, Profile>& profiles)
|
||||
{
|
||||
if (persistInternal(profiles))
|
||||
{
|
||||
profiles_ = profiles;
|
||||
lastLoadTime_ = std::chrono::system_clock::now();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -15,64 +15,93 @@
|
||||
*/
|
||||
|
||||
#include <alibabacloud/core/InstanceProfileCredentialsProvider.h>
|
||||
#include <alibabacloud/core/EcsInstanceProfileConfigLoader.h>
|
||||
#include <alibabacloud/core/Profile.h>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <json/json.h>
|
||||
#include "EcsMetadataFetcher.h"
|
||||
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
namespace
|
||||
InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const std::string & roleName, int durationSeconds):
|
||||
CredentialsProvider(),
|
||||
durationSeconds_(durationSeconds),
|
||||
cachedMutex_(),
|
||||
cachedCredentials_("", ""),
|
||||
fetcher_(new EcsMetadataFetcher),
|
||||
expiry_()
|
||||
{
|
||||
const char* const INSTANCE_PROFILE_KEY = "InstanceProfile";
|
||||
fetcher_->setRoleName(roleName);
|
||||
}
|
||||
|
||||
InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(size_t refreshRateMs) :
|
||||
metadataConfigLoader_(std::make_shared<EcsInstanceProfileConfigLoader>()),
|
||||
loadFrequencyMs_(refreshRateMs),
|
||||
lastLoaded_()
|
||||
InstanceProfileCredentialsProvider::~InstanceProfileCredentialsProvider()
|
||||
{
|
||||
delete fetcher_;
|
||||
}
|
||||
|
||||
InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const std::shared_ptr<EcsInstanceProfileConfigLoader>& loader, size_t refreshRateMs) :
|
||||
metadataConfigLoader_(loader),
|
||||
loadFrequencyMs_(refreshRateMs),
|
||||
lastLoaded_()
|
||||
std::string InstanceProfileCredentialsProvider::roleName()const
|
||||
{
|
||||
return fetcher_->roleName();
|
||||
}
|
||||
|
||||
Credentials InstanceProfileCredentialsProvider::getCredentials()
|
||||
{
|
||||
refreshIfExpired();
|
||||
|
||||
auto profileIter = metadataConfigLoader_->allProfiles().find(INSTANCE_PROFILE_KEY);
|
||||
if (profileIter != metadataConfigLoader_->allProfiles().end())
|
||||
{
|
||||
return profileIter->second.credentials();
|
||||
}
|
||||
|
||||
return Credentials("","");
|
||||
loadCredentials();
|
||||
std::lock_guard<std::mutex> locker(cachedMutex_);
|
||||
return cachedCredentials_;
|
||||
}
|
||||
|
||||
bool InstanceProfileCredentialsProvider::isTimeToRefresh(long reloadFrequency)
|
||||
bool InstanceProfileCredentialsProvider::checkExpiry()const
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto diff = std::chrono::duration_cast<std::chrono::seconds>(now - lastLoaded_).count();
|
||||
|
||||
if(diff > reloadFrequency)
|
||||
{
|
||||
lastLoaded_ = now;
|
||||
return true;
|
||||
}
|
||||
auto diff = std::chrono::duration_cast<std::chrono::seconds>(now - expiry_).count();
|
||||
|
||||
return false;
|
||||
return (diff > 0 - 60);
|
||||
}
|
||||
|
||||
void InstanceProfileCredentialsProvider::refreshIfExpired()
|
||||
void InstanceProfileCredentialsProvider::loadCredentials()
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(m_reloadMutex);
|
||||
if (isTimeToRefresh(loadFrequencyMs_))
|
||||
metadataConfigLoader_->load();
|
||||
if (checkExpiry())
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(cachedMutex_);
|
||||
if (checkExpiry())
|
||||
{
|
||||
auto outcome = fetcher_->getMetadata();
|
||||
Json::Value value;
|
||||
Json::Reader reader;
|
||||
if (reader.parse(outcome, value))
|
||||
{
|
||||
if (value["Code"] == nullptr
|
||||
&&value["AccessKeyId"] == nullptr
|
||||
&&value["AccessKeySecret"] == nullptr
|
||||
&&value["SecurityToken"] == nullptr
|
||||
&&value["Expiration"] == nullptr)
|
||||
{
|
||||
cachedCredentials_ = Credentials("","");
|
||||
return;
|
||||
}
|
||||
|
||||
auto code = value["Code"].asString();
|
||||
auto accessKeyId = value["AccessKeyId"].asString();
|
||||
auto accessKeySecret = value["AccessKeySecret"].asString();
|
||||
auto securityToken = value["SecurityToken"].asString();
|
||||
auto expiration = value["Expiration"].asString();
|
||||
|
||||
cachedCredentials_ = Credentials(accessKeyId,
|
||||
accessKeySecret,
|
||||
securityToken);
|
||||
|
||||
std::tm tm = {};
|
||||
#if defined(__GNUG__) && __GNUC__ < 5
|
||||
strptime(expiration.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm);
|
||||
#else
|
||||
std::stringstream ss(expiration);
|
||||
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
|
||||
#endif
|
||||
expiry_ = std::chrono::system_clock::from_time_t(std::mktime(&tm));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <alibabacloud/core/Profile.h>
|
||||
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
std::string Profile::name() const
|
||||
{ return name_; }
|
||||
|
||||
void Profile::setName(const std::string& value)
|
||||
{ name_ = value; }
|
||||
|
||||
Credentials Profile::credentials() const
|
||||
{ return credentials_; }
|
||||
|
||||
void Profile::setCredentials(const Credentials& value)
|
||||
{ credentials_ = value; }
|
||||
|
||||
std::string Profile::region() const
|
||||
{ return region_; }
|
||||
|
||||
void Profile::setRegion(const std::string& value)
|
||||
{ region_ = value; }
|
||||
|
||||
std::string Profile::roleArn() const
|
||||
{ return roleArn_; }
|
||||
|
||||
void Profile::setRoleArn(const std::string& value)
|
||||
{ roleArn_ = value; }
|
||||
|
||||
std::string Profile::sourceProfile() const
|
||||
{ return sourceProfile_; }
|
||||
|
||||
void Profile::setSourceProfile(const std::string& value)
|
||||
{ sourceProfile_ = value; }
|
||||
|
||||
void Profile::setAllKeyValPairs(const std::map<std::string, std::string>& map)
|
||||
{
|
||||
allKeyValPairs_ = map;
|
||||
}
|
||||
|
||||
std::string Profile::value(const std::string& key)
|
||||
{
|
||||
auto iter = allKeyValPairs_.find(key);
|
||||
if (iter == allKeyValPairs_.end()) return "";
|
||||
return iter->second;
|
||||
}
|
||||
@@ -150,7 +150,8 @@ HttpRequest RoaServiceClient::buildHttpRequest(const std::string & endpoint, con
|
||||
request.setHeader("Host", url.host());
|
||||
request.setHeader("x-sdk-client", std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
|
||||
request.setHeader("x-acs-region-id", configuration().regionId());
|
||||
request.setHeader("x-acs-security-token", credentials.sessionToken());
|
||||
if(!credentials.sessionToken().empty())
|
||||
request.setHeader("x-acs-security-token", credentials.sessionToken());
|
||||
request.setHeader("x-acs-signature-method", signer_->name());
|
||||
request.setHeader("x-acs-signature-nonce", GenerateUuid());
|
||||
request.setHeader("x-acs-signature-version", signer_->version());
|
||||
|
||||
Reference in New Issue
Block a user