-
Notifications
You must be signed in to change notification settings - Fork 209
PHPC-2613: Test x509 authentication on Atlas #1858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
033ce9c
0e55a07
988c53d
78465fd
34b9e9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,56 @@ Atlas Connectivity Tests | |
<?php | ||
require_once __DIR__ . "/utils/basic.inc"; | ||
|
||
function extractUri(string $env): ?string | ||
{ | ||
return getenv($env) ?: null; | ||
} | ||
|
||
function extractUriWithCertificate(string $env): ?array | ||
{ | ||
$uri = getenv($env); | ||
if (! is_string($uri)) { | ||
return null; | ||
} | ||
|
||
$cert = getenv($env . '_CERT_BASE64'); | ||
if (! is_string($cert)) { | ||
return null; | ||
} | ||
|
||
$certPath = tempnam(sys_get_temp_dir(), 'cert_'); | ||
$certContents = base64_decode($cert); | ||
if (! $certPath || ! $certContents) { | ||
return null; | ||
} | ||
|
||
file_put_contents($certPath, $certContents); | ||
chmod($certPath, 0600); | ||
|
||
return [ | ||
'uri' => $uri . '&tlsCertificateKeyFile=' . $certPath, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think it should be safe (famous last words) |
||
'certPath' => $certPath, | ||
]; | ||
} | ||
|
||
function testConnection(string $uri): void | ||
{ | ||
try { | ||
$m = new \MongoDB\Driver\Manager($uri); | ||
$m->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', | ||
|
@@ -21,28 +71,41 @@ $envs = [ | |
'ATLAS_TLS12', | ||
'ATLAS_SRV_TLS12', | ||
]; | ||
|
||
$command = new \MongoDB\Driver\Command(['ping' => 1]); | ||
$query = new \MongoDB\Driver\Query([]); | ||
$x509Envs = [ | ||
'ATLAS_X509', | ||
'ATLAS_X509_DEV', | ||
]; | ||
|
||
foreach ($envs as $env) { | ||
echo $env, ': '; | ||
$uri = getenv($env); | ||
$uri = extractUri($env); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the only purpose of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I was a little eager with my refactoring. Removed it in favour of |
||
|
||
if (! is_string($uri)) { | ||
echo "FAIL: env var is undefined\n"; | ||
continue; | ||
} | ||
|
||
testConnection($uri); | ||
} | ||
|
||
foreach ($x509Envs as $env) { | ||
echo $env, ': '; | ||
$uriWithCertificate = extractUriWithCertificate($env); | ||
|
||
if (! is_array($uriWithCertificate)) { | ||
echo "FAIL: env var is undefined\n"; | ||
continue; | ||
} | ||
|
||
['uri' => $uri, 'certPath' => $certPath] = $uriWithCertificate; | ||
|
||
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); | ||
} finally { | ||
@unlink($certPath); | ||
} | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
|
@@ -59,4 +122,6 @@ ATLAS_TLS11: PASS | |
ATLAS_SRV_TLS11: PASS | ||
ATLAS_TLS12: PASS | ||
ATLAS_SRV_TLS12: PASS | ||
ATLAS_X509: PASS | ||
ATLAS_X509_DEV: PASS | ||
===DONE=== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary?
tempnam()
should already create the file with these perms.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL:
Removed the
chmod
call.