-
Notifications
You must be signed in to change notification settings - Fork 1.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
Open
mznet
wants to merge
14
commits into
open-telemetry:main
Choose a base branch
from
mznet:opensearch-transport-query-sanitization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
dea68d6
sanitize opensearch transport query body
mznet ff7a85c
do not extract query body in case of non-search request
mznet 5c48733
logging extraction and sanitization failures
mznet fa40f55
fix markdown formatting and remove specific jackson-databind version
mznet 29a2c2d
fix: remove duplicated and unused code
mznet 864c25f
add overhead description
mznet ccc1846
remove unrelated code
mznet a2c772e
change implementation to compileOnly
mznet 0d2452b
sanitize query with JsonGenerator
mznet 5283ac7
fix Readme.md format
mznet c4bae7b
refactor: replace else-if chain with early return pattern in OpenSear…
mznet 2760da2
Merge branch 'main' into opensearch-transport-query-sanitization
mznet 01ef0ea
fix: replace AgentInstrumentationConfig with ConfigPropertiesUtil
mznet 0bad411
refactor: migrate OpenSearch config to DeclarativeConfigUtil
mznet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...a/io/opentelemetry/javaagent/instrumentation/opensearch/v3_0/OpenSearchBodyExtractor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * 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.core.JsonFactory; | ||
| 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.json.jackson.JacksonJsonpGenerator; | ||
| import org.opensearch.client.json.jackson.JacksonJsonpMapper; | ||
| import org.opensearch.client.transport.GenericSerializable; | ||
|
|
||
| public final class OpenSearchBodyExtractor { | ||
|
|
||
| private static final Logger logger = Logger.getLogger(OpenSearchBodyExtractor.class.getName()); | ||
| private static final String QUERY_SEPARATOR = ";"; | ||
| private static final JsonFactory JSON_FACTORY = new JsonFactory(); | ||
|
|
||
| @Nullable | ||
| public static String extractSanitized(JsonpMapper mapper, Object request) { | ||
| try { | ||
| if (request instanceof NdJsonpSerializable) { | ||
| return serializeNdJsonSanitized(mapper, (NdJsonpSerializable) request); | ||
| } | ||
|
|
||
| if (request instanceof GenericSerializable) { | ||
| // GenericSerializable writes directly to output stream, cannot sanitize | ||
| // This path is typically not used for search queries | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| ((GenericSerializable) request).serialize(baos); | ||
| String body = baos.toString(StandardCharsets.UTF_8); | ||
| return body.isEmpty() ? null : body; | ||
| } | ||
|
|
||
| return serializeSanitized(mapper, request); | ||
| } catch (RuntimeException e) { | ||
| logger.log(FINE, "Failure extracting body", e); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static String serializeSanitized(JsonpMapper mapper, Object item) { | ||
| try { | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
|
|
||
| if (mapper instanceof JacksonJsonpMapper) { | ||
| // Use Jackson-based sanitizing generator for JacksonJsonpMapper | ||
| com.fasterxml.jackson.core.JsonGenerator jacksonGenerator = | ||
| JSON_FACTORY.createGenerator(baos); | ||
| com.fasterxml.jackson.core.JsonGenerator sanitizingGenerator = | ||
| new SanitizingJacksonJsonGenerator(jacksonGenerator); | ||
| JsonGenerator generator = new JacksonJsonpGenerator(sanitizingGenerator); | ||
| mapper.serialize(item, generator); | ||
| generator.close(); | ||
| } else { | ||
| // Fallback for other mappers (may not work for all implementations) | ||
| JsonGenerator rawGenerator = mapper.jsonProvider().createGenerator(baos); | ||
| JsonGenerator generator = new SanitizingJsonGenerator(rawGenerator); | ||
| mapper.serialize(item, generator); | ||
| generator.close(); | ||
| } | ||
|
|
||
| String result = baos.toString(StandardCharsets.UTF_8).trim(); | ||
| return result.isEmpty() ? null : result; | ||
| } catch (Exception e) { | ||
| logger.log(FINE, "Failure serializing item", e); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static String serializeNdJsonSanitized(JsonpMapper mapper, NdJsonpSerializable value) { | ||
| try { | ||
| StringBuilder result = new StringBuilder(); | ||
| Iterator<?> values = value._serializables(); | ||
| boolean first = true; | ||
|
|
||
| while (values.hasNext()) { | ||
| Object item = values.next(); | ||
| String itemStr; | ||
|
|
||
| if (item instanceof NdJsonpSerializable && item != value) { | ||
| // Recursively handle nested NdJsonpSerializable | ||
| itemStr = serializeNdJsonSanitized(mapper, (NdJsonpSerializable) item); | ||
| } else { | ||
| itemStr = serializeSanitized(mapper, item); | ||
| } | ||
|
|
||
| if (itemStr != null && !itemStr.isEmpty()) { | ||
| if (!first) { | ||
| result.append(QUERY_SEPARATOR); | ||
| } | ||
| result.append(itemStr); | ||
| first = false; | ||
| } | ||
| } | ||
|
|
||
| return result.length() == 0 ? null : result.toString(); | ||
| } catch (RuntimeException e) { | ||
| logger.log(FINE, "Failure serializing NdJson", e); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private OpenSearchBodyExtractor() {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Try changing it to something similar to
opentelemetry-java-instrumentation/instrumentation/pulsar/pulsar-2.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/pulsar/v2_8/telemetry/PulsarSingletons.java
Lines 146 to 147 in 2bc92d2
replace
pulsarwithopensearchandexperimental_span_attributes/developmentwithcapture_search_query