Skip to content

Commit f12bfaa

Browse files
Redact headers when exporting internal telemetry through OTLP
1 parent 85c4dac commit f12bfaa

9 files changed

Lines changed: 188 additions & 2 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package migration // import "go.opentelemetry.io/collector/service/telemetry/otelconftelemetry/internal/migration"
5+
6+
import "strings"
7+
8+
func redactHeaderPath(config any, path []string) {
9+
if len(path) == 0 {
10+
return
11+
}
12+
next, rest := path[0], path[1:]
13+
if next == "*" {
14+
if configArray, ok := config.([]any); ok {
15+
for _, elem := range configArray {
16+
redactHeaderPath(elem, rest)
17+
}
18+
}
19+
} else if configMap, ok := config.(map[string]any); ok {
20+
for _, nextKey := range strings.Split(next, "|") {
21+
if len(path) == 1 {
22+
configMap[nextKey] = "[REDACTED]"
23+
} else if elem, ok := configMap[nextKey]; ok {
24+
redactHeaderPath(elem, rest)
25+
}
26+
}
27+
}
28+
}
29+
30+
func redactHeaders(config any, path string) {
31+
redactHeaderPath(config, strings.Split(path, "."))
32+
}

service/telemetry/otelconftelemetry/internal/migration/testdata/v0.3.0_logs.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ processors:
1919
headers:
2020
- name: "key1"
2121
value: "value1"
22+
error_output_paths: []
23+
output_paths: []
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
level: "info"
2+
processors:
3+
- batch:
4+
exporter:
5+
otlp:
6+
protocol: http/protobuf
7+
endpoint: https://127.0.0.1:4317
8+
headers:
9+
- name: "key1"
10+
value: "[REDACTED]"
11+
- simple:
12+
exporter:
13+
console: {}
14+
- simple:
15+
exporter:
16+
otlp:
17+
protocol: http/protobuf
18+
endpoint: http://127.0.0.1:4317
19+
headers:
20+
- name: "key1"
21+
value: "[REDACTED]"
22+
error_output_paths: []
23+
output_paths: []
24+
encoding: ""
25+
sampling: null

service/telemetry/otelconftelemetry/internal/migration/testdata/v0.3.0_metrics.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
level: detailed
1+
level: Detailed
22
readers:
33
- periodic:
44
exporter:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
level: Detailed
2+
readers:
3+
- periodic:
4+
exporter:
5+
otlp:
6+
protocol: http/protobuf
7+
endpoint: https://127.0.0.1:4317
8+
headers:
9+
- name: "key1"
10+
value: "[REDACTED]"
11+
- pull:
12+
exporter:
13+
prometheus:
14+
host: 127.0.0.1
15+
port: 8902

service/telemetry/otelconftelemetry/internal/migration/testdata/v0.3.0_traces.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
level: "none"
1+
level: "None"
22
processors:
33
- batch:
44
exporter:
@@ -19,6 +19,7 @@ processors:
1919
headers:
2020
- name: "key1"
2121
value: "value1"
22+
propagators: []
2223
sampler:
2324
parent_based:
2425
root:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
level: "None"
2+
processors:
3+
- batch:
4+
exporter:
5+
otlp:
6+
protocol: http/protobuf
7+
endpoint: https://127.0.0.1:4317
8+
headers:
9+
- name: "key1"
10+
value: "[REDACTED]"
11+
- simple:
12+
exporter:
13+
console: {}
14+
- simple:
15+
exporter:
16+
otlp:
17+
protocol: http/protobuf
18+
endpoint: http://127.0.0.1:4317
19+
headers:
20+
- name: "key1"
21+
value: "[REDACTED]"
22+
propagators: []
23+
sampler:
24+
parent_based:
25+
root:
26+
trace_id_ratio_based:
27+
ratio: 0.01

service/telemetry/otelconftelemetry/internal/migration/v0.3.0.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package migration // import "go.opentelemetry.io/collector/service/telemetry/otelconftelemetry/internal/migration"
55

