RamRoleArnCredential.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace AlibabaCloud\Credentials;
  3. use AlibabaCloud\Credentials\Providers\RamRoleArnProvider;
  4. use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
  5. use Exception;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. use InvalidArgumentException;
  8. /**
  9. * Use the AssumeRole of the RAM account to complete the authentication.
  10. */
  11. class RamRoleArnCredential implements CredentialsInterface
  12. {
  13. /**
  14. * @var string
  15. */
  16. private $accessKeyId;
  17. /**
  18. * @var string
  19. */
  20. private $accessKeySecret;
  21. /**
  22. * @var string
  23. */
  24. private $roleArn;
  25. /**
  26. * @var string
  27. */
  28. private $roleSessionName;
  29. /**
  30. * @var string
  31. */
  32. private $policy;
  33. /**
  34. * @var array
  35. */
  36. private $config;
  37. /**
  38. * RamRoleArnCredential constructor.
  39. *
  40. * @param array $credential
  41. * @param array $config
  42. */
  43. public function __construct(array $credential = [], array $config = [])
  44. {
  45. $this->filterParameters($credential);
  46. $this->filterPolicy($credential);
  47. Filter::accessKey($credential['access_key_id'], $credential['access_key_secret']);
  48. $this->config = $config;
  49. $this->accessKeyId = $credential['access_key_id'];
  50. $this->accessKeySecret = $credential['access_key_secret'];
  51. $this->roleArn = $credential['role_arn'];
  52. $this->roleSessionName = $credential['role_session_name'];
  53. }
  54. /**
  55. * @param array $credential
  56. */
  57. private function filterParameters(array $credential)
  58. {
  59. if (!isset($credential['access_key_id'])) {
  60. throw new InvalidArgumentException('Missing required access_key_id option in config for ram_role_arn');
  61. }
  62. if (!isset($credential['access_key_secret'])) {
  63. throw new InvalidArgumentException('Missing required access_key_secret option in config for ram_role_arn');
  64. }
  65. if (!isset($credential['role_arn'])) {
  66. throw new InvalidArgumentException('Missing required role_arn option in config for ram_role_arn');
  67. }
  68. if (!isset($credential['role_session_name'])) {
  69. throw new InvalidArgumentException('Missing required role_session_name option in config for ram_role_arn');
  70. }
  71. }
  72. /**
  73. * @param array $credential
  74. */
  75. private function filterPolicy(array $credential)
  76. {
  77. if (isset($credential['policy'])) {
  78. if (is_string($credential['policy'])) {
  79. $this->policy = $credential['policy'];
  80. }
  81. if (is_array($credential['policy'])) {
  82. $this->policy = json_encode($credential['policy']);
  83. }
  84. }
  85. }
  86. /**
  87. * @return array
  88. */
  89. public function getConfig()
  90. {
  91. return $this->config;
  92. }
  93. /**
  94. * @return string
  95. */
  96. public function getRoleArn()
  97. {
  98. return $this->roleArn;
  99. }
  100. /**
  101. * @return string
  102. */
  103. public function getRoleSessionName()
  104. {
  105. return $this->roleSessionName;
  106. }
  107. /**
  108. * @return string
  109. */
  110. public function getPolicy()
  111. {
  112. return $this->policy;
  113. }
  114. /**
  115. * @return string
  116. */
  117. public function __toString()
  118. {
  119. return "$this->accessKeyId#$this->accessKeySecret#$this->roleArn#$this->roleSessionName";
  120. }
  121. /**
  122. * @return ShaHmac1Signature
  123. */
  124. public function getSignature()
  125. {
  126. return new ShaHmac1Signature();
  127. }
  128. /**
  129. * @return string
  130. */
  131. public function getOriginalAccessKeyId()
  132. {
  133. return $this->accessKeyId;
  134. }
  135. /**
  136. * @return string
  137. */
  138. public function getOriginalAccessKeySecret()
  139. {
  140. return $this->accessKeySecret;
  141. }
  142. /**
  143. * @return string
  144. * @throws Exception
  145. * @throws GuzzleException
  146. */
  147. public function getAccessKeyId()
  148. {
  149. return $this->getSessionCredential()->getAccessKeyId();
  150. }
  151. /**
  152. * @return StsCredential
  153. * @throws Exception
  154. * @throws GuzzleException
  155. */
  156. protected function getSessionCredential()
  157. {
  158. return (new RamRoleArnProvider($this))->get();
  159. }
  160. /**
  161. * @return string
  162. * @throws Exception
  163. * @throws GuzzleException
  164. */
  165. public function getAccessKeySecret()
  166. {
  167. return $this->getSessionCredential()->getAccessKeySecret();
  168. }
  169. /**
  170. * @return string
  171. * @throws Exception
  172. * @throws GuzzleException
  173. */
  174. public function getSecurityToken()
  175. {
  176. return $this->getSessionCredential()->getSecurityToken();
  177. }
  178. /**
  179. * @return string
  180. * @throws Exception
  181. * @throws GuzzleException
  182. */
  183. public function getExpiration()
  184. {
  185. return $this->getSessionCredential()->getExpiration();
  186. }
  187. }