mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 18:48:51 +02:00
Compare commits
5 Commits
3.0.0-beta
...
2.7.5-2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec1dcc8df6 | ||
|
|
47ed863da9 | ||
|
|
88290f9e91 | ||
|
|
cfdbc8ae62 | ||
|
|
aaa8f6d311 |
@@ -154,6 +154,16 @@ class CMDBSource
|
|||||||
|
|
||||||
/** @var mysqli $m_oMysqli */
|
/** @var mysqli $m_oMysqli */
|
||||||
protected static $m_oMysqli;
|
protected static $m_oMysqli;
|
||||||
|
/**
|
||||||
|
* The mysqli object is really hard to mock ! This attribute is used only in certain methods, so that we can mock only a very little subset of the mysqli object.
|
||||||
|
* We are setting it in {@see Init}, by default it is a copy of {@see $m_oMysqli}
|
||||||
|
* The mock can be injected using the setter {@see SetMySQLiForQuery}
|
||||||
|
*
|
||||||
|
* @var mysqli $oMySQLiForQuery
|
||||||
|
* @see GetMySQLiForQuery
|
||||||
|
* @see SetMySQLiForQuery
|
||||||
|
* @since 2.7.5 N°3513 new var to allow mock in tests ({@see \Combodo\iTop\Test\UnitTest\Core\TransactionsTest})
|
||||||
|
*/
|
||||||
protected static $oMySQLiForQuery;
|
protected static $oMySQLiForQuery;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -235,6 +245,8 @@ class CMDBSource
|
|||||||
*
|
*
|
||||||
* @return \mysqli
|
* @return \mysqli
|
||||||
* @throws \MySQLException
|
* @throws \MySQLException
|
||||||
|
*
|
||||||
|
* @uses IsOpenedDbConnectionUsingTls when asking for a TLS connection, to check if it was really opened using TLS
|
||||||
*/
|
*/
|
||||||
public static function GetMysqliInstance(
|
public static function GetMysqliInstance(
|
||||||
$sDbHost, $sUser, $sPwd, $sSource = '', $bTlsEnabled = false, $sTlsCa = null, $bCheckTlsAfterConnection = false
|
$sDbHost, $sUser, $sPwd, $sSource = '', $bTlsEnabled = false, $sTlsCa = null, $bCheckTlsAfterConnection = false
|
||||||
@@ -337,41 +349,41 @@ class CMDBSource
|
|||||||
* parameters were used.<br>
|
* parameters were used.<br>
|
||||||
* This method can be called to ensure that the DB connection really uses TLS.
|
* This method can be called to ensure that the DB connection really uses TLS.
|
||||||
*
|
*
|
||||||
* <p>We're using this object connection : {@link self::$m_oMysqli}
|
* <p>We're using our own mysqli instance to do the check as this check is done when creating the mysqli instance : the consumer
|
||||||
|
* might want a dedicated object, and if so we don't want to overwrite the one saved in CMDBSource !<br>
|
||||||
|
* This is the case for example with {@see \iTopMutex} !
|
||||||
*
|
*
|
||||||
* @param \mysqli $oMysqli
|
* @param \mysqli $oMysqli
|
||||||
*
|
*
|
||||||
* @return boolean true if the connection was really established using TLS
|
* @return boolean true if the connection was really established using TLS, false otherwise
|
||||||
* @throws \MySQLException
|
* @throws \MySQLException
|
||||||
*
|
*
|
||||||
|
* @used-by GetMysqliInstance
|
||||||
* @uses IsMySqlVarNonEmpty
|
* @uses IsMySqlVarNonEmpty
|
||||||
|
* @uses 'ssl_version' MySQL var
|
||||||
|
* @uses 'ssl_cipher' MySQL var
|
||||||
*/
|
*/
|
||||||
private static function IsOpenedDbConnectionUsingTls($oMysqli)
|
private static function IsOpenedDbConnectionUsingTls($oMysqli)
|
||||||
{
|
{
|
||||||
if (self::$m_oMysqli == null)
|
$bNonEmptySslVersionVar = self::IsMySqlVarNonEmpty('ssl_version', $oMysqli);
|
||||||
{
|
$bNonEmptySslCipherVar = self::IsMySqlVarNonEmpty('ssl_cipher', $oMysqli);
|
||||||
self::$m_oMysqli = $oMysqli;
|
|
||||||
}
|
|
||||||
|
|
||||||
$bNonEmptySslVersionVar = self::IsMySqlVarNonEmpty('ssl_version');
|
|
||||||
$bNonEmptySslCipherVar = self::IsMySqlVarNonEmpty('ssl_cipher');
|
|
||||||
|
|
||||||
return ($bNonEmptySslVersionVar && $bNonEmptySslCipherVar);
|
return ($bNonEmptySslVersionVar && $bNonEmptySslCipherVar);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sVarName
|
* @param string $sVarName
|
||||||
|
* @param mysqli $oMysqli connection to use for the query
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws \MySQLException
|
* @throws \MySQLException
|
||||||
*
|
* @uses 'SHOW SESSION STATUS' queries
|
||||||
* @uses SHOW STATUS queries
|
|
||||||
*/
|
*/
|
||||||
private static function IsMySqlVarNonEmpty($sVarName)
|
private static function IsMySqlVarNonEmpty($sVarName, $oMysqli)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$sResult = self::QueryToScalar("SHOW SESSION STATUS LIKE '$sVarName'", 1);
|
$sResult = self::QueryToScalar("SHOW SESSION STATUS LIKE '$sVarName'", 1, $oMysqli);
|
||||||
}
|
}
|
||||||
catch (MySQLQueryHasNoResultException $e)
|
catch (MySQLQueryHasNoResultException $e)
|
||||||
{
|
{
|
||||||
@@ -709,7 +721,7 @@ class CMDBSource
|
|||||||
{
|
{
|
||||||
$aContext = array('query' => $sSql);
|
$aContext = array('query' => $sSql);
|
||||||
|
|
||||||
$iMySqlErrorNo = self::$m_oMysqli->errno;
|
$iMySqlErrorNo = self::GetMySQLiForQuery()->errno;
|
||||||
$aMySqlHasGoneAwayErrorCodes = MySQLHasGoneAwayException::getErrorCodes();
|
$aMySqlHasGoneAwayErrorCodes = MySQLHasGoneAwayException::getErrorCodes();
|
||||||
if (in_array($iMySqlErrorNo, $aMySqlHasGoneAwayErrorCodes))
|
if (in_array($iMySqlErrorNo, $aMySqlHasGoneAwayErrorCodes))
|
||||||
{
|
{
|
||||||
@@ -731,7 +743,7 @@ class CMDBSource
|
|||||||
private static function LogDeadLock(Exception $e)
|
private static function LogDeadLock(Exception $e)
|
||||||
{
|
{
|
||||||
// checks MySQL error code
|
// checks MySQL error code
|
||||||
$iMySqlErrorNo = self::$m_oMysqli->errno;
|
$iMySqlErrorNo = self::GetMySQLiForQuery()->errno;
|
||||||
if (!in_array($iMySqlErrorNo, array(self::MYSQL_ERRNO_WAIT_TIMEOUT, self::MYSQL_ERRNO_DEADLOCK)))
|
if (!in_array($iMySqlErrorNo, array(self::MYSQL_ERRNO_WAIT_TIMEOUT, self::MYSQL_ERRNO_DEADLOCK)))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -739,7 +751,7 @@ class CMDBSource
|
|||||||
|
|
||||||
// Get error info
|
// Get error info
|
||||||
$sUser = UserRights::GetUser();
|
$sUser = UserRights::GetUser();
|
||||||
$oError = self::$m_oMysqli->query('SHOW ENGINE INNODB STATUS');
|
$oError = self::GetMySQLiForQuery()->query('SHOW ENGINE INNODB STATUS');
|
||||||
if ($oError !== false)
|
if ($oError !== false)
|
||||||
{
|
{
|
||||||
$aData = $oError->fetch_all(MYSQLI_ASSOC);
|
$aData = $oError->fetch_all(MYSQLI_ASSOC);
|
||||||
@@ -967,17 +979,21 @@ class CMDBSource
|
|||||||
/**
|
/**
|
||||||
* @param string $sSql
|
* @param string $sSql
|
||||||
* @param int $iCol beginning at 0
|
* @param int $iCol beginning at 0
|
||||||
|
* @param mysqli $oMysqli if not null will query using this connection, otherwise will use {@see GetMySQLiForQuery}
|
||||||
*
|
*
|
||||||
* @return string corresponding cell content on the first line
|
* @return string corresponding cell content on the first line
|
||||||
* @throws \MySQLException
|
* @throws \MySQLException
|
||||||
* @throws \MySQLQueryHasNoResultException
|
* @throws \MySQLQueryHasNoResultException
|
||||||
|
* @since 2.7.5-2 2.7.6 3.0.0 N°4215 new optional mysqli param
|
||||||
*/
|
*/
|
||||||
public static function QueryToScalar($sSql, $iCol = 0)
|
public static function QueryToScalar($sSql, $iCol = 0, $oMysqli = null)
|
||||||
{
|
{
|
||||||
|
$oMysqliToQuery = (is_null($oMysqli)) ? self::GetMySQLiForQuery() : $oMysqli;
|
||||||
|
|
||||||
$oKPI = new ExecutionKPI();
|
$oKPI = new ExecutionKPI();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$oResult = self::GetMySQLiForQuery()->query($sSql);
|
$oResult = $oMysqliToQuery->query($sSql);
|
||||||
}
|
}
|
||||||
catch(mysqli_sql_exception $e)
|
catch(mysqli_sql_exception $e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Combodo\iTop\Test\UnitTest\Core;
|
|||||||
|
|
||||||
use CMDBSource;
|
use CMDBSource;
|
||||||
use Combodo\iTop\Test\UnitTest\ItopTestCase;
|
use Combodo\iTop\Test\UnitTest\ItopTestCase;
|
||||||
|
use utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @since 2.7.0
|
* @since 2.7.0
|
||||||
@@ -115,4 +116,23 @@ class CMDBSourceTest extends ItopTestCase
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \ConfigException
|
||||||
|
* @throws \CoreException
|
||||||
|
* @throws \MySQLException
|
||||||
|
* @since 3.0.0 N°4215
|
||||||
|
*/
|
||||||
|
public function testIsOpenedDbConnectionUsingTls() {
|
||||||
|
$oConfig = utils::GetConfig();
|
||||||
|
CMDBSource::InitFromConfig($oConfig);
|
||||||
|
$oMysqli = CMDBSource::GetMysqli();
|
||||||
|
|
||||||
|
// resets \CMDBSource::$oMySQLiForQuery to simulate call to \CMDBSource::Init with a TLS connexion
|
||||||
|
$this->InvokeNonPublicStaticMethod(CMDBSource::class, 'SetMySQLiForQuery',[null]);
|
||||||
|
|
||||||
|
// before N°4215 fix, this was crashing : "Call to a member function query() on null"
|
||||||
|
$bIsTlsCnx = $this->InvokeNonPublicStaticMethod(CMDBSource::class, 'IsOpenedDbConnectionUsingTls',[$oMysqli]);
|
||||||
|
$this->assertFalse($bIsTlsCnx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user