PHP Velho Oeste 2024

Conectar sobre SSL

El controlador admite conexiones a » MongoDB sobre SSL y puede opcionalmente usar opciones Contexto de flujos de SSL para proporcionar más detalles, tales como verificar certificados con cadenas de certificado específicas, o autenticar a » MongoDB usando la certificación X509.

Ejemplo #1 Conectar a una instancia de MongoDB con encriptación SSL

<?php
$mc 
= new MongoClient("mongodb://server1", array("ssl" => true));
?>

Ejemplo #2 Conectar a una instancia de MongoDB con encriptación SSL, verificando que es quien se espera

<?php
$SSL_DIR 
"/vagrant/certs";
$SSL_FILE "CA_Root_Certificate.pem";

$ctx stream_context_create(array(
    
"ssl" => array(
        
/* Certificate Authority the remote server certificate must be signed by */
        
"cafile"            => $SSL_DIR "/" $SSL_FILE,

        
/* Disable self signed certificates */
        
"allow_self_signed" => false,

        
/* Verify the peer certificate against our provided Certificate Authority root certificate */
        
"verify_peer"       => true/* Default to false pre PHP 5.6 */

        /* Verify the peer name (e.g. hostname validation) */
        /* Will use the hostname used to connec to the node */
        
"verify_peer_name"  => true,

        
/* Verify the server certificate has not expired */
        
"verify_expiry"     => true/* Only available in the MongoDB PHP Driver */
    
),
);

$mc = new MongoClient(
    
"mongodb://server1"
    array(
"ssl" => true), 
    array(
"context" => $ctx)
);
?>

Nota:

"verify_peer_name" es nuevo en PHP 5.6.0. Sin embargo, el controlador de MongoDB, a partir de la versión 1.6.5, ha hecho retrocompatible esta característica incluyéndola en el mismo controlador, por lo que funciona con PHP 5.3 y 5.4 también.

Ejemplo #3 Conectar a una instancia de MongoDB que requiera certificados cliente

<?php
$SSL_DIR  
"/vagrant/certs";
$SSL_FILE "CA_Root_Certificate.pem";

$MYCERT   "/vagrant/certs/ca-signed-client.pem";

$ctx stream_context_create(array(
    
"ssl" => array(
        
"local_cert"        => $MYCERT,
        
/* If the certificate we are providing was passphrase encoded, we need to set it here */
        
"passphrase"        => "My Passphrase for the local_cert",

        
/* Optionally verify the server is who he says he is */
        
"cafile"            => $SSL_DIR "/" $SSL_FILE,
        
"allow_self_signed" => false,
        
"verify_peer"       => true,
        
"verify_peer_name"  => true,
        
"verify_expiry"     => true,
    ),
));

$mc = new MongoClient(
    
"mongodb://server1/?ssl=true"
    array(), 
    array(
"context" => $ctx)
);
?>

Ejemplo #4 Autenticación con certificados X.509

El nombre de usuario es el sujeto del certificado de X509, el cual puede extraerse así:

openssl x509 -in /vagrant/certs/ca-signed-client.pem -inform PEM -subject -nameopt RFC2253
<?php
$ctx 
stream_context_create( array(
    
"ssl" => array(
        
"local_cert" => "/vagrant/certs/ca-signed-client.pem",
    )
) );

$mc = new MongoClient(
    
'mongodb://username@server1/?authSource=$external&authMechanism=MONGODB-X509&ssl=true'
    array(), 
    array(
"context" => $ctx)
);
?>

Donde username es el sujeto del certificado.

Historial de cambios

Versión Descripción
1.5.0 Se añadió soporte para autenticación X509.
1.4.0 Se añadió soporte para conectar con MongoDB habilitado para SSL.
add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top