|
| 1 | +package com.sap.hcf.cf.logging.opentelemetry.agent.ext.exporter; |
| 2 | + |
| 3 | +import io.opentelemetry.api.common.AttributeKey; |
| 4 | +import io.opentelemetry.api.common.Attributes; |
| 5 | +import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; |
| 6 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 7 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 11 | +import org.mockito.ArgumentCaptor; |
| 12 | +import org.mockito.Captor; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +@ExtendWith(MockitoExtension.class) |
| 25 | +class SanitizeSpanExporterCustomizerTest { |
| 26 | + |
| 27 | + @Mock(strictness = Mock.Strictness.LENIENT) |
| 28 | + private SpanData spanData; |
| 29 | + |
| 30 | + @Mock |
| 31 | + private SpanExporter delegateExporter; |
| 32 | + |
| 33 | + @Captor |
| 34 | + private ArgumentCaptor<List<SpanData>> spanDataCaptor; |
| 35 | + |
| 36 | + private SpanExporter sanitizeExporter; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + void setUp() { |
| 40 | + when(spanData.getName()).thenReturn("test-span"); |
| 41 | + this.sanitizeExporter = new SanitizeSpanExporterCustomizer().apply(delegateExporter, null); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void forwardsSpanWithoutAttributes() { |
| 46 | + List<SpanData> spans = List.of(spanData); |
| 47 | + sanitizeExporter.export(spans); |
| 48 | + |
| 49 | + verify(delegateExporter).export(spans); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void forwardsSpanWithEmptyAttributes() { |
| 54 | + List<SpanData> spans = List.of(spanData); |
| 55 | + when(spanData.getAttributes()).thenReturn(Attributes.empty()); |
| 56 | + sanitizeExporter.export(spans); |
| 57 | + |
| 58 | + verify(delegateExporter).export(spans); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void forwardsSpanWithoutSensitiveAttributeKey() { |
| 63 | + Attributes attributes = Attributes.builder().put("some.key", "some value").build(); |
| 64 | + when(spanData.getAttributes()).thenReturn(attributes); |
| 65 | + List<SpanData> spans = List.of(spanData); |
| 66 | + sanitizeExporter.export(spans); |
| 67 | + |
| 68 | + verify(delegateExporter).export(spans); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void forwardsSpanWithSensitiveAttributeKeyButWithoutSensitiveValue() { |
| 73 | + Attributes attributes = Attributes.builder().put("db.query.text", "some safe value").build(); |
| 74 | + when(spanData.getAttributes()).thenReturn(attributes); |
| 75 | + List<SpanData> spans = List.of(spanData); |
| 76 | + sanitizeExporter.export(spans); |
| 77 | + |
| 78 | + verify(delegateExporter).export(spans); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void redactsSensitiveDbQueryTextValue() { |
| 83 | + Attributes attributes = Attributes.builder().put("db.query.text", "Connect somewhere").build(); |
| 84 | + when(spanData.getAttributes()).thenReturn(attributes); |
| 85 | + List<SpanData> spans = List.of(spanData); |
| 86 | + sanitizeExporter.export(spans); |
| 87 | + |
| 88 | + verify(delegateExporter).export(spanDataCaptor.capture()); |
| 89 | + SpanData sanitizedSpan = spanDataCaptor.getValue().get(0); |
| 90 | + assertThat(sanitizedSpan).extracting(SpanData::getName).isEqualTo("test-span"); |
| 91 | + assertThat(sanitizedSpan).extracting(SpanData::getAttributes) |
| 92 | + .extracting(attrs -> attrs.get(AttributeKey.stringKey("db.query.text"))) |
| 93 | + .isEqualTo("Connect [REDACTED]"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void redactsSensitiveDbStatementValue() { |
| 98 | + Attributes attributes = Attributes.builder().put("db.statement", "CONNECT somewhere").build(); |
| 99 | + when(spanData.getAttributes()).thenReturn(attributes); |
| 100 | + List<SpanData> spans = List.of(spanData); |
| 101 | + sanitizeExporter.export(spans); |
| 102 | + |
| 103 | + verify(delegateExporter).export(spanDataCaptor.capture()); |
| 104 | + SpanData sanitizedSpan = spanDataCaptor.getValue().get(0); |
| 105 | + assertThat(sanitizedSpan).extracting(SpanData::getName).isEqualTo("test-span"); |
| 106 | + assertThat(sanitizedSpan).extracting(SpanData::getAttributes) |
| 107 | + .extracting(attrs -> attrs.get(AttributeKey.stringKey("db.statement"))) |
| 108 | + .isEqualTo("CONNECT [REDACTED]"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void keepsOtherAttributesOnRedaction() { |
| 113 | + Attributes attributes = |
| 114 | + Attributes.builder().put("db.query.text", "connect somewhere").put("some.key", "some.value").build(); |
| 115 | + when(spanData.getAttributes()).thenReturn(attributes); |
| 116 | + List<SpanData> spans = List.of(spanData); |
| 117 | + sanitizeExporter.export(spans); |
| 118 | + |
| 119 | + verify(delegateExporter).export(spanDataCaptor.capture()); |
| 120 | + SpanData sanitizedSpan = spanDataCaptor.getValue().get(0); |
| 121 | + assertThat(sanitizedSpan).extracting(SpanData::getName).isEqualTo("test-span"); |
| 122 | + assertThat(sanitizedSpan).extracting(SpanData::getAttributes) |
| 123 | + .extracting(attrs -> attrs.get(AttributeKey.stringKey("db.query.text"))) |
| 124 | + .isEqualTo("connect [REDACTED]"); |
| 125 | + assertThat(sanitizedSpan).extracting(SpanData::getAttributes) |
| 126 | + .extracting(attrs -> attrs.get(AttributeKey.stringKey("some.key"))) |
| 127 | + .isEqualTo("some.value"); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void canBeDisabledViaConfig() { |
| 132 | + Map<String, String> configEntries = new HashMap<>(); |
| 133 | + configEntries.put("sap.cf.integration.otel.extension.sanitizer.enabled", "false"); |
| 134 | + DefaultConfigProperties configProperties = DefaultConfigProperties.createFromMap(configEntries); |
| 135 | + SpanExporter spanExporter = new SanitizeSpanExporterCustomizer().apply(delegateExporter, configProperties); |
| 136 | + assertThat(spanExporter).isSameAs(delegateExporter); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void delegatesFlush() { |
| 141 | + sanitizeExporter.flush(); |
| 142 | + verify(delegateExporter).flush(); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void delegatesShutdown() { |
| 147 | + sanitizeExporter.shutdown(); |
| 148 | + verify(delegateExporter).shutdown(); |
| 149 | + } |
| 150 | +} |
0 commit comments