InstanceProvider.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types=1);
  3. namespace AsyncAws\Core\Credentials;
  4. use AsyncAws\Core\Configuration;
  5. use Psr\Log\LoggerInterface;
  6. use Psr\Log\NullLogger;
  7. use Symfony\Component\HttpClient\Exception\JsonException;
  8. use Symfony\Component\HttpClient\Exception\TransportException;
  9. use Symfony\Component\HttpClient\HttpClient;
  10. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  11. use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
  12. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  13. use Symfony\Contracts\HttpClient\HttpClientInterface;
  14. use Symfony\Contracts\HttpClient\ResponseInterface;
  15. /**
  16. * Provides Credentials from the running EC2 metadata server using the IMDSv1 and IMDSv2.
  17. *
  18. * @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
  19. *
  20. * @author Jérémy Derussé <jeremy@derusse.com>
  21. */
  22. final class InstanceProvider implements CredentialProvider
  23. {
  24. private const TOKEN_ENDPOINT = 'http://169.254.169.254/latest/api/token';
  25. private const METADATA_ENDPOINT = 'http://169.254.169.254/latest/meta-data/iam/security-credentials';
  26. private $logger;
  27. private $httpClient;
  28. private $timeout;
  29. private $tokenTtl;
  30. public function __construct(?HttpClientInterface $httpClient = null, ?LoggerInterface $logger = null, float $timeout = 1.0, int $tokenTtl = 21600)
  31. {
  32. $this->logger = $logger ?? new NullLogger();
  33. $this->httpClient = $httpClient ?? HttpClient::create();
  34. $this->timeout = $timeout;
  35. $this->tokenTtl = $tokenTtl;
  36. }
  37. public function getCredentials(Configuration $configuration): ?Credentials
  38. {
  39. $token = $this->getToken();
  40. $headers = [];
  41. if (null !== $token) {
  42. $headers = ['X-aws-ec2-metadata-token' => $token];
  43. }
  44. try {
  45. // Fetch current Profile
  46. $response = $this->httpClient->request('GET', self::METADATA_ENDPOINT, [
  47. 'timeout' => $this->timeout,
  48. 'headers' => $headers,
  49. ]);
  50. $profile = $response->getContent();
  51. // Fetch credentials from profile
  52. $response = $this->httpClient->request('GET', self::METADATA_ENDPOINT . '/' . $profile, [
  53. 'timeout' => $this->timeout,
  54. 'headers' => $headers,
  55. ]);
  56. $result = $this->toArray($response);
  57. if ('Success' !== $result['Code']) {
  58. $this->logger->info('Unexpected instance profile.', ['response_code' => $result['Code']]);
  59. return null;
  60. }
  61. } catch (DecodingExceptionInterface $e) {
  62. $this->logger->info('Failed to decode Credentials.', ['exception' => $e]);
  63. return null;
  64. } catch (TransportExceptionInterface|HttpExceptionInterface $e) {
  65. $this->logger->info('Failed to fetch Profile from Instance Metadata.', ['exception' => $e]);
  66. return null;
  67. }
  68. if (null !== $date = $response->getHeaders(false)['date'][0] ?? null) {
  69. $date = new \DateTimeImmutable($date);
  70. }
  71. return new Credentials(
  72. $result['AccessKeyId'],
  73. $result['SecretAccessKey'],
  74. $result['Token'],
  75. Credentials::adjustExpireDate(new \DateTimeImmutable($result['Expiration']), $date)
  76. );
  77. }
  78. /**
  79. * Copy of Symfony\Component\HttpClient\Response::toArray without assertion on Content-Type header.
  80. */
  81. private function toArray(ResponseInterface $response): array
  82. {
  83. if ('' === $content = $response->getContent(true)) {
  84. throw new TransportException('Response body is empty.');
  85. }
  86. try {
  87. $content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | (\PHP_VERSION_ID >= 70300 ? \JSON_THROW_ON_ERROR : 0));
  88. } catch (\JsonException $e) {
  89. /** @psalm-suppress all */
  90. throw new JsonException(sprintf('%s for "%s".', $e->getMessage(), $response->getInfo('url')), $e->getCode());
  91. }
  92. if (\PHP_VERSION_ID < 70300 && \JSON_ERROR_NONE !== json_last_error()) {
  93. /** @psalm-suppress InvalidArgument */
  94. throw new JsonException(sprintf('%s for "%s".', json_last_error_msg(), $response->getInfo('url')), json_last_error());
  95. }
  96. if (!\is_array($content)) {
  97. /** @psalm-suppress InvalidArgument */
  98. throw new JsonException(sprintf('JSON content was expected to decode to an array, %s returned for "%s".', \gettype($content), $response->getInfo('url')));
  99. }
  100. return $content;
  101. }
  102. private function getToken(): ?string
  103. {
  104. try {
  105. $response = $this->httpClient->request('PUT', self::TOKEN_ENDPOINT,
  106. [
  107. 'timeout' => $this->timeout,
  108. 'headers' => ['X-aws-ec2-metadata-token-ttl-seconds' => $this->tokenTtl],
  109. ]
  110. );
  111. return $response->getContent();
  112. } catch (TransportExceptionInterface|HttpExceptionInterface $e) {
  113. $this->logger->info('Failed to fetch metadata token for IMDSv2, fallback to IMDSv1.', ['exception' => $e]);
  114. return null;
  115. }
  116. }
  117. }