InstanceProfileCredentialsProvider.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 1999-2019 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 "EcsMetadataFetcher.h"
  17. #include <alibabacloud/core/Utils.h>
  18. #include <alibabacloud/core/InstanceProfileCredentialsProvider.h>
  19. #include <chrono>
  20. #include <iomanip>
  21. #include <json/json.h>
  22. #include <mutex>
  23. #include <sstream>
  24. namespace AlibabaCloud
  25. {
  26. InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(
  27. const std::string &roleName, int durationSeconds)
  28. : CredentialsProvider(), EcsMetadataFetcher(),
  29. durationSeconds_(durationSeconds), cachedMutex_(),
  30. cachedCredentials_("", ""), expiry_()
  31. {
  32. setRoleName(roleName);
  33. }
  34. InstanceProfileCredentialsProvider::~InstanceProfileCredentialsProvider() {}
  35. Credentials InstanceProfileCredentialsProvider::getCredentials()
  36. {
  37. loadCredentials();
  38. std::lock_guard<std::mutex> locker(cachedMutex_);
  39. return cachedCredentials_;
  40. }
  41. bool InstanceProfileCredentialsProvider::checkExpiry() const
  42. {
  43. auto now = std::chrono::system_clock::now();
  44. auto diff =
  45. std::chrono::duration_cast<std::chrono::seconds>(now - expiry_).count();
  46. return (diff > 0 - 60);
  47. }
  48. void InstanceProfileCredentialsProvider::loadCredentials()
  49. {
  50. if (checkExpiry())
  51. {
  52. std::lock_guard<std::mutex> locker(cachedMutex_);
  53. if (checkExpiry())
  54. {
  55. auto outcome = getMetadata();
  56. Json::Value value;
  57. Json::Reader reader;
  58. if (reader.parse(outcome, value))
  59. {
  60. if (value["Code"].empty() && value["AccessKeyId"].empty() && value["AccessKeySecret"].empty() && value["SecurityToken"].empty() && value["Expiration"].empty())
  61. {
  62. cachedCredentials_ = Credentials("", "");
  63. return;
  64. }
  65. auto code = value["Code"].asString();
  66. auto accessKeyId = value["AccessKeyId"].asString();
  67. auto accessKeySecret = value["AccessKeySecret"].asString();
  68. auto securityToken = value["SecurityToken"].asString();
  69. auto expiration = value["Expiration"].asString();
  70. cachedCredentials_ = Credentials(accessKeyId,
  71. accessKeySecret,
  72. securityToken);
  73. std::tm tm = {};
  74. #if defined(__GNUG__) && __GNUC__ < 5
  75. strptime(expiration.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm);
  76. #else
  77. std::stringstream ss(expiration);
  78. ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
  79. #endif
  80. expiry_ = std::chrono::system_clock::from_time_t(std::mktime(&tm));
  81. }
  82. }
  83. }
  84. }
  85. } // namespace AlibabaCloud