Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrub secrets from process arguments #13225

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions instrumentation/resources/library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ testing {
}

tasks {
test {
dependsOn(jar)
doFirst {
// use the final jar instead of directories with built classes to test the mrjar functionality
classpath = jar.get().outputs.files + classpath
}
systemProperty("testSecret", "test")
systemProperty("testPassword", "test")
systemProperty("testNotRedacted", "test")
}

check {
dependsOn(testing.suites)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
Expand All @@ -35,6 +34,9 @@ public final class ProcessResource {
// Important: This is statically used in buildResource, so must be declared/initialized first.
private static final Pattern JAR_FILE_PATTERN =
Pattern.compile("^\\S+\\.(jar|war)", Pattern.CASE_INSENSITIVE);
// scrub values for system properties containing "secret" or "password" in the name
private static final Pattern SCRUB_PATTERN =
Pattern.compile("(-D.*(password|secret).*=).*", Pattern.CASE_INSENSITIVE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth to also scrub properties containing pwd or credential?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave that to @trask to decide.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it's another option to provide an environment variable for users so that they can set some keys themselves.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ok to go with just password and secret initially and see if we get further reports


private static final Resource INSTANCE = buildResource();

Expand Down Expand Up @@ -94,12 +96,14 @@ private static Resource doBuildResource() {
if (args.length > 0) {
List<String> commandArgs = new ArrayList<>(args.length + 1);
commandArgs.add(executablePath.toString());
commandArgs.addAll(Arrays.asList(args));
for (String arg : args) {
commandArgs.add(scrub(arg));
}
attributes.put(PROCESS_COMMAND_ARGS, commandArgs);
} else { // Java 8
StringBuilder commandLine = new StringBuilder(executablePath);
for (String arg : runtime.getInputArguments()) {
commandLine.append(' ').append(arg);
commandLine.append(' ').append(scrub(arg));
}
// sun.java.command isn't well document and may not be available on all systems.
String javaCommand = System.getProperty("sun.java.command");
Expand All @@ -118,5 +122,9 @@ private static Resource doBuildResource() {
return Resource.create(attributes.build(), SchemaUrls.V1_24_0);
}

private static String scrub(String argument) {
return SCRUB_PATTERN.matcher(argument).replaceFirst("$1***");
}

private ProcessResource() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,39 @@ class ProcessResourceTest {
@Test
@SetSystemProperty(key = "os.name", value = "Linux 4.12")
void notWindows() {
Resource resource = ProcessResource.buildResource();
assertThat(resource.getSchemaUrl()).isEqualTo(SchemaUrls.V1_24_0);
Attributes attributes = resource.getAttributes();

assertThat(attributes.get(ProcessIncubatingAttributes.PROCESS_PID)).isGreaterThan(1);
assertThat(attributes.get(ProcessIncubatingAttributes.PROCESS_EXECUTABLE_PATH))
.matches(".*[/\\\\]java");
assertThat(attributes.get(ProcessIncubatingAttributes.PROCESS_COMMAND_LINE))
.contains(attributes.get(ProcessIncubatingAttributes.PROCESS_EXECUTABLE_PATH));
// With Java 9+ and a compiled jar, ResourceAttributes.PROCESS_COMMAND_ARGS
// will be set instead of ResourceAttributes.PROCESS_COMMAND_LINE
assertResource(false);
}

@Test
@SetSystemProperty(key = "os.name", value = "Windows 10")
void windows() {
assertResource(true);
}

private static void assertResource(boolean windows) {
Resource resource = ProcessResource.buildResource();
assertThat(resource.getSchemaUrl()).isEqualTo(SchemaUrls.V1_24_0);
Attributes attributes = resource.getAttributes();

assertThat(attributes.get(ProcessIncubatingAttributes.PROCESS_PID)).isGreaterThan(1);
assertThat(attributes.get(ProcessIncubatingAttributes.PROCESS_EXECUTABLE_PATH))
.matches(".*[/\\\\]java\\.exe");
assertThat(attributes.get(ProcessIncubatingAttributes.PROCESS_COMMAND_LINE))
.contains(attributes.get(ProcessIncubatingAttributes.PROCESS_EXECUTABLE_PATH));
.matches(windows ? ".*[/\\\\]java\\.exe" : ".*[/\\\\]java");

// With Java 9+ and a compiled jar, ResourceAttributes.PROCESS_COMMAND_ARGS
// will be set instead of ResourceAttributes.PROCESS_COMMAND_LINE
boolean java8 = "1.8".equals(System.getProperty("java.specification.version"));
if (java8) {
assertThat(attributes.get(ProcessIncubatingAttributes.PROCESS_COMMAND_LINE))
.contains(attributes.get(ProcessIncubatingAttributes.PROCESS_EXECUTABLE_PATH))
.contains("-DtestSecret=***")
.contains("-DtestPassword=***")
.contains("-DtestNotRedacted=test");
} else {
assertThat(attributes.get(ProcessIncubatingAttributes.PROCESS_COMMAND_ARGS))
.contains(attributes.get(ProcessIncubatingAttributes.PROCESS_EXECUTABLE_PATH))
.contains("-DtestSecret=***")
.contains("-DtestPassword=***")
.contains("-DtestNotRedacted=test");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void endToEnd() {
+ " value: my-service\n"
+ "tracer_provider:\n";

boolean java8 = "1.8".equals(System.getProperty("java.specification.version"));
OpenTelemetrySdk openTelemetrySdk =
FileConfiguration.parseAndCreate(
new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8)));
Expand All @@ -56,7 +57,8 @@ void endToEnd() {
assertThat(attributeKeys).contains("os.description");
assertThat(attributeKeys).contains("os.type");
// ProcessResourceComponentProvider
assertThat(attributeKeys).contains("process.command_line");
assertThat(attributeKeys)
.contains(java8 ? "process.command_line" : "process.command_args");
assertThat(attributeKeys).contains("process.executable.path");
assertThat(attributeKeys).contains("process.pid");
// ProcessRuntimeResourceComponentProvider
Expand Down
Loading