From 34e2c03d0dc6c9b54b54ad93ae5bac3295e1b982 Mon Sep 17 00:00:00 2001 From: jbostoen <6421683+jbostoen@users.noreply.github.com> Date: Mon, 15 Jan 2024 19:26:13 +0100 Subject: [PATCH 1/8] JsonCollector: Introduce GetOptionalHeaders() and GetCurlOptions() methods. --- core/jsoncollector.class.inc.php | 52 ++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/core/jsoncollector.class.inc.php b/core/jsoncollector.class.inc.php index c9c472a..b2f12ec 100644 --- a/core/jsoncollector.class.inc.php +++ b/core/jsoncollector.class.inc.php @@ -141,12 +141,13 @@ public function Prepare() } else { $aDataGet = []; } - $iSynchroTimeout = (int)Utils::GetConfigurationValue('itop_synchro_timeout', 600); // timeout in seconds, for a synchro to run - $aCurlOptions = Utils::GetCurlOptions($iSynchroTimeout); + + $aCurlOptions = static::GetCurlOptions(); + //logs Utils::Log(LOG_DEBUG, 'Source aDataGet: '.json_encode($aDataGet)); - $this->sFileJson = Utils::DoPostRequest($this->sURL, $aDataGet, '', $aResponseHeaders, $aCurlOptions); + $this->sFileJson = Utils::DoPostRequest($this->sURL, $aDataGet, static::GetOptionalHeaders(), $aResponseHeaders, $aCurlOptions); Utils::Log(LOG_DEBUG, 'Source sFileJson: '.$this->sFileJson); Utils::Log(LOG_INFO, 'Synchro URL (target): '.Utils::GetConfigurationValue('itop_url', array())); } else { @@ -258,6 +259,51 @@ public function Fetch() return false; } + + /** + * Returns optional HTTP headers which can be sent to the specified URL. + * For example, a subclass can be override this method to send an Authorization: header. + */ + public function GetOptionalHeaders() : string + { + + return ''; + + } + + /** + * Returns cURL options to use. + * For example, a subclass can be override this method to add the CURLOPT_USERPWD and a value. + */ + public function GetCurlOptions() : array + { + + $iSynchroTimeout = (int)Utils::GetConfigurationValue('itop_synchro_timeout', 600); // timeout in seconds, for a synchro to run + $aParamsSourceJson = $this->aCollectorConfig; + + // Step 1: + // Use cURL options from "config" XML. + $aConfigCurlOptions = Utils::GetCurlOptions($iSynchroTimeout); + + // Step 2: + // If available: Add or update options defined in the "collector" XML. + // Apply the same mechanism to convert constants. The flexibility in place is to allow adding and overriding values, so do not pass on synchro timeout. + $aCollectorCurlOptions = array(); + + if(isset($aParamsSourceJson['curl_options'])) { + $aCollectorCurlOptions = $aParamsSourceJson['curl_options']; + } + if(isset($aParamsSourceJson['CURL_OPTIONS'])) { + $aCollectorCurlOptions = $aParamsSourceJson['CURL_OPTIONS']; + } + + if(count($aCollectorCurlOptions) > 0) { + $aCollectorCurlOptions = utils::GetCurlOptions($aCollectorCurlOptions, -1); + } + + return array_replace($aConfigCurlOptions, $aCollectorCurlOptions); + + } /** * @param array $aData From cdd4a745e844d7c2d5c8d7ef15b1c378ea520e68 Mon Sep 17 00:00:00 2001 From: Molkobain Date: Tue, 16 Jan 2024 10:01:35 +0100 Subject: [PATCH 2/8] Apply suggestions from code review --- core/jsoncollector.class.inc.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/jsoncollector.class.inc.php b/core/jsoncollector.class.inc.php index b2f12ec..95e48fa 100644 --- a/core/jsoncollector.class.inc.php +++ b/core/jsoncollector.class.inc.php @@ -261,23 +261,20 @@ public function Fetch() } /** - * Returns optional HTTP headers which can be sent to the specified URL. + * @return Optional HTTP headers which can be sent to the specified URL. * For example, a subclass can be override this method to send an Authorization: header. */ public function GetOptionalHeaders() : string { - return ''; - } /** - * Returns cURL options to use. + * @return cURL options to use. * For example, a subclass can be override this method to add the CURLOPT_USERPWD and a value. */ public function GetCurlOptions() : array { - $iSynchroTimeout = (int)Utils::GetConfigurationValue('itop_synchro_timeout', 600); // timeout in seconds, for a synchro to run $aParamsSourceJson = $this->aCollectorConfig; @@ -302,7 +299,6 @@ public function GetCurlOptions() : array } return array_replace($aConfigCurlOptions, $aCollectorCurlOptions); - } /** From 3037fb7e2801474e1dfaff4ca69a1c7752ad9e18 Mon Sep 17 00:00:00 2001 From: jbostoen <6421683+jbostoen@users.noreply.github.com> Date: Wed, 17 Jan 2024 13:17:26 +0100 Subject: [PATCH 3/8] Update core/jsoncollector.class.inc.php Co-authored-by: Thomas Casteleyn --- core/jsoncollector.class.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/jsoncollector.class.inc.php b/core/jsoncollector.class.inc.php index 95e48fa..fcbbd9d 100644 --- a/core/jsoncollector.class.inc.php +++ b/core/jsoncollector.class.inc.php @@ -295,7 +295,7 @@ public function GetCurlOptions() : array } if(count($aCollectorCurlOptions) > 0) { - $aCollectorCurlOptions = utils::GetCurlOptions($aCollectorCurlOptions, -1); + $aCollectorCurlOptions = Utils::GetCurlOptions($aCollectorCurlOptions, -1); } return array_replace($aConfigCurlOptions, $aCollectorCurlOptions); From ccba487664ed47f9f2675eabf24bccf20f2abc02 Mon Sep 17 00:00:00 2001 From: jbostoen <6421683+jbostoen@users.noreply.github.com> Date: Wed, 17 Jan 2024 13:17:31 +0100 Subject: [PATCH 4/8] Update core/jsoncollector.class.inc.php Co-authored-by: Thomas Casteleyn --- core/jsoncollector.class.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/jsoncollector.class.inc.php b/core/jsoncollector.class.inc.php index fcbbd9d..ff209f9 100644 --- a/core/jsoncollector.class.inc.php +++ b/core/jsoncollector.class.inc.php @@ -142,7 +142,7 @@ public function Prepare() $aDataGet = []; } - $aCurlOptions = static::GetCurlOptions(); + $aCurlOptions = $this->GetCurlOptions(); //logs From b668ca5dfa73583657da4022ccc66c21ac287647 Mon Sep 17 00:00:00 2001 From: jbostoen <6421683+jbostoen@users.noreply.github.com> Date: Wed, 17 Jan 2024 13:17:42 +0100 Subject: [PATCH 5/8] Update core/jsoncollector.class.inc.php Co-authored-by: Thomas Casteleyn --- core/jsoncollector.class.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/jsoncollector.class.inc.php b/core/jsoncollector.class.inc.php index ff209f9..2c519ff 100644 --- a/core/jsoncollector.class.inc.php +++ b/core/jsoncollector.class.inc.php @@ -147,7 +147,7 @@ public function Prepare() //logs Utils::Log(LOG_DEBUG, 'Source aDataGet: '.json_encode($aDataGet)); - $this->sFileJson = Utils::DoPostRequest($this->sURL, $aDataGet, static::GetOptionalHeaders(), $aResponseHeaders, $aCurlOptions); + $this->sFileJson = Utils::DoPostRequest($this->sURL, $aDataGet, $this->GetOptionalHeaders(), $aResponseHeaders, $aCurlOptions); Utils::Log(LOG_DEBUG, 'Source sFileJson: '.$this->sFileJson); Utils::Log(LOG_INFO, 'Synchro URL (target): '.Utils::GetConfigurationValue('itop_url', array())); } else { From 7794995749cecb086f7ad1782d404f48af68b6f1 Mon Sep 17 00:00:00 2001 From: jbostoen <6421683+jbostoen@users.noreply.github.com> Date: Thu, 18 Jan 2024 10:07:00 +0100 Subject: [PATCH 6/8] Apply suggestions, fixes --- core/jsoncollector.class.inc.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/jsoncollector.class.inc.php b/core/jsoncollector.class.inc.php index 2c519ff..aa647fb 100644 --- a/core/jsoncollector.class.inc.php +++ b/core/jsoncollector.class.inc.php @@ -262,7 +262,7 @@ public function Fetch() /** * @return Optional HTTP headers which can be sent to the specified URL. - * For example, a subclass can be override this method to send an Authorization: header. + * For example, a subclass can be override this method to send an Authorization: header. {@see \Utils::DoPostRequest() $sOptionnalHeaders parameter for the expected syntax} */ public function GetOptionalHeaders() : string { @@ -295,7 +295,7 @@ public function GetCurlOptions() : array } if(count($aCollectorCurlOptions) > 0) { - $aCollectorCurlOptions = Utils::GetCurlOptions($aCollectorCurlOptions, -1); + $aCollectorCurlOptions = Utils::ComputeCurlOptions($aCollectorCurlOptions, -1); } return array_replace($aConfigCurlOptions, $aCollectorCurlOptions); From dc6e1e1e296da4fc38784f69f009dd067a879b8e Mon Sep 17 00:00:00 2001 From: jbostoen <6421683+jbostoen@users.noreply.github.com> Date: Thu, 18 Jan 2024 11:02:19 +0100 Subject: [PATCH 7/8] Apply suggestion --- core/jsoncollector.class.inc.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/jsoncollector.class.inc.php b/core/jsoncollector.class.inc.php index aa647fb..1c1bcec 100644 --- a/core/jsoncollector.class.inc.php +++ b/core/jsoncollector.class.inc.php @@ -294,9 +294,7 @@ public function GetCurlOptions() : array $aCollectorCurlOptions = $aParamsSourceJson['CURL_OPTIONS']; } - if(count($aCollectorCurlOptions) > 0) { - $aCollectorCurlOptions = Utils::ComputeCurlOptions($aCollectorCurlOptions, -1); - } + $aCollectorCurlOptions = Utils::ComputeCurlOptions($aCollectorCurlOptions, -1); return array_replace($aConfigCurlOptions, $aCollectorCurlOptions); } From 0bba4cc2fa07eaf7950b9935fb37ac90e1b9160b Mon Sep 17 00:00:00 2001 From: Molkobain Date: Tue, 6 Feb 2024 11:23:31 +0100 Subject: [PATCH 8/8] Apply suggestions from code review --- core/jsoncollector.class.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/jsoncollector.class.inc.php b/core/jsoncollector.class.inc.php index 1c1bcec..ecaf804 100644 --- a/core/jsoncollector.class.inc.php +++ b/core/jsoncollector.class.inc.php @@ -261,7 +261,7 @@ public function Fetch() } /** - * @return Optional HTTP headers which can be sent to the specified URL. + * @return Optional HTTP headers which can be sent to the specified URL. {@see \Utils::DoPostRequest() $sOptionnalHeaders parameter for the expected synthax} * For example, a subclass can be override this method to send an Authorization: header. {@see \Utils::DoPostRequest() $sOptionnalHeaders parameter for the expected syntax} */ public function GetOptionalHeaders() : string