-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
142 lines (126 loc) · 3.46 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
package main
import (
"flag"
"fmt"
"github.com/khulnasoft-lab/reportgen/data"
"github.com/khulnasoft-lab/reportgen/pdfrender"
"github.com/khulnasoft-lab/reportgen/rest"
"github.com/joho/godotenv"
"os"
"strings"
)
var (
serverUrl string
registryName string
imageName string
host string
user string
password string
output string
severities []string
severityParams string
severitiesTypes = map[string]struct{}{
"critical": {},
"high": {},
"medium": {},
"low": {},
}
)
const (
cmdRegistry = "registry"
cmdServer = "server"
cmdImage = "image"
cmdHost = "host"
cmdUser = "user"
cmdPassword = "password"
cmdOutput = "output"
cmdSeverity = "severity"
)
func init() {
flag.StringVar(&serverUrl, cmdServer, "", "URL of a data server")
flag.StringVar(®istryName, cmdRegistry, "", "name of a registry")
flag.StringVar(&imageName, cmdImage, "", "name of an image")
flag.StringVar(&user, cmdUser, "", "a user for the basic authentication")
flag.StringVar(&password, cmdPassword, "", "a user's password for the basic authentication")
flag.StringVar(&output, cmdOutput, "report.pdf", "a name of output pdf file")
flag.StringVar(&severityParams, cmdSeverity, "", "to get list of vulnerabilities. critical,high,medium,low")
flag.StringVar(&host, cmdHost, "", "PDF generation to a host name")
}
func checkRequiredParams() bool {
if (host != "" && imageName != "") || (host == "" && imageName == "") {
fmt.Println("Wrong params: you should setup either a host or an image!")
return false
}
if serverUrl == "" {
if serverUrl = os.Getenv("server"); serverUrl == "" {
fmt.Println("Server isn't setup (as -server param or environment variable)")
return false
}
}
if user == "" {
if user = os.Getenv("user"); user == "" {
fmt.Println("User isn't setup (as -user param or environment variable)")
return false
}
}
if password == "" {
if password = os.Getenv("password"); password == "" {
fmt.Println("Password isn't setup (as -password param or environment variable)")
return false
}
}
if host != "" {
return true
}
var missingRequiredFlags []string
if registryName == "" {
missingRequiredFlags = append(missingRequiredFlags, cmdRegistry)
}
if imageName == "" {
missingRequiredFlags = append(missingRequiredFlags, cmdImage)
}
if len(missingRequiredFlags) > 0 {
message := "Param '%s' is missing or the value is empty.\n"
for _, f := range missingRequiredFlags {
fmt.Printf(message, f)
}
return false
}
if severityParams != "" {
severities = strings.Split(strings.ToLower(severityParams), ",")
for _, severity := range severities {
if _, ok := severitiesTypes[severity]; !ok {
fmt.Println("Wrong severity type:", severity)
return false
}
}
}
return true
}
func main() {
flag.Parse()
godotenv.Load()
if ok := checkRequiredParams(); !ok {
fmt.Println("Run with key '-h' for usage.")
os.Exit(1)
}
var filename string
if !strings.HasSuffix(output, ".pdf") {
filename = output + ".pdf"
} else {
filename = output
}
var report *data.Report
if imageName != "" {
report = rest.GetImageData(serverUrl, user, password, registryName, imageName, severities)
} else if host != "" {
report = rest.GetHostData(serverUrl, user, password, host)
}
report.ServerUrl = serverUrl
err := pdfrender.Render(filename, report)
if err != nil {
fmt.Println("Error:", err.Error())
os.Exit(1)
}
fmt.Println("Report was written to", filename)
}