Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Partition.php « Endpoint « src « aws-sdk-php « aws - github.com/nextcloud/3rdparty.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5125485b3e81cc0511c5a56ea5115988cadd7bdb (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
namespace Aws\Endpoint;

use ArrayAccess;
use Aws\HasDataTrait;
use Aws\Sts\RegionalEndpoints\ConfigurationProvider;
use Aws\S3\RegionalEndpoint\ConfigurationProvider as S3ConfigurationProvider;
use InvalidArgumentException as Iae;

/**
 * Default implementation of an AWS partition.
 */
final class Partition implements ArrayAccess, PartitionInterface
{
    use HasDataTrait;

    private $stsLegacyGlobalRegions = [
        'ap-northeast-1',
        'ap-south-1',
        'ap-southeast-1',
        'ap-southeast-2',
        'aws-global',
        'ca-central-1',
        'eu-central-1',
        'eu-north-1',
        'eu-west-1',
        'eu-west-2',
        'eu-west-3',
        'sa-east-1',
        'us-east-1',
        'us-east-2',
        'us-west-1',
        'us-west-2',
    ];

    /**
     * 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'];
    }

    /**
     * @internal
     * @return mixed
     */
    public function getDnsSuffix()
    {
        return $this->data['dnsSuffix'];
    }

    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';
        $options = isset($args['options']) ? $args['options'] : [];
        $data = $this->getEndpointData($service, $region, $options);
        $variant = $this->getVariant($options, $data);
        if (isset($variant['hostname'])) {
            $template = $variant['hostname'];
        } else {
            $template = isset($data['hostname']) ? $data['hostname'] : '';
        }
        $dnsSuffix = isset($variant['dnsSuffix'])
            ? $variant['dnsSuffix']
            : $this->data['dnsSuffix'];
        return [
            'endpoint' => "{$scheme}://" . $this->formatEndpoint(
                    $template,
                    $service,
                    $region,
                    $dnsSuffix
                ),
            '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, $options)
    {
        $defaultRegion = $this->resolveRegion($service, $region, $options);
        $data = isset($this->data['services'][$service]['endpoints'][$defaultRegion])
            ? $this->data['services'][$service]['endpoints'][$defaultRegion]
            : [];
        $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, $options)
    {
        if (isset($this->data['services'][$service]['endpoints'][$region])
            && $this->isFipsEndpointUsed($region)
        ) {
            return $region;
        }

        if ($this->isServicePartitionGlobal($service)
            || $this->isStsLegacyEndpointUsed($service, $region, $options)
            || $this->isS3LegacyEndpointUsed($service, $region, $options)
        ) {
            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']);
    }

    /**
     * STS legacy endpoints used for valid regions unless option is explicitly
     * set to 'regional'
     *
     * @param string $service
     * @param string $region
     * @param array $options
     * @return bool
     */
    private function isStsLegacyEndpointUsed($service, $region, $options)
    {
        return $service === 'sts'
            && in_array($region, $this->stsLegacyGlobalRegions)
            && (empty($options['sts_regional_endpoints'])
                || ConfigurationProvider::unwrap(
                    $options['sts_regional_endpoints']
                )->getEndpointsType() !== 'regional'
            );
    }

    /**
     * S3 legacy us-east-1 endpoint used for valid regions unless option is explicitly
     * set to 'regional'
     *
     * @param string $service
     * @param string $region
     * @param array $options
     * @return bool
     */
    private function isS3LegacyEndpointUsed($service, $region, $options)
    {
        return $service === 's3'
            && $region === 'us-east-1'
            && (empty($options['s3_us_east_1_regional_endpoint'])
                || S3ConfigurationProvider::unwrap(
                    $options['s3_us_east_1_regional_endpoint']
                )->getEndpointsType() !== 'regional'
            );
    }

    private function getPartitionEndpoint($service)
    {
        return $this->data['services'][$service]['partitionEndpoint'];
    }

    private function formatEndpoint($template, $service, $region, $dnsSuffix)
    {
        return strtr($template, [
            '{service}' => $service,
            '{region}' => $region,
            '{dnsSuffix}' => $dnsSuffix,
        ]);
    }

    /**
     * @param $region
     * @return bool
     */
    private function isFipsEndpointUsed($region)
    {
        return strpos($region, "fips") !== false;
    }

    /**
     * @param array $options
     * @param array $data
     * @return array
     */
    private function getVariant(array $options, array $data)
    {
        $variantTags = [];
        if (isset($options['use_fips_endpoint'])) {
            if ($options['use_fips_endpoint']->isUseFipsEndpoint()) {
                $variantTags[] = 'fips';
            }
        }
        if (isset($options['use_dual_stack_endpoint'])) {
            if ($options['use_dual_stack_endpoint']->isUseDualStackEndpoint()) {
                $variantTags[] = 'dualstack';
            }
        }
        if (!empty($variantTags)) {
            if (isset($data['variants'])) {
                foreach ($data['variants'] as $variant) {
                    if (array_count_values($variant['tags']) == array_count_values($variantTags)) {
                        return $variant;
                    }
                }
            }
            if (isset($this->data['defaults']['variants'])) {
                foreach ($this->data['defaults']['variants'] as $variant) {
                    if (array_count_values($variant['tags']) == array_count_values($variantTags)) {
                        return $variant;
                    }
                }
            }
        }
    }
}