Peer certificate CN=`project_name:instance_name' did not match expected CN=`00.101.251.00'

34 Views Asked by At

im tryng to connect to a SQL database in GCP with PHP using SSL/TLS, but im getting the error:

PHP Fatal error:  Uncaught PDOException: PDO::__construct(): Peer certificate CN=`project_name-411214:database-sql-teste' did not match expected CN=`00.101.211.214'

When the PDO option PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT is true, i get this error, but when i turn to false, i can connect to the database, how i can resolve this ? I think is the server-ca.pem file its wrong but when i try to connect using MYSQL CLI i can connect normally. If i disable the option: PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, my connection will dont use SSL/TLS anymore ?

enter image description here

<?php
$servername = "publicipdatabase";
$username = "root";
$password = "";
$certificado_ca = __DIR__ . "/server-ca.pem";
$certificado_cliente = __DIR__ . "/client-cert.pem";
$chave_privada_cliente = __DIR__ . "/client-key.pem";
$dbnome = "";
$options = array(
    PDO::MYSQL_ATTR_SSL_CA => $certificado_ca,
    PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => true,
        PDO::MYSQL_ATTR_SSL_CERT => $certificado_cliente,
        PDO::MYSQL_ATTR_SSL_KEY => $chave_privada_cliente,
);

try {
    $dsn = sprintf('mysql:dbname=%s;host=%s', $dbnome, $servername);
    $conn = new PDO($dsn, $username, $password,$options);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $ssl_status = $conn->getAttribute(PDO::ATTR_CONNECTION_STATUS);
    var_dump($ssl_status);
    echo "Connected successfully"; 
    var_dump($conn->query("SHOW DATABASES")->fetchAll());
    $conn = null;
} catch (TypeError $e) {
    throw new RuntimeException(
        sprintf(
            'Invalid or missing configuration! Make sure you have set ' .
                '$username, $password, $dbName, and $instanceHost (for TCP mode). ' .
                'The PHP error was %s',
            $e->getMessage()
        ),
        $e->getCode(),
        $e
    );
} catch (PDOException $e) {
    throw new RuntimeException(
        sprintf(
            'Could not connect to the Cloud SQL Database. Check that ' .
                'your username and password are correct, that the Cloud SQL ' .
                'proxy is running, and that the database exists and is ready ' .
                'for use. For more assistance, refer to %s. The PDO error was %s',
            'https://cloud.google.com/sql/docs/mysql/connect-external-app',
            $e->getMessage()
        ),
        $e->getCode(),
        $e
    );
}

Im trying resolve this to grantee the security of my connections to the database using SSL/TLS.

0

There are 0 best solutions below