66
import (
7+
"fmt"
78
"time"
89

910
config "go.opentelemetry.io/contrib/otelconf/v0.3.0"
@@ -58,6 +59,19 @@ func (c *TracesConfigV030) Unmarshal(conf *confmap.Conf) error {
5859
return nil
5960
}
6061

62+
var _ confmap.Marshaler = TracesConfigV030{}
63+
64+
func (c TracesConfigV030) Marshal(conf *confmap.Conf) error {
65+
if err := conf.Marshal(c); err != nil {
66+
return fmt.Errorf("otelconftelemetry: failed to marshal traces configuration: %w", err)
67+
}
68+
69+
// Redact header values the way configopaque would
70+
sm := conf.ToStringMap()
71+
redactHeaders(sm, "processors.*.simple|batch.exporter.otlp.headers.*.value")
72+
return conf.Marshal(sm)
73+
}
74+
6175
type MetricsConfigV030 struct {
6276
// Level is the level of telemetry metrics, the possible values are:
6377
// - "none" indicates that no telemetry data should be collected;
@@ -95,6 +109,19 @@ func (c *MetricsConfigV030) Unmarshal(conf *confmap.Conf) error {
95109
return nil
96110
}
97111

112+
var _ confmap.Marshaler = MetricsConfigV030{}
113+
114+
func (c MetricsConfigV030) Marshal(conf *confmap.Conf) error {
115+
if err := conf.Marshal(c); err != nil {
116+
return fmt.Errorf("otelconftelemetry: failed to marshal metrics configuration: %w", err)
117+
}
118+
119+
// Redact header values the way configopaque would
120+
sm := conf.ToStringMap()
121+
redactHeaders(sm, "readers.*.periodic.exporter.otlp.headers.*.value")
122+
return conf.Marshal(sm)
123+
}
124+
98125
type LogsConfigV030 struct {
99126
// Level is the minimum enabled logging level.
100127
// (default = "INFO")
@@ -220,3 +247,16 @@ func (c *LogsConfigV030) Unmarshal(conf *confmap.Conf) error {
220247
}
221248
return nil
222249
}
250+
251+
var _ confmap.Marshaler = LogsConfigV030{}
252+
253+
func (c LogsConfigV030) Marshal(conf *confmap.Conf) error {
254+
if err := conf.Marshal(c); err != nil {
255+
return fmt.Errorf("otelconftelemetry: failed to marshal logs configuration: %w", err)
256+
}
257+
258+
// Redact header values the way configopaque would
259+
sm := conf.ToStringMap()
260+
redactHeaders(sm, "processors.*.simple|batch.exporter.otlp.headers.*.value")
261+
return conf.Marshal(sm)
262+
}

service/telemetry/otelconftelemetry/internal/migration/v0.3.0_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"path/filepath"
88
"testing"
99

10+
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112

13+
"go.opentelemetry.io/collector/confmap"
1214
"go.opentelemetry.io/collector/confmap/confmaptest"
1315
)
1416

@@ -25,6 +27,20 @@ func TestUnmarshalLogsConfigV030(t *testing.T) {
2527
require.Equal(t, "http://127.0.0.1:4317", *cfg.Processors[2].Simple.Exporter.OTLP.Endpoint)
2628
}
2729

30+
func TestMarshalLogsConfigV030(t *testing.T) {
31+
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "v0.3.0_logs.yaml"))
32+
require.NoError(t, err)
33+
cm2, err := confmaptest.LoadConf(filepath.Join("testdata", "v0.3.0_logs_marshaled.yaml"))
34+
require.NoError(t, err)
35+
36+
cfg := LogsConfigV030{}
37+
require.NoError(t, cm.Unmarshal(&cfg))
38+
cm3 := confmap.New()
39+
require.NoError(t, cm3.Marshal(&cfg))
40+
41+
assert.Equal(t, cm2.ToStringMap(), cm3.ToStringMap())
42+
}
43+
2844
func TestUnmarshalTracesConfigV030(t *testing.T) {
2945
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "v0.3.0_traces.yaml"))
3046
require.NoError(t, err)
@@ -38,6 +54,20 @@ func TestUnmarshalTracesConfigV030(t *testing.T) {
3854
require.Equal(t, "http://127.0.0.1:4317", *cfg.Processors[2].Simple.Exporter.OTLP.Endpoint)
3955
}
4056

57+
func TestMarshalTracesConfigV030(t *testing.T) {
58+
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "v0.3.0_traces.yaml"))
59+
require.NoError(t, err)
60+
cm2, err := confmaptest.LoadConf(filepath.Join("testdata", "v0.3.0_traces_marshaled.yaml"))
61+
require.NoError(t, err)
62+
63+
cfg := TracesConfigV030{}
64+
require.NoError(t, cm.Unmarshal(&cfg))
65+
cm3 := confmap.New()
66+
require.NoError(t, cm3.Marshal(&cfg))
67+
68+
assert.Equal(t, cm2.ToStringMap(), cm3.ToStringMap())
69+
}
70+
4171
func TestUnmarshalMetricsConfigV030(t *testing.T) {
4272
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "v0.3.0_metrics.yaml"))
4373
require.NoError(t, err)
@@ -49,3 +79,17 @@ func TestUnmarshalMetricsConfigV030(t *testing.T) {
4979
// check the endpoint is prefixed w/ https
5080
require.Equal(t, "https://127.0.0.1:4317", *cfg.Readers[0].Periodic.Exporter.OTLP.Endpoint)
5181
}
82+
83+
func TestMarshalMetricsConfigV030(t *testing.T) {
84+
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "v0.3.0_metrics.yaml"))
85+
require.NoError(t, err)
86+
cm2, err := confmaptest.LoadConf(filepath.Join("testdata", "v0.3.0_metrics_marshaled.yaml"))
87+
require.NoError(t, err)
88+
89+
cfg := MetricsConfigV030{}
90+
require.NoError(t, cm.Unmarshal(&cfg))
91+
cm3 := confmap.New()
92+
require.NoError(t, cm3.Marshal(&cfg))
93+
94+
assert.Equal(t, cm2.ToStringMap(), cm3.ToStringMap())
95+
}

0 commit comments

Comments
 (0)