-
Notifications
You must be signed in to change notification settings - Fork 51
/
default.go
59 lines (49 loc) · 1.76 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
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) {}
// 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
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.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("Cannot create hash")
}
}
r.ID = fmt.Sprintf("%d", hash.Sum64())
return nil
}