feat(client): optional TLS for the MongoDB connection

Add a `$tls` constructor flag (default false, BC-preserving) that opens the
Swoole socket with SWOOLE_SSL and enables encryption. Peer verification is
disabled (ssl_verify_peer=false, ssl_allow_self_signed=true) because the
backend is reached through a TLS-terminating proxy whose hostname is not on the
backend's own certificate. Self-hosted plaintext connections are unaffected.

Needed so Appwrite Cloud can reach a dedicated MongoDB backend (documentsdb)
through the edge TCP proxy, which mandates TLS.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jake BarnbyandClaude Opus 4.8 committed 2026-06-08 00:25:29 +12:00
1 parent 73593682de
commit 8e872f7778
1 file changed
+21 -5
+21 -5
View File
@@ -142,7 +142,8 @@ class Client
int $port,
string $user,
string $password,
bool $useCoroutine = false
bool $useCoroutine = false,
bool $tls = false
) {
if (empty($database)) {
throw new \InvalidArgumentException('Database name cannot be empty');
@@ -177,18 +178,33 @@ class Client
}
}
$flags = SWOOLE_SOCK_TCP | SWOOLE_KEEP;
if ($tls) {
$flags |= SWOOLE_SSL;
}
$this->client = $useCoroutine
? new CoroutineClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP)
: new SwooleClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP);
? new CoroutineClient($flags)
: new SwooleClient($flags);
// Set socket options to prevent hanging
$this->client->set([
$options = [
'open_tcp_keepalive' => true,
'tcp_keepidle' => 4, // Start keepalive after 4s idle
'tcp_keepinterval' => 3, // Keepalive interval 3s
'tcp_keepcount' => 2, // Close after 2 failed keepalives
'timeout' => 30 // 30 second connection timeout
]);
];
if ($tls) {
// The backend is reached through a TLS-terminating proxy presenting a
// trusted platform certificate; encrypt the hop without enforcing peer
// verification (the proxy hostname is not on the backend's own cert).
$options['ssl_verify_peer'] = false;
$options['ssl_allow_self_signed'] = true;
}
$this->client->set($options);
$this->auth = new Auth([
'authcid' => $user,