StsAssumeRoleCredentialsProvider.cc 2.8 KB

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