17
17
use Bolt \Bolt ;
18
18
use Bolt \connection \StreamSocket ;
19
19
use Exception ;
20
- use function explode ;
21
- use const FILTER_VALIDATE_IP ;
22
- use function filter_var ;
23
20
use Laudis \Neo4j \Common \BoltConnection ;
24
21
use Laudis \Neo4j \Contracts \AuthenticateInterface ;
25
22
use Laudis \Neo4j \Contracts \ConnectionInterface ;
28
25
use Laudis \Neo4j \Databags \DriverConfiguration ;
29
26
use Laudis \Neo4j \Databags \SessionConfiguration ;
30
27
use Laudis \Neo4j \Enum \ConnectionProtocol ;
31
- use Laudis \Neo4j \Enum \SslMode ;
32
28
use Laudis \Neo4j \Neo4j \RoutingTable ;
33
29
use Psr \Http \Message \UriInterface ;
34
30
use Throwable ;
@@ -44,13 +40,15 @@ final class BoltConnectionPool implements ConnectionPoolInterface
44
40
/** @var array<string, list<BoltConnection>> */
45
41
private static array $ connectionCache = [];
46
42
private DriverConfiguration $ driverConfig ;
43
+ private SslConfigurator $ sslConfigurator ;
47
44
48
45
/**
49
46
* @psalm-external-mutation-free
50
47
*/
51
- public function __construct (DriverConfiguration $ driverConfig )
48
+ public function __construct (DriverConfiguration $ driverConfig, SslConfigurator $ sslConfigurator )
52
49
{
53
50
$ this ->driverConfig = $ driverConfig ;
51
+ $ this ->sslConfigurator = $ sslConfigurator ;
54
52
}
55
53
56
54
/**
@@ -100,56 +98,12 @@ public function acquire(
100
98
return $ connection ;
101
99
}
102
100
103
- private function configureSsl (UriInterface $ uri , UriInterface $ server , StreamSocket $ socket , ?RoutingTable $ table ): void
104
- {
105
- $ sslMode = $ this ->driverConfig ->getSslConfiguration ()->getMode ();
106
- $ sslConfig = '' ;
107
- if ($ sslMode === SslMode::FROM_URL ()) {
108
- $ scheme = $ uri ->getScheme ();
109
- $ explosion = explode ('+ ' , $ scheme , 2 );
110
- $ sslConfig = $ explosion [1 ] ?? '' ;
111
- } elseif ($ sslMode === SslMode::ENABLE ()) {
112
- $ sslConfig = 's ' ;
113
- } elseif ($ sslMode === SslMode::ENABLE_WITH_SELF_SIGNED ()) {
114
- $ sslConfig = 'ssc ' ;
115
- }
116
-
117
- if (str_starts_with ($ sslConfig , 's ' )) {
118
- // We have to pass a different host when working with ssl on aura.
119
- // There is a strange behaviour where if we pass the uri host on a single
120
- // instance aura deployment, we need to pass the original uri for the
121
- // ssl configuration to be valid.
122
- if ($ table && count ($ table ->getWithRole ()) > 1 ) {
123
- $ this ->enableSsl ($ server ->getHost (), $ sslConfig , $ socket );
124
- } else {
125
- $ this ->enableSsl ($ uri ->getHost (), $ sslConfig , $ socket );
126
- }
127
- }
128
- }
129
-
130
- private function enableSsl (string $ host , string $ sslConfig , StreamSocket $ sock ): void
131
- {
132
- $ options = [
133
- 'verify_peer ' => $ this ->driverConfig ->getSslConfiguration ()->isVerifyPeer (),
134
- 'peer_name ' => $ host ,
135
- ];
136
- if (!filter_var ($ host , FILTER_VALIDATE_IP )) {
137
- $ options ['SNI_enabled ' ] = true ;
138
- }
139
- if ($ sslConfig === 's ' ) {
140
- $ sock ->setSslContextOptions ($ options );
141
- } elseif ($ sslConfig === 'ssc ' ) {
142
- $ options ['allow_self_signed ' ] = true ;
143
- $ sock ->setSslContextOptions ($ options );
144
- }
145
- }
146
-
147
101
public function canConnect (UriInterface $ uri , AuthenticateInterface $ authenticate , ?RoutingTable $ table = null , ?UriInterface $ server = null ): bool
148
102
{
149
103
$ connectingTo = $ server ?? $ uri ;
150
104
$ socket = new StreamSocket ($ uri ->getHost (), $ connectingTo ->getPort () ?? 7687 );
151
105
152
- $ this ->configureSsl ($ uri , $ connectingTo , $ socket , $ table );
106
+ $ this ->sslConfigurator -> configure ($ uri , $ connectingTo , $ socket , $ table, $ this -> driverConfig );
153
107
154
108
try {
155
109
$ bolt = new Bolt ($ socket );
@@ -172,7 +126,7 @@ private function openConnection(
172
126
): BoltConnection {
173
127
$ socket = new StreamSocket ($ connectingTo ->getHost (), $ connectingTo ->getPort () ?? 7687 , $ socketTimeout );
174
128
175
- $ this ->configureSsl ($ uri , $ connectingTo , $ socket , $ table );
129
+ $ this ->sslConfigurator -> configure ($ uri , $ connectingTo , $ socket , $ table, $ this -> driverConfig );
176
130
177
131
$ bolt = new Bolt ($ socket );
178
132
$ authenticate ->authenticateBolt ($ bolt , $ connectingTo , $ userAgent );
0 commit comments