-
Notifications
You must be signed in to change notification settings - Fork 30
/
merger.go
59 lines (49 loc) · 1.19 KB
/
merger.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 merger
import (
"fmt"
"iptables-logger/parser"
"iptables-logger/repository"
"code.cloudfoundry.org/lager"
)
//go:generate counterfeiter -o fakes/containerRepo.go --fake-name ContainerRepo . containerRepo
type containerRepo interface {
GetByIP(string) (repository.Container, error)
}
type IPTablesLogData struct {
Message string
Data lager.Data
}
type Merger struct {
ContainerRepo containerRepo
HostIp string
HostGuid string
}
func (m *Merger) Merge(parsedData parser.ParsedData) (IPTablesLogData, error) {
message := parsedData.Direction
if parsedData.Allowed {
message += "-allowed"
} else {
message += "-denied"
}
var key, ipToLookup string
if parsedData.Direction == "ingress" {
key = "destination"
ipToLookup = parsedData.DestinationIP
} else {
key = "source"
ipToLookup = parsedData.SourceIP
}
containerData, err := m.ContainerRepo.GetByIP(ipToLookup)
if err != nil {
return IPTablesLogData{}, fmt.Errorf("get container by ip: %s", err)
}
containerData.HostIp = m.HostIp
containerData.HostGuid = m.HostGuid
return IPTablesLogData{
Message: message,
Data: lager.Data{
key: containerData,
"packet": parsedData,
},
}, nil
}