From 68e6b091fd0d21ac049c404033085db6f38f6d67 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 26 Mar 2024 15:41:58 -0400 Subject: [PATCH 01/21] add bare-metal host ID --- src/SDK/Resource/Detectors/Host.php | 116 ++++++++++++++++++ .../Unit/SDK/Resource/Detectors/HostTest.php | 56 +++++++++ 2 files changed, 172 insertions(+) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index dd2554540..e47651745 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -8,6 +8,7 @@ use OpenTelemetry\SDK\Resource\ResourceDetectorInterface; use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SemConv\ResourceAttributes; +use MachineIdSource; use function php_uname; /** @@ -15,13 +16,128 @@ */ final class Host implements ResourceDetectorInterface { + private string $dir; + + public function __construct(string $dir = '/') + { + $this->dir = $dir; + } + public function getResource(): ResourceInfo { $attributes = [ ResourceAttributes::HOST_NAME => php_uname('n'), ResourceAttributes::HOST_ARCH => php_uname('m'), + ResourceAttributes::HOST_ID => $this->getMachineId(), ]; return ResourceInfo::create(Attributes::create($attributes), ResourceAttributes::SCHEMA_URL); } + + private function getMachineId() + { + switch (strtolower(PHP_OS_FAMILY)) + { + case 'linux': + { + return $this->getLinuxId(); + } + case 'freebsd': + case 'netbsd': + case 'openbsd': + { + return $this->getBsdId(); + } + case 'darwin': + { + $out = $this->getMacOsId(); + return Host::parseMacOsId($out); + } + case 'windows': + { + $out = $this->getWindowsId(); + return Host::parseWindowsId($out); + } + } + + return ''; + } + + private function getLinuxId(): string + { + $paths = ['etc/machine-id', 'var/lib/dbus/machine-id']; + + foreach ($paths as $path) + { + if (file_exists($this->dir . $path)) + { + return trim(file_get_contents($this->dir . $path)); + } + } + + return ''; + } + + private function getBsdId(): string + { + if (file_exists('/etc/hostid')) + { + return trim(file_get_contents('/etc/hostid')); + } + + $out = exec('kenv -q smbios.system.uuid'); + + if ($out != FALSE) + { + return $out; + } + + return ''; + } + + private function getMacOsId(): string + { + $out = exec('ioreg -rd1 -c "IOPlatformExpertDevice"'); + + if ($out != FALSE) + { + return $out; + } + + return ''; + } + + private function getWindowsId(): string + { + $out = exec('%windir%\System32\REG.exe QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography /v MachineGuid'); + + if ($out != FALSE) + { + return $out; + } + + return ''; + } + + public static function parseMacOsId(string $out): string + { + $lines = explode("\n", $out); + + foreach ($lines as $line) + { + if (strpos($line, 'IOPlatformUUID') !== FALSE) + { + $parts = explode('=', $line); + return trim($parts[1]); + } + } + + return ''; + } + + public static function parseWindowsId(string $out): string + { + $parts = explode('REG_SZ', $out); + return trim($parts[1]); + } } diff --git a/tests/Unit/SDK/Resource/Detectors/HostTest.php b/tests/Unit/SDK/Resource/Detectors/HostTest.php index 9264dc0e6..7ec77812d 100644 --- a/tests/Unit/SDK/Resource/Detectors/HostTest.php +++ b/tests/Unit/SDK/Resource/Detectors/HostTest.php @@ -6,6 +6,7 @@ use OpenTelemetry\SDK\Resource\Detectors; use OpenTelemetry\SemConv\ResourceAttributes; +use org\bovigo\vfs\vfsStream; use PHPUnit\Framework\TestCase; /** @@ -21,5 +22,60 @@ public function test_host_get_resource(): void $this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl()); $this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_NAME)); $this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH)); + $this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_ID)); + } + + public function test_host_parse_macos_id(): void + { + $out = 'IOPlatformUUID=1234567890'; + $hostId = Detectors\Host::parseMacOsId($out); + $this->assertIsString($hostId); + $this->assertSame('1234567890', $hostId); + } + + public function test_host_parse_windows_id(): void + { + $out = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\MachineGuid REG_SZ 1234567890'; + $hostId = Detectors\Host::parseWindowsId($out); + $this->assertIsString($hostId); + $this->assertSame('1234567890', $hostId); + } + + /** + * @dataProvider hostIdData + */ + public function test_host_id_linux(array $files, string $expectedId): void + { + $root = vfsStream::setup('/', null, $files); + $resouceDetector = new Detectors\Host($root->url()); + $resource = $resouceDetector->getResource(); + $hostId = $resource->getAttributes()->get(ResourceAttributes::HOST_ID); + $this->assertIsString($hostId); + $this->assertSame($expectedId, $hostId); + } + + public static function hostIdData(): array + { + $etc = [ + 'etc' => [ + 'machine-id' => '1234567890', + ] + ]; + $varLibDbus = [ + 'var' => [ + 'lib' => [ + 'dbus' => [ + 'machine-id' => '0987654321', + ], + ], + ], + ]; + + return [ + [[], ''], + [$etc, '1234567890'], + [array_merge($etc, $varLibDbus), '1234567890'], + [$varLibDbus, '0987654321'], + ]; } } From 4ca3c9f5df969b21bd5a7dbb0f55f3e66371b051 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Tue, 26 Mar 2024 15:58:39 -0400 Subject: [PATCH 02/21] linter nit --- tests/Unit/SDK/Resource/Detectors/HostTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/SDK/Resource/Detectors/HostTest.php b/tests/Unit/SDK/Resource/Detectors/HostTest.php index 7ec77812d..4f43b21dc 100644 --- a/tests/Unit/SDK/Resource/Detectors/HostTest.php +++ b/tests/Unit/SDK/Resource/Detectors/HostTest.php @@ -59,7 +59,7 @@ public static function hostIdData(): array $etc = [ 'etc' => [ 'machine-id' => '1234567890', - ] + ], ]; $varLibDbus = [ 'var' => [ From 626a4b01b75264d472602f69bac14ac1e6bb6796 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 10:28:27 -0400 Subject: [PATCH 03/21] feedback --- src/SDK/Resource/Detectors/Host.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index e47651745..d27cae56a 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -51,12 +51,12 @@ private function getMachineId() case 'darwin': { $out = $this->getMacOsId(); - return Host::parseMacOsId($out); + return self::parseMacOsId($out); } case 'windows': { $out = $this->getWindowsId(); - return Host::parseWindowsId($out); + return self::parseWindowsId($out); } } @@ -125,7 +125,7 @@ public static function parseMacOsId(string $out): string foreach ($lines as $line) { - if (strpos($line, 'IOPlatformUUID') !== FALSE) + if (str_contains($line, 'IOPlatformUUID') !== FALSE) { $parts = explode('=', $line); return trim($parts[1]); From 94591e7f59d34c93903ed7705b1f645dd90c8157 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 10:40:00 -0400 Subject: [PATCH 04/21] linter --- src/SDK/Resource/Detectors/Host.php | 62 +++++++++++++---------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index d27cae56a..1881cbbdc 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -8,7 +8,6 @@ use OpenTelemetry\SDK\Resource\ResourceDetectorInterface; use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SemConv\ResourceAttributes; -use MachineIdSource; use function php_uname; /** @@ -16,7 +15,7 @@ */ final class Host implements ResourceDetectorInterface { - private string $dir; + private readonly string $dir; public function __construct(string $dir = '/') { @@ -36,28 +35,29 @@ public function getResource(): ResourceInfo private function getMachineId() { - switch (strtolower(PHP_OS_FAMILY)) - { + switch (strtolower(PHP_OS_FAMILY)) { case 'linux': - { - return $this->getLinuxId(); - } + { + return $this->getLinuxId(); + } case 'freebsd': case 'netbsd': case 'openbsd': - { - return $this->getBsdId(); - } + { + return $this->getBsdId(); + } case 'darwin': - { - $out = $this->getMacOsId(); - return self::parseMacOsId($out); - } + { + $out = $this->getMacOsId(); + + return self::parseMacOsId($out); + } case 'windows': - { - $out = $this->getWindowsId(); - return self::parseWindowsId($out); - } + { + $out = $this->getWindowsId(); + + return self::parseWindowsId($out); + } } return ''; @@ -67,10 +67,8 @@ private function getLinuxId(): string { $paths = ['etc/machine-id', 'var/lib/dbus/machine-id']; - foreach ($paths as $path) - { - if (file_exists($this->dir . $path)) - { + foreach ($paths as $path) { + if (file_exists($this->dir . $path)) { return trim(file_get_contents($this->dir . $path)); } } @@ -80,15 +78,13 @@ private function getLinuxId(): string private function getBsdId(): string { - if (file_exists('/etc/hostid')) - { + if (file_exists('/etc/hostid')) { return trim(file_get_contents('/etc/hostid')); } $out = exec('kenv -q smbios.system.uuid'); - if ($out != FALSE) - { + if ($out != false) { return $out; } @@ -99,8 +95,7 @@ private function getMacOsId(): string { $out = exec('ioreg -rd1 -c "IOPlatformExpertDevice"'); - if ($out != FALSE) - { + if ($out != false) { return $out; } @@ -111,8 +106,7 @@ private function getWindowsId(): string { $out = exec('%windir%\System32\REG.exe QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography /v MachineGuid'); - if ($out != FALSE) - { + if ($out != false) { return $out; } @@ -123,11 +117,10 @@ public static function parseMacOsId(string $out): string { $lines = explode("\n", $out); - foreach ($lines as $line) - { - if (str_contains($line, 'IOPlatformUUID') !== FALSE) - { + foreach ($lines as $line) { + if (str_contains($line, 'IOPlatformUUID')) { $parts = explode('=', $line); + return trim($parts[1]); } } @@ -138,6 +131,7 @@ public static function parseMacOsId(string $out): string public static function parseWindowsId(string $out): string { $parts = explode('REG_SZ', $out); + return trim($parts[1]); } } From de3819325577745e3dbc67fe558e8181386daf11 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 11:04:23 -0400 Subject: [PATCH 05/21] class constants --- src/SDK/Resource/Detectors/Host.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 1881cbbdc..50de2b69c 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -15,6 +15,8 @@ */ final class Host implements ResourceDetectorInterface { + private const PATH_ETC_MACHINEID = 'etc/machine-id'; + private const PATH_VAR_LIB_DBUS_MACHINEID = 'var/lib/dbus/machine-id'; private readonly string $dir; public function __construct(string $dir = '/') @@ -65,7 +67,7 @@ private function getMachineId() private function getLinuxId(): string { - $paths = ['etc/machine-id', 'var/lib/dbus/machine-id']; + $paths = [self::PATH_ETC_MACHINEID, self::PATH_VAR_LIB_DBUS_MACHINEID]; foreach ($paths as $path) { if (file_exists($this->dir . $path)) { From 61aa975fd6c22595ebb9e60445ee4b2ae4588e8c Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 11:07:46 -0400 Subject: [PATCH 06/21] feedback --- src/SDK/Resource/Detectors/Host.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 50de2b69c..1a3003f64 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -117,7 +117,7 @@ private function getWindowsId(): string public static function parseMacOsId(string $out): string { - $lines = explode("\n", $out); + $lines = explode(PHP_EOL, $out); foreach ($lines as $line) { if (str_contains($line, 'IOPlatformUUID')) { From 7c27a8b1e5ad1a57df10cfe966d10c2b9b31b13c Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 11:24:11 -0400 Subject: [PATCH 07/21] bsd testing --- src/SDK/Resource/Detectors/Host.php | 11 ++++++---- .../Unit/SDK/Resource/Detectors/HostTest.php | 21 ++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 1a3003f64..076aef403 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -17,11 +17,14 @@ final class Host implements ResourceDetectorInterface { private const PATH_ETC_MACHINEID = 'etc/machine-id'; private const PATH_VAR_LIB_DBUS_MACHINEID = 'var/lib/dbus/machine-id'; + private const PATH_ETC_HOSTID = 'etc/hostid'; private readonly string $dir; + private readonly string $os; - public function __construct(string $dir = '/') + public function __construct(string $dir = '/', string $os = PHP_OS_FAMILY) { $this->dir = $dir; + $this->os = $os; } public function getResource(): ResourceInfo @@ -37,7 +40,7 @@ public function getResource(): ResourceInfo private function getMachineId() { - switch (strtolower(PHP_OS_FAMILY)) { + switch (strtolower($this->os)) { case 'linux': { return $this->getLinuxId(); @@ -80,8 +83,8 @@ private function getLinuxId(): string private function getBsdId(): string { - if (file_exists('/etc/hostid')) { - return trim(file_get_contents('/etc/hostid')); + if (file_exists($this->dir . self::PATH_ETC_HOSTID)) { + return trim(file_get_contents($this->dir . self::PATH_ETC_HOSTID)); } $out = exec('kenv -q smbios.system.uuid'); diff --git a/tests/Unit/SDK/Resource/Detectors/HostTest.php b/tests/Unit/SDK/Resource/Detectors/HostTest.php index 4f43b21dc..dd207364e 100644 --- a/tests/Unit/SDK/Resource/Detectors/HostTest.php +++ b/tests/Unit/SDK/Resource/Detectors/HostTest.php @@ -44,10 +44,10 @@ public function test_host_parse_windows_id(): void /** * @dataProvider hostIdData */ - public function test_host_id_linux(array $files, string $expectedId): void + public function test_host_id_filesystem(string $os, array $files, string $expectedId): void { $root = vfsStream::setup('/', null, $files); - $resouceDetector = new Detectors\Host($root->url()); + $resouceDetector = new Detectors\Host($root->url(), $os); $resource = $resouceDetector->getResource(); $hostId = $resource->getAttributes()->get(ResourceAttributes::HOST_ID); $this->assertIsString($hostId); @@ -56,7 +56,7 @@ public function test_host_id_linux(array $files, string $expectedId): void public static function hostIdData(): array { - $etc = [ + $etc_machineid = [ 'etc' => [ 'machine-id' => '1234567890', ], @@ -70,12 +70,19 @@ public static function hostIdData(): array ], ], ]; + $etc_hostid = [ + 'etc' => [ + 'hostid' => '1234567890', + ], + ]; return [ - [[], ''], - [$etc, '1234567890'], - [array_merge($etc, $varLibDbus), '1234567890'], - [$varLibDbus, '0987654321'], + ['Linux', [], ''], + ['Linux', $etc_machineid, '1234567890'], + ['Linux', array_merge($etc_machineid, $varLibDbus), '1234567890'], + ['Linux', $etc_machineid, '1234567890'], + ['OpenBSD', [], ''], + ['OpenBSD', $etc_hostid, '1234567890'], ]; } } From f0720737444d7cb56950d3fe08dcd6fe419b60b8 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 12:19:19 -0400 Subject: [PATCH 08/21] macos improvements --- src/SDK/Resource/Detectors/Host.php | 2 +- .../Unit/SDK/Resource/Detectors/HostTest.php | 33 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 076aef403..0f8920dff 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -126,7 +126,7 @@ public static function parseMacOsId(string $out): string if (str_contains($line, 'IOPlatformUUID')) { $parts = explode('=', $line); - return trim($parts[1]); + return trim(str_replace('"', '', $parts[1])); } } diff --git a/tests/Unit/SDK/Resource/Detectors/HostTest.php b/tests/Unit/SDK/Resource/Detectors/HostTest.php index dd207364e..b8a396a40 100644 --- a/tests/Unit/SDK/Resource/Detectors/HostTest.php +++ b/tests/Unit/SDK/Resource/Detectors/HostTest.php @@ -27,10 +27,39 @@ public function test_host_get_resource(): void public function test_host_parse_macos_id(): void { - $out = 'IOPlatformUUID=1234567890'; + $out = << + "AAPL,phandle" = <01000000> + "serial-number" = <432123465233514651303544000000000000000000000000000000$ + "IOBusyInterest" = "IOCommand is not serializable" + "target-type" = <"J293"> + "platform-name" = <743831303300000000000000000000000000000000000000000000$ + "secure-root-prefix" = <"md"> + "name" = <"device-tree"> + "region-info" = <4c4c2f41000000000000000000000000000000000000000000000000$ + "manufacturer" = <"Apple Inc."> + "compatible" = <"J293AP","MacBookPro17,1","AppleARM"> + "config-number" = <000000000000000000000000000000000000000000000000000000$ + "IOPlatformSerialNumber" = "A01BC3QFQ05D" + "regulatory-model-number" = <41323333380000000000000000000000000000000000$ + "time-stamp" = <"Mon Jun 27 20:12:10 PDT 2022"> + "clock-frequency" = <00366e01> + "model" = <"MacBookPro17,1"> + "mlb-serial-number" = <432123413230363030455151384c4c314a0000000000000000$ + "model-number" = <4d59443832000000000000000000000000000000000000000000000$ + "IONWInterrupts" = "IONWInterrupts" + "model-config" = <"SUNWAY;MoPED=0x803914B08BE6C5AF0E6C990D7D8240DA4CAC2FF$ + "device_type" = <"bootrom"> + "#size-cells" = <02000000> + "IOPlatformUUID" = "1AB2345C-03E4-57D4-A375-1234D48DE123" + } +END; $hostId = Detectors\Host::parseMacOsId($out); $this->assertIsString($hostId); - $this->assertSame('1234567890', $hostId); + $this->assertSame('1AB2345C-03E4-57D4-A375-1234D48DE123', $hostId); } public function test_host_parse_windows_id(): void From bf1e81cc7512cee87c6507a04cae38f9f377d734 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 12:35:10 -0400 Subject: [PATCH 09/21] formatting --- tests/Unit/SDK/Resource/Detectors/HostTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Unit/SDK/Resource/Detectors/HostTest.php b/tests/Unit/SDK/Resource/Detectors/HostTest.php index b8a396a40..e1be70578 100644 --- a/tests/Unit/SDK/Resource/Detectors/HostTest.php +++ b/tests/Unit/SDK/Resource/Detectors/HostTest.php @@ -56,7 +56,7 @@ public function test_host_parse_macos_id(): void "#size-cells" = <02000000> "IOPlatformUUID" = "1AB2345C-03E4-57D4-A375-1234D48DE123" } -END; +END; $hostId = Detectors\Host::parseMacOsId($out); $this->assertIsString($hostId); $this->assertSame('1AB2345C-03E4-57D4-A375-1234D48DE123', $hostId); @@ -87,7 +87,7 @@ public static function hostIdData(): array { $etc_machineid = [ 'etc' => [ - 'machine-id' => '1234567890', + 'machine-id' => '1234567890', ], ]; $varLibDbus = [ @@ -101,7 +101,7 @@ public static function hostIdData(): array ]; $etc_hostid = [ 'etc' => [ - 'hostid' => '1234567890', + 'hostid' => '1234567890', ], ]; From f4ee303d9323a53cdc1fa7787620abf926c5df78 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 14:46:42 -0400 Subject: [PATCH 10/21] fix psalm error --- tests/Unit/SDK/Resource/Detectors/HostTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/SDK/Resource/Detectors/HostTest.php b/tests/Unit/SDK/Resource/Detectors/HostTest.php index e1be70578..a983c794d 100644 --- a/tests/Unit/SDK/Resource/Detectors/HostTest.php +++ b/tests/Unit/SDK/Resource/Detectors/HostTest.php @@ -22,7 +22,7 @@ public function test_host_get_resource(): void $this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl()); $this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_NAME)); $this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH)); - $this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_ID)); + $this->assertTrue($resource->getAttributes()->has(ResourceAttributes::HOST_ID)); } public function test_host_parse_macos_id(): void From 2c653f329f880d5f9394941e3067d4c0d523d99e Mon Sep 17 00:00:00 2001 From: Matt Hensley <130569+matt-hensley@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:07:33 -0400 Subject: [PATCH 11/21] Update src/SDK/Resource/Detectors/Host.php Co-authored-by: Tobias Bachert --- src/SDK/Resource/Detectors/Host.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 0f8920dff..12f167076 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -109,7 +109,7 @@ private function getMacOsId(): string private function getWindowsId(): string { - $out = exec('%windir%\System32\REG.exe QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography /v MachineGuid'); + $out = exec('powershell.exe -Command "Get-ItemPropertyValue -Path HKLM:\SOFTWARE\Microsoft\Cryptography -Name MachineGuid"'); if ($out != false) { return $out; From dd4c3ba951141b5aaf80064ddee62c24c745e4a5 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 18:11:18 -0400 Subject: [PATCH 12/21] update OS name check --- src/SDK/Resource/Detectors/Host.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 12f167076..0e52fa4b5 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -40,24 +40,22 @@ public function getResource(): ResourceInfo private function getMachineId() { - switch (strtolower($this->os)) { - case 'linux': + switch ($this->os) { + case 'Linux': { return $this->getLinuxId(); } - case 'freebsd': - case 'netbsd': - case 'openbsd': + case 'BSD': { return $this->getBsdId(); } - case 'darwin': + case 'Darwin': { $out = $this->getMacOsId(); return self::parseMacOsId($out); } - case 'windows': + case 'Windows': { $out = $this->getWindowsId(); From 1403ce89f245a3ec54f71e912a1f9a5ce3306ae3 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 18:13:07 -0400 Subject: [PATCH 13/21] empty string -> null --- src/SDK/Resource/Detectors/Host.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 0e52fa4b5..767d2f710 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -63,7 +63,7 @@ private function getMachineId() } } - return ''; + return null; } private function getLinuxId(): string @@ -76,7 +76,7 @@ private function getLinuxId(): string } } - return ''; + return null; } private function getBsdId(): string @@ -91,7 +91,7 @@ private function getBsdId(): string return $out; } - return ''; + return null; } private function getMacOsId(): string @@ -102,7 +102,7 @@ private function getMacOsId(): string return $out; } - return ''; + return null; } private function getWindowsId(): string @@ -113,7 +113,7 @@ private function getWindowsId(): string return $out; } - return ''; + return null; } public static function parseMacOsId(string $out): string @@ -128,7 +128,7 @@ public static function parseMacOsId(string $out): string } } - return ''; + return null; } public static function parseWindowsId(string $out): string From 6f91a71038d1407c3304d694a86d25f80bc02844 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 18:14:50 -0400 Subject: [PATCH 14/21] remove windows output parsing --- src/SDK/Resource/Detectors/Host.php | 11 +---------- tests/Unit/SDK/Resource/Detectors/HostTest.php | 8 -------- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 767d2f710..e1d02ec05 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -57,9 +57,7 @@ private function getMachineId() } case 'Windows': { - $out = $this->getWindowsId(); - - return self::parseWindowsId($out); + return $this->getWindowsId(); } } @@ -130,11 +128,4 @@ public static function parseMacOsId(string $out): string return null; } - - public static function parseWindowsId(string $out): string - { - $parts = explode('REG_SZ', $out); - - return trim($parts[1]); - } } diff --git a/tests/Unit/SDK/Resource/Detectors/HostTest.php b/tests/Unit/SDK/Resource/Detectors/HostTest.php index a983c794d..6b11b760a 100644 --- a/tests/Unit/SDK/Resource/Detectors/HostTest.php +++ b/tests/Unit/SDK/Resource/Detectors/HostTest.php @@ -62,14 +62,6 @@ public function test_host_parse_macos_id(): void $this->assertSame('1AB2345C-03E4-57D4-A375-1234D48DE123', $hostId); } - public function test_host_parse_windows_id(): void - { - $out = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\MachineGuid REG_SZ 1234567890'; - $hostId = Detectors\Host::parseWindowsId($out); - $this->assertIsString($hostId); - $this->assertSame('1234567890', $hostId); - } - /** * @dataProvider hostIdData */ From e8b6578c5e2063396f9656c8fcd4058275ecca56 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 18:21:32 -0400 Subject: [PATCH 15/21] replace macos parsing with awk --- src/SDK/Resource/Detectors/Host.php | 21 +---------- .../Unit/SDK/Resource/Detectors/HostTest.php | 37 ------------------- 2 files changed, 2 insertions(+), 56 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index e1d02ec05..bf310172e 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -51,9 +51,7 @@ private function getMachineId() } case 'Darwin': { - $out = $this->getMacOsId(); - - return self::parseMacOsId($out); + return $this->getMacOsId(); } case 'Windows': { @@ -94,7 +92,7 @@ private function getBsdId(): string private function getMacOsId(): string { - $out = exec('ioreg -rd1 -c "IOPlatformExpertDevice"'); + $out = exec('ioreg -rd1 -c IOPlatformExpertDevice | awk \'/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }\''); if ($out != false) { return $out; @@ -113,19 +111,4 @@ private function getWindowsId(): string return null; } - - public static function parseMacOsId(string $out): string - { - $lines = explode(PHP_EOL, $out); - - foreach ($lines as $line) { - if (str_contains($line, 'IOPlatformUUID')) { - $parts = explode('=', $line); - - return trim(str_replace('"', '', $parts[1])); - } - } - - return null; - } } diff --git a/tests/Unit/SDK/Resource/Detectors/HostTest.php b/tests/Unit/SDK/Resource/Detectors/HostTest.php index 6b11b760a..2e33fca01 100644 --- a/tests/Unit/SDK/Resource/Detectors/HostTest.php +++ b/tests/Unit/SDK/Resource/Detectors/HostTest.php @@ -25,43 +25,6 @@ public function test_host_get_resource(): void $this->assertTrue($resource->getAttributes()->has(ResourceAttributes::HOST_ID)); } - public function test_host_parse_macos_id(): void - { - $out = << - "AAPL,phandle" = <01000000> - "serial-number" = <432123465233514651303544000000000000000000000000000000$ - "IOBusyInterest" = "IOCommand is not serializable" - "target-type" = <"J293"> - "platform-name" = <743831303300000000000000000000000000000000000000000000$ - "secure-root-prefix" = <"md"> - "name" = <"device-tree"> - "region-info" = <4c4c2f41000000000000000000000000000000000000000000000000$ - "manufacturer" = <"Apple Inc."> - "compatible" = <"J293AP","MacBookPro17,1","AppleARM"> - "config-number" = <000000000000000000000000000000000000000000000000000000$ - "IOPlatformSerialNumber" = "A01BC3QFQ05D" - "regulatory-model-number" = <41323333380000000000000000000000000000000000$ - "time-stamp" = <"Mon Jun 27 20:12:10 PDT 2022"> - "clock-frequency" = <00366e01> - "model" = <"MacBookPro17,1"> - "mlb-serial-number" = <432123413230363030455151384c4c314a0000000000000000$ - "model-number" = <4d59443832000000000000000000000000000000000000000000000$ - "IONWInterrupts" = "IONWInterrupts" - "model-config" = <"SUNWAY;MoPED=0x803914B08BE6C5AF0E6C990D7D8240DA4CAC2FF$ - "device_type" = <"bootrom"> - "#size-cells" = <02000000> - "IOPlatformUUID" = "1AB2345C-03E4-57D4-A375-1234D48DE123" - } -END; - $hostId = Detectors\Host::parseMacOsId($out); - $this->assertIsString($hostId); - $this->assertSame('1AB2345C-03E4-57D4-A375-1234D48DE123', $hostId); - } - /** * @dataProvider hostIdData */ From d9eb9e5e03e8be7274a0ef72f2807f0b7baa80ca Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 18:27:59 -0400 Subject: [PATCH 16/21] mark nullable --- src/SDK/Resource/Detectors/Host.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index bf310172e..74b0d3a55 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -38,7 +38,7 @@ public function getResource(): ResourceInfo return ResourceInfo::create(Attributes::create($attributes), ResourceAttributes::SCHEMA_URL); } - private function getMachineId() + private function getMachineId(): ?string { switch ($this->os) { case 'Linux': @@ -62,7 +62,7 @@ private function getMachineId() return null; } - private function getLinuxId(): string + private function getLinuxId(): ?string { $paths = [self::PATH_ETC_MACHINEID, self::PATH_VAR_LIB_DBUS_MACHINEID]; @@ -75,7 +75,7 @@ private function getLinuxId(): string return null; } - private function getBsdId(): string + private function getBsdId(): ?string { if (file_exists($this->dir . self::PATH_ETC_HOSTID)) { return trim(file_get_contents($this->dir . self::PATH_ETC_HOSTID)); @@ -90,7 +90,7 @@ private function getBsdId(): string return null; } - private function getMacOsId(): string + private function getMacOsId(): ?string { $out = exec('ioreg -rd1 -c IOPlatformExpertDevice | awk \'/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }\''); @@ -101,7 +101,7 @@ private function getMacOsId(): string return null; } - private function getWindowsId(): string + private function getWindowsId(): ?string { $out = exec('powershell.exe -Command "Get-ItemPropertyValue -Path HKLM:\SOFTWARE\Microsoft\Cryptography -Name MachineGuid"'); From 96219c31a271b8d2be12cc39db6a2b4fe63166fe Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 18:28:07 -0400 Subject: [PATCH 17/21] fix tests --- tests/Unit/SDK/Resource/Detectors/HostTest.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/Unit/SDK/Resource/Detectors/HostTest.php b/tests/Unit/SDK/Resource/Detectors/HostTest.php index 2e33fca01..c69a8aa4e 100644 --- a/tests/Unit/SDK/Resource/Detectors/HostTest.php +++ b/tests/Unit/SDK/Resource/Detectors/HostTest.php @@ -22,17 +22,22 @@ public function test_host_get_resource(): void $this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl()); $this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_NAME)); $this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH)); - $this->assertTrue($resource->getAttributes()->has(ResourceAttributes::HOST_ID)); } /** * @dataProvider hostIdData */ - public function test_host_id_filesystem(string $os, array $files, string $expectedId): void + public function test_host_id_filesystem(string $os, array $files, ?string $expectedId): void { $root = vfsStream::setup('/', null, $files); $resouceDetector = new Detectors\Host($root->url(), $os); $resource = $resouceDetector->getResource(); + + if ($expectedId === null) { + $this->assertFalse($resource->getAttributes()->has(ResourceAttributes::HOST_ID)); + return; + } + $hostId = $resource->getAttributes()->get(ResourceAttributes::HOST_ID); $this->assertIsString($hostId); $this->assertSame($expectedId, $hostId); @@ -61,12 +66,12 @@ public static function hostIdData(): array ]; return [ - ['Linux', [], ''], + ['Linux', [], null], ['Linux', $etc_machineid, '1234567890'], ['Linux', array_merge($etc_machineid, $varLibDbus), '1234567890'], ['Linux', $etc_machineid, '1234567890'], - ['OpenBSD', [], ''], - ['OpenBSD', $etc_hostid, '1234567890'], + ['BSD', [], null], + ['BSD', $etc_hostid, '1234567890'], ]; } } From 270020d744a4fee6003cf6790b9f350438582cb9 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Wed, 27 Mar 2024 18:34:24 -0400 Subject: [PATCH 18/21] linter error --- tests/Unit/SDK/Resource/Detectors/HostTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Unit/SDK/Resource/Detectors/HostTest.php b/tests/Unit/SDK/Resource/Detectors/HostTest.php index c69a8aa4e..2bcddfddd 100644 --- a/tests/Unit/SDK/Resource/Detectors/HostTest.php +++ b/tests/Unit/SDK/Resource/Detectors/HostTest.php @@ -32,9 +32,10 @@ public function test_host_id_filesystem(string $os, array $files, ?string $expec $root = vfsStream::setup('/', null, $files); $resouceDetector = new Detectors\Host($root->url(), $os); $resource = $resouceDetector->getResource(); - + if ($expectedId === null) { $this->assertFalse($resource->getAttributes()->has(ResourceAttributes::HOST_ID)); + return; } From b03379650b388bf8ba8b4e7a22f9d506ca437d61 Mon Sep 17 00:00:00 2001 From: Matt Hensley <130569+matt-hensley@users.noreply.github.com> Date: Thu, 28 Mar 2024 08:21:11 -0400 Subject: [PATCH 19/21] Update src/SDK/Resource/Detectors/Host.php Co-authored-by: Chris Lightfoot-Wild --- src/SDK/Resource/Detectors/Host.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 74b0d3a55..6c6dbf029 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -18,13 +18,10 @@ final class Host implements ResourceDetectorInterface private const PATH_ETC_MACHINEID = 'etc/machine-id'; private const PATH_VAR_LIB_DBUS_MACHINEID = 'var/lib/dbus/machine-id'; private const PATH_ETC_HOSTID = 'etc/hostid'; - private readonly string $dir; - private readonly string $os; - - public function __construct(string $dir = '/', string $os = PHP_OS_FAMILY) - { - $this->dir = $dir; - $this->os = $os; + public function __construct( + private readonly string $dir = '/', + private readonly string $os = PHP_OS_FAMILY, + ) { } public function getResource(): ResourceInfo From ddb5180de8bbdb9e13204e6c5dfcba02a4e611d0 Mon Sep 17 00:00:00 2001 From: Matt Hensley <130569+matt-hensley@users.noreply.github.com> Date: Thu, 28 Mar 2024 08:21:35 -0400 Subject: [PATCH 20/21] Update src/SDK/Resource/Detectors/Host.php Co-authored-by: Chris Lightfoot-Wild --- src/SDK/Resource/Detectors/Host.php | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 6c6dbf029..9c849c981 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -37,26 +37,13 @@ public function getResource(): ResourceInfo private function getMachineId(): ?string { - switch ($this->os) { - case 'Linux': - { - return $this->getLinuxId(); - } - case 'BSD': - { - return $this->getBsdId(); - } - case 'Darwin': - { - return $this->getMacOsId(); - } - case 'Windows': - { - return $this->getWindowsId(); - } - } - - return null; + return match ($this->os) { + 'Linux' => $this->getLinuxId(), + 'BSD' => $this->getBsdId(), + 'Darwin' => $this->getMacOsId(), + 'Windows' => $this->getWindowsId(), + default => null, + }; } private function getLinuxId(): ?string From 13b1e5978dbd27e14317a29199e3267908fbe52e Mon Sep 17 00:00:00 2001 From: Matt Hensley <130569+matt-hensley@users.noreply.github.com> Date: Thu, 28 Mar 2024 10:00:09 -0400 Subject: [PATCH 21/21] Update src/SDK/Resource/Detectors/Host.php Co-authored-by: Chris Lightfoot-Wild --- src/SDK/Resource/Detectors/Host.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 9c849c981..af5082778 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -18,6 +18,7 @@ final class Host implements ResourceDetectorInterface private const PATH_ETC_MACHINEID = 'etc/machine-id'; private const PATH_VAR_LIB_DBUS_MACHINEID = 'var/lib/dbus/machine-id'; private const PATH_ETC_HOSTID = 'etc/hostid'; + public function __construct( private readonly string $dir = '/', private readonly string $os = PHP_OS_FAMILY,