EcsRamRoleCredential.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace AlibabaCloud\Credentials;
  3. use AlibabaCloud\Credentials\Providers\EcsRamRoleProvider;
  4. use AlibabaCloud\Credentials\Request\Request;
  5. use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
  6. use Exception;
  7. use GuzzleHttp\Exception\GuzzleException;
  8. use InvalidArgumentException;
  9. use RuntimeException;
  10. /**
  11. * Use the RAM role of an ECS instance to complete the authentication.
  12. */
  13. class EcsRamRoleCredential implements CredentialsInterface
  14. {
  15. /**
  16. * @var string
  17. */
  18. private $roleName;
  19. /**
  20. * EcsRamRoleCredential constructor.
  21. *
  22. * @param $role_name
  23. */
  24. public function __construct($role_name = null)
  25. {
  26. Filter::roleName($role_name);
  27. $this->roleName = $role_name;
  28. }
  29. /**
  30. * @return string
  31. * @throws GuzzleException
  32. * @throws Exception
  33. */
  34. public function getRoleName()
  35. {
  36. if ($this->roleName !== null) {
  37. return $this->roleName;
  38. }
  39. $this->roleName = $this->getRoleNameFromMeta();
  40. return $this->roleName;
  41. }
  42. /**
  43. * @return string
  44. * @throws Exception
  45. */
  46. public function getRoleNameFromMeta()
  47. {
  48. $options = [
  49. 'http_errors' => false,
  50. 'timeout' => 1,
  51. 'connect_timeout' => 1,
  52. ];
  53. $result = Request::createClient()->request(
  54. 'GET',
  55. 'http://100.100.100.200/latest/meta-data/ram/security-credentials/',
  56. $options
  57. );
  58. if ($result->getStatusCode() === 404) {
  59. throw new InvalidArgumentException('The role name was not found in the instance');
  60. }
  61. if ($result->getStatusCode() !== 200) {
  62. throw new RuntimeException('Error retrieving credentials from result: ' . $result->getBody());
  63. }
  64. $role_name = (string)$result;
  65. if (!$role_name) {
  66. throw new RuntimeException('Error retrieving credentials from result is empty');
  67. }
  68. return $role_name;
  69. }
  70. /**
  71. * @return string
  72. */
  73. public function __toString()
  74. {
  75. return "roleName#$this->roleName";
  76. }
  77. /**
  78. * @return ShaHmac1Signature
  79. */
  80. public function getSignature()
  81. {
  82. return new ShaHmac1Signature();
  83. }
  84. /**
  85. * @return string
  86. * @throws Exception
  87. * @throws GuzzleException
  88. */
  89. public function getAccessKeyId()
  90. {
  91. return $this->getSessionCredential()->getAccessKeyId();
  92. }
  93. /**
  94. * @return StsCredential
  95. * @throws Exception
  96. * @throws GuzzleException
  97. */
  98. protected function getSessionCredential()
  99. {
  100. return (new EcsRamRoleProvider($this))->get();
  101. }
  102. /**
  103. * @return string
  104. * @throws Exception
  105. * @throws GuzzleException
  106. */
  107. public function getAccessKeySecret()
  108. {
  109. return $this->getSessionCredential()->getAccessKeySecret();
  110. }
  111. /**
  112. * @return string
  113. * @throws Exception
  114. * @throws GuzzleException
  115. */
  116. public function getSecurityToken()
  117. {
  118. return $this->getSessionCredential()->getSecurityToken();
  119. }
  120. /**
  121. * @return int
  122. * @throws Exception
  123. * @throws GuzzleException
  124. */
  125. public function getExpiration()
  126. {
  127. return $this->getSessionCredential()->getExpiration();
  128. }
  129. }