InstanceProfileCredentialsProvider.cc 2.9 KB

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