-
Notifications
You must be signed in to change notification settings - Fork 1k
Opensearch transport query sanitization #15634
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
base: main
Are you sure you want to change the base?
Changes from all commits
dea68d6
ff7a85c
5c48733
fa40f55
29a2c2d
864c25f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,19 +18,20 @@ dependencies { | |
| library("org.opensearch.client:opensearch-java:3.0.0") | ||
| compileOnly("com.google.auto.value:auto-value-annotations") | ||
| annotationProcessor("com.google.auto.value:auto-value") | ||
| implementation("com.fasterxml.jackson.core:jackson-databind") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
|
|
||
| testImplementation("org.opensearch.client:opensearch-rest-client:3.0.0") | ||
| testImplementation(project(":instrumentation:opensearch:opensearch-rest-common:testing")) | ||
| testInstrumentation(project(":instrumentation:apache-httpclient:apache-httpclient-5.0:javaagent")) | ||
|
|
||
| // For testing AwsSdk2Transport | ||
| // AwsSdk2Transport supports awssdk version 2.26.0 | ||
| testInstrumentation(project(":instrumentation:apache-httpclient:apache-httpclient-4.0:javaagent")) | ||
| testInstrumentation(project(":instrumentation:netty:netty-4.1:javaagent")) | ||
| testImplementation("software.amazon.awssdk:auth:2.22.0") | ||
| testImplementation("software.amazon.awssdk:identity-spi:2.22.0") | ||
| testImplementation("software.amazon.awssdk:apache-client:2.22.0") | ||
| testImplementation("software.amazon.awssdk:netty-nio-client:2.22.0") | ||
| testImplementation("software.amazon.awssdk:regions:2.22.0") | ||
| testImplementation("software.amazon.awssdk:auth:2.26.0") | ||
| testImplementation("software.amazon.awssdk:identity-spi:2.26.0") | ||
| testImplementation("software.amazon.awssdk:apache-client:2.26.0") | ||
| testImplementation("software.amazon.awssdk:netty-nio-client:2.26.0") | ||
| testImplementation("software.amazon.awssdk:regions:2.26.0") | ||
| } | ||
|
|
||
| tasks { | ||
|
|
@@ -39,14 +40,47 @@ tasks { | |
| systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false") | ||
| } | ||
|
|
||
| test { | ||
| filter { | ||
| excludeTestsMatching("OpenSearchCaptureSearchQueryTest") | ||
| } | ||
| } | ||
|
|
||
| val testCaptureSearchQuery by registering(Test::class) { | ||
| testClassesDirs = sourceSets.test.get().output.classesDirs | ||
| classpath = sourceSets.test.get().runtimeClasspath | ||
|
|
||
| filter { | ||
| includeTestsMatching("OpenSearchCaptureSearchQueryTest") | ||
| } | ||
| jvmArgs("-Dotel.instrumentation.opensearch.capture-search-query=true") | ||
| } | ||
|
|
||
| val testStableSemconv by registering(Test::class) { | ||
| testClassesDirs = sourceSets.test.get().output.classesDirs | ||
| classpath = sourceSets.test.get().runtimeClasspath | ||
|
|
||
| filter { | ||
| excludeTestsMatching("OpenSearchCaptureSearchQueryTest") | ||
| } | ||
| jvmArgs("-Dotel.semconv-stability.opt-in=database") | ||
| systemProperty("metadataConfig", "otel.semconv-stability.opt-in=database") | ||
| } | ||
|
|
||
| val testCaptureSearchQueryStableSemconv by registering(Test::class) { | ||
| testClassesDirs = sourceSets.test.get().output.classesDirs | ||
| classpath = sourceSets.test.get().runtimeClasspath | ||
|
|
||
| filter { | ||
| includeTestsMatching("OpenSearchCaptureSearchQueryTest") | ||
| } | ||
| jvmArgs("-Dotel.instrumentation.opensearch.capture-search-query=true") | ||
| jvmArgs("-Dotel.semconv-stability.opt-in=database") | ||
| } | ||
|
|
||
| check { | ||
| dependsOn(testCaptureSearchQuery) | ||
| dependsOn(testStableSemconv) | ||
| dependsOn(testCaptureSearchQueryStableSemconv) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.opensearch.v3_0; | ||
|
|
||
| import static java.util.logging.Level.FINE; | ||
|
|
||
| import jakarta.json.stream.JsonGenerator; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Iterator; | ||
| import java.util.logging.Logger; | ||
| import javax.annotation.Nullable; | ||
| import org.opensearch.client.json.JsonpMapper; | ||
| import org.opensearch.client.json.NdJsonpSerializable; | ||
| import org.opensearch.client.transport.GenericSerializable; | ||
|
|
||
| public final class OpenSearchBodyExtractor { | ||
|
|
||
| private static final Logger logger = Logger.getLogger(OpenSearchBodyExtractor.class.getName()); | ||
|
|
||
| @Nullable | ||
| public static String extract(JsonpMapper mapper, Object request) { | ||
| try { | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
|
|
||
| if (request instanceof NdJsonpSerializable) { | ||
| writeNdJson(mapper, (NdJsonpSerializable) request, baos); | ||
| } else if (request instanceof GenericSerializable) { | ||
| ((GenericSerializable) request).serialize(baos); | ||
| } else { | ||
| JsonGenerator generator = mapper.jsonProvider().createGenerator(baos); | ||
| mapper.serialize(request, generator); | ||
| generator.close(); | ||
| } | ||
|
|
||
| String body = baos.toString(StandardCharsets.UTF_8); | ||
| return body.isEmpty() ? null : body; | ||
| } catch (RuntimeException e) { | ||
| logger.log(FINE, "Failure extracting body", e); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static void writeNdJson( | ||
| JsonpMapper mapper, NdJsonpSerializable value, ByteArrayOutputStream baos) { | ||
| try { | ||
| Iterator<?> values = value._serializables(); | ||
| while (values.hasNext()) { | ||
| Object item = values.next(); | ||
| if (item instanceof NdJsonpSerializable && item != value) { | ||
| // do not recurse on the item itself | ||
| writeNdJson(mapper, (NdJsonpSerializable) item, baos); | ||
| } else { | ||
| JsonGenerator generator = mapper.jsonProvider().createGenerator(baos); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering whether instead of serializing the query to json string and then parsing that json, sanitizing it and serializing it again to json string you could wrap the |
||
| mapper.serialize(item, generator); | ||
| generator.close(); | ||
| baos.write('\n'); | ||
| } | ||
| } | ||
| } catch (RuntimeException e) { | ||
| logger.log(FINE, "Failure serializing NdJson", e); | ||
| } | ||
| } | ||
|
|
||
| private OpenSearchBodyExtractor() {} | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.opensearch.v3_0; | ||
|
|
||
| import static java.util.logging.Level.FINE; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import com.fasterxml.jackson.databind.node.TextNode; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.logging.Logger; | ||
|
|
||
| public final class OpenSearchBodySanitizer { | ||
|
|
||
| private static final Logger logger = Logger.getLogger(OpenSearchBodySanitizer.class.getName()); | ||
|
|
||
| private static final ObjectMapper DEFAULT_OBJECT_MAPPER = new ObjectMapper(); | ||
| private static final String MASKED_VALUE = "?"; | ||
| private static final OpenSearchBodySanitizer DEFAULT_INSTANCE = | ||
| new OpenSearchBodySanitizer(DEFAULT_OBJECT_MAPPER); | ||
|
|
||
| private final ObjectMapper objectMapper; | ||
|
|
||
| private OpenSearchBodySanitizer(ObjectMapper objectMapper) { | ||
| this.objectMapper = objectMapper; | ||
| } | ||
|
|
||
| public static String sanitize(String jsonString) { | ||
| return DEFAULT_INSTANCE.sanitizeInstance(jsonString); | ||
| } | ||
|
|
||
| public String sanitizeInstance(String jsonString) { | ||
| if (jsonString == null) { | ||
| return null; | ||
| } | ||
|
|
||
| List<String> queries = QuerySplitter.splitQueries(jsonString); | ||
| if (queries.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| List<String> sanitizedQueries = new ArrayList<>(); | ||
| for (String query : queries) { | ||
| String sanitized = sanitizeSingleQuery(query); | ||
| sanitizedQueries.add(sanitized); | ||
| } | ||
|
|
||
| return QuerySplitter.joinQueries(sanitizedQueries); | ||
| } | ||
|
|
||
| private String sanitizeSingleQuery(String query) { | ||
| try { | ||
| JsonNode rootNode = objectMapper.readTree(query); | ||
| JsonNode sanitizedNode = sanitizeNode(rootNode); | ||
| return objectMapper.writeValueAsString(sanitizedNode); | ||
| } catch (Exception e) { | ||
| logger.log(FINE, "Failure sanitizing single query", e); | ||
| return query; | ||
| } | ||
| } | ||
|
|
||
| private JsonNode sanitizeNode(JsonNode node) { | ||
| if (node == null || node.isNull()) { | ||
| return node; | ||
| } | ||
|
|
||
| if (node.isTextual()) { | ||
| return new TextNode(MASKED_VALUE); | ||
| } | ||
|
|
||
| if (node.isNumber() || node.isBoolean()) { | ||
| return new TextNode(MASKED_VALUE); | ||
| } | ||
|
|
||
| if (node.isArray()) { | ||
| ArrayNode arrayNode = objectMapper.createArrayNode(); | ||
| for (JsonNode element : node) { | ||
| arrayNode.add(sanitizeNode(element)); | ||
| } | ||
| return arrayNode; | ||
| } | ||
|
|
||
| if (node.isObject()) { | ||
| ObjectNode objectNode = objectMapper.createObjectNode(); | ||
|
|
||
| for (Map.Entry<String, JsonNode> field : node.properties()) { | ||
| String key = field.getKey(); | ||
| JsonNode value = field.getValue(); | ||
|
|
||
| objectNode.set(key, sanitizeNode(value)); | ||
| } | ||
| return objectNode; | ||
| } | ||
|
|
||
| return node; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change seems unrelated to opensearch