InstanceProfileCredentialsProvider.cc 2.9 KB

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