-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
138 lines (125 loc) · 3.5 KB
/
utils.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
package main
import (
"fmt"
"runtime"
"strconv"
"strings"
)
const (
// OsctrlURL to send request to
OsctrlURL = "%s/%s"
// OsctrlURLFlags to send request for flags
OsctrlURLFlags = "%s/osctrld-flags"
// OsctrlURLCert to send request for certificate
OsctrlURLCert = "%s/osctrld-cert"
// OsctrlURLVerify to send request for verification
OsctrlURLVerify = "%s/osctrld-verify"
// OsctrlURLScript to send request for enroll/remove
OsctrlURLScript = "%s/%s/%s/osctrld-script"
// OsctrlEnroll to identify enrolls
OsctrlEnroll = "enroll"
// OsctrlRemove to identify removals
OsctrlRemove = "remove"
)
// OsctrlURLs keeps all osctrl URLs
type OsctrlURLs struct {
URL string
Flags string
Cert string
Verify string
Enroll string
Remove string
}
// Helper to generate osctrl main URL
func genOsctrlURL(base, env string) string {
return fmt.Sprintf(OsctrlURL, base, env)
}
// Helper to generate osctrl flags URL
func genFlagsURL(osctrl string) string {
return fmt.Sprintf(OsctrlURLFlags, osctrl)
}
// Helper to generate osctrl cert URL
func genCertURL(osctrl string) string {
return fmt.Sprintf(OsctrlURLCert, osctrl)
}
// Helper to generate osctrl verify URL
func genVerifyURL(osctrl string) string {
return fmt.Sprintf(OsctrlURLVerify, osctrl)
}
// Helper to generate osctrl script URL for enrolling/removing osquery nodes
func genScriptURL(osctrl, action, platform string) string {
return fmt.Sprintf(OsctrlURLScript, osctrl, action, platform)
}
// Helper to generate osctrl script URL for enrolling osquery nodes
func genEnrollURL(osctrl, platform string) string {
return fmt.Sprintf(OsctrlURLScript, osctrl, OsctrlEnroll, platform)
}
// Helper to generate osctrl script URL for removing osquery nodes
func genRemoveURL(osctrl, platform string) string {
return fmt.Sprintf(OsctrlURLScript, osctrl, OsctrlRemove, platform)
}
// Helper to generate all URLs
func genURLs(host, env string, insecure bool) OsctrlURLs {
var urls OsctrlURLs
osctrlURL := genOsctrlURL(host, env)
urls.URL = osctrlURL
urls.Flags = genFlagsURL(osctrlURL)
urls.Cert = genCertURL(osctrlURL)
urls.Verify = genVerifyURL(osctrlURL)
urls.Enroll = genEnrollURL(osctrlURL, runtime.GOOS)
urls.Remove = genRemoveURL(osctrlURL, runtime.GOOS)
return urls
}
// Helper to compare SemVer strings and return the highest or zero if they are the same
// returns 1 if existing is higher, 2 if required is higher, 0 is they are the same, and -1 if error
func osqueryVersionCompare(existing, required string) int {
if existing == required {
return 0
}
if existing == "" || required == "" {
return -1
}
ex := strings.Split(existing, ".")
req := strings.Split(required, ".")
// Make sure both slices are the same length
if len(ex) > len(req) {
for i := 0; i < len(ex)-len(req); i++ {
req = append(req, "0")
}
}
if len(req) > len(ex) {
for i := 0; i < len(req)-len(ex); i++ {
ex = append(ex, "0")
}
}
res := 2
// Iterate through all elements to compare and check what is higher
for v := 0; v < len(ex); v++ {
exConv, err := strconv.Atoi(ex[v])
if err != nil {
return -1
}
reqConv, err := strconv.Atoi(req[v])
if err != nil {
return -1
}
if exConv > reqConv {
return 1
}
}
return res
}
// Helper to compose a full path given partial path and file
func genFullPath(path, file string) string {
if path == "" {
return file
}
fFile := file
if strings.HasPrefix(file, "/") {
fFile = file[1:]
}
if strings.HasSuffix(path, "/") {
return fmt.Sprintf("%s%s", path, fFile)
}
return fmt.Sprintf("%s/%s", path, fFile)
}