-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
330 lines (319 loc) · 9.98 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"runtime"
"github.com/urfave/cli/v2"
)
const (
// Application name
appName = "osctrld"
// Application version
appVersion = OsctrldVersion
// Application usage
appUsage = "Daemon for osctrl, the fast and efficient osquery management"
// Application description
appDescription = appUsage + ", to manage secret, flags and osquery deployment"
)
const (
// Default secret file
defSecretFile = "osquery.secret"
// Default flag file
defFlagFile = "osquery.flags"
// Default certificate
defCertificate = "osctrl.crt"
// Default enroll script
defEnrollScript = appName + "-enroll"
// Default remove script
defRemoveScript = appName + "-remove"
// Script extension for linux/darwin
shExtension = ".sh"
// Script extension for windows
ps1Extension = ".ps1"
// Default empty value
defEmptyValue = ""
// Default osquery path for darwin
defDarwinPath = "/private/var/osquery/"
// Default osquery path for linux
defLinuxPath = "/etc/osquery/"
// Default osquery path for windows
defWindowsPath = "C:\\Program Files\\osquery\\"
)
const (
// DarwinOS value for GOOS
DarwinOS = "darwin"
// LinuxOS value for GOOS
LinuxOS = "linux"
// WindowsOS value for GOOS
WindowsOS = "windows"
)
// Global variables
var (
err error
app *cli.App
flags []cli.Flag
commands []*cli.Command
)
// Variables for flags
var (
configFile string
jsonConfig JSONConfiguration
osctrlURLs OsctrlURLs
)
// Initialization code
func init() {
// Initialize CLI flags
flags = []cli.Flag{
&cli.StringFlag{
Name: "configuration",
Aliases: []string{"c", "conf", "config"},
Value: defEmptyValue,
Usage: "Configuration file for osctrld to load all necessary values",
EnvVars: []string{"OSCTRL_CONFIG"},
Destination: &configFile,
},
&cli.StringFlag{
Name: "secret",
Aliases: []string{"s"},
Value: defEmptyValue,
Usage: "Enroll secret to authenticate against osctrl server",
EnvVars: []string{"OSCTRL_SECRET"},
Destination: &jsonConfig.Secret,
},
&cli.StringFlag{
Name: "environment",
Aliases: []string{"e", "env"},
Value: defEmptyValue,
Usage: "Environment in osctrl to enrolled nodes to",
EnvVars: []string{"OSCTRL_ENV"},
Destination: &jsonConfig.Environment,
},
&cli.StringFlag{
Name: "secret-file",
Aliases: []string{"S"},
Value: defEmptyValue,
Usage: "Use `FILE` as secret file for osquery. Default depends on OS",
EnvVars: []string{"OSQUERY_SECRET"},
Destination: &jsonConfig.SecretFile,
},
&cli.StringFlag{
Name: "flagfile",
Aliases: []string{"F"},
Value: defEmptyValue,
Usage: "Use `FILE` as flagfile for osquery. Default depends on OS",
EnvVars: []string{"OSQUERY_FLAGFILE"},
Destination: &jsonConfig.FlagFile,
},
&cli.StringFlag{
Name: "certificate",
Aliases: []string{"C"},
Value: defEmptyValue,
Usage: "Use `FILE` as certificate for osquery, if needed. Default depends on OS",
EnvVars: []string{"OSQUERY_CERTIFICATE"},
Destination: &jsonConfig.CertFile,
},
&cli.StringFlag{
Name: "osctrl-url",
Aliases: []string{"U"},
Value: defEmptyValue,
Usage: "Base URL for the osctrl server",
EnvVars: []string{"OSCTRL_URL"},
Destination: &jsonConfig.BaseURL,
},
&cli.StringFlag{
Name: "osquery-path",
Aliases: []string{"osquery", "o"},
Value: defEmptyValue,
Usage: "Use `FILE` as path for osquery installation, if needed. Default depends on OS",
EnvVars: []string{"OSQUERY_PATH"},
Destination: &jsonConfig.OsqueryPath,
},
&cli.BoolFlag{
Name: "insecure",
Aliases: []string{"i"},
Value: false,
Usage: "Ignore TLS warnings, often used with self-signed certificates",
EnvVars: []string{"OSCTRL_INSECURE"},
Destination: &jsonConfig.Insecure,
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"V"},
Value: false,
Usage: "Enable verbose informational messages",
EnvVars: []string{"OSCTRL_VERBOSE"},
Destination: &jsonConfig.Verbose,
},
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Value: false,
Usage: "Overwrite existing files for flags, certificate and secret",
EnvVars: []string{"OSCTRL_FORCE"},
Destination: &jsonConfig.Verbose,
},
}
// Initialize CLI flags commands
commands = []*cli.Command{
{
Name: "enroll",
Usage: "Enroll a new node in osctrl, using new secret and flag files",
Action: cliWrapper(enrollNode),
},
{
Name: "remove",
Usage: "Remove enrolled node from osctrl, clearing secret and flag files",
Action: cliWrapper(removeNode),
},
{
Name: "verify",
Usage: "Verify flags, cert and secret for an enrolled node in osctrl",
Action: cliWrapper(verifyNode),
},
{
Name: "flags",
Usage: "Retrieve flags for osquery from osctrl and write them locally",
Action: cliWrapper(getFlags),
},
{
Name: "cert",
Usage: "Retrieve server certificate for osquery from osctrl and write it locally",
Action: cliWrapper(getCert),
},
}
}
// Function to wrap actions
func cliWrapper(action func(*cli.Context) error) func(*cli.Context) error {
return func(c *cli.Context) error {
if configFile != defEmptyValue {
jsonConfig, err = loadConfiguration(configFile, c.Bool("verbose"))
if err != nil {
exitError := fmt.Sprintf("\n❌ Error reading configuration file (%s) - %v", configFile, err)
return cli.Exit(exitError, 2)
}
}
if jsonConfig.Verbose {
log.Printf("⏳ Initializing %s...", appName)
fmt.Println()
}
// Based on OS, assign values for flag and secret file, if they have not been assigned already
switch runtime.GOOS {
case DarwinOS:
if jsonConfig.OsqueryPath == defEmptyValue {
jsonConfig.OsqueryPath = defDarwinPath
}
if jsonConfig.FlagFile == defEmptyValue {
jsonConfig.FlagFile = genFullPath(jsonConfig.OsqueryPath, defFlagFile)
}
if jsonConfig.SecretFile == defEmptyValue {
jsonConfig.SecretFile = genFullPath(jsonConfig.OsqueryPath, defSecretFile)
}
if jsonConfig.CertFile == defEmptyValue {
jsonConfig.CertFile = genFullPath(jsonConfig.OsqueryPath, defCertificate)
}
if jsonConfig.EnrollScript == "" {
jsonConfig.EnrollScript = genFullPath(jsonConfig.OsqueryPath, defEnrollScript+shExtension)
}
if jsonConfig.RemoveScript == "" {
jsonConfig.RemoveScript = genFullPath(jsonConfig.OsqueryPath, defRemoveScript+shExtension)
}
case LinuxOS:
if jsonConfig.OsqueryPath == defEmptyValue {
jsonConfig.OsqueryPath = defLinuxPath
}
if jsonConfig.FlagFile == defEmptyValue {
jsonConfig.FlagFile = genFullPath(jsonConfig.OsqueryPath, defFlagFile)
}
if jsonConfig.SecretFile == defEmptyValue {
jsonConfig.SecretFile = genFullPath(jsonConfig.OsqueryPath, defSecretFile)
}
if jsonConfig.CertFile == defEmptyValue {
jsonConfig.CertFile = genFullPath(jsonConfig.OsqueryPath, defCertificate)
}
if jsonConfig.EnrollScript == "" {
jsonConfig.EnrollScript = genFullPath(jsonConfig.OsqueryPath, defEnrollScript+shExtension)
}
if jsonConfig.RemoveScript == "" {
jsonConfig.RemoveScript = genFullPath(jsonConfig.OsqueryPath, defRemoveScript+shExtension)
}
case WindowsOS:
if jsonConfig.OsqueryPath == defEmptyValue {
jsonConfig.OsqueryPath = defWindowsPath
}
if jsonConfig.FlagFile == defEmptyValue {
jsonConfig.FlagFile = genFullPath(jsonConfig.OsqueryPath, defFlagFile)
}
if jsonConfig.SecretFile == defEmptyValue {
jsonConfig.SecretFile = genFullPath(jsonConfig.OsqueryPath, defSecretFile)
}
if jsonConfig.CertFile == defEmptyValue {
jsonConfig.CertFile = genFullPath(jsonConfig.OsqueryPath, defCertificate)
}
if jsonConfig.EnrollScript == "" {
jsonConfig.EnrollScript = genFullPath(jsonConfig.OsqueryPath, defEnrollScript+ps1Extension)
}
if jsonConfig.RemoveScript == "" {
jsonConfig.RemoveScript = genFullPath(jsonConfig.OsqueryPath, defRemoveScript+ps1Extension)
}
}
// Check for required parameters
if jsonConfig.Environment == defEmptyValue {
exitError := fmt.Sprintf("\n❌ Environment for osctrl is required")
return cli.Exit(exitError, 2)
}
if jsonConfig.BaseURL == defEmptyValue {
exitError := fmt.Sprintf("\n❌ Base URL for osctrl is required")
return cli.Exit(exitError, 2)
}
// Initialize URLs
osctrlURLs = genURLs(jsonConfig.BaseURL, jsonConfig.Environment, jsonConfig.Insecure)
if jsonConfig.Verbose {
log.Printf("📌 Osquery Path: %s", jsonConfig.OsqueryPath)
log.Printf("🔎 Flag file: %s", jsonConfig.FlagFile)
log.Printf("🔑 Secret file: %s", jsonConfig.SecretFile)
log.Printf("🔏 Certificate: %s", jsonConfig.CertFile)
log.Printf("+ Enroll script: %s", jsonConfig.EnrollScript)
log.Printf("- Remove script: %s", jsonConfig.RemoveScript)
log.Printf("🔗 BaseURL: %s", jsonConfig.BaseURL)
log.Printf("📍 Environment: %s", jsonConfig.Environment)
log.Printf("🔴 Insecure: %v", jsonConfig.Insecure)
log.Printf("📢 Verbose: %v", jsonConfig.Verbose)
log.Printf("🦾 Force: %v", jsonConfig.Force)
log.Printf("💻 Command: %s", c.Command.Name)
fmt.Println()
}
return action(c)
}
}
// Action to run when no flags are provided
func cliAction(c *cli.Context) error {
if c.NumFlags() == 0 {
if err := cli.ShowAppHelp(c); err != nil {
log.Fatalf("Error with help - %s", err)
}
return cli.Exit("❌ No command provided", 2)
}
if c.Command.Name == "" {
if err := cli.ShowAppHelp(c); err != nil {
log.Fatalf("Error with help - %s", err)
}
return cli.Exit("❌ Invalid command", 2)
}
return nil
}
// Go go!
func main() {
// Let's go!
app = cli.NewApp()
app.Name = appName
app.Usage = appUsage
app.Version = appVersion
app.Description = appDescription
app.Flags = flags
app.Commands = commands
app.Action = cliAction
if err := app.Run(os.Args); err != nil {
log.Fatalf("Failed to execute %v", err)
}
}