From 402222b59bc4578040bfd570a07d753d7e88f7c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joao=20Gilberto=20Magalh=C3=A3es?= Date: Fri, 1 Jun 2018 12:55:12 -0300 Subject: [PATCH 1/3] Added the MySQL connection via SSL. --- docs/Connecting-to-MySQL-via-SSL.md | 17 +++++++++++++++++ src/Store/PdoMysql.php | 9 +++++++++ 2 files changed, 26 insertions(+) create mode 100644 docs/Connecting-to-MySQL-via-SSL.md diff --git a/docs/Connecting-to-MySQL-via-SSL.md b/docs/Connecting-to-MySQL-via-SSL.md new file mode 100644 index 0000000..ff1668d --- /dev/null +++ b/docs/Connecting-to-MySQL-via-SSL.md @@ -0,0 +1,17 @@ +# Connecting To MySQL via SSL + +```php +getIterator('select * from table where field = :value', ['value' => 10]); +foreach ($iterator as $row) { + // Do Something + // $row->getField('field'); +} diff --git a/src/Store/PdoMysql.php b/src/Store/PdoMysql.php index dd4b28e..ba38514 100644 --- a/src/Store/PdoMysql.php +++ b/src/Store/PdoMysql.php @@ -25,6 +25,15 @@ public function __construct(Uri $connUri) PDO::ATTR_EMULATE_PREPARES => true ]; + $sslCa = $connUri->getQueryPart("ca"); + $sslCert = $connUri->getQueryPart("cert"); + $sslKey = $connUri->getQueryPart("key"); + if (!empty($sslCa) && !empty($sslCert) && !empty($sslKey)) { + $preOptions[PDO::MYSQL_ATTR_SSL_KEY] = urldecode($sslKey); + $preOptions[PDO::MYSQL_ATTR_SSL_CERT] = urldecode($sslCert); + $preOptions[PDO::MYSQL_ATTR_SSL_CA] = urldecode($sslCa); + } + $this->setSupportMultRowset(true); parent::__construct($connUri, $preOptions, $postOptions); From f75ea5c1f4e3d6340b875273ed47e66bbb52e194 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Mon, 4 Jun 2018 23:12:19 -0300 Subject: [PATCH 2/3] Added verifyssl, cipher and capath --- docs/Connecting-to-MySQL-via-SSL.md | 5 ++++- example.php | 2 +- src/Store/PdoMysql.php | 23 ++++++++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/docs/Connecting-to-MySQL-via-SSL.md b/docs/Connecting-to-MySQL-via-SSL.md index ff1668d..c76be42 100644 --- a/docs/Connecting-to-MySQL-via-SSL.md +++ b/docs/Connecting-to-MySQL-via-SSL.md @@ -5,9 +5,12 @@ $sslCa = "/path/to/ca"; $sslKey = "/path/to/Key"; $sslCert = "/path/to/cert"; +$sslCaPath = "/path"; +$sslCipher = "DHE-RSA-AES256-SHA:AES128-SHA"; +$verifySsl = 0; // Since PHP 7.1 $db = \ByJG\AnyDataset\Factory::getDbRelationalInstance( - "mysql://localhost/database?ca=$sslCa&key=$sslKey&cert=$sslCert" + "mysql://localhost/database?ca=$sslCa&key=$sslKey&cert=$sslCert&capath=$sslCaPath&verifyssl=$verifySsl&cipher=$sslCipher" ); $iterator = $db->getIterator('select * from table where field = :value', ['value' => 10]); diff --git a/example.php b/example.php index 0a10b60..e8d8079 100644 --- a/example.php +++ b/example.php @@ -1,7 +1,7 @@ getIterator('select * from airports where idairports = [[idairports]]', ['idairports' => 898]); diff --git a/src/Store/PdoMysql.php b/src/Store/PdoMysql.php index ba38514..502d61b 100644 --- a/src/Store/PdoMysql.php +++ b/src/Store/PdoMysql.php @@ -8,6 +8,15 @@ class PdoMysql extends DbPdoDriver { + protected $mysqlAttr = [ + "ca" => PDO::MYSQL_ATTR_SSL_CA, + "capath" => PDO::MYSQL_ATTR_SSL_CAPATH, + "cert" => PDO::MYSQL_ATTR_SSL_CERT, + "key" => PDO::MYSQL_ATTR_SSL_KEY, + "cipher" => PDO::MYSQL_ATTR_SSL_CIPHER, + "verifyssl" => 1014 // PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT (>=7.1) + ]; + /** * PdoMysql constructor. * @@ -25,13 +34,13 @@ public function __construct(Uri $connUri) PDO::ATTR_EMULATE_PREPARES => true ]; - $sslCa = $connUri->getQueryPart("ca"); - $sslCert = $connUri->getQueryPart("cert"); - $sslKey = $connUri->getQueryPart("key"); - if (!empty($sslCa) && !empty($sslCert) && !empty($sslKey)) { - $preOptions[PDO::MYSQL_ATTR_SSL_KEY] = urldecode($sslKey); - $preOptions[PDO::MYSQL_ATTR_SSL_CERT] = urldecode($sslCert); - $preOptions[PDO::MYSQL_ATTR_SSL_CA] = urldecode($sslCa); + if (!empty($connUri->getQuery())) { + foreach ($this->mysqlAttr as $key => $property) { + $value = $connUri->getQueryPart("key"); + if (!empty($value)) { + $preOptions[$property] = urldecode($value); + } + } } $this->setSupportMultRowset(true); From f920454aadbd51aeb889440a2239631286fe1c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joao=20Gilberto=20Magalh=C3=A3es?= Date: Sun, 1 Jul 2018 14:39:28 -0300 Subject: [PATCH 3/3] Some changes in the MySQL connection via SSL. --- docs/Connecting-to-MySQL-via-SSL.md | 4 +++- src/Store/PdoMysql.php | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/Connecting-to-MySQL-via-SSL.md b/docs/Connecting-to-MySQL-via-SSL.md index c76be42..8154f58 100644 --- a/docs/Connecting-to-MySQL-via-SSL.md +++ b/docs/Connecting-to-MySQL-via-SSL.md @@ -1,5 +1,7 @@ # Connecting To MySQL via SSL +(Read here https://gist.github.com/byjg/860065a828150caf29c20209ecbd5692 about create server mysql) + ```php getQuery())) { foreach ($this->mysqlAttr as $key => $property) { - $value = $connUri->getQueryPart("key"); + $value = $connUri->getQueryPart($key); if (!empty($value)) { - $preOptions[$property] = urldecode($value); + $prepValue = urldecode($value); + if ($prepValue === 'false') { + $prepValue = false; + } else if ($prepValue === 'true') { + $prepValue = true; + } + $preOptions[$property] = $prepValue; } } }