EcsRamRoleCredential.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace AlibabaCloud\Credentials;
  3. use AlibabaCloud\Credentials\Providers\EcsRamRoleCredentialsProvider;
  4. use AlibabaCloud\Credentials\Credential\CredentialModel;
  5. use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
  6. use AlibabaCloud\Credentials\Request\Request;
  7. use AlibabaCloud\Credentials\Utils\Filter;
  8. use Exception;
  9. use GuzzleHttp\Exception\GuzzleException;
  10. use InvalidArgumentException;
  11. use RuntimeException;
  12. /**
  13. * @deprecated
  14. * Use the RAM role of an ECS instance to complete the authentication.
  15. */
  16. class EcsRamRoleCredential implements CredentialsInterface
  17. {
  18. /**
  19. * @var string
  20. */
  21. private $roleName;
  22. /**
  23. * @var boolean
  24. */
  25. private $disableIMDSv1;
  26. /**
  27. * @var int
  28. */
  29. private $metadataTokenDuration;
  30. /**
  31. * EcsRamRoleCredential constructor.
  32. *
  33. * @param $role_name
  34. */
  35. public function __construct($role_name = null, $disable_imdsv1 = false, $metadata_token_duration = 21600)
  36. {
  37. Filter::roleName($role_name);
  38. $this->roleName = $role_name;
  39. Filter::disableIMDSv1($disable_imdsv1);
  40. $this->disableIMDSv1 = $disable_imdsv1;
  41. $this->metadataTokenDuration = $metadata_token_duration;
  42. }
  43. /**
  44. * @return string
  45. * @throws GuzzleException
  46. * @throws Exception
  47. */
  48. public function getRoleName()
  49. {
  50. if ($this->roleName !== null) {
  51. return $this->roleName;
  52. }
  53. $this->roleName = $this->getRoleNameFromMeta();
  54. return $this->roleName;
  55. }
  56. /**
  57. * @return string
  58. * @throws Exception
  59. */
  60. public function getRoleNameFromMeta()
  61. {
  62. $options = [
  63. 'http_errors' => false,
  64. 'timeout' => 1,
  65. 'connect_timeout' => 1,
  66. ];
  67. $result = Request::createClient()->request(
  68. 'GET',
  69. 'http://100.100.100.200/latest/meta-data/ram/security-credentials/',
  70. $options
  71. );
  72. if ($result->getStatusCode() === 404) {
  73. throw new InvalidArgumentException('The role name was not found in the instance');
  74. }
  75. if ($result->getStatusCode() !== 200) {
  76. throw new RuntimeException('Error retrieving credentials from result: ' . $result->getBody());
  77. }
  78. $role_name = (string) $result;
  79. if (!$role_name) {
  80. throw new RuntimeException('Error retrieving credentials from result is empty');
  81. }
  82. return $role_name;
  83. }
  84. /**
  85. * @return string
  86. */
  87. public function __toString()
  88. {
  89. return "roleName#$this->roleName";
  90. }
  91. /**
  92. * @return ShaHmac1Signature
  93. */
  94. public function getSignature()
  95. {
  96. return new ShaHmac1Signature();
  97. }
  98. /**
  99. * @return string
  100. * @throws Exception
  101. * @throws GuzzleException
  102. */
  103. public function getAccessKeyId()
  104. {
  105. return $this->getSessionCredential()->getAccessKeyId();
  106. }
  107. /**
  108. * @return AlibabaCloud\Credentials\Providers\Credentials
  109. * @throws Exception
  110. * @throws GuzzleException
  111. */
  112. protected function getSessionCredential()
  113. {
  114. $params = [
  115. "roleName" => $this->roleName,
  116. 'disableIMDSv1' => $this->disableIMDSv1,
  117. 'metadataTokenDuration' => $this->metadataTokenDuration,
  118. ];
  119. return (new EcsRamRoleCredentialsProvider($params))->getCredentials();
  120. }
  121. /**
  122. * @return string
  123. * @throws Exception
  124. * @throws GuzzleException
  125. */
  126. public function getAccessKeySecret()
  127. {
  128. return $this->getSessionCredential()->getAccessKeySecret();
  129. }
  130. /**
  131. * @return string
  132. * @throws Exception
  133. * @throws GuzzleException
  134. */
  135. public function getSecurityToken()
  136. {
  137. return $this->getSessionCredential()->getSecurityToken();
  138. }
  139. /**
  140. * @return int
  141. * @throws Exception
  142. * @throws GuzzleException
  143. */
  144. public function getExpiration()
  145. {
  146. return $this->getSessionCredential()->getExpiration();
  147. }
  148. /**
  149. * @return bool
  150. */
  151. public function isDisableIMDSv1()
  152. {
  153. return $this->disableIMDSv1;
  154. }
  155. /**
  156. * @inheritDoc
  157. */
  158. public function getCredential()
  159. {
  160. $credentials = $this->getSessionCredential();
  161. return new CredentialModel([
  162. 'accessKeyId' => $credentials->getAccessKeyId(),
  163. 'accessKeySecret' => $credentials->getAccessKeySecret(),
  164. 'securityToken' => $credentials->getSecurityToken(),
  165. 'type' => 'ecs_ram_role',
  166. ]);
  167. }
  168. }