-
Notifications
You must be signed in to change notification settings - Fork 50
Add sanitization for span attributes #314
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
86 changes: 86 additions & 0 deletions
86
...m/sap/hcf/cf/logging/opentelemetry/agent/ext/exporter/SanitizeSpanExporterCustomizer.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,86 @@ | ||
| package com.sap.hcf.cf.logging.opentelemetry.agent.ext.exporter; | ||
|
|
||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.AttributesBuilder; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
| import io.opentelemetry.sdk.common.CompletableResultCode; | ||
| import io.opentelemetry.sdk.trace.data.DelegatingSpanData; | ||
| import io.opentelemetry.sdk.trace.data.SpanData; | ||
| import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.function.BiFunction; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static io.opentelemetry.api.common.AttributeKey.stringKey; | ||
|
|
||
| public class SanitizeSpanExporterCustomizer implements BiFunction<SpanExporter, ConfigProperties, SpanExporter> { | ||
|
|
||
| private static final String PROPERTY_ENABLED_KEY = "sap.cf.integration.otel.extension.sanitizer.enabled"; | ||
| private static final AttributeKey<String> DB_QUERY_TEXT = stringKey("db.query.text"); | ||
| //@Deprecated | ||
| private static final AttributeKey<String> DB_STATEMENT = stringKey("db.statement"); | ||
|
|
||
| @Override | ||
| public SpanExporter apply(SpanExporter delegate, ConfigProperties config) { | ||
| if (config != null && !config.getBoolean(PROPERTY_ENABLED_KEY, true)) { | ||
| return delegate; | ||
| } | ||
| return new SpanExporter() { | ||
| @Override | ||
| public CompletableResultCode export(Collection<SpanData> spans) { | ||
| return delegate.export(spans.stream().map(this::sanitizeSpanData).collect(Collectors.toList())); | ||
| } | ||
|
|
||
| private SpanData sanitizeSpanData(SpanData spanData) { | ||
| Attributes attributes = spanData.getAttributes(); | ||
| if (attributes == null) { | ||
| return spanData; | ||
| } | ||
| String dbQueryText = attributes.get(DB_QUERY_TEXT); | ||
| String dbStatement = attributes.get(DB_STATEMENT); | ||
| if (isClean(dbQueryText) && isClean(dbStatement)) { | ||
| return spanData; | ||
| } | ||
| AttributesBuilder sanitized = attributes.toBuilder(); | ||
| if (!isClean(dbQueryText)) { | ||
| sanitized.put(DB_QUERY_TEXT, dbQueryText.substring(0, 7) + " [REDACTED]"); | ||
| } | ||
| if (!isClean(dbStatement)) { | ||
| sanitized.put(DB_STATEMENT, dbStatement.substring(0, 7) + " [REDACTED]"); | ||
| } | ||
| return new SanitizedSpanData(spanData, sanitized.build()); | ||
| } | ||
|
|
||
| private boolean isClean(String query) { | ||
| return query == null || !query.toLowerCase().startsWith("connect"); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode flush() { | ||
| return delegate.flush(); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode shutdown() { | ||
| return delegate.shutdown(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private static class SanitizedSpanData extends DelegatingSpanData { | ||
|
|
||
| private final Attributes filteredAttributes; | ||
|
|
||
| protected SanitizedSpanData(SpanData delegate, Attributes filteredAttributes) { | ||
| super(delegate); | ||
| this.filteredAttributes = filteredAttributes; | ||
| } | ||
|
|
||
| @Override | ||
| public Attributes getAttributes() { | ||
| return filteredAttributes; | ||
| } | ||
| } | ||
| } | ||
150 changes: 150 additions & 0 deletions
150
...p/hcf/cf/logging/opentelemetry/agent/ext/exporter/SanitizeSpanExporterCustomizerTest.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,150 @@ | ||
| package com.sap.hcf.cf.logging.opentelemetry.agent.ext.exporter; | ||
|
|
||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; | ||
| import io.opentelemetry.sdk.trace.data.SpanData; | ||
| import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.Captor; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class SanitizeSpanExporterCustomizerTest { | ||
|
|
||
| @Mock(strictness = Mock.Strictness.LENIENT) | ||
| private SpanData spanData; | ||
|
|
||
| @Mock | ||
| private SpanExporter delegateExporter; | ||
|
|
||
| @Captor | ||
| private ArgumentCaptor<List<SpanData>> spanDataCaptor; | ||
|
|
||
| private SpanExporter sanitizeExporter; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| when(spanData.getName()).thenReturn("test-span"); | ||
| this.sanitizeExporter = new SanitizeSpanExporterCustomizer().apply(delegateExporter, null); | ||
| } | ||
|
|
||
| @Test | ||
| void forwardsSpanWithoutAttributes() { | ||
| List<SpanData> spans = List.of(spanData); | ||
| sanitizeExporter.export(spans); | ||
|
|
||
| verify(delegateExporter).export(spans); | ||
| } | ||
|
|
||
| @Test | ||
| void forwardsSpanWithEmptyAttributes() { | ||
| List<SpanData> spans = List.of(spanData); | ||
| when(spanData.getAttributes()).thenReturn(Attributes.empty()); | ||
| sanitizeExporter.export(spans); | ||
|
|
||
| verify(delegateExporter).export(spans); | ||
| } | ||
|
|
||
| @Test | ||
| void forwardsSpanWithoutSensitiveAttributeKey() { | ||
| Attributes attributes = Attributes.builder().put("some.key", "some value").build(); | ||
| when(spanData.getAttributes()).thenReturn(attributes); | ||
| List<SpanData> spans = List.of(spanData); | ||
| sanitizeExporter.export(spans); | ||
|
|
||
| verify(delegateExporter).export(spans); | ||
| } | ||
|
|
||
| @Test | ||
| void forwardsSpanWithSensitiveAttributeKeyButWithoutSensitiveValue() { | ||
| Attributes attributes = Attributes.builder().put("db.query.text", "some safe value").build(); | ||
| when(spanData.getAttributes()).thenReturn(attributes); | ||
| List<SpanData> spans = List.of(spanData); | ||
| sanitizeExporter.export(spans); | ||
|
|
||
| verify(delegateExporter).export(spans); | ||
| } | ||
|
|
||
| @Test | ||
| void redactsSensitiveDbQueryTextValue() { | ||
| Attributes attributes = Attributes.builder().put("db.query.text", "Connect somewhere").build(); | ||
| when(spanData.getAttributes()).thenReturn(attributes); | ||
| List<SpanData> spans = List.of(spanData); | ||
| sanitizeExporter.export(spans); | ||
|
|
||
| verify(delegateExporter).export(spanDataCaptor.capture()); | ||
| SpanData sanitizedSpan = spanDataCaptor.getValue().get(0); | ||
| assertThat(sanitizedSpan).extracting(SpanData::getName).isEqualTo("test-span"); | ||
| assertThat(sanitizedSpan).extracting(SpanData::getAttributes) | ||
| .extracting(attrs -> attrs.get(AttributeKey.stringKey("db.query.text"))) | ||
| .isEqualTo("Connect [REDACTED]"); | ||
| } | ||
|
|
||
| @Test | ||
| void redactsSensitiveDbStatementValue() { | ||
| Attributes attributes = Attributes.builder().put("db.statement", "CONNECT somewhere").build(); | ||
| when(spanData.getAttributes()).thenReturn(attributes); | ||
| List<SpanData> spans = List.of(spanData); | ||
| sanitizeExporter.export(spans); | ||
|
|
||
| verify(delegateExporter).export(spanDataCaptor.capture()); | ||
| SpanData sanitizedSpan = spanDataCaptor.getValue().get(0); | ||
| assertThat(sanitizedSpan).extracting(SpanData::getName).isEqualTo("test-span"); | ||
| assertThat(sanitizedSpan).extracting(SpanData::getAttributes) | ||
| .extracting(attrs -> attrs.get(AttributeKey.stringKey("db.statement"))) | ||
| .isEqualTo("CONNECT [REDACTED]"); | ||
| } | ||
|
|
||
| @Test | ||
| void keepsOtherAttributesOnRedaction() { | ||
| Attributes attributes = | ||
| Attributes.builder().put("db.query.text", "connect somewhere").put("some.key", "some.value").build(); | ||
| when(spanData.getAttributes()).thenReturn(attributes); | ||
| List<SpanData> spans = List.of(spanData); | ||
| sanitizeExporter.export(spans); | ||
|
|
||
| verify(delegateExporter).export(spanDataCaptor.capture()); | ||
| SpanData sanitizedSpan = spanDataCaptor.getValue().get(0); | ||
| assertThat(sanitizedSpan).extracting(SpanData::getName).isEqualTo("test-span"); | ||
| assertThat(sanitizedSpan).extracting(SpanData::getAttributes) | ||
| .extracting(attrs -> attrs.get(AttributeKey.stringKey("db.query.text"))) | ||
| .isEqualTo("connect [REDACTED]"); | ||
| assertThat(sanitizedSpan).extracting(SpanData::getAttributes) | ||
| .extracting(attrs -> attrs.get(AttributeKey.stringKey("some.key"))) | ||
| .isEqualTo("some.value"); | ||
| } | ||
|
|
||
| @Test | ||
| void canBeDisabledViaConfig() { | ||
| Map<String, String> configEntries = new HashMap<>(); | ||
| configEntries.put("sap.cf.integration.otel.extension.sanitizer.enabled", "false"); | ||
| DefaultConfigProperties configProperties = DefaultConfigProperties.createFromMap(configEntries); | ||
| SpanExporter spanExporter = new SanitizeSpanExporterCustomizer().apply(delegateExporter, configProperties); | ||
| assertThat(spanExporter).isSameAs(delegateExporter); | ||
| } | ||
|
|
||
| @Test | ||
| void delegatesFlush() { | ||
| sanitizeExporter.flush(); | ||
| verify(delegateExporter).flush(); | ||
| } | ||
|
|
||
| @Test | ||
| void delegatesShutdown() { | ||
| sanitizeExporter.shutdown(); | ||
| verify(delegateExporter).shutdown(); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.