Credential.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace AlibabaCloud\Credentials;
  3. use AlibabaCloud\Credentials\Credential\Config;
  4. use AlibabaCloud\Credentials\Credential\CredentialModel;
  5. use AlibabaCloud\Credentials\Providers\DefaultCredentialsProvider;
  6. use AlibabaCloud\Credentials\Providers\EcsRamRoleCredentialsProvider;
  7. use AlibabaCloud\Credentials\Providers\OIDCRoleArnCredentialsProvider;
  8. use AlibabaCloud\Credentials\Providers\RamRoleArnCredentialsProvider;
  9. use AlibabaCloud\Credentials\Providers\RsaKeyPairCredentialsProvider;
  10. use AlibabaCloud\Credentials\Providers\StaticAKCredentialsProvider;
  11. use AlibabaCloud\Credentials\Providers\StaticSTSCredentialsProvider;
  12. use AlibabaCloud\Credentials\Providers\URLCredentialsProvider;
  13. use AlibabaCloud\Credentials\Utils\Helper;
  14. use GuzzleHttp\Exception\GuzzleException;
  15. use InvalidArgumentException;
  16. use RuntimeException;
  17. /**
  18. * Class Credential
  19. *
  20. * @package AlibabaCloud\Credentials
  21. *
  22. */
  23. class Credential
  24. {
  25. /**
  26. * Version of the Client
  27. */
  28. const VERSION = '1.1.5';
  29. /**
  30. * @var Config
  31. */
  32. protected $config;
  33. /**
  34. * @var CredentialsInterface
  35. */
  36. protected $credential;
  37. /**
  38. * Credential constructor.
  39. *
  40. * @param array|Config $config
  41. */
  42. public function __construct($config = [])
  43. {
  44. if (\is_array($config)) {
  45. if (empty($config)) {
  46. $this->config = null;
  47. } else {
  48. $this->config = new Config($this->parseConfig($config));
  49. }
  50. } else {
  51. $this->config = $config;
  52. }
  53. $this->credential = $this->getCredentials($this->config);
  54. }
  55. /**
  56. * @param array $config
  57. *
  58. * @return array
  59. */
  60. private function parseConfig($config)
  61. {
  62. $res = [];
  63. foreach (\array_change_key_case($config) as $key => $value) {
  64. $res[Helper::snakeToCamelCase($key)] = $value;
  65. }
  66. return $res;
  67. }
  68. /**
  69. * Credentials getter.
  70. *
  71. * @param Config $config
  72. * @return CredentialsInterface
  73. *
  74. */
  75. private function getCredentials($config)
  76. {
  77. if (is_null($config)) {
  78. return new CredentialsProviderWrap('default', new DefaultCredentialsProvider());
  79. }
  80. switch ($config->type) {
  81. case 'access_key':
  82. $provider = new StaticAKCredentialsProvider([
  83. 'accessKeyId' => $config->accessKeyId,
  84. 'accessKeySecret' => $config->accessKeySecret,
  85. ]);
  86. return new CredentialsProviderWrap('access_key', $provider);
  87. case 'sts':
  88. $provider = new StaticSTSCredentialsProvider([
  89. 'accessKeyId' => $config->accessKeyId,
  90. 'accessKeySecret' => $config->accessKeySecret,
  91. 'securityToken' => $config->securityToken,
  92. ]);
  93. return new CredentialsProviderWrap('sts', $provider);
  94. case 'bearer':
  95. return new BearerTokenCredential($config->bearerToken);
  96. case 'ram_role_arn':
  97. if (!is_null($config->securityToken) && $config->securityToken !== '') {
  98. $innerProvider = new StaticSTSCredentialsProvider([
  99. 'accessKeyId' => $config->accessKeyId,
  100. 'accessKeySecret' => $config->accessKeySecret,
  101. 'securityToken' => $config->securityToken,
  102. ]);
  103. } else {
  104. $innerProvider = new StaticAKCredentialsProvider([
  105. 'accessKeyId' => $config->accessKeyId,
  106. 'accessKeySecret' => $config->accessKeySecret,
  107. ]);
  108. }
  109. $provider = new RamRoleArnCredentialsProvider([
  110. 'credentialsProvider' => $innerProvider,
  111. 'roleArn' => $config->roleArn,
  112. 'roleSessionName' => $config->roleSessionName,
  113. 'policy' => $config->policy,
  114. 'durationSeconds' => $config->roleSessionExpiration,
  115. 'externalId' => $config->externalId,
  116. 'stsEndpoint' => $config->STSEndpoint,
  117. ], [
  118. 'connectTimeout' => $config->connectTimeout,
  119. 'readTimeout' => $config->readTimeout,
  120. ]);
  121. return new CredentialsProviderWrap('ram_role_arn', $provider);
  122. case 'rsa_key_pair':
  123. $provider = new RsaKeyPairCredentialsProvider([
  124. 'publicKeyId' => $config->publicKeyId,
  125. 'privateKeyFile' => $config->privateKeyFile,
  126. 'durationSeconds' => $config->roleSessionExpiration,
  127. 'stsEndpoint' => $config->STSEndpoint,
  128. ], [
  129. 'connectTimeout' => $config->connectTimeout,
  130. 'readTimeout' => $config->readTimeout,
  131. ]);
  132. return new CredentialsProviderWrap('rsa_key_pair', $provider);
  133. case 'ecs_ram_role':
  134. $provider = new EcsRamRoleCredentialsProvider([
  135. 'roleName' => $config->roleName,
  136. 'disableIMDSv1' => $config->disableIMDSv1,
  137. ], [
  138. 'connectTimeout' => $config->connectTimeout,
  139. 'readTimeout' => $config->readTimeout,
  140. ]);
  141. return new CredentialsProviderWrap('ecs_ram_role', $provider);
  142. case 'oidc_role_arn':
  143. $provider = new OIDCRoleArnCredentialsProvider([
  144. 'roleArn' => $config->roleArn,
  145. 'oidcProviderArn' => $config->oidcProviderArn,
  146. 'oidcTokenFilePath' => $config->oidcTokenFilePath,
  147. 'roleSessionName' => $config->roleSessionName,
  148. 'policy' => $config->policy,
  149. 'durationSeconds' => $config->roleSessionExpiration,
  150. 'stsEndpoint' => $config->STSEndpoint,
  151. ], [
  152. 'connectTimeout' => $config->connectTimeout,
  153. 'readTimeout' => $config->readTimeout,
  154. ]);
  155. return new CredentialsProviderWrap('oidc_role_arn', $provider);
  156. case "credentials_uri":
  157. $provider = new URLCredentialsProvider([
  158. 'credentialsURI' => $config->credentialsURI,
  159. ], [
  160. 'connectTimeout' => $config->connectTimeout,
  161. 'readTimeout' => $config->readTimeout,
  162. ]);
  163. return new CredentialsProviderWrap('credentials_uri', $provider);
  164. default:
  165. throw new InvalidArgumentException('Unsupported credential type option: ' . $config->type . ', support: access_key, sts, bearer, ecs_ram_role, ram_role_arn, rsa_key_pair, oidc_role_arn, credentials_uri');
  166. }
  167. }
  168. /**
  169. * @return CredentialModel
  170. * @throws RuntimeException
  171. * @throws GuzzleException
  172. */
  173. public function getCredential()
  174. {
  175. return $this->credential->getCredential();
  176. }
  177. /**
  178. * @return array
  179. */
  180. public function getConfig()
  181. {
  182. return $this->config->toMap();
  183. }
  184. /**
  185. * @deprecated use getCredential() instead
  186. *
  187. * @return string
  188. * @throws RuntimeException
  189. * @throws GuzzleException
  190. */
  191. public function getType()
  192. {
  193. return $this->credential->getCredential()->getType();
  194. }
  195. /**
  196. * @deprecated use getCredential() instead
  197. *
  198. * @return string
  199. * @throws RuntimeException
  200. * @throws GuzzleException
  201. */
  202. public function getAccessKeyId()
  203. {
  204. return $this->credential->getCredential()->getAccessKeyId();
  205. }
  206. /**
  207. * @deprecated use getCredential() instead
  208. *
  209. * @return string
  210. * @throws RuntimeException
  211. * @throws GuzzleException
  212. */
  213. public function getAccessKeySecret()
  214. {
  215. return $this->credential->getCredential()->getAccessKeySecret();
  216. }
  217. /**
  218. * @deprecated use getCredential() instead
  219. *
  220. * @return string
  221. * @throws RuntimeException
  222. * @throws GuzzleException
  223. */
  224. public function getSecurityToken()
  225. {
  226. return $this->credential->getCredential()->getSecurityToken();
  227. }
  228. /**
  229. * @deprecated use getCredential() instead
  230. *
  231. * @return string
  232. * @throws RuntimeException
  233. * @throws GuzzleException
  234. */
  235. public function getBearerToken()
  236. {
  237. return $this->credential->getCredential()->getBearerToken();
  238. }
  239. /**
  240. * @param string $name
  241. * @param array $arguments
  242. *
  243. * @return mixed
  244. */
  245. public function __call($name, $arguments)
  246. {
  247. return $this->credential->$name($arguments);
  248. }
  249. }