-
Notifications
You must be signed in to change notification settings - Fork 24
/
transform_network_direction.go
64 lines (59 loc) · 1.77 KB
/
transform_network_direction.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
package transform
import (
"fmt"
"github.com/netobserv/flowlogs-pipeline/pkg/api"
"github.com/netobserv/flowlogs-pipeline/pkg/config"
)
const (
ingress = 0
egress = 1
inner = 2
)
func validateReinterpretDirectionConfig(info *api.NetworkTransformDirectionInfo) error {
if info.FlowDirectionField == "" {
return fmt.Errorf("invalid config for transform.Network rule %s: missing FlowDirectionField", api.NetworkReinterpretDirection)
}
if info.ReporterIPField == "" {
return fmt.Errorf("invalid config for transform.Network rule %s: missing ReporterIPField", api.NetworkReinterpretDirection)
}
if info.SrcHostField == "" {
return fmt.Errorf("invalid config for transform.Network rule %s: missing SrcHostField", api.NetworkReinterpretDirection)
}
if info.DstHostField == "" {
return fmt.Errorf("invalid config for transform.Network rule %s: missing DstHostField", api.NetworkReinterpretDirection)
}
return nil
}
func reinterpretDirection(output config.GenericMap, info *api.NetworkTransformDirectionInfo) {
if fd, ok := output[info.FlowDirectionField]; ok && len(info.IfDirectionField) > 0 {
output[info.IfDirectionField] = fd
}
var srcNode, dstNode, reporter string
if gen, ok := output[info.ReporterIPField]; ok {
if str, ok := gen.(string); ok {
reporter = str
}
}
if len(reporter) == 0 {
return
}
if gen, ok := output[info.SrcHostField]; ok {
if str, ok := gen.(string); ok {
srcNode = str
}
}
if gen, ok := output[info.DstHostField]; ok {
if str, ok := gen.(string); ok {
dstNode = str
}
}
if srcNode != dstNode {
if srcNode == reporter {
output[info.FlowDirectionField] = egress
} else if dstNode == reporter {
output[info.FlowDirectionField] = ingress
}
} else if srcNode != "" {
output[info.FlowDirectionField] = inner
}
}