Skip to content

Commit

Permalink
read GUID from windows
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Feb 14, 2024
1 parent 8fc4d8a commit a50d854
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,161 @@
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
import io.opentelemetry.sdk.resources.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/** {@link ResourceProvider} for automatically configuring <code>host.id</code>. */
public final class HostIdResourceProvider implements ConditionalResourceProvider {

private static final Logger logger = Logger.getLogger(HostIdResourceProvider.class.getName());

public static final AttributeKey<String> HOST_ID = AttributeKey.stringKey("host.id");
public static final String REGISTRY_QUERY =
"reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid";

private final Supplier<OsType> getOsType;

private final Function<Path, List<String>> pathReader;

private final Supplier<ExecResult> queryWindowsRegistry;

enum OsType {
WINDOWS,
LINUX
}

static class ExecResult {
int exitCode;
List<String> lines;

public ExecResult(int exitCode, List<String> lines) {
this.exitCode = exitCode;
this.lines = lines;
}
}

public HostIdResourceProvider() {
this(
HostIdResourceProvider::getOsType,
path -> {
try {
return Files.readAllLines(path);
} catch (IOException e) {
throw new IllegalStateException(e);

Check warning on line 66 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java

View check run for this annotation

Codecov / codecov/patch

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java#L65-L66

Added lines #L65 - L66 were not covered by tests
}
});
},
HostIdResourceProvider::queryWindowsRegistry);
}

