Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ConfigCollectorTest extends DDSpecification {
// ConfigProvider.getStringNotEmpty
AppSecConfig.APPSEC_AUTOMATED_USER_EVENTS_TRACKING | UserEventTrackingMode.EXTENDED.toString()
// ConfigProvider.getStringExcludingSource
GeneralConfig.APPLICATION_KEY | "app-key"
DDTags.SERVICE | "my-service"
// ConfigProvider.getBoolean
TraceInstrumentationConfig.RESOLVER_USE_URL_CACHES | "true"
// ConfigProvider.getInteger
Expand Down Expand Up @@ -184,11 +184,11 @@ class ConfigCollectorTest extends DDSpecification {
ConfigCollector.get().collect()

when:
ConfigCollector.get().put('DD_API_KEY', 'sensitive data', ConfigOrigin.ENV, ABSENT_SEQ_ID)
ConfigCollector.get().put('api-key', 'sensitive data', ConfigOrigin.ENV, ABSENT_SEQ_ID)

then:
def collected = ConfigCollector.get().collect()
collected.get(ConfigOrigin.ENV).get('DD_API_KEY').stringValue() == '<hidden>'
collected.get(ConfigOrigin.ENV).get('api-key').stringValue() == '<hidden>'
}

def "collects common setting default values"() {
Expand Down
36 changes: 24 additions & 12 deletions metadata/supported-configurations.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
"version": "A",
"type": "string",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"DD_API_KEY_FILE": [
Expand Down Expand Up @@ -198,7 +199,8 @@
"version": "A",
"type": "string",
"default": null,
"aliases": ["DD_APP_KEY"]
"aliases": ["DD_APP_KEY"],
"sensitive": true
}
],
"DD_APPLICATION_KEY_FILE": [
Expand Down Expand Up @@ -1006,7 +1008,8 @@
"version": "A",
"type": "string",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"DD_CRASHTRACKING_PROXY_PORT": [
Expand Down Expand Up @@ -2318,7 +2321,8 @@
"version": "A",
"type": "map",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"DD_OTLP_LOGS_PROTOCOL": [
Expand Down Expand Up @@ -2462,7 +2466,8 @@
"version": "B",
"type": "map",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"DD_OTLP_METRICS_PROTOCOL": [
Expand Down Expand Up @@ -2510,7 +2515,8 @@
"version": "B",
"type": "map",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"DD_OTLP_TRACES_PROTOCOL": [
Expand Down Expand Up @@ -3254,7 +3260,8 @@
"version": "A",
"type": "string",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"DD_PROFILING_PROXY_PORT": [
Expand Down Expand Up @@ -3638,7 +3645,8 @@
"version": "A",
"type": "string",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"DD_RUM_DEFAULT_PRIVACY_LEVEL": [
Expand Down Expand Up @@ -11654,7 +11662,8 @@
"version": "B",
"type": "map",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"OTEL_EXPORTER_OTLP_PROTOCOL": [
Expand Down Expand Up @@ -11694,7 +11703,8 @@
"version": "A",
"type": "string",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"OTEL_EXPORTER_OTLP_LOGS_PROTOCOL": [
Expand Down Expand Up @@ -11734,7 +11744,8 @@
"version": "B",
"type": "string",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"OTEL_EXPORTER_OTLP_METRICS_PROTOCOL": [
Expand Down Expand Up @@ -11790,7 +11801,8 @@
"version": "A",
"type": "string",
"default": null,
"aliases": []
"aliases": [],
"sensitive": true
}
],
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ public final class ConfigSetting {
/** The config ID associated with this setting, or {@code null} if not applicable. */
public final String configId;

// Configuration property names whose values are excluded from configuration telemetry by
// replacing them with "<hidden>". These are the keys under which the values are collected (the
// property-name form used by ConfigProvider); every sensitive setting is collected under one of
// these regardless of which env-var/alias the user set. Keep in sync with the "sensitive": true
// entries in metadata/supported-configurations.json.
private static final Set<String> CONFIG_FILTER_LIST =
new HashSet<>(
Arrays.asList("DD_API_KEY", "dd.api-key", "dd.profiling.api-key", "dd.profiling.apikey"));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was able to prove that the only format that occurs here is dd.api-key and not DD_API_KEY. Cleaning this up

Arrays.asList(
"api-key",
"application-key",
"crashtracking.proxy.password",
"otlp.logs.headers",
"otlp.metrics.headers",
"otlp.traces.headers",
"profiling.proxy.password",
"rum.client.token"));

public static ConfigSetting of(String key, Object value, ConfigOrigin origin) {
return new ConfigSetting(key, value, origin, ABSENT_SEQ_ID, null);
Expand All @@ -47,7 +60,7 @@ public static ConfigSetting of(

private ConfigSetting(String key, Object value, ConfigOrigin origin, int seqId, String configId) {
this.key = key;
this.value = CONFIG_FILTER_LIST.contains(key) ? "<hidden>" : value;
this.value = (value != null && CONFIG_FILTER_LIST.contains(key)) ? "<hidden>" : value;
this.origin = origin;
this.seqId = seqId;
this.configId = configId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ void supportsEqualityCheck(
}
}

// Sensitive values are redacted by the property-name key under which they are collected. A couple
// of representative sensitive keys plus a non-sensitive control; the full filter list is kept in
// sync with the registry by SensitiveConfigRedactionTest.
@TableTest({
"scenario | key | value | filteredValue",
"DD_API_KEY | DD_API_KEY | somevalue | <hidden> ",
"dd.api-key | dd.api-key | somevalue | <hidden> ",
"dd.profiling.api-key | dd.profiling.api-key | somevalue | <hidden> ",
"dd.profiling.apikey | dd.profiling.apikey | somevalue | <hidden> ",
"some.other.key | some.other.key | somevalue | somevalue "
"scenario | key | value | filteredValue",
"api key | api-key | somevalue | <hidden> ",
"otlp traces headers | otlp.traces.headers | somevalue | <hidden> ",
"proxy password | profiling.proxy.password | somevalue | <hidden> ",
"non-sensitive key | some.other.key | somevalue | somevalue "
})
void filtersKeyValues(String key, String value, String filteredValue) {
assertEquals(filteredValue, ConfigSetting.of(key, value, ConfigOrigin.DEFAULT).stringValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package datadog.trace.api;

import static datadog.trace.util.ConfigStrings.toEnvVar;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.snakeyaml.engine.v2.api.Load;
import org.snakeyaml.engine.v2.api.LoadSettings;

/**
* Drift-guard test keeping the {@code "sensitive": true} entries in {@code
* metadata/supported-configurations.json} in sync with {@code ConfigSetting.CONFIG_FILTER_LIST}.
* The registry attribute is not read at runtime, so this test is what keeps the two from drifting.
*/
public class SensitiveConfigRedactionTest {

private static final String REGISTRY_RELATIVE_PATH = "metadata/supported-configurations.json";

// Normalizes a config name to the canonical token under which its value is COLLECTED, so the
// registry's public names line up with the property-name forms in CONFIG_FILTER_LIST. toEnvVar
// upper-cases and replaces "." / "-" with "_"; we strip a leading "DD_" so the dotted property
// name and the DD_ env-var form of the same config collapse together. OTLP exporter headers set
// via the OpenTelemetry env vars are collected under the Datadog otlp.<signal>.headers keys, so
// the OTEL_ names map onto that collected form.
private static String canonical(String name) {
String env = toEnvVar(name);
if (env.startsWith("DD_")) {
env = env.substring("DD_".length());
}
if (env.equals("OTEL_EXPORTER_OTLP_HEADERS")) {
// The generic OTEL header env var funnels into every otlp.<signal>.headers; traces stands in.
return "OTLP_TRACES_HEADERS";
}
if (env.startsWith("OTEL_EXPORTER_OTLP_") && env.endsWith("_HEADERS")) {
return "OTLP_" + env.substring("OTEL_EXPORTER_OTLP_".length());
}
return env;
}

@Test
void sensitiveRegistryEntriesAndFilterListStayInSync() {
Set<String> registryCanonical =
sensitiveRegistryKeys().stream()
.map(SensitiveConfigRedactionTest::canonical)
.collect(toTreeSet());
Set<String> filterCanonical =
configFilterList().stream()
.map(SensitiveConfigRedactionTest::canonical)
.collect(toTreeSet());

assertFalse(registryCanonical.isEmpty(), "expected at least one \"sensitive\": true config");
assertEquals(
registryCanonical,
filterCanonical,
"Registry \"sensitive\": true entries and ConfigSetting.CONFIG_FILTER_LIST must match after "
+ "canonicalization. Reconcile metadata/supported-configurations.json and "
+ "CONFIG_FILTER_LIST in ConfigSetting.java.");
}

// Registry keys for every entry marked "sensitive": true. Aliases are not collected separately --
// a value is always collected under its primary key's property name -- so they are not needed
// here.
@SuppressWarnings("unchecked")
private static Set<String> sensitiveRegistryKeys() {
Path registry = locateRegistry();
String content;
try {
content = new String(Files.readAllBytes(registry), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new UncheckedIOException("Failed to read " + registry, e);
}

Object parsed = new Load(LoadSettings.builder().build()).loadFromString(content);
Map<String, Object> root = (Map<String, Object>) parsed;
Map<String, Object> supported = (Map<String, Object>) root.get("supportedConfigurations");

Set<String> sensitive = new TreeSet<>();
for (Map.Entry<String, Object> entry : supported.entrySet()) {
for (Object def : (List<Object>) entry.getValue()) {
Map<String, Object> definition = (Map<String, Object>) def;
if (Boolean.TRUE.equals(definition.get("sensitive"))) {
sensitive.add(entry.getKey());
}
}
}
return sensitive;
}

// Reads CONFIG_FILTER_LIST from ConfigSetting via reflection.
@SuppressWarnings("unchecked")
private static Set<String> configFilterList() {
try {
Field field = ConfigSetting.class.getDeclaredField("CONFIG_FILTER_LIST");
field.setAccessible(true);
return (Set<String>) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException("Could not read ConfigSetting.CONFIG_FILTER_LIST", e);
}
}

// Walks up from the working directory until metadata/supported-configurations.json is found.
private static Path locateRegistry() {
Path dir = Paths.get(System.getProperty("user.dir")).toAbsolutePath();
for (Path current = dir; current != null; current = current.getParent()) {
Path candidate = current.resolve(REGISTRY_RELATIVE_PATH);
if (Files.isRegularFile(candidate)) {
return candidate;
}
}
throw new IllegalStateException("Could not locate " + REGISTRY_RELATIVE_PATH + " from " + dir);
}

private static java.util.stream.Collector<String, ?, TreeSet<String>> toTreeSet() {
return Collectors.toCollection(TreeSet::new);
}
}