StsAssumeRoleCredentialsProvider.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/StsAssumeRoleCredentialsProvider.h>
  17. #include <iomanip>
  18. #include <sstream>
  19. namespace AlibabaCloud {
  20. StsAssumeRoleCredentialsProvider::StsAssumeRoleCredentialsProvider(
  21. const Credentials &credentials, const ClientConfiguration &configuration,
  22. const std::string &roleArn, const std::string &roleSessionName,
  23. const std::string &policy, int durationSeconds)
  24. : CredentialsProvider(), StsClient(credentials, configuration),
  25. roleArn_(roleArn), roleSessionName_(roleSessionName), policy_(policy),
  26. durationSeconds_(durationSeconds), cachedMutex_(),
  27. cachedCredentials_("", ""), expiry_() {}
  28. StsAssumeRoleCredentialsProvider::~StsAssumeRoleCredentialsProvider() {}
  29. Credentials StsAssumeRoleCredentialsProvider::getCredentials() {
  30. loadCredentials();
  31. std::lock_guard<std::mutex> locker(cachedMutex_);
  32. return cachedCredentials_;
  33. }
  34. bool StsAssumeRoleCredentialsProvider::checkExpiry() const {
  35. auto now = std::chrono::system_clock::now();
  36. auto diff =
  37. std::chrono::duration_cast<std::chrono::seconds>(now - expiry_).count();
  38. return (diff > 0 - 60);
  39. }
  40. void StsAssumeRoleCredentialsProvider::loadCredentials() {
  41. if (checkExpiry()) {
  42. std::lock_guard<std::mutex> locker(cachedMutex_);
  43. if (checkExpiry()) {
  44. Sts::Model::AssumeRoleRequest request;
  45. request.setRoleArn(roleArn_);
  46. request.setRoleSessionName(roleSessionName_);
  47. request.setPolicy(policy_);
  48. request.setDurationSeconds(durationSeconds_);
  49. auto assumeRoleOutcome = assumeRole(request);
  50. if (assumeRoleOutcome.isSuccess()) {
  51. const auto stsCredentials = assumeRoleOutcome.result().credentials();
  52. cachedCredentials_ = Credentials(stsCredentials.accessKeyId,
  53. stsCredentials.accessKeySecret,
  54. stsCredentials.securityToken);
  55. std::tm tm = {};
  56. #if defined(__GNUG__) && __GNUC__ < 5
  57. strptime(stsCredentials.expiration.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm);
  58. #else
  59. std::stringstream ss(stsCredentials.expiration);
  60. ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
  61. #endif
  62. expiry_ = std::chrono::system_clock::from_time_t(std::mktime(&tm));
  63. }
  64. }
  65. }
  66. }
  67. } // namespace AlibabaCloud