// Visible for testing
HostIdResourceProvider(Function<Path, List<String>> pathReader) {
HostIdResourceProvider(
Supplier<OsType> getOsType,
Function<Path, List<String>> pathReader,
Supplier<ExecResult> queryWindowsRegistry) {
this.getOsType = getOsType;
this.pathReader = pathReader;
this.queryWindowsRegistry = queryWindowsRegistry;
}

@Override
public Resource createResource(ConfigProperties config) {
OsType osType = getOsType.get();
switch (osType) {
case WINDOWS:
return readWindowsGuid();
case LINUX:
return readLinuxMachineId();
}
throw new IllegalStateException("Unsupported OS type: " + osType);

Check warning on line 91 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java

View check run for this annotation

Codecov / codecov/patch

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java#L91

Added line #L91 was not covered by tests
}

private Resource readLinuxMachineId() {
Path path = FileSystems.getDefault().getPath("/etc/machine-id");
try {
List<String> lines = pathReader.apply(path);
if (lines.isEmpty()) {
logger.fine("Failed to read /etc/machine-id: empty file");
logger.warning("Failed to read /etc/machine-id: empty file");
return Resource.empty();
}
return Resource.create(Attributes.of(HOST_ID, lines.get(0)));
} catch (RuntimeException e) {
logger.log(Level.FINE, "Failed to read /etc/machine-id", e);
logger.log(Level.WARNING, "Failed to read /etc/machine-id", e);
return Resource.empty();
}
}

private static OsType getOsType() {
String osName = System.getProperty("os.name");
return osName != null && osName.startsWith("Windows") ? OsType.WINDOWS : OsType.LINUX;
}

private Resource readWindowsGuid() {

try {
ExecResult execResult = queryWindowsRegistry.get();

if (execResult.exitCode != 0) {
logger.warning(
"Failed to read Windows registry. Exit code: "
+ execResult.exitCode
+ " Output: "
+ String.join("\n", execResult.lines));
return Resource.empty();
}

for (String line : execResult.lines) {
if (line.contains("MachineGuid")) {
String[] parts = line.trim().split("\\s+");
if (parts.length == 3) {
return Resource.create(Attributes.of(HOST_ID, parts[2]));
}
}
}
logger.warning(
"Failed to read Windows registry: No MachineGuid found in output: " + execResult.lines);
return Resource.empty();
} catch (RuntimeException e) {
logger.log(Level.WARNING, "Failed to read Windows registry", e);
return Resource.empty();

Check warning on line 141 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java

View check run for this annotation

Codecov / codecov/patch

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java#L139-L141

Added lines #L139 - L141 were not covered by tests
}
}

private static ExecResult queryWindowsRegistry() {
try {
Process process = Runtime.getRuntime().exec(REGISTRY_QUERY);

Check warning on line 147 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java

View check run for this annotation

Codecov / codecov/patch

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java#L147

Added line #L147 was not covered by tests

if (process.waitFor() != 0) {
return new ExecResult(process.exitValue(), getLines(process.getErrorStream()));

Check warning on line 150 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java

View check run for this annotation

Codecov / codecov/patch

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java#L150

Added line #L150 was not covered by tests
}

return new ExecResult(0, getLines(process.getInputStream()));
} catch (IOException | InterruptedException e) {
throw new IllegalStateException(e);

Check warning on line 155 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java

View check run for this annotation

Codecov / codecov/patch

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java#L153-L155

Added lines #L153 - L155 were not covered by tests
}
}

private static List<String> getLines(InputStream inputStream) {
return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
.lines()
.collect(Collectors.toList());

Check warning on line 162 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java

View check run for this annotation

Codecov / codecov/patch

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java#L160-L162

Added lines #L160 - L162 were not covered by tests
}

@Override
public boolean shouldApply(ConfigProperties config, Resource existing) {
return !config.getMap("otel.resource.attributes").containsKey(HOST_ID.getKey());
return !config.getMap("otel.resource.attributes").containsKey(HOST_ID.getKey())
&& existing.getAttribute(HOST_ID) == null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,130 @@

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import io.opentelemetry.sdk.resources.Resource;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.assertj.core.api.MapAssert;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;

class HostIdResourceProviderTest {

private static class TestCase {
private static class LinuxTestCase {
private final String name;
private final String expectedValue;
private final Function<Path, List<String>> pathReader;

private TestCase(String name, String expectedValue, Function<Path, List<String>> pathReader) {
private LinuxTestCase(
String name, String expectedValue, Function<Path, List<String>> pathReader) {
this.name = name;
this.expectedValue = expectedValue;
this.pathReader = pathReader;
}
}

private static final List<TestCase> TEST_CASES =
Arrays.asList(
new TestCase("default", "test", path -> Collections.singletonList("test")),
new TestCase("empty file", null, path -> Collections.emptyList()),
new TestCase(
"error reading",
null,
path -> {
throw new IllegalStateException("can't read file");
}));
private static class WindowsTestCase {
private final String name;
private final String expectedValue;
private final Supplier<HostIdResourceProvider.ExecResult> queryWindowsRegistry;

private WindowsTestCase(
String name,
String expectedValue,
Supplier<HostIdResourceProvider.ExecResult> queryWindowsRegistry) {
this.name = name;
this.expectedValue = expectedValue;
this.queryWindowsRegistry = queryWindowsRegistry;
}
}

@TestFactory
Collection<DynamicTest> createResource() {
return TEST_CASES.stream()
Collection<DynamicTest> createResourceLinux() {
return Stream.of(
new LinuxTestCase("default", "test", path -> Collections.singletonList("test")),
new LinuxTestCase("empty file", null, path -> Collections.emptyList()),
new LinuxTestCase(
"error reading",
null,
path -> {
throw new IllegalStateException("can't read file");
}))
.map(
testCase ->
DynamicTest.dynamicTest(
testCase.name,
() -> {
HostIdResourceProvider provider =
new HostIdResourceProvider(testCase.pathReader);
MapAssert<AttributeKey<?>, Object> that =
assertThat(provider.createResource(null).getAttributes().asMap());
new HostIdResourceProvider(
() -> HostIdResourceProvider.OsType.LINUX, testCase.pathReader, null);

if (testCase.expectedValue == null) {
that.isEmpty();
} else {
that.containsEntry(HostIdResourceProvider.HOST_ID, testCase.expectedValue);
}
assertHostId(testCase.expectedValue, provider);
}))
.collect(Collectors.toList());
}

@TestFactory
Collection<DynamicTest> createResourceWindows() {
return Stream.of(
new WindowsTestCase(
"default",
"test",
() ->
new HostIdResourceProvider.ExecResult(
0,
Arrays.asList(
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography",
" MachineGuid REG_SZ test"))),
new WindowsTestCase(
"error code",
null,
() -> new HostIdResourceProvider.ExecResult(1, Collections.emptyList())),
new WindowsTestCase(
"short output",
null,
() -> new HostIdResourceProvider.ExecResult(0, Collections.emptyList())))
.map(
testCase ->
DynamicTest.dynamicTest(
testCase.name,
() -> {
HostIdResourceProvider provider =
new HostIdResourceProvider(
() -> HostIdResourceProvider.OsType.WINDOWS,
null,
testCase.queryWindowsRegistry);

assertHostId(testCase.expectedValue, provider);
}))
.collect(Collectors.toList());
}

private static void assertHostId(String expectedValue, HostIdResourceProvider provider) {
MapAssert<AttributeKey<?>, Object> that =
assertThat(provider.createResource(null).getAttributes().asMap());

if (expectedValue == null) {
that.isEmpty();
} else {
that.containsEntry(HostIdResourceProvider.HOST_ID, expectedValue);
}
}

@Test
void shouldApply() {
HostIdResourceProvider provider = new HostIdResourceProvider();
assertThat(
provider.shouldApply(
DefaultConfigProperties.createFromMap(Collections.emptyMap()), null))
DefaultConfigProperties.createFromMap(Collections.emptyMap()),
Resource.getDefault()))
.isTrue();
assertThat(
provider.shouldApply(
Expand Down

0 comments on commit a50d854

Please sign in to comment.