From 31a064d1568f59e0731485a1b0452cfd5d767e42 Mon Sep 17 00:00:00 2001 From: "haowei.yao" Date: Fri, 26 Jan 2018 15:00:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=B1=E4=B8=B0=E9=B2=81=E6=88=90=E5=8F=91?= =?UTF-8?q?=E8=B5=B7=E7=9A=84CORE=20SDK=E8=87=AA=E5=8A=A8=E5=8F=91?= =?UTF-8?q?=E5=B8=83,=20BUILD=5FID=3D147,=20=E7=89=88=E6=9C=AC=E5=8F=B7?= =?UTF-8?q?=EF=BC=9A1.0.9=20=E5=8F=91=E5=B8=83=E6=97=A5=E5=BF=97=EF=BC=9A?= =?UTF-8?q?=201,=20Update=20=20InstanceProfileCredentialsProvider=202,=20U?= =?UTF-8?q?pdate=20common=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VERSION | 2 +- core/CMakeLists.txt | 10 +- core/ChangeLog.txt | 4 + .../include/alibabacloud/core/CommonRequest.h | 14 ++- .../core/EcsInstanceProfileConfigLoader.h | 40 -------- .../core/InstanceProfileConfigLoader.h | 47 --------- .../core/InstanceProfileCredentialsProvider.h | 27 +++--- core/include/alibabacloud/core/Profile.h | 52 ---------- core/src/CommonClient.cc | 6 +- core/src/CommonRequest.cc | 12 +-- core/src/EcsInstanceProfileConfigLoader.cc | 41 -------- core/src/EcsMetadataClient.cc | 27 ------ core/src/EcsMetadataClient.h | 32 ------ core/src/EcsMetadataFetcher.cc | 69 +++++++++++++ core/src/EcsMetadataFetcher.h | 39 ++++++++ core/src/InstanceProfileConfigLoader.cc | 65 ------------- .../src/InstanceProfileCredentialsProvider.cc | 97 ++++++++++++------- core/src/Profile.cc | 61 ------------ core/src/RoaServiceClient.cc | 3 +- 19 files changed, 214 insertions(+), 434 deletions(-) delete mode 100644 core/include/alibabacloud/core/EcsInstanceProfileConfigLoader.h delete mode 100644 core/include/alibabacloud/core/InstanceProfileConfigLoader.h delete mode 100644 core/include/alibabacloud/core/Profile.h delete mode 100644 core/src/EcsInstanceProfileConfigLoader.cc delete mode 100644 core/src/EcsMetadataClient.cc delete mode 100644 core/src/EcsMetadataClient.h create mode 100644 core/src/EcsMetadataFetcher.cc create mode 100644 core/src/EcsMetadataFetcher.h delete mode 100644 core/src/InstanceProfileConfigLoader.cc delete mode 100644 core/src/Profile.cc diff --git a/VERSION b/VERSION index 337a6a8f1..e5a4a5e7d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.8 \ No newline at end of file +1.0.9 \ No newline at end of file diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index c9d992849..78e37890d 100755 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -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 diff --git a/core/ChangeLog.txt b/core/ChangeLog.txt index ae40f40a6..72706e6b9 100644 --- a/core/ChangeLog.txt +++ b/core/ChangeLog.txt @@ -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 diff --git a/core/include/alibabacloud/core/CommonRequest.h b/core/include/alibabacloud/core/CommonRequest.h index aeba8851e..92c679685 100644 --- a/core/include/alibabacloud/core/CommonRequest.h +++ b/core/include/alibabacloud/core/CommonRequest.h @@ -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_; diff --git a/core/include/alibabacloud/core/EcsInstanceProfileConfigLoader.h b/core/include/alibabacloud/core/EcsInstanceProfileConfigLoader.h deleted file mode 100644 index 6739901da..000000000 --- a/core/include/alibabacloud/core/EcsInstanceProfileConfigLoader.h +++ /dev/null @@ -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 -#include "InstanceProfileConfigLoader.h" - -namespace AlibabaCloud -{ - class EcsMetadataClient; - class EcsInstanceProfileConfigLoader : public InstanceProfileConfigLoader - { - public: - EcsInstanceProfileConfigLoader(); - EcsInstanceProfileConfigLoader(const std::shared_ptr& metadataClient); - ~EcsInstanceProfileConfigLoader(); - - protected: - virtual bool loadInternal() override; - - private: - std::shared_ptr metadataClient_; - }; -} -#endif // !ALIBABACLOUD_CORE_ECSINSTANCEPROFILECONFIGLOADER_H_ diff --git a/core/include/alibabacloud/core/InstanceProfileConfigLoader.h b/core/include/alibabacloud/core/InstanceProfileConfigLoader.h deleted file mode 100644 index 257cde546..000000000 --- a/core/include/alibabacloud/core/InstanceProfileConfigLoader.h +++ /dev/null @@ -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 -#include -#include -#include "Profile.h" - -namespace AlibabaCloud -{ - class InstanceProfileConfigLoader - { - public: - InstanceProfileConfigLoader(); - virtual ~InstanceProfileConfigLoader(); - - bool load(); - bool persistProfiles(const std::map& profiles); - - std::map allProfiles() const; - std::chrono::system_clock::time_point lastLoadTime() const; - - protected: - virtual bool loadInternal() = 0; - virtual bool persistInternal(const std::map&); - - std::map profiles_; - std::chrono::system_clock::time_point lastLoadTime_; - }; -} -#endif // !ALIBABACLOUD_CORE_INSTANCEPROFILECONFIGLOADER_H_ diff --git a/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h b/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h index ad6ae81ff..5757b161f 100644 --- a/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h +++ b/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h @@ -20,28 +20,31 @@ #include #include #include +#include #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&, 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 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 \ No newline at end of file diff --git a/core/include/alibabacloud/core/Profile.h b/core/include/alibabacloud/core/Profile.h deleted file mode 100644 index 2e59866a4..000000000 --- a/core/include/alibabacloud/core/Profile.h +++ /dev/null @@ -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 -#include -#include - -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& 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 allKeyValPairs_; - }; - -} -#endif // !ALIBABACLOUD_CORE_PROFILE_H_ diff --git a/core/src/CommonClient.cc b/core/src/CommonClient.cc index ad9d55747..920bae48a 100644 --- a/core/src/CommonClient.cc +++ b/core/src/CommonClient.cc @@ -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()); diff --git a/core/src/CommonRequest.cc b/core/src/CommonRequest.cc index 1e1c6bb97..f839933cf 100644 --- a/core/src/CommonRequest.cc +++ b/core/src/CommonRequest.cc @@ -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) diff --git a/core/src/EcsInstanceProfileConfigLoader.cc b/core/src/EcsInstanceProfileConfigLoader.cc deleted file mode 100644 index 895dfd7df..000000000 --- a/core/src/EcsInstanceProfileConfigLoader.cc +++ /dev/null @@ -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 -#include "EcsMetadataClient.h" - -using namespace AlibabaCloud; - -EcsInstanceProfileConfigLoader::EcsInstanceProfileConfigLoader() : - metadataClient_(std::make_shared()) -{ -} - -EcsInstanceProfileConfigLoader::EcsInstanceProfileConfigLoader(const std::shared_ptr& client) : - metadataClient_(client) -{ -} - -EcsInstanceProfileConfigLoader::~EcsInstanceProfileConfigLoader() -{ -} - -bool EcsInstanceProfileConfigLoader::loadInternal() -{ - //TODO(fenglc): load form remote server - - return false; -} \ No newline at end of file diff --git a/core/src/EcsMetadataClient.cc b/core/src/EcsMetadataClient.cc deleted file mode 100644 index 7a613362b..000000000 --- a/core/src/EcsMetadataClient.cc +++ /dev/null @@ -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() -{ -} \ No newline at end of file diff --git a/core/src/EcsMetadataClient.h b/core/src/EcsMetadataClient.h deleted file mode 100644 index 3abab443d..000000000 --- a/core/src/EcsMetadataClient.h +++ /dev/null @@ -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_ diff --git a/core/src/EcsMetadataFetcher.cc b/core/src/EcsMetadataFetcher.cc new file mode 100644 index 000000000..4605ec7a9 --- /dev/null +++ b/core/src/EcsMetadataFetcher.cc @@ -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 +#include +#include +#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(); + + 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(); +} \ No newline at end of file diff --git a/core/src/EcsMetadataFetcher.h b/core/src/EcsMetadataFetcher.h new file mode 100644 index 000000000..5f2f40b12 --- /dev/null +++ b/core/src/EcsMetadataFetcher.h @@ -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 + +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_ diff --git a/core/src/InstanceProfileConfigLoader.cc b/core/src/InstanceProfileConfigLoader.cc deleted file mode 100644 index 8eefeb158..000000000 --- a/core/src/InstanceProfileConfigLoader.cc +++ /dev/null @@ -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 - -using namespace AlibabaCloud; - -InstanceProfileConfigLoader::InstanceProfileConfigLoader() -{ -} - -InstanceProfileConfigLoader::~InstanceProfileConfigLoader() -{ -} - -std::map InstanceProfileConfigLoader::allProfiles() const -{ - return profiles_; -} - -std::chrono::system_clock::time_point InstanceProfileConfigLoader::lastLoadTime() const -{ - return lastLoadTime_; -} - -bool InstanceProfileConfigLoader::persistInternal(const std::map&) -{ - return false; -} - -bool InstanceProfileConfigLoader::load() -{ - if (loadInternal()) - { - lastLoadTime_ = std::chrono::system_clock::now(); - return true; - } - - return false; -} - -bool InstanceProfileConfigLoader::persistProfiles(const std::map& profiles) -{ - if (persistInternal(profiles)) - { - profiles_ = profiles; - lastLoadTime_ = std::chrono::system_clock::now(); - return true; - } - - return false; -} diff --git a/core/src/InstanceProfileCredentialsProvider.cc b/core/src/InstanceProfileCredentialsProvider.cc index e857c68a1..c104055bc 100644 --- a/core/src/InstanceProfileCredentialsProvider.cc +++ b/core/src/InstanceProfileCredentialsProvider.cc @@ -15,64 +15,93 @@ */ #include -#include -#include #include #include #include #include +#include +#include +#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()), - loadFrequencyMs_(refreshRateMs), - lastLoaded_() +InstanceProfileCredentialsProvider::~InstanceProfileCredentialsProvider() { + delete fetcher_; } -InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const std::shared_ptr& 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 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(now - lastLoaded_).count(); - - if(diff > reloadFrequency) - { - lastLoaded_ = now; - return true; - } + auto diff = std::chrono::duration_cast(now - expiry_).count(); - return false; + return (diff > 0 - 60); } -void InstanceProfileCredentialsProvider::refreshIfExpired() +void InstanceProfileCredentialsProvider::loadCredentials() { - std::lock_guard locker(m_reloadMutex); - if (isTimeToRefresh(loadFrequencyMs_)) - metadataConfigLoader_->load(); + if (checkExpiry()) + { + std::lock_guard 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)); + } + } + } } diff --git a/core/src/Profile.cc b/core/src/Profile.cc deleted file mode 100644 index 8b8aaeb5a..000000000 --- a/core/src/Profile.cc +++ /dev/null @@ -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 - -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& map) -{ - allKeyValPairs_ = map; -} - -std::string Profile::value(const std::string& key) -{ - auto iter = allKeyValPairs_.find(key); - if (iter == allKeyValPairs_.end()) return ""; - return iter->second; -} \ No newline at end of file diff --git a/core/src/RoaServiceClient.cc b/core/src/RoaServiceClient.cc index 9f6d03197..fc6e5903a 100644 --- a/core/src/RoaServiceClient.cc +++ b/core/src/RoaServiceClient.cc @@ -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());