github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/extractors/util.go (about) 1 package extractors 2 3 import ( 4 "crypto/md5" 5 "io" 6 "os" 7 "time" 8 9 "go.aporeto.io/enforcerd/trireme-lib/common" 10 "go.aporeto.io/enforcerd/trireme-lib/controller/constants" 11 "go.aporeto.io/enforcerd/trireme-lib/policy" 12 "go.aporeto.io/enforcerd/trireme-lib/utils/fqdn" 13 ) 14 15 // ComputeFileMd5 computes the Md5 of a file 16 func ComputeFileMd5(filePath string) ([]byte, error) { 17 18 var result []byte 19 file, err := os.Open(filePath) 20 if err != nil { 21 return result, err 22 } 23 defer file.Close() // nolint: errcheck 24 25 hash := md5.New() 26 if _, err := io.Copy(hash, file); err != nil { 27 return result, err 28 } 29 30 return hash.Sum(result), nil 31 } 32 33 func findFQDN(expiration time.Duration) string { 34 35 hostname, err := os.Hostname() 36 if err != nil { 37 return "unknown" 38 } 39 40 // Try to find FQDN 41 globalHostname := make(chan string, 1) 42 go func() { 43 globalHostname <- fqdn.Find() 44 45 }() 46 47 // Use OS hostname if we dont hear back in a second 48 select { 49 case <-time.After(expiration): 50 return hostname 51 case name := <-globalHostname: 52 return name 53 } 54 } 55 56 // policyExtensions retrieves policy extensions. Moving this function from extractor package. 57 func policyExtensions(runtime policy.RuntimeReader) (extensions policy.ExtendedMap) { 58 59 if runtime == nil { 60 return nil 61 } 62 63 if runtime.Options().PolicyExtensions == nil { 64 return nil 65 } 66 67 if extensions, ok := runtime.Options().PolicyExtensions.(policy.ExtendedMap); ok { 68 return extensions 69 } 70 return nil 71 } 72 73 // IsHostmodePU returns true if puType stored by policy extensions is hostmode PU 74 func IsHostmodePU(runtime policy.RuntimeReader, mode constants.ModeType) bool { 75 76 if runtime == nil { 77 return false 78 } 79 80 if mode != constants.LocalServer { 81 return false 82 } 83 84 return runtime.PUType() == common.HostPU || runtime.PUType() == common.HostNetworkPU 85 } 86 87 // IsHostPU returns true if puType stored by policy extensions is host PU 88 func IsHostPU(runtime policy.RuntimeReader, mode constants.ModeType) bool { 89 90 if runtime == nil { 91 return false 92 } 93 94 if mode != constants.LocalServer { 95 return false 96 } 97 98 return runtime.PUType() == common.HostPU 99 }