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

FOUN-1871: Support mysql connection over tls based on config #12592

Open
wants to merge 5 commits into
base: Tucana-20.10.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions alpha/apps/kaltura/lib/db/DbManager.php
Expand Up @@ -150,13 +150,13 @@ public static function shutdown()
/**
* @return KalturaPDO
*/
public static function createSphinxConnection($sphinxServer, $port = 9312)
public static function createSphinxConnection($sphinxServer, $port = 9312, $userName = null, $password = null, $driver_options = array(), $config_key = null)
{
$dsn = "mysql:host=$sphinxServer;port=$port;";

try
{
$con = new KalturaPDO($dsn);
$con = new KalturaPDO($dsn, $userName, $password, $driver_options, $config_key);
$con->setCommentsEnabled(false);
return $con;
}
Expand Down Expand Up @@ -373,12 +373,15 @@ protected static function getPreferredSphinxIndexByWeight($hostToLag, $hostToInd

private static function getSphinxConnectionInternal($key, $connectTimeout, $indexName)
{
if(!isset(self::$config['datasources'][$key]['connection']['dsn']))
$conparams = self::$config['datasources'][$key]['connection'];
if(!isset($conparams['dsn']))
throw new Exception("DB Config [$key] not found");

$dataSource = self::$config['datasources'][$key]['connection']['dsn'];

$dataSource = $conparams['dsn'];
$user = isset($conparams['user']) ? $conparams['user'] : null;
$password = isset($conparams['password']) ? $conparams['password'] : null;
self::$sphinxConnection[$indexName] =
new KalturaPDO($dataSource, null, null, array(PDO::ATTR_TIMEOUT => $connectTimeout, KalturaPDO::KALTURA_ATTR_NAME => $key), $key);
new KalturaPDO($dataSource, $user, $password, array(PDO::ATTR_TIMEOUT => $connectTimeout, KalturaPDO::KALTURA_ATTR_NAME => $key), $key);
self::$sphinxConnection[$indexName]->setCommentsEnabled(false);

return self::$sphinxConnection[$indexName];
Expand Down
16 changes: 15 additions & 1 deletion alpha/apps/kaltura/lib/db/KalturaPDO.php
Expand Up @@ -16,10 +16,14 @@ class KalturaPDO extends PropelPDO
*/
const KALTURA_ATTR_NO_TRANSACTION = 'noTransaction';

const KALTURA_ATTR_SSL_CA = 'sslCa';

const KALTURA_ATTR_SSL_VERIFY_SERVER_CERT = 'verifyServerCert';

/**
* Sets the number of retries of doSave()
*/
const SAVE_MAX_RETRIES = 4;
const SAVE_MAX_RETRIES = 4;

protected static $comment = null;
protected $kalturaOptions = array();
Expand All @@ -39,6 +43,16 @@ public function __construct($dsn, $username = null, $password = null, $driver_op
$this->kalturaOptions = DbManager::getKalturaConfig($this->connectionName);
}

if(isset($this->kalturaOptions[KalturaPDO::KALTURA_ATTR_SSL_CA]))
{
$driver_options[PDO::MYSQL_ATTR_SSL_CA] = $this->getKalturaOption(KalturaPDO::KALTURA_ATTR_SSL_CA);
}

if(isset($this->kalturaOptions[KalturaPDO::KALTURA_ATTR_SSL_VERIFY_SERVER_CERT]))
{
$driver_options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = $this->getKalturaOption(KalturaPDO::KALTURA_ATTR_SSL_VERIFY_SERVER_CERT);
}

list($mysql, $connection) = explode(':', $dsn);
$arguments = explode(';', $connection);
foreach($arguments as $argument)
Expand Down
16 changes: 11 additions & 5 deletions alpha/scripts/sphinxCompatCheck.php
@@ -1,12 +1,18 @@
<?php

function createSphinxConnection($sphinxServer, $port = 9312)
function createSphinxConnection($sphinxServer, $port = 9312, $user = null, $pass = null, $tlsEnabled = false)
{
$dsn = "mysql:host=$sphinxServer;port=$port;";
$driver_options = array();
if($tlsEnabled)
{
$driver_options[PDO::MYSQL_ATTR_SSL_CA] = true;
$driver_options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = false;
}

try
{
$con = new PDO($dsn);
$con = new PDO($dsn, $user, $pass, $driver_options);
return $con;
}
catch(PropelException $pex)
Expand Down Expand Up @@ -35,10 +41,10 @@ function addQuotes($str)
}

if ($argc < 3)
die("Usage:\n\tphp sphinxCompatCheck <sphinx1 host> <sphinx1 port> <sphinx2 host> <sphinx2 port>\n");
die("Usage:\n\tphp sphinxCompatCheck <sphinx1 host> <sphinx1 port> <sphinx2 host> <sphinx2 port> <sphinx user> <sphinx password> <tls enabled>\n");

$conn1 = createSphinxConnection($argv[1], $argv[2]);
$conn2 = createSphinxConnection($argv[3], $argv[4]);
$conn1 = createSphinxConnection($argv[1], $argv[2], $argv[5], $argv[6], $argv[7]);
$conn2 = createSphinxConnection($argv[3], $argv[4], $argv[5], $argv[6], $argv[7]);
$strictMode = false;

$serverTime1 = 0;
Expand Down
89 changes: 83 additions & 6 deletions plugins/search/providers/sphinx_search/scripts/populateFromLog.php
Expand Up @@ -70,13 +70,14 @@ function filter($i) {
$splitIndexSettings = $dbConf['sphinx_split_index'];
}

$limit = 1000; // The number of sphinxLog records we want to query
$gap = 500; // The gap from 'getLastLogId' we want to query
$limit = 1000; // The number of sphinxLog records we want to query
$maxIndexHistory = 2000; //The maximum array size to save unique object ids update and their sphinx log id

$sphinxReadConn = myDbHelper::getConnection(myDbHelper::DB_HELPER_CONN_SPHINX_LOG_READ);

$serverLastLogs = SphinxLogServerPeer::retrieveByServer($sphinxServer, $sphinxReadConn);
list($sphinxUser, $sphinxPassword, $dataSourceKey) = getSphinxConnParams($sphinxServer, $dbConf);

$lastLogs = array();
$handledRecords = array();
$sphinxRtTables = array();
Expand All @@ -102,7 +103,8 @@ function filter($i) {
$sphinxCon = null;
try
{
$sphinxCon = DbManager::createSphinxConnection($sphinxServer,$sphinxPort);
$sphinxCon = DbManager::createSphinxConnection($sphinxServer, $sphinxPort, $sphinxUser, $sphinxPassword,
array(KalturaPDO::KALTURA_ATTR_NAME => $dataSourceKey), $dataSourceKey);
if(!count($sphinxRtTables))
{
$sphinxRtTables = getSphinxRtTables($sphinxCon);
Expand All @@ -115,7 +117,7 @@ function filter($i) {
sleep(5);
continue;
}

foreach($sphinxLogs as $sphinxLog)
{
/* @var $sphinxLog SphinxLog */
Expand Down Expand Up @@ -151,7 +153,7 @@ function filter($i) {

$handledRecords[$dc][] = $sphinxLogId;
KalturaLog::log("Sphinx log id $sphinxLogId dc [$dc] executed server id [$executedServerId] Memory: [" . memory_get_usage() . "]");

try
{
$objectId = $sphinxLog->getObjectId();
Expand Down Expand Up @@ -220,7 +222,7 @@ function filter($i) {
}

unset($sphinxCon);

SphinxLogPeer::clearInstancePool();
}

Expand All @@ -242,3 +244,78 @@ function getSphinxRtTables($sphinxCon)

return $sphinxRtTables;
}

function getSphinxConnParams($sphinxServer, $dbConf)
{
$sphinxDataSources = isset($dbConf['sphinx_datasources']['datasources']) ?
$dbConf['sphinx_datasources']['datasources'] :
array(DbManager::DB_CONFIG_SPHINX);


$sphinxServerIps = array();
$sphinxServerDnsRecords = dns_get_record($sphinxServer);
foreach ($sphinxServerDnsRecords as $sphinxServerDnsRecord)
{
if(trim($sphinxServerDnsRecord['ip']) == '')
{
continue;
}

$sphinxServerIps[] = $sphinxServerDnsRecord['ip'];
}


foreach ($sphinxDataSources as $sphinxDataSource)
{
if(!isset($dbConf['datasources'][$sphinxDataSource]))
{
KalturaLog::log("Sphinx source [$sphinxDataSource] not found in datasource list");
continue;
}

$confParams = $dbConf['datasources'][$sphinxDataSource]['connection'];
$sphinxDataSourceDsn = $confParams['dsn'];
list($mysql, $connectionStr) = explode(':', $sphinxDataSourceDsn);

$host = null;
$connectionArguments = explode(';', $connectionStr);
foreach($connectionArguments as $connectionArgument)
{
if(trim($connectionArgument) == '')
{
continue;
}

list($argumentName, $argumentValue) = explode('=', $connectionArgument);
if(strtolower($argumentName) == 'host')
{
$host = $argumentValue;
}
}

if(!$host)
{
KalturaLog::log("Failed to find host for sphinx source [$sphinxDataSource]");
continue;
}

$hostIps = array();
$hostRecords = dns_get_record($host);
foreach ($hostRecords as $hostRecord)
{
if(trim($hostRecord['ip']) == '')
{
continue;
}

$hostIps[] = $hostRecord['ip'];
}

if(count(array_intersect($sphinxServerIps, $hostIps)))
{
return array($confParams['user'], $confParams['password'], $sphinxDataSource);
}
}

return array(null, null, null);
}