github.com/hernad/nomad@v1.6.112/command/agent/host/host.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package host 5 6 import ( 7 "io" 8 "os" 9 "strings" 10 ) 11 12 type HostData struct { 13 OS string 14 Network []map[string]string 15 ResolvConf string 16 Hosts string 17 Environment map[string]string 18 Disk map[string]DiskUsage 19 } 20 21 type DiskUsage struct { 22 DiskMB int64 23 UsedMB int64 24 } 25 26 func MakeHostData() (*HostData, error) { 27 du := make(map[string]DiskUsage) 28 for _, path := range mountedPaths() { 29 u, err := diskUsage(path) 30 if err != nil { 31 continue 32 } 33 du[path] = u 34 } 35 36 return &HostData{ 37 OS: uname(), 38 Network: network(), 39 ResolvConf: resolvConf(), 40 Hosts: etcHosts(), 41 Environment: environment(), 42 Disk: du, 43 }, nil 44 } 45 46 // diskUsage calculates the DiskUsage 47 func diskUsage(path string) (du DiskUsage, err error) { 48 s, err := makeDf(path) 49 if err != nil { 50 return du, err 51 } 52 53 disk := float64(s.total()) 54 // Bavail is blocks available to unprivileged users, Bfree includes reserved blocks 55 free := float64(s.available()) 56 used := disk - free 57 mb := float64(1048576) 58 59 disk = disk / mb 60 used = used / mb 61 62 du.DiskMB = int64(disk) 63 du.UsedMB = int64(used) 64 return du, nil 65 } 66 67 var ( 68 envRedactSet = makeEnvRedactSet() 69 ) 70 71 // environment returns the process environment in a map 72 func environment() map[string]string { 73 env := make(map[string]string) 74 75 for _, e := range os.Environ() { 76 s := strings.SplitN(e, "=", 2) 77 k := s[0] 78 up := strings.ToUpper(k) 79 v := s[1] 80 81 _, redact := envRedactSet[k] 82 if redact || 83 strings.Contains(up, "TOKEN") || 84 strings.Contains(up, "SECRET") { 85 v = "<redacted>" 86 } 87 88 env[k] = v 89 } 90 return env 91 } 92 93 // DefaultEnvDenyList is the default set of environment variables that are 94 // filtered when passing the environment variables of the host to the task. 95 // 96 // Update https://www.nomadproject.io/docs/configuration/client#env-denylist 97 // whenever this is changed. 98 var DefaultEnvDenyList = []string{ 99 "CONSUL_TOKEN", 100 "CONSUL_HTTP_TOKEN", 101 "VAULT_TOKEN", 102 "NOMAD_LICENSE", 103 "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", 104 "GOOGLE_APPLICATION_CREDENTIALS", 105 } 106 107 // makeEnvRedactSet creates a set of well known environment variables that should be 108 // redacted in the output 109 func makeEnvRedactSet() map[string]struct{} { 110 set := make(map[string]struct{}) 111 for _, e := range DefaultEnvDenyList { 112 set[e] = struct{}{} 113 } 114 115 return set 116 } 117 118 // slurp returns the file contents as a string, returning an error string 119 func slurp(path string) string { 120 fh, err := os.Open(path) 121 if err != nil { 122 return err.Error() 123 } 124 125 bs, err := io.ReadAll(fh) 126 if err != nil { 127 return err.Error() 128 } 129 130 return string(bs) 131 }