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

Test MySQLi connection via TLS on Travis #4209

Merged
merged 3 commits into from
Aug 18, 2020
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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ jobs:
- stage: Test
php: 7.4
env: DB=mysqli.docker IMAGE=mysql:8.0
- stage: Test
php: 7.4
env: DB=mysqli-tls.docker IMAGE=mysql:8.0 TLS=yes
- stage: Test
php: 7.4
env: DB=mariadb.docker IMAGE=mariadb:10.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public function testConnectsWithoutDatabaseNameParameter(): void
$params = $this->connection->getParams();
unset($params['dbname']);

$user = $params['user'] ?? null;
$password = $params['password'] ?? null;
$user = $params['user'] ?? null;
$password = $params['password'] ?? null;
$driverOptions = $params['driverOptions'] ?? [];

$connection = $this->driver->connect($params, $user, $password);
$connection = $this->driver->connect($params, $user, $password, $driverOptions);

self::assertInstanceOf(DriverConnection::class, $connection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Doctrine\Tests\DbalFunctionalTestCase;
use Doctrine\Tests\TestUtil;

use function array_merge;

use const MYSQLI_OPT_CONNECT_TIMEOUT;

/**
Expand Down Expand Up @@ -54,6 +56,10 @@ private function getConnection(array $driverOptions): MysqliConnection
{
$params = TestUtil::getConnectionParams();

if (isset($params['driverOptions'])) {
$driverOptions = array_merge($params['driverOptions'], $driverOptions);
}

return new MysqliConnection(
$params,
$params['user'] ?? '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Doctrine\Tests\TestUtil;
use PDO;

use function array_merge;

/**
* @requires extension pdo_sqlsrv
*/
Expand Down Expand Up @@ -42,6 +44,10 @@ private function getConnection(array $driverOptions): PDOConnection
{
$params = TestUtil::getConnectionParams();

if (isset($params['driverOptions'])) {
$driverOptions = array_merge($params['driverOptions'], $driverOptions);
}

return $this->connection->getDriver()->connect(
$params,
$params['user'] ?? '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ public function testDropsDatabaseWithActiveConnections(): void
$params['dbname'] = 'test_drop_database';
}

$user = $params['user'] ?? null;
$password = $params['password'] ?? null;
$user = $params['user'] ?? null;
$password = $params['password'] ?? null;
$driverOptions = $params['driverOptions'] ?? [];

$connection = $this->connection->getDriver()->connect($params, $user, $password);
$connection = $this->connection->getDriver()->connect($params, $user, $password, $driverOptions);

self::assertInstanceOf(Connection::class, $connection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public function testDropsDatabaseWithActiveConnections(): void
$params = $this->connection->getParams();
$params['dbname'] = 'test_drop_database';

$user = $params['user'] ?? null;
$password = $params['password'] ?? null;
$user = $params['user'] ?? null;
$password = $params['password'] ?? null;
$driverOptions = $params['driverOptions'] ?? [];

$connection = $this->connection->getDriver()->connect($params, $user, $password);
$connection = $this->connection->getDriver()->connect($params, $user, $password, $driverOptions);

self::assertInstanceOf(Connection::class, $connection);

Expand Down
16 changes: 16 additions & 0 deletions tests/Doctrine/Tests/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

use function explode;
use function extension_loaded;
use function strlen;
use function strpos;
use function substr;
use function unlink;

/**
Expand Down Expand Up @@ -175,6 +178,11 @@ private static function mapConnectionParameters(array $configuration, string $pr
'dbname',
'port',
'server',
'ssl_key',
'ssl_cert',
'ssl_ca',
'ssl_capath',
'ssl_cipher',
'unix_socket',
] as $parameter
) {
Expand All @@ -185,6 +193,14 @@ private static function mapConnectionParameters(array $configuration, string $pr
$parameters[$parameter] = $configuration[$prefix . $parameter];
}

foreach ($configuration as $param => $value) {
if (strpos($param, $prefix . 'driver_option_') !== 0) {
continue;
}

$parameters['driverOptions'][substr($param, strlen($prefix . 'driver_option_'))] = $value;
}

return $parameters;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/travis/docker-run-mysql-or-mariadb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ while true; do
;;
esac
done

if [[ "$TLS" == "yes" ]]
Copy link
Member

@greg0ire greg0ire Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this in several of our packages so it must be ok, but is that a standard way of doing this? I mean we could use set TLS to true which is a program that always succeeds, and then write:

Suggested change
if [[ "$TLS" == "yes" ]]
if ${TLS:false}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I haven't ever seen the syntax like if ${TLS:false} being used used but have seen if [[ "$TLS" == "yes" ]] or even if [[ "x$TLS" == "xyes" ]]. AFAIK, it's all about compatibility with other shells, etc.

I don't think any of us wants to spend our time on coming up with a Bash coding standard right now :-)

then
for file in "ca.pem" "client-cert.pem" "client-key.pem"
do
docker cp "rdbms:/var/lib/mysql/$file" .
done
fi
44 changes: 44 additions & 0 deletions tests/travis/mysqli-tls.docker.travis.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
>
<php>
<var name="db_driver" value="mysqli"/>
<var name="db_host" value="127.0.0.1"/>
<var name="db_port" value="33306"/>
<var name="db_ssl_ca" value="ca.pem"/>
<var name="db_ssl_cert" value="client-cert.pem"/>
<var name="db_ssl_key" value="client-key.pem"/>

<!-- Use MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT since there's no way to generate a certificate
with a proper common name (CN) on Travis. This flag must be not used in production settings. -->
<var name="db_driver_option_flags" value="64"/>

<var name="db_user" value="root"/>
<var name="db_dbname" value="doctrine_tests"/>
</php>

<testsuites>
<testsuite name="Doctrine DBAL Test Suite">
<directory>../Doctrine/Tests/DBAL</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">../../lib/Doctrine</directory>
</whitelist>
</filter>

<groups>
<exclude>
<group>performance</group>
<group>locking_functional</group>
</exclude>
</groups>
</phpunit>