Skip to content
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

Allow secure connections using SSL on mysqli #2570

Merged
merged 1 commit into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/en/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ mysqli
the database.
- ``charset`` (string): The charset used when connecting to the
database.
- ``ssl_key`` (string): The path name to the key file to use for SSL encryption.
- ``ssl_cert`` (string): The path name to the certificate file to use for SSL encryption.
- ``ssl_ca`` (string): The path name to the certificate authority file to use for SSL encryption.
- ``ssl_capath`` (string): The pathname to a directory that contains trusted SSL CA certificates in PEM format.
- ``ssl_cipher`` (string): A list of allowable ciphers to use for SSL encryption.
- ``driverOptions`` Any supported flags for mysqli found on `http://www.php.net/manual/en/mysqli.real-connect.php`

pdo\_pgsql
Expand Down
32 changes: 32 additions & 0 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function __construct(array $params, $username, $password, array $driverOp

$this->_conn = mysqli_init();

$this->setSecureConnection($params);
$this->setDriverOptions($driverOptions);

set_error_handler(function () {});
Expand Down Expand Up @@ -263,4 +264,35 @@ public function ping()
{
return $this->_conn->ping();
}

/**
* Establish a secure connection
*
* @param array $params
* @throws MysqliException
*/
private function setSecureConnection(array $params)
{
if (! isset($params['ssl_key']) &&
! isset($params['ssl_cert']) &&
! isset($params['ssl_ca']) &&
! isset($params['ssl_capath']) &&
! isset($params['ssl_cipher'])
) {
return;
}

if (! isset($params['ssl_key']) || ! isset($params['ssl_cert'])) {
$msg = '"ssl_key" and "ssl_cert" parameters are mandatory when using secure connection parameters.';
throw new MysqliException($msg);
}

$this->_conn->ssl_set(
$params['ssl_key'],
$params['ssl_cert'],
$params['ssl_ca'] ?? null,
$params['ssl_capath'] ?? null,
$params['ssl_cipher'] ?? null
);
}
}
35 changes: 34 additions & 1 deletion tests/Doctrine/Tests/DBAL/Driver/Mysqli/MysqliConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Doctrine\Tests\DBAL\Driver\Mysqli;

use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Doctrine\Tests\DbalTestCase;

class MysqliConnectionTest extends DbalTestCase
Expand All @@ -21,7 +23,7 @@ protected function setUp()

parent::setUp();

$this->connectionMock = $this->getMockBuilder('Doctrine\DBAL\Driver\Mysqli\MysqliConnection')
$this->connectionMock = $this->getMockBuilder(MysqliConnection::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
}
Expand All @@ -30,4 +32,35 @@ public function testDoesNotRequireQueryForServerVersion()
{
$this->assertFalse($this->connectionMock->requiresQueryForServerVersion());
}

/**
* @dataProvider secureMissingParamsProvider
*/
public function testThrowsExceptionWhenMissingMandatorySecureParams(array $secureParams)
{
$this->expectException(MysqliException::class);
$msg = '"ssl_key" and "ssl_cert" parameters are mandatory when using secure connection parameters.';
$this->expectExceptionMessage($msg);

new MysqliConnection($secureParams, 'xxx', 'xxx');
}

public function secureMissingParamsProvider()
{
return [
[
['ssl_cert' => 'cert.pem']
],
[
['ssl_key' => 'key.pem']
],
[
['ssl_key' => 'key.pem', 'ssl_ca' => 'ca.pem', 'ssl_capath' => 'xxx', 'ssl_cipher' => 'xxx']
],
[
['ssl_ca' => 'ca.pem', 'ssl_capath' => 'xxx', 'ssl_cipher' => 'xxx']
]
];
}
}