From 033ce9c45df9971b610f4ac4ed3b08650a2a566e Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 7 Aug 2025 09:22:54 +0200 Subject: [PATCH 1/5] PHPC-2613: Test x509 authentication on Atlas --- tests/atlas.phpt | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/atlas.phpt b/tests/atlas.phpt index 00875b0db..677ac7184 100644 --- a/tests/atlas.phpt +++ b/tests/atlas.phpt @@ -21,6 +21,10 @@ $envs = [ 'ATLAS_TLS12', 'ATLAS_SRV_TLS12', ]; +$x509Envs = [ + 'ATLAS_X509', + 'ATLAS_X509_DEV', +]; $command = new \MongoDB\Driver\Command(['ping' => 1]); $query = new \MongoDB\Driver\Query([]); @@ -43,6 +47,34 @@ foreach ($envs as $env) { echo "FAIL: ", $e->getMessage(), "\n"; } } + +foreach ($x509Envs as $env) { + echo $env, ': '; + $uri = getenv($env); + $cert = getenv($env . '_CERT_BASE64'); + + if (! is_string($uri)) { + echo "FAIL: env var is undefined\n"; + continue; + } + + if (! is_string($cert)) { + echo "FAIL: cert env var is undefined\n"; + continue; + } + + file_put_contents('/tmp/cert.pem', base64_decode($cert)); + + try { + $m = new \MongoDB\Driver\Manager($uri . '&tlsCertificateKeyFile=/tmp/cert.pem'); + $m->executeCommand('admin', $command); + iterator_to_array($m->executeQuery('test.test', $query)); + echo "PASS\n"; + } catch(Exception $e) { + echo "FAIL: ", $e->getMessage(), "\n"; + } +} + ?> ===DONE=== @@ -59,4 +91,6 @@ ATLAS_TLS11: PASS ATLAS_SRV_TLS11: PASS ATLAS_TLS12: PASS ATLAS_SRV_TLS12: PASS +ATLAS_X509: PASS +ATLAS_X509_DEV: PASS ===DONE=== From 0e55a07475d9f58278775ff625be503633a2e707 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 7 Aug 2025 10:14:47 +0200 Subject: [PATCH 2/5] Refactor code for less duplication --- tests/atlas.phpt | 78 +++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/tests/atlas.phpt b/tests/atlas.phpt index 677ac7184..022ef9c5c 100644 --- a/tests/atlas.phpt +++ b/tests/atlas.phpt @@ -7,6 +7,47 @@ Atlas Connectivity Tests executeCommand( + 'admin', + new \MongoDB\Driver\Command(['ping' => 1]), + ); + iterator_to_array($m->executeQuery( + 'test.test', + new \MongoDB\Driver\Query([]), + )); + echo "PASS\n"; + } catch(Exception $e) { + echo "FAIL: ", $e->getMessage(), "\n"; + } +} + $envs = [ 'ATLAS_SERVERLESS', 'ATLAS_SRV_SERVERLESS', @@ -22,57 +63,32 @@ $envs = [ 'ATLAS_SRV_TLS12', ]; $x509Envs = [ - 'ATLAS_X509', - 'ATLAS_X509_DEV', + 'ATLAS_X509', + 'ATLAS_X509_DEV', ]; -$command = new \MongoDB\Driver\Command(['ping' => 1]); -$query = new \MongoDB\Driver\Query([]); - foreach ($envs as $env) { echo $env, ': '; - $uri = getenv($env); + $uri = extractUri($env); if (! is_string($uri)) { echo "FAIL: env var is undefined\n"; continue; } - try { - $m = new \MongoDB\Driver\Manager($uri); - $m->executeCommand('admin', $command); - iterator_to_array($m->executeQuery('test.test', $query)); - echo "PASS\n"; - } catch(Exception $e) { - echo "FAIL: ", $e->getMessage(), "\n"; - } + testConnection($uri); } foreach ($x509Envs as $env) { echo $env, ': '; - $uri = getenv($env); - $cert = getenv($env . '_CERT_BASE64'); + $uri = extractUriWithCertificate($env); if (! is_string($uri)) { echo "FAIL: env var is undefined\n"; continue; } - if (! is_string($cert)) { - echo "FAIL: cert env var is undefined\n"; - continue; - } - - file_put_contents('/tmp/cert.pem', base64_decode($cert)); - - try { - $m = new \MongoDB\Driver\Manager($uri . '&tlsCertificateKeyFile=/tmp/cert.pem'); - $m->executeCommand('admin', $command); - iterator_to_array($m->executeQuery('test.test', $query)); - echo "PASS\n"; - } catch(Exception $e) { - echo "FAIL: ", $e->getMessage(), "\n"; - } + testConnection($uri); } ?> From 988c53d1af357b284b303bc0cc6f71249ebc63d9 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 7 Aug 2025 11:16:26 +0200 Subject: [PATCH 3/5] Apply review feedback from Copilot --- tests/atlas.phpt | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/tests/atlas.phpt b/tests/atlas.phpt index 022ef9c5c..a94f85e3c 100644 --- a/tests/atlas.phpt +++ b/tests/atlas.phpt @@ -12,7 +12,7 @@ function extractUri(string $env): ?string return getenv($env) ?: null; } -function extractUriWithCertificate(string $env): ?string +function extractUriWithCertificate(string $env): ?array { $uri = getenv($env); if (! is_string($uri)) { @@ -24,10 +24,19 @@ function extractUriWithCertificate(string $env): ?string return null; } - $certPath = '/tmp/cert.pem'; - file_put_contents($certPath, base64_decode($cert)); + $certPath = tempnam(sys_get_temp_dir(), 'cert_'); + $certContents = base64_decode($cert); + if (! $certPath || ! $certContents) { + return null; + } - return $uri . '&tlsCertificateKeyFile=' . $certPath; + file_put_contents($certPath, $certContents); + chmod($certPath, 0600); + + return [ + 'uri' => $uri . '&tlsCertificateKeyFile=' . $certPath, + 'certPath' => $certPath, + ]; } function testConnection(string $uri): void @@ -81,14 +90,20 @@ foreach ($envs as $env) { foreach ($x509Envs as $env) { echo $env, ': '; - $uri = extractUriWithCertificate($env); + $uriWithCertificate = extractUriWithCertificate($env); - if (! is_string($uri)) { + if (! is_array($uriWithCertificate)) { echo "FAIL: env var is undefined\n"; continue; } - testConnection($uri); + ['uri' => $uri, 'certPath' => $certPath] = $uriWithCertificate; + + try { + testConnection($uri); + } finally { + @unlink($certPath); + } } ?> From 78465fd629adb338a398a9f47fb9991d389902ae Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Fri, 8 Aug 2025 09:57:06 +0200 Subject: [PATCH 4/5] Remove unnecessary extractUri function --- tests/atlas.phpt | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/atlas.phpt b/tests/atlas.phpt index a94f85e3c..f587a97ce 100644 --- a/tests/atlas.phpt +++ b/tests/atlas.phpt @@ -7,11 +7,6 @@ Atlas Connectivity Tests Date: Fri, 8 Aug 2025 09:57:16 +0200 Subject: [PATCH 5/5] Remove unnecessary call to chmod --- tests/atlas.phpt | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/atlas.phpt b/tests/atlas.phpt index f587a97ce..9fb83e840 100644 --- a/tests/atlas.phpt +++ b/tests/atlas.phpt @@ -26,7 +26,6 @@ function extractUriWithCertificate(string $env): ?array } file_put_contents($certPath, $certContents); - chmod($certPath, 0600); return [ 'uri' => $uri . '&tlsCertificateKeyFile=' . $certPath,