InstanceProfileCredentialsProvider.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2009-2017 Alibaba Cloud All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <alibabacloud/core/InstanceProfileCredentialsProvider.h>
  17. #include <chrono>
  18. #include <iomanip>
  19. #include <chrono>
  20. #include <mutex>
  21. #include <sstream>
  22. #include <json/json.h>
  23. #include "EcsMetadataFetcher.h"
  24. using namespace AlibabaCloud;
  25. InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const std::string & roleName, int durationSeconds):
  26. CredentialsProvider(),
  27. durationSeconds_(durationSeconds),
  28. cachedMutex_(),
  29. cachedCredentials_("", ""),
  30. fetcher_(new EcsMetadataFetcher),
  31. expiry_()
  32. {
  33. fetcher_->setRoleName(roleName);
  34. }
  35. InstanceProfileCredentialsProvider::~InstanceProfileCredentialsProvider()
  36. {
  37. delete fetcher_;
  38. }
  39. std::string InstanceProfileCredentialsProvider::roleName()const
  40. {
  41. return fetcher_->roleName();
  42. }
  43. Credentials InstanceProfileCredentialsProvider::getCredentials()
  44. {
  45. loadCredentials();
  46. std::lock_guard<std::mutex> locker(cachedMutex_);
  47. return cachedCredentials_;
  48. }
  49. bool InstanceProfileCredentialsProvider::checkExpiry()const
  50. {
  51. auto now = std::chrono::system_clock::now();
  52. auto diff = std::chrono::duration_cast<std::chrono::seconds>(now - expiry_).count();
  53. return (diff > 0 - 60);
  54. }
  55. void InstanceProfileCredentialsProvider::loadCredentials()
  56. {
  57. if (checkExpiry())
  58. {
  59. std::lock_guard<std::mutex> locker(cachedMutex_);
  60. if (checkExpiry())
  61. {
  62. auto outcome = fetcher_->getMetadata();
  63. Json::Value value;
  64. Json::Reader reader;
  65. if (reader.parse(outcome, value))
  66. {
  67. if (value["Code"] == nullptr
  68. &&value["AccessKeyId"] == nullptr
  69. &&value["AccessKeySecret"] == nullptr
  70. &&value["SecurityToken"] == nullptr
  71. &&value["Expiration"] == nullptr)
  72. {
  73. cachedCredentials_ = Credentials("","");
  74. return;
  75. }
  76. auto code = value["Code"].asString();
  77. auto accessKeyId = value["AccessKeyId"].asString();
  78. auto accessKeySecret = value["AccessKeySecret"].asString();
  79. auto securityToken = value["SecurityToken"].asString();
  80. auto expiration = value["Expiration"].asString();
  81. cachedCredentials_ = Credentials(accessKeyId,
  82. accessKeySecret,
  83. securityToken);
  84. std::tm tm = {};
  85. #if defined(__GNUG__) && __GNUC__ < 5
  86. strptime(expiration.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm);
  87. #else
  88. std::stringstream ss(expiration);
  89. ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
  90. #endif
  91. expiry_ = std::chrono::system_clock::from_time_t(std::mktime(&tm));
  92. }
  93. }
  94. }
  95. }