Skip to content

Commit 872bc99

Browse files
committed
fixed ssl configuration bug when connecting to neo4j aura pro instances and up
fixes #62
1 parent 29960e8 commit 872bc99

File tree

4 files changed

+38
-46
lines changed

4 files changed

+38
-46
lines changed

src/Bolt/BoltConnectionPool.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
use Bolt\connection\StreamSocket;
1717
use Exception;
1818
use function explode;
19-
use const FILTER_VALIDATE_IP;
20-
use function filter_var;
19+
use Laudis\Neo4j\Common\TransactionHelper;
2120
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2221
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
2322
use Laudis\Neo4j\Enum\AccessMode;
@@ -42,25 +41,9 @@ public function acquire(UriInterface $uri, AccessMode $mode, AuthenticateInterfa
4241
$sslConfig = $explosion[1] ?? '';
4342

4443
if (str_starts_with('s', $sslConfig)) {
45-
$this->enableSsl($host, $sslConfig, $socket);
44+
TransactionHelper::enableSsl($host, $sslConfig, $socket);
4645
}
4746

4847
return $socket;
4948
}
50-
51-
private function enableSsl(string $host, string $sslConfig, StreamSocket $sock): void
52-
{
53-
// Pass a standard option to enable ssl as there is no direct flag
54-
// and \Bolt\Bolt only turns on ssl if an option is passed.
55-
$options = ['verify_peer' => true];
56-
if (!filter_var($host, FILTER_VALIDATE_IP)) {
57-
$options['SNI_enabled'] = true;
58-
}
59-
if ($sslConfig === 's') {
60-
$sock->setSslContextOptions($options);
61-
} elseif ($sslConfig === 'ssc') {
62-
$options['allow_self_signed'] = true;
63-
$sock->setSslContextOptions($options);
64-
}
65-
}
6649
}

src/Common/TransactionHelper.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace Laudis\Neo4j\Common;
1515

16+
use Bolt\connection\StreamSocket;
17+
use const FILTER_VALIDATE_IP;
18+
use function filter_var;
1619
use Laudis\Neo4j\Contracts\TransactionInterface;
1720
use Laudis\Neo4j\Contracts\UnmanagedTransactionInterface;
1821
use Laudis\Neo4j\Databags\TransactionConfiguration;
@@ -52,4 +55,22 @@ public static function retry(callable $tsxFactory, callable $tsxHandler, Transac
5255
}
5356
}
5457
}
58+
59+
public static function enableSsl(string $host, string $sslConfig, StreamSocket $sock): void
60+
{
61+
$options = [
62+
'verify_peer' => true,
63+
// 'verify_peer_name' => false,
64+
'peer_name' => $host,
65+
];
66+
if (!filter_var($host, FILTER_VALIDATE_IP)) {
67+
$options['SNI_enabled'] = true;
68+
}
69+
if ($sslConfig === 's') {
70+
$sock->setSslContextOptions($options);
71+
} elseif ($sslConfig === 'ssc') {
72+
$options['allow_self_signed'] = true;
73+
$sock->setSslContextOptions($options);
74+
}
75+
}
5576
}

src/Neo4j/Neo4jConnectionPool.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
use Bolt\connection\StreamSocket;
1717
use Exception;
1818
use function explode;
19-
use const FILTER_VALIDATE_IP;
20-
use function filter_var;
2119
use Laudis\Neo4j\Bolt\BoltDriver;
20+
use Laudis\Neo4j\Common\TransactionHelper;
2221
use Laudis\Neo4j\Common\Uri;
2322
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2423
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
@@ -57,50 +56,39 @@ public function acquire(UriInterface $uri, AccessMode $mode, AuthenticateInterfa
5756
$table = $this->routingTable($uri, $authenticate);
5857
$server = $this->getNextServer($table, $mode);
5958

60-
$socket = $this->pool->acquire(Uri::create($server), $mode, $authenticate);
59+
$socket = $this->pool->acquire($server, $mode, $authenticate);
6160

6261
$scheme = $uri->getScheme();
6362
$explosion = explode('+', $scheme, 2);
6463
$sslConfig = $explosion[1] ?? '';
6564

6665
if (str_starts_with('s', $sslConfig)) {
67-
$this->enableSsl($server, $sslConfig, $socket, $uri);
66+
// We have to pass a different host when working with ssl on aura.
67+
// There is a strange behaviour where if we pass the uri host on a single
68+
// instance aura deployment, we need to pass the original uri for the
69+
// ssl configuration to be valid.
70+
if ($table->getWithRole()->count() > 1) {
71+
TransactionHelper::enableSsl($server->getHost(), $sslConfig, $socket);
72+
} else {
73+
TransactionHelper::enableSsl($uri->getHost(), $sslConfig, $socket);
74+
}
6875
}
6976

7077
return $socket;
7178
}
7279

73-
private function enableSsl(string $host, string $sslConfig, StreamSocket $sock, UriInterface $uri): void
74-
{
75-
// Pass a standard option to enable ssl as there is no direct flag
76-
// and \Bolt\Bolt only turns on ssl if an option is passed.
77-
$options = [
78-
'verify_peer' => true,
79-
'peer_name' => $uri->getHost(),
80-
];
81-
if (!filter_var($host, FILTER_VALIDATE_IP)) {
82-
$options['SNI_enabled'] = true;
83-
}
84-
if ($sslConfig === 's') {
85-
$sock->setSslContextOptions($options);
86-
} elseif ($sslConfig === 'ssc') {
87-
$options['allow_self_signed'] = true;
88-
$sock->setSslContextOptions($options);
89-
}
90-
}
91-
9280
/**
9381
* @throws Exception
9482
*/
95-
private function getNextServer(RoutingTable $table, AccessMode $mode): string
83+
private function getNextServer(RoutingTable $table, AccessMode $mode): Uri
9684
{
9785
if (AccessMode::WRITE() === $mode) {
9886
$servers = $table->getWithRole(RoutingRoles::LEADER());
9987
} else {
10088
$servers = $table->getWithRole(RoutingRoles::FOLLOWER());
10189
}
10290

103-
return $servers->get(random_int(0, $servers->count() - 1));
91+
return Uri::create($servers->get(random_int(0, $servers->count() - 1)));
10492
}
10593

10694
/**

src/Neo4j/RoutingTable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public function getTtl(): int
4040
/**
4141
* @return Set<string>
4242
*/
43-
public function getWithRole(RoutingRoles $role): Set
43+
public function getWithRole(RoutingRoles $role = null): Set
4444
{
4545
/** @psalm-var Set<string> $tbr */
4646
$tbr = new Set();
4747
foreach ($this->servers as $server) {
48-
if (in_array($server['role'], $role->getValue(), true)) {
48+
if ($role === null || in_array($server['role'], $role->getValue(), true)) {
4949
foreach ($server['addresses'] as $address) {
5050
$tbr->add($address);
5151
}

0 commit comments

Comments
 (0)