From c88a1dc32df5abb9bb4131bb90d681cbf9c76839 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Fri, 13 Dec 2024 11:52:17 +1100 Subject: [PATCH 1/5] handle host detector with custom error handler and open_basedir if open_basedir is configured such that files that it tries to open are denied with a php warning, and a custom error handler is installed which converts warnings to exceptions, an unhandled exception occurs. add a test for this, and some try/catch/ignore blocks around the code that can trigger this. --- src/SDK/Resource/Detectors/Host.php | 23 +++++++++---- .../test_host_detector_with_open_basedir.phpt | 34 +++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 3a353cc67..d64296c34 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -9,6 +9,7 @@ use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SemConv\ResourceAttributes; use function php_uname; +use Throwable; /** * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/resource/semantic_conventions/host.md#host @@ -53,10 +54,15 @@ private function getLinuxId(): ?string foreach ($paths as $path) { $file = $this->dir . $path; - if (is_file($file) && is_readable($file)) { - $contents = file_get_contents($file); - return $contents !== false ? trim($contents) : null; + try { + if (is_file($file) && is_readable($file)) { + $contents = file_get_contents($file); + + return $contents !== false ? trim($contents) : null; + } + } catch (Throwable $t) { + //do nothing } } @@ -66,10 +72,15 @@ private function getLinuxId(): ?string private function getBsdId(): ?string { $file = $this->dir . self::PATH_ETC_HOSTID; - if (is_file($file) && is_readable($file)) { - $contents = file_get_contents($file); - return $contents !== false ? trim($contents) : null; + try { + if (is_file($file) && is_readable($file)) { + $contents = file_get_contents($file); + + return $contents !== false ? trim($contents) : null; + } + } catch (Throwable $t) { + //do nothing } $out = exec('which kenv && kenv -q smbios.system.uuid'); diff --git a/tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt b/tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt new file mode 100644 index 000000000..0e92cb168 --- /dev/null +++ b/tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt @@ -0,0 +1,34 @@ +--TEST-- +Host detector with custom error handler and open_basedir +--DESCRIPTION-- +An error handler is installed which converts PHP warnings to exceptions, and open_basedir +is configured such that /etc/machine-id and friends cannot be read +--SKIPIF-- + +--ENV-- +OTEL_PHP_FIBERS_ENABLED=0 +--INI-- +open_basedir=${PWD} +--FILE-- +getResource(); +var_dump($resource->getAttributes()->toArray()); +?> +--EXPECTF-- +array(2) { + ["host.name"]=> + string(%d) "%s" + ["host.arch"]=> + string(%d) "%s" +} From bc5fbb95903fe047527e9ea31eaea3635b375f88 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Sat, 14 Dec 2024 13:21:45 +1100 Subject: [PATCH 2/5] continue on read failure --- src/SDK/Resource/Detectors/Host.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index d64296c34..ee6a00997 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -59,7 +59,11 @@ private function getLinuxId(): ?string if (is_file($file) && is_readable($file)) { $contents = file_get_contents($file); - return $contents !== false ? trim($contents) : null; + if ($contents === false) { + continue; + } + + return trim($contents); } } catch (Throwable $t) { //do nothing @@ -77,7 +81,9 @@ private function getBsdId(): ?string if (is_file($file) && is_readable($file)) { $contents = file_get_contents($file); - return $contents !== false ? trim($contents) : null; + if ($contents !== false) { + return trim($contents); + } } } catch (Throwable $t) { //do nothing From bb75c6c26869880a2e424d739fb789d30150158b Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Thu, 19 Dec 2024 16:38:25 +1100 Subject: [PATCH 3/5] disable error handler instead of catch --- src/SDK/Resource/Detectors/Host.php | 50 ++++++++++++++++------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index ee6a00997..decf44af5 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -9,7 +9,6 @@ use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SemConv\ResourceAttributes; use function php_uname; -use Throwable; /** * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/resource/semantic_conventions/host.md#host @@ -48,6 +47,27 @@ private function getMachineId(): ?string }; } + /** + * @phan-suppress PhanTypeMismatchArgumentInternal + */ + private function readFile(string $file): string|false + { + set_error_handler(static fn () => true); + + try { + if (is_file($file) && is_readable($file)) { + $contents = file_get_contents($file); + if ($contents !== false) { + return $contents; + } + } + } finally { + restore_error_handler(); + } + + return false; + } + private function getLinuxId(): ?string { $paths = [self::PATH_ETC_MACHINEID, self::PATH_VAR_LIB_DBUS_MACHINEID]; @@ -55,18 +75,9 @@ private function getLinuxId(): ?string foreach ($paths as $path) { $file = $this->dir . $path; - try { - if (is_file($file) && is_readable($file)) { - $contents = file_get_contents($file); - - if ($contents === false) { - continue; - } - - return trim($contents); - } - } catch (Throwable $t) { - //do nothing + $contents = $this->readFile($file); + if ($contents !== false) { + return $contents; } } @@ -77,16 +88,9 @@ private function getBsdId(): ?string { $file = $this->dir . self::PATH_ETC_HOSTID; - try { - if (is_file($file) && is_readable($file)) { - $contents = file_get_contents($file); - - if ($contents !== false) { - return trim($contents); - } - } - } catch (Throwable $t) { - //do nothing + $contents = $this->readFile($file); + if ($contents !== false) { + return $contents; } $out = exec('which kenv && kenv -q smbios.system.uuid'); From 86deb4ed5591cc182c3629b7006295cb3c01034e Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Thu, 19 Dec 2024 20:40:53 +1100 Subject: [PATCH 4/5] trim --- src/SDK/Resource/Detectors/Host.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index decf44af5..73c7afa86 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -55,17 +55,12 @@ private function readFile(string $file): string|false set_error_handler(static fn () => true); try { - if (is_file($file) && is_readable($file)) { - $contents = file_get_contents($file); - if ($contents !== false) { - return $contents; - } - } + $contents = file_get_contents($file); + + return $contents !== false ? trim($contents) : false; } finally { restore_error_handler(); } - - return false; } private function getLinuxId(): ?string From a52aa40a4db53f8955fc0949ebebde1dff12feca Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Fri, 20 Dec 2024 18:56:18 +1100 Subject: [PATCH 5/5] Update tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt Co-authored-by: Tobias Bachert --- .../Detectors/test_host_detector_with_open_basedir.phpt | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt b/tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt index 0e92cb168..ef8dbc4ef 100644 --- a/tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt +++ b/tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt @@ -5,8 +5,6 @@ An error handler is installed which converts PHP warnings to exceptions, and ope is configured such that /etc/machine-id and friends cannot be read --SKIPIF-- ---ENV-- -OTEL_PHP_FIBERS_ENABLED=0 --INI-- open_basedir=${PWD} --FILE--