Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace Aws\EndpointV2;
4
 
5
use Aws\Api\Serializer\RestSerializer;
6
use GuzzleHttp\Psr7\Uri;
7
 
8
/**
9
 * Set of helper functions used to set endpoints and endpoint
10
 * properties derived from dynamic endpoint resolution.
11
 *
12
 * @internal
13
 */
14
trait EndpointV2SerializerTrait
15
{
16
    /**
17
     * Merges endpoint resolution arguments passed from the client
18
     * and command and attempts to resolve an endpoint. Headers and
19
     * auth schemes may be returned in a resolved endpoint object.
20
     * A resolved endpoint uri and headers will be applied to the request.
21
     * Auth schemes are applied to the command and compared against the default
22
     * auth scheme at signing.
23
     *
24
     * @internal
25
     */
26
    private function setRequestOptions(
27
        $endpointProvider,
28
        $command,
29
        $operation,
30
        $commandArgs,
31
        $clientArgs,
32
        &$headers
33
    )
34
    {
35
        $providerArgs = $this->resolveProviderArgs(
36
            $endpointProvider,
37
            $operation,
38
            $commandArgs,
39
            $clientArgs
40
        );
41
        $endpoint = $endpointProvider->resolveEndpoint($providerArgs);
42
        $resolvedUrl = $endpoint->getUrl();
43
 
44
        $this->applyScheme($resolvedUrl);
45
        $this->endpoint = $resolvedUrl;
46
        $this->applyAuthSchemeToCommand($endpoint, $command);
47
        $this->applyHeaders($endpoint, $headers);
48
    }
49
 
50
    private function resolveProviderArgs(
51
        $endpointProvider,
52
        $operation,
53
        $commandArgs,
54
        $clientArgs
55
    )
56
    {
57
        $rulesetParams = $endpointProvider->getRuleset()->getParameters();
58
        $endpointCommandArgs = $this->filterEndpointCommandArgs(
59
            $rulesetParams,
60
            $commandArgs
61
        );
62
        $staticContextParams = $this->bindStaticContextParams(
63
            $operation->getStaticContextParams()
64
        );
65
        $contextParams = $this->bindContextParams(
66
            $commandArgs, $operation->getContextParams()
67
        );
68
        $providerArgs = $this->normalizeEndpointProviderArgs(
69
            $endpointCommandArgs,
70
            $clientArgs,
71
            $contextParams,
72
            $staticContextParams
73
        );
74
 
75
        return $providerArgs;
76
    }
77
 
78
    /**
79
     * Merges endpoint provider arguments from different sources.
80
     * Client built-ins are superseded by client context params.
81
     * Client context params are superseded by context params on
82
     * an input member's shape.  Context params are superseded by
83
     * static context params. The result of this combination is then
84
     * merged with any appropriate arguments from the command.
85
     */
86
    private function normalizeEndpointProviderArgs(
87
        $endpointCommandArgs,
88
        $clientArgs,
89
        $contextParams,
90
        $staticContextParams
91
    )
92
    {
93
        $commandContextParams = array_merge($contextParams, $staticContextParams);
94
        $combinedEndpointArgs = array_merge($clientArgs, $commandContextParams);
95
 
96
        return array_merge($combinedEndpointArgs, $endpointCommandArgs);
97
    }
98
 
99
    private function bindContextParams($commandArgs, $contextParams)
100
    {
101
        $scopedParams = [];
102
 
103
        foreach($contextParams as $name => $spec) {
104
            if (isset($commandArgs[$spec['shape']])) {
105
                $scopedParams[$name] = $commandArgs[$spec['shape']];
106
            }
107
        }
108
        return $scopedParams;
109
    }
110
 
111
    private function bindStaticContextParams($staticContextParams)
112
    {
113
        $scopedParams = [];
114
 
115
        forEach($staticContextParams as $paramName => $paramValue) {
116
            $scopedParams[$paramName] = $paramValue['value'];
117
        }
118
        return $scopedParams;
119
    }
120
 
121
    private function filterEndpointCommandArgs(
122
        $rulesetParams,
123
        $commandArgs
124
    )
125
    {
126
        $endpointMiddlewareOpts = [
127
            '@use_dual_stack_endpoint' => 'UseDualStack',
128
            '@use_accelerate_endpoint' => 'Accelerate',
129
            '@use_path_style_endpoint' => 'ForcePathStyle'
130
        ];
131
 
132
        $filteredArgs = [];
133
 
134
        foreach($rulesetParams as $name => $value) {
135
            if (isset($commandArgs[$name])) {
136
                if (!empty($value->getBuiltIn())) {
137
                    continue;
138
                }
139
                $filteredArgs[$name] = $commandArgs[$name];
140
            }
141
        }
142
 
143
        if ($this->api->getServiceName() === 's3') {
144
            foreach($endpointMiddlewareOpts as $optionName => $newValue) {
145
                if (isset($commandArgs[$optionName])) {
146
                    $filteredArgs[$newValue] = $commandArgs[$optionName];
147
                }
148
            }
149
        }
150
 
151
        return $filteredArgs;
152
    }
153
 
154
    private function applyHeaders($endpoint, &$headers)
155
    {
156
        if (!is_null($endpoint->getHeaders())) {
157
           $headers = array_merge(
158
               $headers,
159
               $endpoint->getHeaders()
160
           );
161
        }
162
    }
163
 
164
    private function applyAuthSchemeToCommand($endpoint, $command)
165
    {
166
        if (isset($endpoint->getProperties()['authSchemes'])) {
167
            $authScheme = $this->selectAuthScheme(
168
                $endpoint->getProperties()['authSchemes']
169
            );
170
            $command->setAuthSchemes($authScheme);
171
        }
172
    }
173
 
174
    private function selectAuthScheme($authSchemes)
175
    {
176
        $validAuthSchemes = ['sigv4', 'sigv4a', 'none', 'bearer'];
177
        $invalidAuthSchemes = [];
178
 
179
        foreach($authSchemes as $authScheme) {
180
            if (in_array($authScheme['name'], $validAuthSchemes)) {
181
                return $this->normalizeAuthScheme($authScheme);
182
            } else {
183
                $invalidAuthSchemes[] = "`{$authScheme['name']}`";
184
            }
185
        }
186
 
187
        $invalidAuthSchemesString = implode(', ', $invalidAuthSchemes);
188
        $validAuthSchemesString = '`' . implode('`, `', $validAuthSchemes) . '`';
189
        throw new \InvalidArgumentException(
190
            "This operation requests {$invalidAuthSchemesString}"
191
            . " auth schemes, but the client only supports {$validAuthSchemesString}."
192
        );
193
    }
194
 
195
    private function normalizeAuthScheme($authScheme)
196
    {
197
       /*
198
            sigv4a will contain a regionSet property. which is guaranteed to be `*`
199
            for now.  The SigV4 class handles this automatically for now. It seems
200
            complexity will be added here in the future.
201
       */
202
        $normalizedAuthScheme = [];
203
 
204
        if (isset($authScheme['disableDoubleEncoding'])
205
            && $authScheme['disableDoubleEncoding'] === true
206
            && $authScheme['name'] !== 'sigv4a'
207
        ) {
208
            $normalizedAuthScheme['version'] = 's3v4';
209
        } elseif ($authScheme['name'] === 'none') {
210
            $normalizedAuthScheme['version'] = 'anonymous';
211
        }
212
        else {
213
            $normalizedAuthScheme['version'] = str_replace(
214
                'sig', '', $authScheme['name']
215
            );
216
        }
217
 
218
        $normalizedAuthScheme['name'] = isset($authScheme['signingName']) ?
219
            $authScheme['signingName'] : null;
220
        $normalizedAuthScheme['region'] = isset($authScheme['signingRegion']) ?
221
            $authScheme['signingRegion'] : null;
222
        $normalizedAuthScheme['signingRegionSet'] = isset($authScheme['signingRegionSet']) ?
223
            $authScheme['signingRegionSet'] : null;
224
 
225
        return $normalizedAuthScheme;
226
    }
227
 
228
    private function applyScheme(&$resolvedUrl)
229
    {
230
        $resolvedEndpointScheme = parse_url($resolvedUrl, PHP_URL_SCHEME);
231
        $scheme = $this->endpoint instanceof Uri
232
            ? $this->endpoint->getScheme()
233
            : parse_url($this->endpoint, PHP_URL_SCHEME);
234
 
235
        if (!empty($scheme) && $scheme !== $resolvedEndpointScheme) {
236
            $resolvedUrl = str_replace(
237
                $resolvedEndpointScheme,
238
                $scheme,
239
                $resolvedUrl
240
            );
241
        }
242
    }
243
}