RamRoleArnCredential.php 5.5 KB

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