Skip to content

Commit c632d96

Browse files
authored
check for readable host id files before read (#1400)
* check for readable host id files before read * reset vfs between tests
1 parent f29277a commit c632d96

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/SDK/Resource/Detectors/Host.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ private function getLinuxId(): ?string
5252
$paths = [self::PATH_ETC_MACHINEID, self::PATH_VAR_LIB_DBUS_MACHINEID];
5353

5454
foreach ($paths as $path) {
55-
if (file_exists($this->dir . $path)) {
56-
return trim(file_get_contents($this->dir . $path));
55+
$file = $this->dir . $path;
56+
if (is_file($file) && is_readable($file)) {
57+
return trim(file_get_contents($file));
5758
}
5859
}
5960

@@ -62,13 +63,14 @@ private function getLinuxId(): ?string
6263

6364
private function getBsdId(): ?string
6465
{
65-
if (file_exists($this->dir . self::PATH_ETC_HOSTID)) {
66-
return trim(file_get_contents($this->dir . self::PATH_ETC_HOSTID));
66+
$file = $this->dir . self::PATH_ETC_HOSTID;
67+
if (is_file($file) && is_readable($file)) {
68+
return trim(file_get_contents($file));
6769
}
6870

69-
$out = exec('kenv -q smbios.system.uuid');
71+
$out = exec('which kenv && kenv -q smbios.system.uuid');
7072

71-
if ($out !== false) {
73+
if ($out) {
7274
return $out;
7375
}
7476

tests/Unit/SDK/Resource/Detectors/HostTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77
use OpenTelemetry\SDK\Resource\Detectors\Host;
88
use OpenTelemetry\SemConv\ResourceAttributes;
99
use org\bovigo\vfs\vfsStream;
10+
use org\bovigo\vfs\vfsStreamDirectory;
1011
use PHPUnit\Framework\Attributes\CoversClass;
1112
use PHPUnit\Framework\Attributes\DataProvider;
1213
use PHPUnit\Framework\TestCase;
1314

1415
#[CoversClass(Host::class)]
1516
class HostTest extends TestCase
1617
{
18+
public function setUp(): void
19+
{
20+
//reset vfs between tests
21+
vfsStream::setup('/');
22+
}
23+
1724
public function test_host_get_resource(): void
1825
{
1926
$resourceDetector = new Host();
@@ -72,4 +79,33 @@ public static function hostIdData(): array
7279
['BSD', $etc_hostid, '1234567890'],
7380
];
7481
}
82+
83+
#[DataProvider('nonReadableFileProvider')]
84+
public function test_file_not_readable(string $os, vfsStreamDirectory $root): void
85+
{
86+
$resourceDetector = new Host($root->url(), $os);
87+
$resource = $resourceDetector->getResource();
88+
89+
$hostId = $resource->getAttributes()->get(ResourceAttributes::HOST_ID);
90+
$this->assertNull($hostId);
91+
}
92+
93+
public static function nonReadableFileProvider(): array
94+
{
95+
$root = vfsStream::setup('/');
96+
$etc = vfsStream::newDirectory('etc')->at($root);
97+
vfsStream::newFile('machine-id')
98+
->at($etc)
99+
->setContent('you-cant-see-me')
100+
->chmod(0222);
101+
vfsStream::newFile('hostid')
102+
->at($etc)
103+
->setContent('you-cant-see-me')
104+
->chmod(0222);
105+
106+
return [
107+
['Linux', $root],
108+
['BSD', $root],
109+
];
110+
}
75111
}

0 commit comments

Comments
 (0)