PluginProbe
Media Cloud Sync / 1.2.10
Media Cloud Sync v1.2.10
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / Credentials / EcsCredentialProvider.php

EcsCredentialProvider.php in Media Cloud Sync 1.2.10, at includes/sdk/s3/Aws/Credentials/EcsCredentialProvider.php

103 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\Credentials;
4
5 use Dudlewebs\WPMCS\s3\Aws\Exception\CredentialsException;
6 use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Request;
7 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface;
8 use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface;
9 /**
10 * Credential provider that fetches credentials with GET request.
11 * ECS environment variable is used in constructing request URI.
12 */
13 class EcsCredentialProvider
14 {
15 const SERVER_URI = 'http://169.254.170.2';
16 const ENV_URI = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI";
17 const ENV_FULL_URI = "AWS_CONTAINER_CREDENTIALS_FULL_URI";
18 const ENV_AUTH_TOKEN = "AWS_CONTAINER_AUTHORIZATION_TOKEN";
19 const ENV_TIMEOUT = 'AWS_METADATA_SERVICE_TIMEOUT';
20 /** @var callable */
21 private $client;
22 /** @var float|mixed */
23 private $timeout;
24 /**
25 * The constructor accepts following options:
26 * - timeout: (optional) Connection timeout, in seconds, default 1.0
27 * - client: An EcsClient to make request from
28 *
29 * @param array $config Configuration options
30 */
31 public function __construct(array $config = [])
32 {
33 $timeout = \getenv(self::ENV_TIMEOUT);
34 if (!$timeout) {
35 $timeout = isset($_SERVER[self::ENV_TIMEOUT]) ? $_SERVER[self::ENV_TIMEOUT] : (isset($config['timeout']) ? $config['timeout'] : 1.0);
36 }
37 $this->timeout = (float) $timeout;
38 $this->client = isset($config['client']) ? $config['client'] : \Dudlewebs\WPMCS\s3\Aws\default_http_handler();
39 }
40 /**
41 * Load ECS credentials
42 *
43 * @return PromiseInterface
44 */
45 public function __invoke()
46 {
47 $client = $this->client;
48 $request = new Request('GET', self::getEcsUri());
49 $headers = $this->setHeaderForAuthToken();
50 return $client($request, ['timeout' => $this->timeout, 'proxy' => '', 'headers' => $headers])->then(function (ResponseInterface $response) {
51 $result = $this->decodeResult((string) $response->getBody());
52 return new Credentials($result['AccessKeyId'], $result['SecretAccessKey'], $result['Token'], \strtotime($result['Expiration']));
53 })->otherwise(function ($reason) {
54 $reason = \is_array($reason) ? $reason['exception'] : $reason;
55 $msg = $reason->getMessage();
56 throw new CredentialsException("Error retrieving credential from ECS ({$msg})");
57 });
58 }
59 private function getEcsAuthToken()
60 {
61 return \getenv(self::ENV_AUTH_TOKEN);
62 }
63 public function setHeaderForAuthToken()
64 {
65 $authToken = self::getEcsAuthToken();
66 $headers = [];
67 if (!empty($authToken)) {
68 $headers = ['Authorization' => $authToken];
69 }
70 return $headers;
71 }
72 /**
73 * Fetch credential URI from ECS environment variable
74 *
75 * @return string Returns ECS URI
76 */
77 private function getEcsUri()
78 {
79 $credsUri = \getenv(self::ENV_URI);
80 if ($credsUri === \false) {
81 $credsUri = isset($_SERVER[self::ENV_URI]) ? $_SERVER[self::ENV_URI] : '';
82 }
83 if (empty($credsUri)) {
84 $credFullUri = \getenv(self::ENV_FULL_URI);
85 if ($credFullUri === \false) {
86 $credFullUri = isset($_SERVER[self::ENV_FULL_URI]) ? $_SERVER[self::ENV_FULL_URI] : '';
87 }
88 if (!empty($credFullUri)) {
89 return $credFullUri;
90 }
91 }
92 return self::SERVER_URI . $credsUri;
93 }
94 private function decodeResult($response)
95 {
96 $result = \json_decode($response, \true);
97 if (!isset($result['AccessKeyId'])) {
98 throw new CredentialsException('Unexpected ECS credential value');
99 }
100 return $result;
101 }
102 }
103