/
factory.go
139 lines (127 loc) · 3.78 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package obfuscationprocessor
import (
"context"
"crypto/rand"
"github.com/cyrildever/feistel"
"github.com/cyrildever/feistel/common/utils/hash"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processorhelper"
)
const (
// The value of "type" key in configuration.
typeStr = "obfuscation"
// The stability level of the exporter.
stability = component.StabilityLevelAlpha
defaultRounds = 10
defaultKeyLength = 128
)
// NewFactory creates a factory for the obfuscation processor.
func NewFactory() processor.Factory {
return processor.NewFactory(
component.MustNewType(typeStr),
createDefaultConfig,
processor.WithTraces(createTracesProcessor, stability),
processor.WithLogs(createLogsProcessor, stability),
processor.WithMetrics(createMetricsProcessor, stability),
)
}
func newKey(keyLength int) string {
buf := make([]byte, keyLength)
rand.Reader.Read(buf)
return string(buf)
}
func createDefaultConfig() component.Config {
return &Config{
Rounds: defaultRounds,
KeyLength: defaultKeyLength,
// encrypt all string attributes by default
EncryptAll: true,
}
}
func createMetricsProcessor(
ctx context.Context,
set processor.CreateSettings,
cfg component.Config,
next consumer.Metrics,
) (processor.Metrics, error) {
oCfg := cfg.(*Config)
processor := &obfuscation{
logger: set.Logger,
nextMetrics: next,
encrypt: feistel.NewFPECipher(hash.SHA_256, newKey(oCfg.KeyLength), oCfg.Rounds),
encryptAttributes: makeEncryptList(oCfg),
encryptAll: oCfg.EncryptAll,
}
return processorhelper.NewMetricsProcessor(
ctx,
set,
cfg,
next,
processor.processMetrics,
processorhelper.WithCapabilities(processor.Capabilities()),
processorhelper.WithStart(processor.Start),
processorhelper.WithShutdown(processor.Shutdown))
}
// createTracesProcessor creates an instance of obfuscation for processing traces
func createTracesProcessor(
ctx context.Context,
set processor.CreateSettings,
cfg component.Config,
next consumer.Traces,
) (processor.Traces, error) {
oCfg := cfg.(*Config)
processor := &obfuscation{
logger: set.Logger,
nextTraces: next,
encrypt: feistel.NewFPECipher(hash.SHA_256, newKey(oCfg.KeyLength), oCfg.Rounds),
encryptAttributes: makeEncryptList(oCfg),
encryptAll: oCfg.EncryptAll,
}
return processorhelper.NewTracesProcessor(
ctx,
set,
cfg,
next,
processor.processTraces,
processorhelper.WithCapabilities(processor.Capabilities()),
processorhelper.WithStart(processor.Start),
processorhelper.WithShutdown(processor.Shutdown))
}
// createLogsProcessor creates an instance of obfuscation for processing logs.
func createLogsProcessor(
ctx context.Context,
set processor.CreateSettings,
cfg component.Config,
next consumer.Logs,
) (processor.Logs, error) {
oCfg := cfg.(*Config)
processor := &obfuscation{
logger: set.Logger,
nextLogs: next,
encrypt: feistel.NewFPECipher(hash.SHA_256, newKey(oCfg.KeyLength), oCfg.Rounds),
encryptAttributes: makeEncryptList(oCfg),
encryptAll: oCfg.EncryptAll,
}
return processorhelper.NewLogsProcessor(
ctx,
set,
cfg,
next,
processor.processLogs,
processorhelper.WithCapabilities(processor.Capabilities()),
processorhelper.WithStart(processor.Start),
processorhelper.WithShutdown(processor.Shutdown))
}
// makeEncryptList sets up a lookup table of span attribute keys which need to be encrypted.
func makeEncryptList(c *Config) map[string]struct{} {
allowList := make(map[string]struct{}, len(c.EncryptAttributes))
for _, key := range c.EncryptAttributes {
allowList[key] = struct{}{}
}
if len(allowList) > 0 {
c.EncryptAll = false
}
return allowList
}