From cfdbc8ae625d6c4a9ee2e078b295c9b66ae5a753 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Thu, 23 Sep 2021 11:59:34 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B04215=20When=20checking=20for=20TLS=20cnx?= =?UTF-8?q?,=20don't=20set=20anymore=20CMDBSource=20mysql=20attributes=20!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/cmdbsource.class.inc.php | 36 ++++++++++++++----------- test/core/CMDBSource/CMDBSourceTest.php | 2 +- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/core/cmdbsource.class.inc.php b/core/cmdbsource.class.inc.php index 2038e848f..ba1bb5f61 100644 --- a/core/cmdbsource.class.inc.php +++ b/core/cmdbsource.class.inc.php @@ -241,6 +241,8 @@ class CMDBSource * * @return \mysqli * @throws \MySQLException + * + * @uses IsOpenedDbConnectionUsingTls when asking for a TLS connection, to check if it was really opened using TLS */ public static function GetMysqliInstance( $sDbHost, $sUser, $sPwd, $sSource = '', $bTlsEnabled = false, $sTlsCa = null, $bCheckTlsAfterConnection = false @@ -343,41 +345,41 @@ class CMDBSource * parameters were used.
* This method can be called to ensure that the DB connection really uses TLS. * - *

We're using this object connection : {@link self::$m_oMysqli} + *

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 !
+ * This is the case for example with {@see \iTopMutex} ! * * @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 * + * @used-by GetMysqliInstance * @uses IsMySqlVarNonEmpty + * @uses 'ssl_version' MySQL var + * @uses 'ssl_cipher' MySQL var */ private static function IsOpenedDbConnectionUsingTls($oMysqli) { - if (is_null(self::GetMySQLiForQuery())) - { - self::SetMySQLiForQuery($oMysqli); - } - - $bNonEmptySslVersionVar = self::IsMySqlVarNonEmpty('ssl_version'); - $bNonEmptySslCipherVar = self::IsMySqlVarNonEmpty('ssl_cipher'); + $bNonEmptySslVersionVar = self::IsMySqlVarNonEmpty('ssl_version', $oMysqli); + $bNonEmptySslCipherVar = self::IsMySqlVarNonEmpty('ssl_cipher', $oMysqli); return ($bNonEmptySslVersionVar && $bNonEmptySslCipherVar); } /** * @param string $sVarName + * @param mysqli $oMysqli connection to use for the query * * @return bool * @throws \MySQLException - * - * @uses SHOW STATUS queries + * @uses 'SHOW SESSION STATUS' queries */ - private static function IsMySqlVarNonEmpty($sVarName) + private static function IsMySqlVarNonEmpty($sVarName, $oMysqli) { try { - $sResult = self::QueryToScalar("SHOW SESSION STATUS LIKE '$sVarName'", 1); + $sResult = self::QueryToScalar("SHOW SESSION STATUS LIKE '$sVarName'", 1, $oMysqli); } catch (MySQLQueryHasNoResultException $e) { @@ -973,17 +975,21 @@ class CMDBSource /** * @param string $sSql * @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 * @throws \MySQLException * @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(); try { - $oResult = self::GetMySQLiForQuery()->query($sSql); + $oResult = $oMysqliToQuery->query($sSql); } catch(mysqli_sql_exception $e) { diff --git a/test/core/CMDBSource/CMDBSourceTest.php b/test/core/CMDBSource/CMDBSourceTest.php index 7f118f6ef..592786b02 100644 --- a/test/core/CMDBSource/CMDBSourceTest.php +++ b/test/core/CMDBSource/CMDBSourceTest.php @@ -128,7 +128,7 @@ class CMDBSourceTest extends ItopTestCase CMDBSource::InitFromConfig($oConfig); $oMysqli = CMDBSource::GetMysqli(); - // resets \CMDBSource::$oMySQLiForQuery to simulate call to \CMDBSource::Init with a LTS connexion + // 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"