EcsRamRoleService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. /**
  21. *
  22. */
  23. define('ECS_ROLE_EXPIRE_TIME', 3600);
  24. class EcsRamRoleService
  25. {
  26. /**
  27. * @var IClientProfile
  28. */
  29. private $clientProfile;
  30. /**
  31. * @var string|null
  32. */
  33. private $lastClearTime = null;
  34. /**
  35. * @var string|null
  36. */
  37. private $sessionCredential = null;
  38. /**
  39. * EcsRamRoleService constructor.
  40. *
  41. * @param $clientProfile
  42. */
  43. public function __construct($clientProfile)
  44. {
  45. $this->clientProfile = $clientProfile;
  46. }
  47. /**
  48. * @return Credential|string|null
  49. * @throws ClientException
  50. */
  51. public function getSessionCredential()
  52. {
  53. if ($this->lastClearTime != null && $this->sessionCredential != null) {
  54. $now = time();
  55. $elapsedTime = $now - $this->lastClearTime;
  56. if ($elapsedTime <= ECS_ROLE_EXPIRE_TIME * 0.8) {
  57. return $this->sessionCredential;
  58. }
  59. }
  60. $credential = $this->assumeRole();
  61. if ($credential == null) {
  62. return null;
  63. }
  64. $this->sessionCredential = $credential;
  65. $this->lastClearTime = time();
  66. return $credential;
  67. }
  68. /**
  69. * @return Credential|null
  70. * @throws ClientException
  71. */
  72. private function assumeRole()
  73. {
  74. $ecsRamRoleCredential = $this->clientProfile->getCredential();
  75. $requestUrl =
  76. 'http://100.100.100.200/latest/meta-data/ram/security-credentials/' . $ecsRamRoleCredential->getRoleName();
  77. $httpResponse = HttpHelper::curl($requestUrl, 'GET', null, null);
  78. if (!$httpResponse->isSuccess()) {
  79. return null;
  80. }
  81. $respObj = json_decode($httpResponse->getBody());
  82. $code = $respObj->Code;
  83. if ($code != 'Success') {
  84. return null;
  85. }
  86. $sessionAccessKeyId = $respObj->AccessKeyId;
  87. $sessionAccessKeySecret = $respObj->AccessKeySecret;
  88. $securityToken = $respObj->SecurityToken;
  89. return new Credential($sessionAccessKeyId, $sessionAccessKeySecret, $securityToken);
  90. }
  91. }