-
Notifications
You must be signed in to change notification settings - Fork 51
/
util.go
118 lines (95 loc) · 2.39 KB
/
util.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
package extractors
import (
"crypto/md5"
"io"
"net"
"os"
"strings"
"time"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/controller/constants"
"go.aporeto.io/trireme-lib/policy"
)
// ComputeFileMd5 computes the Md5 of a file
func ComputeFileMd5(filePath string) ([]byte, error) {
var result []byte
file, err := os.Open(filePath)
if err != nil {
return result, err
}
defer file.Close() //nolint : errcheck
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return result, err
}
return hash.Sum(result), nil
}
func findFQDN(expiration time.Duration) string {
hostname, err := os.Hostname()
if err != nil {
return "unknown"
}
// Try to find FQDN
globalHostname := make(chan string, 1)
go func() {
addrs, err := net.LookupIP(hostname)
if err != nil {
globalHostname <- hostname
return
}
for _, addr := range addrs {
ip, err := addr.MarshalText()
if err != nil {
globalHostname <- hostname
return
}
hosts, err := net.LookupAddr(string(ip))
if err != nil || len(hosts) == 0 {
globalHostname <- hostname
return
}
fqdn := hosts[0]
globalHostname <- strings.TrimSuffix(fqdn, ".") // return fqdn without trailing dot
}
}()
// Use OS hostname if we dont hear back in a second
select {
case <-time.After(expiration):
return hostname
case name := <-globalHostname:
return name
}
}
// policyExtensions retrieves policy extensions. Moving this function from extractor package.
func policyExtensions(runtime policy.RuntimeReader) (extensions policy.ExtendedMap) {
if runtime == nil {
return nil
}
if runtime.Options().PolicyExtensions == nil {
return nil
}
if extensions, ok := runtime.Options().PolicyExtensions.(policy.ExtendedMap); ok {
return extensions
}
return nil
}
// IsHostmodePU returns true if puType stored by policy extensions is hostmode PU
func IsHostmodePU(runtime policy.RuntimeReader, mode constants.ModeType) bool {
if runtime == nil {
return false
}
if mode != constants.LocalServer {
return false
}
return runtime.PUType() == common.HostPU || runtime.PUType() == common.HostNetworkPU
}
// IsHostPU returns true if puType stored by policy extensions is host PU
func IsHostPU(runtime policy.RuntimeReader, mode constants.ModeType) bool {
if runtime == nil {
return false
}
if mode != constants.LocalServer {
return false
}
return runtime.PUType() == common.HostPU
}