forked from mozilla/mig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent-context.go
356 lines (326 loc) · 8.74 KB
/
agent-context.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor:
// - Aaron Meihm ameihm@mozilla.com [:alm]
// The agentcontext package provides functionality to obtain information
// about the system a given agent or loader is running on. This includes
// information unrelated to MIG itself, such as the hostname of the system,
// IP addresses, and so on.
package agentcontext /* import "github.com/mozilla/mig/mig-agent/agentcontext" */
import (
"fmt"
"github.com/kardianos/osext"
"io/ioutil"
mrand "math/rand"
"github.com/mozilla/mig"
"os"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
// Information from the system the agent is running on
type AgentContext struct {
Hostname string // Hostname
BinPath string // Path to invoked binary
RunDir string // Agent runtime directory
OS string // Operating System
OSIdent string // OS release identifier
Init string // OS Init
Architecture string // System architecture
Addresses []string // IP addresses
PublicIP string // Systems public IP from perspective of API
UID string // Agent ID
QueueLoc string // Agent queue location
AWS AWSContext // AWS specific information
}
func (ctx *AgentContext) IsZero() bool {
// If we don't have an OS treat it as unset
if ctx.OS == "" {
return true
}
return false
}
// Check of any values in the AgentContext differ from those in comp
func (ctx *AgentContext) Differs(comp AgentContext) bool {
if ctx.Hostname != comp.Hostname ||
ctx.BinPath != comp.BinPath ||
ctx.RunDir != comp.RunDir ||
ctx.OS != comp.OS ||
ctx.OSIdent != comp.OSIdent ||
ctx.Init != comp.Init ||
ctx.Architecture != comp.Architecture ||
ctx.PublicIP != comp.PublicIP ||
ctx.AWS.InstanceID != comp.AWS.InstanceID ||
ctx.AWS.LocalIPV4 != comp.AWS.LocalIPV4 ||
ctx.AWS.AMIID != comp.AWS.AMIID ||
ctx.AWS.InstanceType != comp.AWS.InstanceType {
return true
}
if ctx.Addresses == nil && comp.Addresses == nil {
return false
}
if ctx.Addresses == nil || comp.Addresses == nil {
return true
}
if len(ctx.Addresses) != len(comp.Addresses) {
return true
}
for i := range ctx.Addresses {
if ctx.Addresses[i] != comp.Addresses[i] {
return true
}
}
return false
}
func (ctx *AgentContext) ToAgent() (ret mig.Agent) {
ret.Name = ctx.Hostname
ret.QueueLoc = ctx.QueueLoc
ret.PID = os.Getpid()
ret.Env.OS = ctx.OS
ret.Env.Arch = ctx.Architecture
ret.Env.Ident = ctx.OSIdent
ret.Env.Init = ctx.Init
ret.Env.Addresses = ctx.Addresses
ret.Env.PublicIP = ctx.PublicIP
ret.Env.AWS.InstanceID = ctx.AWS.InstanceID
ret.Env.AWS.LocalIPV4 = ctx.AWS.LocalIPV4
ret.Env.AWS.AMIID = ctx.AWS.AMIID
ret.Env.AWS.InstanceType = ctx.AWS.InstanceType
return
}
// Passed to NewAgentContext() to inform environment discovery
type AgentContextHints struct {
APIUrl string // MIG API URL
Proxies []string // Proxies avialable for use in discovery
DiscoverPublicIP bool // Attempt to discover public IP
DiscoverAWSMeta bool // Attempt to discover AWS metadata
}
// Information used for agents running in AWS environments
type AWSContext struct {
InstanceID string // AWS instance ID
LocalIPV4 string // AWS Local IPV4 address
AMIID string // AWS AMI ID
InstanceType string // AWS instance type
}
var logChan chan mig.Log
// testConfPath if set will be used as the agent configuration and runtime directory
var testConfPath = ""
// EnableTestHooks changes the behavior of the agentcontext package for testing
//
// confpath indicates the configuration path functions like GetConfDir should return
// rather than the standard platform default.
func EnableTestHooks(confpath string) {
testConfPath = confpath
}
// GetConfDir returns the configuration directory for the agent
func GetConfDir() string {
if testConfPath != "" {
return testConfPath
}
switch runtime.GOOS {
case "windows":
return "C:\\mig\\"
default:
return "/etc/mig"
}
}
// GetRunDir returns the runtime directory for the agent
func GetRunDir() string {
if testConfPath != "" {
return testConfPath
}
switch runtime.GOOS {
case "windows":
return GetConfDir()
case "darwin":
return "/Library/Preferences/mig"
default:
return "/var/lib/mig"
}
}
func NewAgentContext(lch chan mig.Log, hints AgentContextHints) (ret AgentContext, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("NewAgentContext() -> %v", e)
}
}()
logChan = lch
ret.BinPath, err = osext.Executable()
if err != nil {
panic(err)
}
ret, err = findHostname(ret)
if err != nil {
panic(err)
}
ret.OS = runtime.GOOS
ret.Architecture = runtime.GOARCH
ret.RunDir = GetRunDir()
ret, err = findOSInfo(ret)
if err != nil {
panic(err)
}
ret, err = findLocalIPs(ret)
if err != nil {
panic(err)
}
ret, err = initAgentID(ret)
if err != nil {
panic(err)
}
// build the agent message queue location
ret.QueueLoc = fmt.Sprintf("%s.%s", ret.OS, ret.UID)
if hints.DiscoverPublicIP {
ret, err = findPublicIP(ret, hints)
if err != nil {
panic(err)
}
}
if hints.DiscoverAWSMeta {
ret, err = addAWSMetadata(ret)
if err != nil {
panic(err)
}
}
return
}
// initAgentID will retrieve an ID from disk, or request one if missing
func initAgentID(orig_ctx AgentContext) (ctx AgentContext, err error) {
ctx = orig_ctx
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("initAgentID() -> %v", e)
}
logChan <- mig.Log{Desc: "leaving initAgentID()"}.Debug()
}()
os.Chmod(ctx.RunDir, 0755)
idFile := path.Join(ctx.RunDir, ".migagtid")
id, err := ioutil.ReadFile(idFile)
if err != nil {
logChan <- mig.Log{Desc: fmt.Sprintf("unable to read agent id from '%s': %v", idFile, err)}.Debug()
// ID file doesn't exist, create it
id, err = createIDFile(ctx)
if err != nil {
panic(err)
}
}
// Make sure the obtained queue location matches the format that we expect, if
// it doesn't create a new one
mtch, err := regexp.Match("^[0-9a-zA-Z]{80,}$", id)
if err != nil {
panic(err)
}
if !mtch {
logChan <- mig.Log{Desc: "invalid or deprecated agent ID, recreating"}.Info()
id, err = createIDFile(ctx)
if err != nil {
panic(err)
}
}
ctx.UID = fmt.Sprintf("%s", id)
os.Chmod(idFile, 0400)
return
}
// createIDFile will generate a new ID for this agent and store it on disk
// the location depends on the operating system
func createIDFile(ctx AgentContext) (id []byte, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("createIDFile() -> %v", e)
}
}()
// generate an ID with 512 bits of entropy
r := mrand.New(mrand.NewSource(time.Now().UnixNano()))
var sid string
for i := 0; i < 8; i++ {
sid += strconv.FormatUint(uint64(r.Int63()), 36)
}
// check that the storage DIR exist, and that it's a dir
tdir, err := os.Open(ctx.RunDir)
defer tdir.Close()
if err != nil {
// dir doesn't exist, create it
logChan <- mig.Log{Desc: fmt.Sprintf("agent rundir is missing from '%s'. creating it", ctx.RunDir)}.Debug()
err = os.MkdirAll(ctx.RunDir, 0755)
if err != nil {
panic(err)
}
} else {
// open worked, verify that it's a dir
tdirMode, err := tdir.Stat()
if err != nil {
panic(err)
}
if !tdirMode.Mode().IsDir() {
logChan <- mig.Log{Desc: fmt.Sprintf("'%s' is not a directory. removing it", ctx.RunDir)}.Debug()
// not a valid dir. destroy whatever it is, and recreate
err = os.Remove(ctx.RunDir)
if err != nil {
panic(err)
}
err = os.MkdirAll(ctx.RunDir, 0755)
if err != nil {
panic(err)
}
}
}
idFile := path.Join(ctx.RunDir, ".migagtid")
// something exists at the location of the id file, just plain remove it
_ = os.Remove(idFile)
// write the ID file
err = ioutil.WriteFile(idFile, []byte(sid), 0400)
if err != nil {
panic(err)
}
// read ID from disk
id, err = ioutil.ReadFile(idFile)
if err != nil {
panic(err)
}
logChan <- mig.Log{Desc: fmt.Sprintf("agent id created in '%s'", idFile)}.Debug()
return
}
// cleanString removes spaces, quotes and newlines
func cleanString(str string) string {
if len(str) < 1 {
return str
}
if str[len(str)-1] == '\n' {
str = str[0 : len(str)-1]
}
// remove heading whitespaces and quotes
for {
if len(str) < 2 {
break
}
switch str[0] {
case ' ', '"', '\'':
str = str[1:len(str)]
default:
goto trailing
}
}
trailing:
// remove trailing whitespaces, quotes and linebreaks
for {
if len(str) < 2 {
break
}
switch str[len(str)-1] {
case ' ', '"', '\'', '\r', '\n':
str = str[0 : len(str)-1]
default:
goto exit
}
}
exit:
// remove in-string linebreaks
str = strings.Replace(str, "\n", " ", -1)
str = strings.Replace(str, "\r", " ", -1)
return str
}