StsAssumeRoleCredentialsProvider.cc 2.6 KB

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