| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
- namespace AlibabaCloud\Credentials\Providers;
- use AlibabaCloud\Credentials\Utils\Helper;
- use AlibabaCloud\Credentials\Utils\Filter;
- use AlibabaCloud\Credentials\Request\Request;
- use GuzzleHttp\Psr7\Uri;
- use GuzzleHttp\Exception\GuzzleException;
- use InvalidArgumentException;
- use RuntimeException;
- /**
- * @internal This class is intended for internal use within the package.
- * Class RamRoleArnCredentialsProvider
- *
- * @package AlibabaCloud\Credentials\Providers
- */
- class RamRoleArnCredentialsProvider extends SessionCredentialsProvider
- {
- /**
- * @var CredentialsProvider
- */
- private $credentialsProvider;
- /**
- * @var string
- */
- private $roleArn;
- /**
- * @var string
- */
- private $roleSessionName;
- /**
- * @description role session expiration
- * @example 3600
- * @var int
- */
- private $durationSeconds = 3600;
- /**
- * @var string
- */
- private $externalId;
- /**
- * @var string
- */
- private $policy;
- /**
- * @var string
- */
- private $stsEndpoint;
- /**
- * @var int
- */
- private $connectTimeout = 5;
- /**
- * @var int
- */
- private $readTimeout = 5;
- /**
- * RamRoleArnCredentialsProvider constructor.
- *
- * @param array $params
- * @param array $options
- */
- public function __construct(array $params = [], array $options = [])
- {
- $this->filterOptions($options);
- $this->filterCredentials($params);
- $this->filterRoleArn($params);
- $this->filterRoleSessionName($params);
- $this->filterDurationSeconds($params);
- $this->filterPolicy($params);
- $this->filterExternalId($params);
- $this->filterSTSEndpoint($params);
- }
- private function filterRoleArn(array $params)
- {
- if (Helper::envNotEmpty('ALIBABA_CLOUD_ROLE_ARN')) {
- $this->roleArn = Helper::env('ALIBABA_CLOUD_ROLE_ARN');
- }
- if (isset($params['roleArn'])) {
- $this->roleArn = $params['roleArn'];
- }
- Filter::roleArn($this->roleArn);
- }
- private function filterRoleSessionName(array $params)
- {
- if (Helper::envNotEmpty('ALIBABA_CLOUD_ROLE_SESSION_NAME')) {
- $this->roleSessionName = Helper::env('ALIBABA_CLOUD_ROLE_SESSION_NAME');
- }
- if (isset($params['roleSessionName'])) {
- $this->roleSessionName = $params['roleSessionName'];
- }
- if (is_null($this->roleSessionName) || $this->roleSessionName === '') {
- $this->roleSessionName = 'phpSdkRoleSessionName';
- }
- }
- private function filterDurationSeconds(array $params)
- {
- if (isset($params['durationSeconds'])) {
- if (is_int($params['durationSeconds'])) {
- $this->durationSeconds = $params['durationSeconds'];
- }
- }
- if ($this->durationSeconds < 900) {
- throw new InvalidArgumentException('Role session expiration should be in the range of 900s - max session duration');
- }
- }
- private function filterPolicy(array $params)
- {
- if (isset($params['policy'])) {
- if (is_string($params['policy'])) {
- $this->policy = $params['policy'];
- }
- if (is_array($params['policy'])) {
- $this->policy = json_encode($params['policy']);
- }
- }
- }
- private function filterExternalId(array $params)
- {
- if (isset($params['externalId'])) {
- if (is_string($params['externalId'])) {
- $this->externalId = $params['externalId'];
- }
- }
- }
- private function filterSTSEndpoint(array $params)
- {
- if (Helper::envNotEmpty('ALIBABA_CLOUD_STS_REGION')) {
- $this->stsEndpoint = 'sts.' . Helper::env('ALIBABA_CLOUD_STS_REGION') . '.aliyuncs.com';
- }
- if (isset($params['stsRegionId'])) {
- $this->stsEndpoint = 'sts.' . $params['stsRegionId'] . '.aliyuncs.com';
- }
- if (isset($params['stsEndpoint'])) {
- $this->stsEndpoint = $params['stsEndpoint'];
- }
- if (is_null($this->stsEndpoint) || $this->stsEndpoint === '') {
- $this->stsEndpoint = 'sts.aliyuncs.com';
- }
- }
- private function filterCredentials(array $params)
- {
- if (isset($params['credentialsProvider'])) {
- if (!($params['credentialsProvider'] instanceof CredentialsProvider)) {
- throw new InvalidArgumentException('Invalid credentialsProvider option for ram_role_arn');
- }
- $this->credentialsProvider = $params['credentialsProvider'];
- } else if (isset($params['accessKeyId']) && isset($params['accessKeySecret']) && isset($params['securityToken'])) {
- Filter::accessKey($params['accessKeyId'], $params['accessKeySecret']);
- Filter::securityToken($params['securityToken']);
- $this->credentialsProvider = new StaticSTSCredentialsProvider($params);
- } else if (isset($params['accessKeyId']) && isset($params['accessKeySecret'])) {
- Filter::accessKey($params['accessKeyId'], $params['accessKeySecret']);
- $this->credentialsProvider = new StaticAKCredentialsProvider($params);
- } else {
- throw new InvalidArgumentException('Missing required credentials option for ram_role_arn');
- }
- }
- private function filterOptions(array $options)
- {
- if (isset($options['connectTimeout'])) {
- $this->connectTimeout = $options['connectTimeout'];
- }
- if (isset($options['readTimeout'])) {
- $this->readTimeout = $options['readTimeout'];
- }
- Filter::timeout($this->connectTimeout, $this->readTimeout);
- }
- /**
- * Get credentials by request.
- *
- * @return array
- * @throws RuntimeException
- * @throws GuzzleException
- */
- public function refreshCredentials()
- {
- $options = Request::commonOptions();
- $options['read_timeout'] = $this->readTimeout;
- $options['connect_timeout'] = $this->connectTimeout;
- $options['query']['Action'] = 'AssumeRole';
- $options['query']['Version'] = '2015-04-01';
- $options['query']['Format'] = 'JSON';
- $options['query']['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
- $options['query']['SignatureMethod'] = 'HMAC-SHA1';
- $options['query']['SignatureVersion'] = '1.0';
- $options['query']['SignatureNonce'] = Request::uuid(json_encode($options['query']));
- $options['query']['RoleArn'] = $this->roleArn;
- $options['query']['RoleSessionName'] = $this->roleSessionName;
- $options['query']['DurationSeconds'] = (string) $this->durationSeconds;
- if (!is_null($this->policy) && $this->policy !== '') {
- $options['query']['Policy'] = $this->policy;
- }
- if (!is_null($this->externalId) && $this->externalId !== '') {
- $options['query']['ExternalId'] = $this->externalId;
- }
- $sessionCredentials = $this->credentialsProvider->getCredentials();
- $options['query']['AccessKeyId'] = $sessionCredentials->getAccessKeyId();
- if (!is_null($sessionCredentials->getSecurityToken())) {
- $options['query']['SecurityToken'] = $sessionCredentials->getSecurityToken();
- }
- $options['query']['Signature'] = Request::shaHmac1sign(
- Request::signString('GET', $options['query']),
- $sessionCredentials->getAccessKeySecret() . '&'
- );
- $url = (new Uri())->withScheme('https')->withHost($this->stsEndpoint);
- $result = Request::createClient()->request('GET', $url, $options);
- if ($result->getStatusCode() !== 200) {
- throw new RuntimeException('Error refreshing credentials from RamRoleArn, statusCode: ' . $result->getStatusCode() . ', result: ' . (string) $result);
- }
- $json = $result->toArray();
- $credentials = $json['Credentials'];
- if (!isset($credentials['AccessKeyId']) || !isset($credentials['AccessKeySecret']) || !isset($credentials['SecurityToken'])) {
- throw new RuntimeException('Error retrieving credentials from RamRoleArn result:' . $result->toJson());
- }
- return $credentials;
- }
- public function key()
- {
- $credentials = $this->credentialsProvider->getCredentials();
- return 'ram_role_arn#credential#' . $credentials->getAccessKeyId() . '#roleArn#' . $this->roleArn . '#roleSessionName#' . $this->roleSessionName;
- }
- public function getProviderName()
- {
- return 'ram_role_arn/' . $this->credentialsProvider->getProviderName();
- }
- /**
- * @return string
- */
- public function getRoleArn()
- {
- return $this->roleArn;
- }
- /**
- * @return string
- */
- public function getRoleSessionName()
- {
- return $this->roleSessionName;
- }
- /**
- * @return string
- */
- public function getPolicy()
- {
- return $this->policy;
- }
- /**
- * @deprecated
- * @return string
- */
- public function getOriginalAccessKeyId()
- {
- return $this->credentialsProvider->getCredentials()->getAccessKeyId();
- }
- /**
- * @deprecated
- * @return string
- */
- public function getOriginalAccessKeySecret()
- {
- return $this->credentialsProvider->getCredentials()->getAccessKeySecret();
- }
- }
|