/src/Endpoint/Partition.php

https://gitlab.com/github-cloud-corp/aws-sdk-php · PHP · 180 lines · 128 code · 25 blank · 27 comment · 8 complexity · e1747f832425c65edf04cd8b9fc06c51 MD5 · raw file

  1. <?php
  2. namespace Aws\Endpoint;
  3. use InvalidArgumentException as Iae;
  4. /**
  5. * Default implementation of an AWS partition.
  6. */
  7. final class Partition implements PartitionInterface
  8. {
  9. private $data;
  10. /**
  11. * The partition constructor accepts the following options:
  12. *
  13. * - `partition`: (string, required) The partition name as specified in an
  14. * ARN (e.g., `aws`)
  15. * - `partitionName`: (string) The human readable name of the partition
  16. * (e.g., "AWS Standard")
  17. * - `dnsSuffix`: (string, required) The DNS suffix of the partition. This
  18. * value is used to determine how endpoints in the partition are resolved.
  19. * - `regionRegex`: (string) A PCRE regular expression that specifies the
  20. * pattern that region names in the endpoint adhere to.
  21. * - `regions`: (array, required) A map of the regions in the partition.
  22. * Each key is the region as present in a hostname (e.g., `us-east-1`),
  23. * and each value is a structure containing region information.
  24. * - `defaults`: (array) A map of default key value pairs to apply to each
  25. * endpoint of the partition. Any value in an `endpoint` definition will
  26. * supersede any values specified in `defaults`.
  27. * - `services`: (array, required) A map of service endpoint prefix name
  28. * (the value found in a hostname) to information about the service.
  29. *
  30. * @param array $definition
  31. *
  32. * @throws Iae if any required options are missing
  33. */
  34. public function __construct(array $definition)
  35. {
  36. foreach (['partition', 'regions', 'services', 'dnsSuffix'] as $key) {
  37. if (!isset($definition[$key])) {
  38. throw new Iae("Partition missing required $key field");
  39. }
  40. }
  41. $this->data = $definition;
  42. }
  43. public function getName()
  44. {
  45. return $this->data['partition'];
  46. }
  47. public function isRegionMatch($region, $service)
  48. {
  49. if (isset($this->data['regions'][$region])
  50. || isset($this->data['services'][$service]['endpoints'][$region])
  51. ) {
  52. return true;
  53. }
  54. if (isset($this->data['regionRegex'])) {
  55. return (bool) preg_match(
  56. "@{$this->data['regionRegex']}@",
  57. $region
  58. );
  59. }
  60. return false;
  61. }
  62. public function getAvailableEndpoints(
  63. $service,
  64. $allowNonRegionalEndpoints = false
  65. ) {
  66. if ($this->isServicePartitionGlobal($service)) {
  67. return [$this->getPartitionEndpoint($service)];
  68. }
  69. if (isset($this->data['services'][$service]['endpoints'])) {
  70. $serviceRegions = array_keys(
  71. $this->data['services'][$service]['endpoints']
  72. );
  73. return $allowNonRegionalEndpoints
  74. ? $serviceRegions
  75. : array_intersect($serviceRegions, array_keys(
  76. $this->data['regions']
  77. ));
  78. }
  79. return [];
  80. }
  81. public function __invoke(array $args = [])
  82. {
  83. $service = isset($args['service']) ? $args['service'] : '';
  84. $region = isset($args['region']) ? $args['region'] : '';
  85. $scheme = isset($args['scheme']) ? $args['scheme'] : 'https';
  86. $data = $this->getEndpointData($service, $region);
  87. return [
  88. 'endpoint' => "{$scheme}://" . $this->formatEndpoint(
  89. isset($data['hostname']) ? $data['hostname'] : '',
  90. $service,
  91. $region
  92. ),
  93. 'signatureVersion' => $this->getSignatureVersion($data),
  94. 'signingRegion' => isset($data['credentialScope']['region'])
  95. ? $data['credentialScope']['region']
  96. : $region,
  97. 'signingName' => isset($data['credentialScope']['service'])
  98. ? $data['credentialScope']['service']
  99. : $service,
  100. ];
  101. }
  102. private function getEndpointData($service, $region)
  103. {
  104. $resolved = $this->resolveRegion($service, $region);
  105. $data = isset($this->data['services'][$service]['endpoints'][$resolved])
  106. ? $this->data['services'][$service]['endpoints'][$resolved]
  107. : [];
  108. $data += isset($this->data['services'][$service]['defaults'])
  109. ? $this->data['services'][$service]['defaults']
  110. : [];
  111. $data += isset($this->data['defaults'])
  112. ? $this->data['defaults']
  113. : [];
  114. return $data;
  115. }
  116. private function getSignatureVersion(array $data)
  117. {
  118. static $supportedBySdk = [
  119. 's3v4',
  120. 'v4',
  121. 'anonymous',
  122. ];
  123. $possibilities = array_intersect(
  124. $supportedBySdk,
  125. isset($data['signatureVersions'])
  126. ? $data['signatureVersions']
  127. : ['v4']
  128. );
  129. return array_shift($possibilities);
  130. }
  131. private function resolveRegion($service, $region)
  132. {
  133. if ($this->isServicePartitionGlobal($service)) {
  134. return $this->getPartitionEndpoint($service);
  135. }
  136. return $region;
  137. }
  138. private function isServicePartitionGlobal($service)
  139. {
  140. return isset($this->data['services'][$service]['isRegionalized'])
  141. && false === $this->data['services'][$service]['isRegionalized'];
  142. }
  143. private function getPartitionEndpoint($service)
  144. {
  145. return $this->data['services'][$service]['partitionEndpoint'];
  146. }
  147. private function formatEndpoint($template, $service, $region)
  148. {
  149. return strtr($template, [
  150. '{service}' => $service,
  151. '{region}' => $region,
  152. '{dnsSuffix}' => $this->data['dnsSuffix'],
  153. ]);
  154. }
  155. }