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