RamRoleArnCredentialsProvider.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace AlibabaCloud\Credentials\Providers;
  3. use AlibabaCloud\Credentials\Utils\Helper;
  4. use AlibabaCloud\Credentials\Utils\Filter;
  5. use AlibabaCloud\Credentials\Request\Request;
  6. use GuzzleHttp\Psr7\Uri;
  7. use GuzzleHttp\Exception\GuzzleException;
  8. use InvalidArgumentException;
  9. use RuntimeException;
  10. /**
  11. * @internal This class is intended for internal use within the package.
  12. * Class RamRoleArnCredentialsProvider
  13. *
  14. * @package AlibabaCloud\Credentials\Providers
  15. */
  16. class RamRoleArnCredentialsProvider extends SessionCredentialsProvider
  17. {
  18. /**
  19. * @var CredentialsProvider
  20. */
  21. private $credentialsProvider;
  22. /**
  23. * @var string
  24. */
  25. private $roleArn;
  26. /**
  27. * @var string
  28. */
  29. private $roleSessionName;
  30. /**
  31. * @description role session expiration
  32. * @example 3600
  33. * @var int
  34. */
  35. private $durationSeconds = 3600;
  36. /**
  37. * @var string
  38. */
  39. private $externalId;
  40. /**
  41. * @var string
  42. */
  43. private $policy;
  44. /**
  45. * @var string
  46. */
  47. private $stsEndpoint;
  48. /**
  49. * @var int
  50. */
  51. private $connectTimeout = 5;
  52. /**
  53. * @var int
  54. */
  55. private $readTimeout = 5;
  56. /**
  57. * RamRoleArnCredentialsProvider constructor.
  58. *
  59. * @param array $params
  60. * @param array $options
  61. */
  62. public function __construct(array $params = [], array $options = [])
  63. {
  64. $this->filterOptions($options);
  65. $this->filterCredentials($params);
  66. $this->filterRoleArn($params);
  67. $this->filterRoleSessionName($params);
  68. $this->filterDurationSeconds($params);
  69. $this->filterPolicy($params);
  70. $this->filterExternalId($params);
  71. $this->filterSTSEndpoint($params);
  72. }
  73. private function filterRoleArn(array $params)
  74. {
  75. if (Helper::envNotEmpty('ALIBABA_CLOUD_ROLE_ARN')) {
  76. $this->roleArn = Helper::env('ALIBABA_CLOUD_ROLE_ARN');
  77. }
  78. if (isset($params['roleArn'])) {
  79. $this->roleArn = $params['roleArn'];
  80. }
  81. Filter::roleArn($this->roleArn);
  82. }
  83. private function filterRoleSessionName(array $params)
  84. {
  85. if (Helper::envNotEmpty('ALIBABA_CLOUD_ROLE_SESSION_NAME')) {
  86. $this->roleSessionName = Helper::env('ALIBABA_CLOUD_ROLE_SESSION_NAME');
  87. }
  88. if (isset($params['roleSessionName'])) {
  89. $this->roleSessionName = $params['roleSessionName'];
  90. }
  91. if (is_null($this->roleSessionName) || $this->roleSessionName === '') {
  92. $this->roleSessionName = 'phpSdkRoleSessionName';
  93. }
  94. }
  95. private function filterDurationSeconds(array $params)
  96. {
  97. if (isset($params['durationSeconds'])) {
  98. if (is_int($params['durationSeconds'])) {
  99. $this->durationSeconds = $params['durationSeconds'];
  100. }
  101. }
  102. if ($this->durationSeconds < 900) {
  103. throw new InvalidArgumentException('Role session expiration should be in the range of 900s - max session duration');
  104. }
  105. }
  106. private function filterPolicy(array $params)
  107. {
  108. if (isset($params['policy'])) {
  109. if (is_string($params['policy'])) {
  110. $this->policy = $params['policy'];
  111. }
  112. if (is_array($params['policy'])) {
  113. $this->policy = json_encode($params['policy']);
  114. }
  115. }
  116. }
  117. private function filterExternalId(array $params)
  118. {
  119. if (isset($params['externalId'])) {
  120. if (is_string($params['externalId'])) {
  121. $this->externalId = $params['externalId'];
  122. }
  123. }
  124. }
  125. private function filterSTSEndpoint(array $params)
  126. {
  127. if (Helper::envNotEmpty('ALIBABA_CLOUD_STS_REGION')) {
  128. $this->stsEndpoint = 'sts.' . Helper::env('ALIBABA_CLOUD_STS_REGION') . '.aliyuncs.com';
  129. }
  130. if (isset($params['stsRegionId'])) {
  131. $this->stsEndpoint = 'sts.' . $params['stsRegionId'] . '.aliyuncs.com';
  132. }
  133. if (isset($params['stsEndpoint'])) {
  134. $this->stsEndpoint = $params['stsEndpoint'];
  135. }
  136. if (is_null($this->stsEndpoint) || $this->stsEndpoint === '') {
  137. $this->stsEndpoint = 'sts.aliyuncs.com';
  138. }
  139. }
  140. private function filterCredentials(array $params)
  141. {
  142. if (isset($params['credentialsProvider'])) {
  143. if (!($params['credentialsProvider'] instanceof CredentialsProvider)) {
  144. throw new InvalidArgumentException('Invalid credentialsProvider option for ram_role_arn');
  145. }
  146. $this->credentialsProvider = $params['credentialsProvider'];
  147. } else if (isset($params['accessKeyId']) && isset($params['accessKeySecret']) && isset($params['securityToken'])) {
  148. Filter::accessKey($params['accessKeyId'], $params['accessKeySecret']);
  149. Filter::securityToken($params['securityToken']);
  150. $this->credentialsProvider = new StaticSTSCredentialsProvider($params);
  151. } else if (isset($params['accessKeyId']) && isset($params['accessKeySecret'])) {
  152. Filter::accessKey($params['accessKeyId'], $params['accessKeySecret']);
  153. $this->credentialsProvider = new StaticAKCredentialsProvider($params);
  154. } else {
  155. throw new InvalidArgumentException('Missing required credentials option for ram_role_arn');
  156. }
  157. }
  158. private function filterOptions(array $options)
  159. {
  160. if (isset($options['connectTimeout'])) {
  161. $this->connectTimeout = $options['connectTimeout'];
  162. }
  163. if (isset($options['readTimeout'])) {
  164. $this->readTimeout = $options['readTimeout'];
  165. }
  166. Filter::timeout($this->connectTimeout, $this->readTimeout);
  167. }
  168. /**
  169. * Get credentials by request.
  170. *
  171. * @return array
  172. * @throws RuntimeException
  173. * @throws GuzzleException
  174. */
  175. public function refreshCredentials()
  176. {
  177. $options = Request::commonOptions();
  178. $options['read_timeout'] = $this->readTimeout;
  179. $options['connect_timeout'] = $this->connectTimeout;
  180. $options['query']['Action'] = 'AssumeRole';
  181. $options['query']['Version'] = '2015-04-01';
  182. $options['query']['Format'] = 'JSON';
  183. $options['query']['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
  184. $options['query']['SignatureMethod'] = 'HMAC-SHA1';
  185. $options['query']['SignatureVersion'] = '1.0';
  186. $options['query']['SignatureNonce'] = Request::uuid(json_encode($options['query']));
  187. $options['query']['RoleArn'] = $this->roleArn;
  188. $options['query']['RoleSessionName'] = $this->roleSessionName;
  189. $options['query']['DurationSeconds'] = (string) $this->durationSeconds;
  190. if (!is_null($this->policy) && $this->policy !== '') {
  191. $options['query']['Policy'] = $this->policy;
  192. }
  193. if (!is_null($this->externalId) && $this->externalId !== '') {
  194. $options['query']['ExternalId'] = $this->externalId;
  195. }
  196. $sessionCredentials = $this->credentialsProvider->getCredentials();
  197. $options['query']['AccessKeyId'] = $sessionCredentials->getAccessKeyId();
  198. if (!is_null($sessionCredentials->getSecurityToken())) {
  199. $options['query']['SecurityToken'] = $sessionCredentials->getSecurityToken();
  200. }
  201. $options['query']['Signature'] = Request::shaHmac1sign(
  202. Request::signString('GET', $options['query']),
  203. $sessionCredentials->getAccessKeySecret() . '&'
  204. );
  205. $url = (new Uri())->withScheme('https')->withHost($this->stsEndpoint);
  206. $result = Request::createClient()->request('GET', $url, $options);
  207. if ($result->getStatusCode() !== 200) {
  208. throw new RuntimeException('Error refreshing credentials from RamRoleArn, statusCode: ' . $result->getStatusCode() . ', result: ' . (string) $result);
  209. }
  210. $json = $result->toArray();
  211. $credentials = $json['Credentials'];
  212. if (!isset($credentials['AccessKeyId']) || !isset($credentials['AccessKeySecret']) || !isset($credentials['SecurityToken'])) {
  213. throw new RuntimeException('Error retrieving credentials from RamRoleArn result:' . $result->toJson());
  214. }
  215. return $credentials;
  216. }
  217. public function key()
  218. {
  219. $credentials = $this->credentialsProvider->getCredentials();
  220. return 'ram_role_arn#credential#' . $credentials->getAccessKeyId() . '#roleArn#' . $this->roleArn . '#roleSessionName#' . $this->roleSessionName;
  221. }
  222. public function getProviderName()
  223. {
  224. return 'ram_role_arn/' . $this->credentialsProvider->getProviderName();
  225. }
  226. /**
  227. * @return string
  228. */
  229. public function getRoleArn()
  230. {
  231. return $this->roleArn;
  232. }
  233. /**
  234. * @return string
  235. */
  236. public function getRoleSessionName()
  237. {
  238. return $this->roleSessionName;
  239. }
  240. /**
  241. * @return string
  242. */
  243. public function getPolicy()
  244. {
  245. return $this->policy;
  246. }
  247. /**
  248. * @deprecated
  249. * @return string
  250. */
  251. public function getOriginalAccessKeyId()
  252. {
  253. return $this->credentialsProvider->getCredentials()->getAccessKeyId();
  254. }
  255. /**
  256. * @deprecated
  257. * @return string
  258. */
  259. public function getOriginalAccessKeySecret()
  260. {
  261. return $this->credentialsProvider->getCredentials()->getAccessKeySecret();
  262. }
  263. }