Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\EventBridge;
3
 
4
use Aws\CommandInterface;
5
use Aws\Endpoint\EndpointProvider;
6
use Aws\Endpoint\PartitionEndpointProvider;
7
use InvalidArgumentException;
8
use Psr\Http\Message\RequestInterface;
9
 
10
/**
11
 * Reroutes an eventbridge request to the proper endpoint
12
 * @internal
13
 */
14
class EventBridgeEndpointMiddleware
15
{
16
    private $nextHandler;
17
    private $region;
18
    private $config;
19
    private $endpointProvider;
20
    private $isCustomEndpoint;
21
 
22
    /**
23
     * Provide the URI scheme of the client sending requests.
24
     * @param EndpointProvider $endpointProvider
25
     * @return callable
26
     */
27
    public static function wrap($region, $config, $endpointProvider, $isCustomEndpoint)
28
    {
29
        return function (callable $handler) use (
30
            $region,
31
            $config,
32
            $endpointProvider,
33
            $isCustomEndpoint
34
        ) {
35
            return new self(
36
                $handler,
37
                $region,
38
                $config,
39
                $endpointProvider,
40
                $isCustomEndpoint
41
            );
42
        };
43
    }
44
 
45
    public function __construct(
46
        callable $nextHandler,
47
        $region,
48
        $config,
49
        $endpointProvider,
50
        $isCustomEndpoint
51
    ) {
52
        $this->nextHandler = $nextHandler;
53
        $this->region = $region;
54
        $this->config = $config;
55
        $this->endpointProvider = is_null($endpointProvider)
56
            ? PartitionEndpointProvider::defaultProvider()
57
            : $endpointProvider;
58
        $this->isCustomEndpoint = $isCustomEndpoint;
59
    }
60
 
61
    public function __invoke(CommandInterface $cmd, RequestInterface $req) {
62
        $sigV4aCommands = ['PutEvents'];
63
        if (in_array($cmd->getName(), $sigV4aCommands)) {
64
            if (isset($cmd['EndpointId'])) {
65
                $endpointID = $cmd['EndpointId'];
66
                $this->validateEndpointId($endpointID);
67
                if (!$this->isCustomEndpoint) {
68
                    $dnsSuffix = $this->endpointProvider
69
                        ->getPartition($this->region, 'eventbridge')
70
                        ->getDnsSuffix();
71
                    $newUri = "{$endpointID}.endpoint.events.{$dnsSuffix}";
72
                    $oldUri = $req->getUri();
73
                    $req = $req->withUri($oldUri->withHost($newUri));
74
                }
75
                $cmd['@context']['signature_version'] = 'v4a';
76
            }
77
        }
78
        $f = $this->nextHandler;
79
        return $f($cmd, $req);
80
    }
81
 
82
    protected static function isValidHostLabel($string)
83
    {
84
        if (empty($string) || strlen($string) > 63) {
85
            return false;
86
        }
87
        if ($value = preg_match("/^[a-zA-Z0-9-.]+$/", $string)) {
88
            return true;
89
        }
90
        return false;
91
    }
92
 
93
    /**
94
     * @param $endpointID
95
     * @param CommandInterface $cmd
96
     */
97
    private function validateEndpointId($endpointID)
98
    {
99
        if (empty($endpointID)) {
100
            throw new \InvalidArgumentException("EventId must be a non-empty string");
101
        }
102
        if (!self::isValidHostLabel($endpointID)) {
103
            throw new InvalidArgumentException("EventId must be a valid host");
104
        }
105
        if ($this->config['use_fips_endpoint']) {
106
            throw new InvalidArgumentException(
107
                "EventId is currently not compatible with FIPS pseudo regions"
108
            );
109
        }
110
        if ($this->config['dual_stack']) {
111
            throw new InvalidArgumentException(
112
                "EventId is currently not compatible with dualstack"
113
            );
114
        }
115
    }
116
}