summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-sdk-php/src/EventBridge/EventBridgeEndpointMiddleware.php
blob: 88ca111eec1b773e0c80c0ac46003f7b04343e8a (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
<?php
namespace Aws\EventBridge;

use Aws\CommandInterface;
use Aws\Endpoint\EndpointProvider;
use Aws\Endpoint\PartitionEndpointProvider;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;

/**
 * Reroutes an eventbridge request to the proper endpoint
 * @internal
 */
class EventBridgeEndpointMiddleware
{
    private $nextHandler;
    private $region;
    private $config;
    private $endpointProvider;
    private $isCustomEndpoint;

    /**
     * Provide the URI scheme of the client sending requests.
     * @param EndpointProvider $endpointProvider
     * @return callable
     */
    public static function wrap($region, $config, $endpointProvider, $isCustomEndpoint)
    {
        return function (callable $handler) use (
            $region,
            $config,
            $endpointProvider,
            $isCustomEndpoint
        ) {
            return new self(
                $handler,
                $region,
                $config,
                $endpointProvider,
                $isCustomEndpoint
            );
        };
    }

    public function __construct(
        callable $nextHandler,
        $region,
        $config,
        $endpointProvider,
        $isCustomEndpoint
    ) {
        $this->nextHandler = $nextHandler;
        $this->region = $region;
        $this->config = $config;
        $this->endpointProvider = is_null($endpointProvider)
            ? PartitionEndpointProvider::defaultProvider()
            : $endpointProvider;
        $this->isCustomEndpoint = $isCustomEndpoint;
    }

    public function __invoke(CommandInterface $cmd, RequestInterface $req) {
        $sigV4aCommands = ['PutEvents'];
        if (in_array($cmd->getName(), $sigV4aCommands)) {
            if (isset($cmd['EndpointId'])) {
                $endpointID = $cmd['EndpointId'];
                $this->validateEndpointId($endpointID);
                if (!$this->isCustomEndpoint) {
                    $dnsSuffix = $this->endpointProvider
                        ->getPartition($this->region, 'eventbridge')
                        ->getDnsSuffix();
                    $newUri = "{$endpointID}.endpoint.events.{$dnsSuffix}";
                    $oldUri = $req->getUri();
                    $req = $req->withUri($oldUri->withHost($newUri));
                }
                $cmd['@context']['signature_version'] = 'v4a';
            }
        }
        $f = $this->nextHandler;
        return $f($cmd, $req);
    }

    protected static function isValidHostLabel($string)
    {
        if (empty($string) || strlen($string) > 63) {
            return false;
        }
        if ($value = preg_match("/^[a-zA-Z0-9-.]+$/", $string)) {
            return true;
        }
        return false;
    }

    /**
     * @param $endpointID
     * @param CommandInterface $cmd
     */
    private function validateEndpointId($endpointID)
    {
        if (empty($endpointID)) {
            throw new \InvalidArgumentException("EventId must be a non-empty string");
        }
        if (!self::isValidHostLabel($endpointID)) {
            throw new InvalidArgumentException("EventId must be a valid host");
        }
        if ($this->config['use_fips_endpoint']) {
            throw new InvalidArgumentException(
                "EventId is currently not compatible with FIPS pseudo regions"
            );
        }
        if ($this->config['dual_stack']) {
            throw new InvalidArgumentException(
                "EventId is currently not compatible with dualstack"
            );
        }
    }
}