forked from DefectDojo/godojo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtargets.go
More file actions
365 lines (313 loc) · 9.74 KB
/
targets.go
File metadata and controls
365 lines (313 loc) · 9.74 KB
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
357
358
359
360
361
362
363
364
365
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
)
// InstallTargets holds the distro and versions supported for Dojo installations
var InstallTargets = map[string][]string{
"ubuntu": {"18.04", "18.10", "19.04"},
"debian": {"stretch", "buster"},
}
// Supported OSes
type targetOS struct {
id string
os string
distro string
release string
}
// OS commands to perform an action e.g. install DB from OS packages
type osCmds struct {
id string // Holds distro + release e.g. ubuntu:18.04
cmds []string // Holds the os commands
errmsg []string // Holds the error messages if the matching command fails
hard []bool // Flag to know if an error on the matching command is fatal
}
func determineOS(tOS *targetOS) {
// Determine OS first
tOS.os = runtime.GOOS
traceMsg(fmt.Sprintf("Determining OS based on GOOS: %+v", tOS.os))
switch tOS.os {
case "linux":
traceMsg("OS determined to be Linux")
determineLinux(tOS)
case "darwin":
traceMsg("OS determined to be Darwin/OS X")
fmt.Println("OS X/Darwin")
errorMsg("OS X is not YET a supported installation platform")
os.Exit(1)
case "windows":
traceMsg("OS determined to be Windows")
errorMsg("Windows is not a supported installation platform")
os.Exit(1)
}
return
}
func determineLinux(tOS *targetOS) {
// Determine the Linux Distro the installer is running on
// Based on Based on https://unix.stackexchange.com/questions/6345/how-can-i-get-distribution-name-and-version-number-in-a-simple-shell-script
traceMsg("Determining what Linux distro is the target OS")
// freedesktop.org and systemd
_, err := os.Stat("/etc/os-release")
if err == nil {
// That file exists
traceMsg("Determining Linux distro from /etc/os-release")
tOS.distro, tOS.release, tOS.id = parseOSRelease("/etc/os-release")
return
}
// lsb_release command is present
lsbCmd, err := exec.LookPath("lsb_release")
if err == nil {
// The command was found
traceMsg("Determining Linux distro from lsb_release command")
tOS.distro, tOS.release, tOS.id = parseLsbCmd(lsbCmd)
return
}
// /etc/lsb-release is present
_, err = os.Stat("/etc/lsb-release")
if err == nil {
// The file was found
traceMsg("Determining Linux distro from /etc/lsb-release")
tOS.distro, tOS.release, tOS.id = parseEtcLsb("/etc/lsb-release")
return
}
// /etc/issue is present
_, err = os.Stat("/etc/issue")
if err == nil {
// The file was found
traceMsg("Determining Linux distro from /etc/issue")
tOS.distro, tOS.release, tOS.id = parseEtcIss("/etc/issue")
return
}
// /etc/debian_version is present
_, err = os.Stat("/etc/debian_version")
if err == nil {
// The file was found
traceMsg("Determining Linux distro from /etc/debian_version")
tOS.distro, tOS.release, tOS.id = parseEtcDeb("/etc/debian_version")
return
}
// Older SUSE Linux installation
_, err = os.Stat("/etc/SuSe-release")
if err == nil {
// Distro is too old, not supported
traceMsg("Older SuSe Linux distro isn't supported by this installer")
errorMsg("Older versions of SuSe Linux are not suppported, quitting")
os.Exit(1)
}
_, err = os.Stat("/etc/redhat-release")
if err == nil {
// Distro is too old, not supported
traceMsg("Older RedHat Linux distro isn't supported by this installer")
errorMsg("Older versions of Redhat Linux are not suppported, quitting")
os.Exit(1)
}
traceMsg("Unable to determine the linux distro, assuming unsupported.")
errorMsg("Unable to determine the Linux install target, quitting")
os.Exit(1)
}
func parseOSRelease(f string) (string, string, string) {
// Setup a map of what we need to what /etc/os-release uses
fields := map[string]string{
"distro": "ID",
"release": "VERSION_ID",
}
linMap := parseFile(f, "=", fields)
return linMap["distro"], linMap["release"], linMap["distro"] + ":" + linMap["release"]
}
func parseLsbCmd(cmd string) (string, string, string) {
// Setup map to hold parsed values
vals := make(map[string]string)
// Execute the lsb_release command with -a (all) and parse the output
runCmd := exec.Command(cmd, "-a")
// Run command and gather its output
cmdOut, err := runCmd.CombinedOutput()
if err != nil {
errorMsg(fmt.Sprintf("Failed to run OS command, error was: %+v", err))
os.Exit(1)
}
// Parse command output for the strings we need
lines := bytes.Split(cmdOut, []byte("\n"))
for _, line := range lines {
l := string(line)
// Look for the distro
if strings.HasPrefix(l, "Distributor ID") {
dis := strings.SplitN(l, ":", 2)
vals["distro"] = strings.ToLower(strings.Trim(dis[1], "\n\t\" "))
}
// Look for the release
if strings.HasPrefix(l, "Release") {
rel := strings.SplitN(l, ":", 2)
vals["release"] = strings.ToLower(strings.Trim(rel[1], "\n\t\" "))
}
}
if _, ok := vals["distro"]; !ok {
// The distro key hasn't been set above
errorMsg("Unable to determine distro from lsb_release command, quitting.")
os.Exit(1)
}
if _, ok := vals["release"]; !ok {
// The distro key hasn't been set above
errorMsg("Unable to determine release from lsb_release command, quitting.")
os.Exit(1)
}
return vals["distro"], vals["release"], vals["distro"] + ":" + vals["release"]
}
func parseEtcLsb(f string) (string, string, string) {
// Setup a map of what we need to what /etc/lsb-release uses
fields := map[string]string{
"distro": "DISTRIB_ID",
"release": "DISTRIB_RELEASE",
}
linMap := parseFile(f, "=", fields)
return linMap["distro"], linMap["release"], linMap["distro"] + ":" + linMap["release"]
}
func parseEtcIss(f string) (string, string, string) {
// Setup return map
vals := make(map[string]string)
// Open the file for parsing
file, err := os.Open(f)
if err != nil {
errorMsg(fmt.Sprintf("Unable to open file: %+v\nError was: %v", f, err))
os.Exit(1)
}
defer func() {
err := file.Close()
if err != nil {
traceMsg(fmt.Sprintf("Erro closing file\nError was: %v", err))
os.Exit(1)
}
}()
// Read the file in, pull off the first line and split it
reader := bufio.NewReader(file)
line, err := reader.ReadString('\n')
if err != nil {
errorMsg(fmt.Sprintf("Unable to read file: %+v\nError was: %v", f, err))
os.Exit(1)
}
fields := strings.Split(line, " ")
vals["distro"] = strings.ToLower(fields[0])
vals["release"] = fields[1]
// Correct for Ubuntu 'minor' releases aka 18.04.2
if vals["distro"] == "ubuntu" {
tmp := strings.Split(vals["release"], ".")
vals["release"] = tmp[0] + "." + tmp[1]
}
return vals["distro"], vals["release"], vals["distro"] + ":" + vals["release"]
}
func parseEtcDeb(f string) (string, string, string) {
// Setup map to hold parsed values
vals := make(map[string]string)
vals["distro"] = "debian"
// Open the file for parsing
file, err := os.Open(f)
if err != nil {
errorMsg(fmt.Sprintf("Unable to open file: %+v\nError was: %v", f, err))
os.Exit(1)
}
defer func() {
err := file.Close()
if err != nil {
errorMsg(fmt.Sprintf("Unable to close file\nError was: %v", err))
os.Exit(1)
}
}()
// Read the file in, pull off the first line
reader := bufio.NewReader(file)
line, err := reader.ReadString('\n')
if err != nil {
errorMsg(fmt.Sprintf("Unable to read file: %+v\nError was: %v", f, err))
os.Exit(1)
}
// TODO: Test this with a Debian docker
vals["release"] = strings.ToLower(strings.Trim(line, "\n\t "))
return vals["distro"], vals["release"], vals["distro"] + ":" + vals["release"]
}
func parseFile(f string, sep string, flds map[string]string) map[string]string {
// Setup return map
vals := make(map[string]string)
// Open the file for parsing
file, err := os.Open(f)
if err != nil {
errorMsg(fmt.Sprintf("Unable to open file: %+v\nError was: %v", f, err))
os.Exit(1)
}
defer func() {
err := file.Close()
if err != nil {
errorMsg(fmt.Sprintf("Unable to close file\nError was: %v", err))
os.Exit(1)
}
}()
// Read the file one line at a time till done
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
// Look for the distro
if strings.HasPrefix(line, flds["distro"]+sep) {
dis := strings.SplitN(line, sep, 2)
vals["distro"] = strings.ToLower(strings.Trim(dis[1], "\n\""))
}
// Look for the release
if strings.HasPrefix(line, flds["release"]+sep) {
rel := strings.SplitN(line, sep, 2)
vals["release"] = strings.ToLower(strings.Trim(rel[1], "\n\""))
}
}
return vals
}
func initBootstrap(id string, b *osCmds) {
switch id {
case "ubuntu:18.04":
b.id = "ubuntu:18.04"
b.cmds = []string{
"DEBIAN_FRONTEND=noninteractive apt-get update",
"DEBIAN_FRONTEND=noninteractive apt-get -y upgrade",
"DEBIAN_FRONTEND=noninteractive apt-get -y install python3 python3-virtualenv ca-certificates curl gnupg git",
}
b.errmsg = []string{
"Unable to update apt database",
"Unable to upgrade OS packages with apt",
"Unable to install prerequisites for installer via apt",
}
b.hard = []bool{
true,
true,
true,
}
return
}
}
func checkPythonVersion() bool {
// DefectDojo is now Python 3+, lets make sure that's installed
_, err := exec.LookPath("python3")
if err != nil {
errorMsg(fmt.Sprintf("Unable to find python binary. Error was: %+v", err))
os.Exit(1)
}
// Execute the python3 command with --version to get the version
runCmd := exec.Command("python3", "--version")
// Run command and gather its output
cmdOut, err := runCmd.CombinedOutput()
if err != nil {
errorMsg(fmt.Sprintf("Failed to run python3 command, error was: %+v", err))
os.Exit(1)
}
// Parse command output for the strings we need
lines := bytes.Split(cmdOut, []byte("\n"))
line := strings.Split(string(lines[0]), " ")
pyVer := line[1]
// TODO: Consider checking the minor version of Python3 as well - probably not needed (yet)
if strings.HasPrefix(pyVer, "3.") {
return true
}
// DefectDojo requires Python 3.x
return false
}