This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcommon.go
373 lines (326 loc) · 10.8 KB
/
common.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Package common contains all of logic that is shared between trace and logs packages.
// The main concern of this package is to format Hubble flow data using OpenTelemetry
// structures. A few different formats are implemented to make it easier to optimise
// the data for different OpenTelemetry backends, e.g. some backends accept arbitrarily
// nested data, while others only handle flat maps.
// This package also implements conversion of label and HTTP headers to maps, which
// makes it easier to query data by label or by header in most of the backends.
package common
import (
"encoding/base64"
"fmt"
"strings"
"github.com/sirupsen/logrus"
commonV1 "go.opentelemetry.io/proto/otlp/common/v1"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
timestamp "google.golang.org/protobuf/types/known/timestamppb"
"github.com/cilium/cilium/api/v1/observer"
hubbleLabels "github.com/cilium/hubble-ui/backend/domain/labels"
)
const (
keyNamespaceCilium = "cilium."
AttributeEventKindVersion = keyNamespaceCilium + "event_kind"
AttributeEventEncoding = keyNamespaceCilium + "event_encoding"
AttributeEventEncodingOptions = keyNamespaceCilium + "event_encoding_options"
AttributeEventKindVersionFlowV1alpha1 = "flow/v1alpha2"
// in order to comply with the spec, cilium.flow_event is used with flat maps,
// and cilium.event_object is used to hold JSON-encoded or nested payloads,
// so that namespace and standalone key collision is avoided
AttributeFlowEventNamespace = keyNamespaceCilium + "flow_event"
AttributeEventObject = keyNamespaceCilium + "event_object"
AttributeEventDescription = keyNamespaceCilium + "event_description"
DefaultLogEncoding = EncodingTypedMap
DefaultTraceEncoding = EncodingFlatStringMap
EncodingJSON = "JSON"
EncodingJSONBASE64 = "JSON+base64"
EncodingFlatStringMap = "FlatStringMap"
EncodingSemiFlatTypedMap = "SemiFlatTypedMap"
EncodingTypedMap = "TypedMap"
)
type leafer func(protoreflect.FieldDescriptor, protoreflect.Value) bool
type mapBuilder interface {
newLeaf(string) leafer
items() []*commonV1.KeyValue
}
func EncodingFormatsForLogs() []string {
return []string{
EncodingJSON,
EncodingJSONBASE64,
EncodingFlatStringMap,
EncodingSemiFlatTypedMap,
EncodingTypedMap,
}
}
func EncodingFormatsForTraces() []string {
return []string{
EncodingJSON,
EncodingJSONBASE64,
EncodingFlatStringMap,
EncodingSemiFlatTypedMap,
}
}
type FlowEncoder struct {
*EncodingOptions
Logger *logrus.Logger
}
type EncodingOptions struct {
Encoding *string `mapstructure:"encoding"`
TopLevelKeys *bool `mapstructure:"top_level_keys"`
LabelsAsMaps *bool `mapstructure:"labels_as_maps"`
HeadersAsMaps *bool `mapstructure:"headers_as_maps"`
LogPayloadAsBody *bool `mapstructure:"log_payload_as_body"`
}
func (o *EncodingOptions) EncodingFormat() string {
if o.Encoding == nil {
return ""
}
return *o.Encoding
}
func (o *EncodingOptions) WithTopLevelKeys() bool {
return (o.TopLevelKeys != nil && *o.TopLevelKeys)
}
func (o *EncodingOptions) WithLabelsAsMaps() bool {
return (o.LabelsAsMaps != nil && *o.LabelsAsMaps)
}
func (o *EncodingOptions) WithHeadersAsMaps() bool {
return (o.HeadersAsMaps != nil && *o.HeadersAsMaps)
}
func (o *EncodingOptions) WithLogPayloadAsBody() bool {
return (o.LogPayloadAsBody != nil && *o.LogPayloadAsBody)
}
func (o *EncodingOptions) String() string {
options := []string{}
if o.WithTopLevelKeys() {
options = append(options, "TopLevelKeys")
}
if o.WithLabelsAsMaps() {
options = append(options, "LabelsAsMaps")
}
if o.WithHeadersAsMaps() {
options = append(options, "HeadersAsMaps")
}
if o.WithLogPayloadAsBody() {
options = append(options, "LogPayloadAsBody")
}
return strings.Join(options, ",")
}
func (o *EncodingOptions) ValidForLogs() error {
if err := o.validateFormat("logs", EncodingFormatsForLogs()); err != nil {
return err
}
switch o.EncodingFormat() {
case EncodingJSON, EncodingJSONBASE64, EncodingTypedMap:
if o.WithTopLevelKeys() && !o.WithLogPayloadAsBody() {
return fmt.Errorf("option \"TopLevelKeys\" without \"LogPayloadAsBody\" is not compatible with %q encoding", o.EncodingFormat())
}
}
return nil
}
func (o *EncodingOptions) ValidForTraces() error {
if err := o.validateFormat("trace", EncodingFormatsForTraces()); err != nil {
return err
}
switch o.EncodingFormat() {
case EncodingJSON, EncodingJSONBASE64:
if o.WithTopLevelKeys() {
return fmt.Errorf("option \"TopLevelKeys\" is not compatible with %q encoding", o.EncodingFormat())
}
}
if o.WithLogPayloadAsBody() {
return fmt.Errorf("option \"LogPayloadAsBody\" is not compatible with \"trace\" data type")
}
return nil
}
func (o *EncodingOptions) validateFormat(dataType string, formats []string) error {
if o.Encoding == nil {
return fmt.Errorf("encoding format must be set")
}
invalidFormat := true
for _, format := range formats {
if *o.Encoding == format {
invalidFormat = false
}
}
if invalidFormat {
return fmt.Errorf("encoding %q is invalid for %s data", *o.Encoding, dataType)
}
return nil
}
func (c *FlowEncoder) ToValue(hubbleResp *observer.GetFlowsResponse) (*commonV1.AnyValue, error) {
overrideOptionsWithWarning := func() {
if c.WithTopLevelKeys() && !c.WithLogPayloadAsBody() {
if c.Logger != nil {
c.Logger.Warnf("encoder: disabling \"TopLevelKeys\" option as it's incompatible"+
" with %q encoding when \"LogPayloadAsBody\" disabled also", c.EncodingFormat())
}
*c.TopLevelKeys = false
}
}
switch format := c.EncodingFormat(); format {
case EncodingJSON, EncodingJSONBASE64:
overrideOptionsWithWarning()
data, err := MarshalJSON(hubbleResp.GetFlow())
if err != nil {
return nil, err
}
var s string
switch format {
case EncodingJSON:
s = string(data)
case EncodingJSONBASE64:
s = base64.RawStdEncoding.EncodeToString(data)
}
return newStringValue(s), nil
case EncodingFlatStringMap, EncodingSemiFlatTypedMap, EncodingTypedMap:
var mb mapBuilder
switch format {
case EncodingFlatStringMap:
mb = &flatStringMap{
labelsAsMaps: c.WithLabelsAsMaps(),
headersAsMaps: c.WithHeadersAsMaps(),
separator: '.',
}
case EncodingSemiFlatTypedMap:
mb = &semiFlatTypedMap{
labelsAsMaps: c.WithLabelsAsMaps(),
headersAsMaps: c.WithHeadersAsMaps(),
separator: '.',
}
case EncodingTypedMap:
overrideOptionsWithWarning()
mb = &typedMap{
labelsAsMaps: c.WithLabelsAsMaps(),
headersAsMaps: c.WithHeadersAsMaps(),
}
}
topLevel := ""
if c.WithTopLevelKeys() {
topLevel = AttributeFlowEventNamespace
}
hubbleResp.GetFlow().ProtoReflect().Range(mb.newLeaf(topLevel))
v := &commonV1.AnyValue{
Value: &commonV1.AnyValue_KvlistValue{
KvlistValue: &commonV1.KeyValueList{
Values: mb.items(),
},
},
}
return v, nil
default:
return nil, fmt.Errorf("unsuported encoding format: %s", format)
}
}
func isRegularMessage(fd protoreflect.FieldDescriptor) bool {
return fd.Kind() == protoreflect.MessageKind &&
!isSpecialCase(fd) &&
(fd.IsMap() || fd.Cardinality() == protoreflect.Optional)
}
func isList(fd protoreflect.FieldDescriptor) bool {
return (fd.Cardinality() == protoreflect.Repeated || fd.Cardinality() == protoreflect.Required)
}
func isMessageList(fd protoreflect.FieldDescriptor) bool {
return fd.Kind() == protoreflect.MessageKind && isList(fd)
}
type specialFomatter func(protoreflect.Value) *commonV1.AnyValue
var specialCases = map[protoreflect.FullName]specialFomatter{
"google.protobuf.Timestamp": formatTimestamp,
"google.protobuf.BoolValue": formatBool,
}
func specialCaseFormatter(fd protoreflect.FieldDescriptor) (specialFomatter, bool) {
fdm := fd.Message()
if fdm == nil {
return nil, false
}
formatter, ok := specialCases[fdm.FullName()]
return formatter, ok
}
func isSpecialCase(fd protoreflect.FieldDescriptor) bool {
_, ok := specialCaseFormatter(fd)
return ok
}
// formatTimestamp handles google.protobuf.Timestamp values, as these are not
// something protoreflect automatically understands
func formatTimestamp(v protoreflect.Value) *commonV1.AnyValue {
ts := ×tamp.Timestamp{}
v.Message().Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
switch fd.JSONName() {
case "seconds":
ts.Seconds = v.Int()
case "nanos":
ts.Nanos = int32(v.Int())
}
return true
})
data, err := MarshalJSON(ts)
if err != nil {
return nil
}
// the result happens to be a quoted JSON string, so trim the quotes...
// (it's safe to do here as the timestamp format won't contain extra quotes)
return newStringValue(strings.Trim(string(data), "\""))
}
// formatBool handles google.protobuf.BoolValue values, as these are not
// something protoreflect automatically understands
func formatBool(v protoreflect.Value) *commonV1.AnyValue {
// in theore the value could be unset, but that doesn't actually need to be handled,
// as in the the case when the field is unset, this logic won't be called at all
var result bool
v.Message().Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
if fd.JSONName() == "value" {
result = v.Bool()
}
return true
})
return &commonV1.AnyValue{
Value: &commonV1.AnyValue_BoolValue{
BoolValue: result,
},
}
}
var jsonMarshaller = &protojson.MarshalOptions{
AllowPartial: false,
UseProtoNames: true,
UseEnumNumbers: false,
EmitUnpopulated: false,
}
func MarshalJSON(m proto.Message) ([]byte, error) {
return jsonMarshaller.Marshal(m)
}
func parseLabel(v protoreflect.Value) (string, *commonV1.AnyValue, error) {
labelKey, labelValue := hubbleLabels.LabelAsKeyValue(v.String(), true)
return labelKey, newStringValue(labelValue), nil
}
func parseHeader(v protoreflect.Value) (string, *commonV1.AnyValue, error) {
headerKey := ""
headerValue := ""
v.Message().Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
switch fd.Name() {
case "key":
headerKey = NormaliseHeaderKey(v.String())
case "value":
headerValue = v.String()
}
return true
})
if headerKey == "" {
return "", nil, fmt.Errorf("cannot use empty header key")
}
return headerKey, newStringArrayValue(headerValue), nil
}
func fmtKeyPath(keyPathPrefix, fieldName string, separator rune) string {
// NB: this format assumes that field names don't contain dots or other charcters,
// which is safe for *flow.Flow, so it's easier to query data as it doesn't
// result in `[` and `\"` characters being used in the keys; i.e. it's only "IP.source"
// and not "[\"IP\"][\"source\"]" (which would be less pressumptions, yet harder to
// query for the user)
switch keyPathPrefix {
case "":
return fieldName
case AttributeFlowEventNamespace:
return keyPathPrefix + string(separator) + fieldName
default:
return keyPathPrefix + string(separator) + fieldName
}
}