|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the tarantool/client package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Middleware; |
|
15
|
|
|
|
|
16
|
|
|
use Tarantool\Client\Exception\RequestDenied; |
|
17
|
|
|
use Tarantool\Client\Handler\Handler; |
|
18
|
|
|
use Tarantool\Client\Request\Request; |
|
19
|
|
|
use Tarantool\Client\RequestTypes; |
|
20
|
|
|
use Tarantool\Client\Response; |
|
21
|
|
|
|
|
22
|
|
|
final class FirewallMiddleware implements Middleware |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var array<int, true> */ |
|
25
|
|
|
private $allowed; |
|
26
|
|
|
|
|
27
|
|
|
/** @var array<int, true> */ |
|
28
|
|
|
private $denied; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $allowed |
|
32
|
|
|
* @param array $denied |
|
33
|
|
|
*/ |
|
34
|
45 |
|
private function __construct($allowed, $denied) |
|
35
|
|
|
{ |
|
36
|
45 |
|
$this->allowed = $allowed ? \array_fill_keys($allowed, true) : []; |
|
37
|
45 |
|
$this->denied = $denied ? \array_fill_keys($denied, true) : []; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
18 |
|
public static function allow(int $requestType, int ...$requestTypes) : self |
|
41
|
|
|
{ |
|
42
|
18 |
|
return new self([-1 => $requestType] + $requestTypes, []); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
21 |
|
public static function deny(int $requestType, int ...$requestTypes) : self |
|
46
|
|
|
{ |
|
47
|
21 |
|
return new self([], [-1 => $requestType] + $requestTypes); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
6 |
|
public static function allowReadOnly() : self |
|
51
|
|
|
{ |
|
52
|
6 |
|
$self = new self([], []); |
|
53
|
6 |
|
$self->allowed = [ |
|
54
|
|
|
RequestTypes::AUTHENTICATE => true, |
|
55
|
|
|
RequestTypes::PING => true, |
|
56
|
|
|
RequestTypes::SELECT => true, |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
6 |
|
return $self; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
6 |
|
public function andAllow(int $requestType, int ...$requestTypes) : self |
|
63
|
|
|
{ |
|
64
|
6 |
|
$new = clone $this; |
|
65
|
6 |
|
$new->allowed += $requestTypes |
|
66
|
|
|
? \array_fill_keys([-1 => $requestType] + $requestTypes, true) |
|
67
|
6 |
|
: [$requestType => true]; |
|
68
|
|
|
|
|
69
|
6 |
|
return $new; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
6 |
|
public function andAllowOnly(int $requestType, int ...$requestTypes) : self |
|
73
|
|
|
{ |
|
74
|
6 |
|
$new = clone $this; |
|
75
|
6 |
|
$new->allowed = $requestTypes |
|
76
|
|
|
? \array_fill_keys([-1 => $requestType] + $requestTypes, true) |
|
77
|
6 |
|
: [$requestType => true]; |
|
78
|
|
|
|
|
79
|
6 |
|
return $new; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
6 |
|
public function andDeny(int $requestType, int ...$requestTypes) : self |
|
83
|
|
|
{ |
|
84
|
6 |
|
$new = clone $this; |
|
85
|
6 |
|
$new->denied += $requestTypes |
|
86
|
|
|
? \array_fill_keys([-1 => $requestType] + $requestTypes, true) |
|
87
|
6 |
|
: [$requestType => true]; |
|
88
|
|
|
|
|
89
|
6 |
|
return $new; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
6 |
|
public function andDenyOnly(int $requestType, int ...$requestTypes) : self |
|
93
|
|
|
{ |
|
94
|
6 |
|
$new = clone $this; |
|
95
|
6 |
|
$new->denied = $requestTypes |
|
96
|
|
|
? \array_fill_keys([-1 => $requestType] + $requestTypes, true) |
|
97
|
6 |
|
: [$requestType => true]; |
|
98
|
|
|
|
|
99
|
6 |
|
return $new; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
51 |
|
public function process(Request $request, Handler $handler) : Response |
|
103
|
|
|
{ |
|
104
|
51 |
|
$requestType = $request->getType(); |
|
105
|
|
|
|
|
106
|
51 |
|
if (isset($this->denied[$requestType])) { |
|
107
|
15 |
|
throw RequestDenied::fromObject($request); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
36 |
|
if ([] !== $this->allowed && !isset($this->allowed[$requestType])) { |
|
111
|
12 |
|
throw RequestDenied::fromObject($request); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
24 |
|
return $handler->handle($request); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|