@@ -12,7 +12,7 @@ function extractUri(string $env): ?string
1212 return getenv ($ env ) ?: null ;
1313}
1414
15- function extractUriWithCertificate (string $ env ): ?string
15+ function extractUriWithCertificate (string $ env ): ?array
1616{
1717 $ uri = getenv ($ env );
1818 if (! is_string ($ uri )) {
@@ -24,10 +24,19 @@ function extractUriWithCertificate(string $env): ?string
2424 return null ;
2525 }
2626
27- $ certPath = '/tmp/cert.pem ' ;
28- file_put_contents ($ certPath , base64_decode ($ cert ));
27+ $ certPath = tempnam (sys_get_temp_dir (), 'cert_ ' );
28+ $ certContents = base64_decode ($ cert );
29+ if (! $ certPath || ! $ certContents ) {
30+ return null ;
31+ }
2932
30- return $ uri . '&tlsCertificateKeyFile= ' . $ certPath ;
33+ file_put_contents ($ certPath , $ certContents );
34+ chmod ($ certPath , 0600 );
35+
36+ return [
37+ 'uri ' => $ uri . '&tlsCertificateKeyFile= ' . $ certPath ,
38+ 'certPath ' => $ certPath ,
39+ ];
3140}
3241
3342function testConnection (string $ uri ): void
@@ -81,14 +90,20 @@ foreach ($envs as $env) {
8190
8291foreach ($ x509Envs as $ env ) {
8392 echo $ env , ': ' ;
84- $ uri = extractUriWithCertificate ($ env );
93+ $ uriWithCertificate = extractUriWithCertificate ($ env );
8594
86- if (! is_string ( $ uri )) {
95+ if (! is_array ( $ uriWithCertificate )) {
8796 echo "FAIL: env var is undefined \n" ;
8897 continue ;
8998 }
9099
91- testConnection ($ uri );
100+ ['uri ' => $ uri , 'certPath ' => $ certPath ] = $ uriWithCertificate ;
101+
102+ try {
103+ testConnection ($ uri );
104+ } finally {
105+ @unlink ($ certPath );
106+ }
92107}
93108
94109?>
0 commit comments