-
Notifications
You must be signed in to change notification settings - Fork 51
/
default.go
95 lines (74 loc) · 3.1 KB
/
default.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
package collector
import (
"encoding/binary"
"fmt"
"sort"
"strings"
"github.com/cespare/xxhash"
)
// DefaultCollector implements a default collector infrastructure to syslog
type DefaultCollector struct{}
// NewDefaultCollector returns a default implementation of an EventCollector
func NewDefaultCollector() EventCollector {
return &DefaultCollector{}
}
// CollectFlowEvent is part of the EventCollector interface.
func (d *DefaultCollector) CollectFlowEvent(record *FlowRecord) {}
// CollectContainerEvent is part of the EventCollector interface.
func (d *DefaultCollector) CollectContainerEvent(record *ContainerRecord) {}
// CollectUserEvent is part of the EventCollector interface.
func (d *DefaultCollector) CollectUserEvent(record *UserRecord) {}
// CollectTraceEvent collects iptables trace events
func (d *DefaultCollector) CollectTraceEvent(records []string) {}
// CollectPacketEvent collects packet events from the datapath
func (d *DefaultCollector) CollectPacketEvent(report *PacketReport) {}
// CollectCounterEvent collect counters from the datapath
func (d *DefaultCollector) CollectCounterEvent(report *CounterReport) {}
// CollectDNSRequests collect counters from the datapath
func (d *DefaultCollector) CollectDNSRequests(report *DNSRequestReport) {}
// CollectPingEvent collects ping events from the datapath
func (d *DefaultCollector) CollectPingEvent(report *PingReport) {}
// StatsFlowHash is a hash function to hash flows
func StatsFlowHash(r *FlowRecord) string {
hash := xxhash.New()
hash.Write([]byte(r.Source.ID)) // nolint errcheck
hash.Write([]byte(r.Destination.ID)) // nolint errcheck
hash.Write([]byte(r.Source.IP)) // nolint errcheck
hash.Write([]byte(r.Destination.IP)) // nolint errcheck
port := make([]byte, 2)
binary.BigEndian.PutUint16(port, r.Destination.Port)
hash.Write(port) // nolint errcheck
hash.Write([]byte(r.Action.String())) // nolint errcheck
hash.Write([]byte(r.ObservedAction.String())) // nolint errcheck
hash.Write([]byte(r.DropReason)) // nolint errcheck
hash.Write([]byte(r.Destination.URI)) // nolint errcheck
return fmt.Sprintf("%d", hash.Sum64())
}
// StatsUserHash is a hash function to hash user records.
func StatsUserHash(r *UserRecord) error {
// Order matters for the hash function loop
sort.Strings(r.Claims)
hash := xxhash.New()
for i := 0; i < len(r.Claims); i++ {
if strings.HasPrefix(r.Claims[i], "sub") {
continue
}
if _, err := hash.Write([]byte(r.Claims[i])); err != nil {
return fmt.Errorf("unable to create hash: %v", err)
}
}
hashWithNS, err := HashHashWithNamespace(fmt.Sprintf("%d", hash.Sum64()), r.Namespace)
if err != nil {
return err
}
r.ID = hashWithNS
return nil
}
// HashHashWithNamespace hash the given claim hash with the given namespace.
func HashHashWithNamespace(claimsHash string, namespace string) (string, error) {
hash := xxhash.New()
if _, err := hash.Write(append([]byte(claimsHash), []byte(namespace)...)); err != nil {
return "", fmt.Errorf("unable to create namespace hash: %v", err)
}
return fmt.Sprintf("%d", hash.Sum64()), nil
}