This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
forked from optiopay/klar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
klar.go
217 lines (189 loc) · 5.62 KB
/
klar.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/elstak/klar/clair"
"github.com/elstak/klar/docker"
"github.com/elstak/klar/utils"
"gopkg.in/yaml.v2"
)
//Used to represent the structure of the whitelist YAML file
type vulnerabilitiesWhitelistYAML struct {
General []string
Images map[string][]string
}
//Map structure used for ease of searching for whitelisted vulnerabilites
type vulnerabilitiesWhitelist struct {
General map[string]bool //key: CVE and value: true
Images map[string]map[string]bool //key: image name and value: [key: CVE and value: true]
}
const (
optionClairOutput = "CLAIR_OUTPUT"
optionClairAddress = "CLAIR_ADDR"
optionKlarTrace = "KLAR_TRACE"
optionClairThreshold = "CLAIR_THRESHOLD"
optionClairTimeout = "CLAIR_TIMEOUT"
optionDockerTimeout = "DOCKER_TIMEOUT"
optionJSONOutput = "JSON_OUTPUT" // deprecate?
optionFormatOutput = "FORMAT_OUTPUT"
optionDockerUser = "DOCKER_USER"
optionDockerPassword = "DOCKER_PASSWORD"
optionDockerToken = "DOCKER_TOKEN"
optionDockerInsecure = "DOCKER_INSECURE"
optionDockerPlatformOS = "DOCKER_PLATFORM_OS"
optionDockerPlatformArch = "DOCKER_PLATFORM_ARCH"
optionRegistryInsecure = "REGISTRY_INSECURE"
optionWhiteListFile = "WHITELIST_FILE"
optionIgnoreUnfixed = "IGNORE_UNFIXED"
)
var priorities = []string{"Unknown", "Negligible", "Low", "Medium", "High", "Critical", "Defcon1"}
var formatTypes = []string{"standard", "json", "table"}
func parseOutputPriority() (string, error) {
clairOutput := priorities[0]
outputEnv := os.Getenv(optionClairOutput)
if outputEnv != "" {
output := strings.Title(strings.ToLower(outputEnv))
correct := false
for _, sev := range priorities {
if sev == output {
clairOutput = sev
correct = true
break
}
}
if !correct {
return "", fmt.Errorf("Clair output level %s is not supported, only support %v\n", outputEnv, priorities)
}
}
return clairOutput, nil
}
func parseIntOption(key string) int {
val := 0
valStr := os.Getenv(key)
if valStr != "" {
val, _ = strconv.Atoi(valStr)
}
return val
}
func parseBoolOption(key string) bool {
val := false
if envVal, err := strconv.ParseBool(os.Getenv(key)); err == nil {
val = envVal
}
return val
}
func parseFormatTypes() (string, error) {
// until JSON_OUTPUT is actually removed, it should override FORMAT_OUTPUT
if parseBoolOption(optionJSONOutput) {
return "json", nil
}
formatStyle := formatTypes[0]
formatOutputEnv := os.Getenv(optionFormatOutput)
if formatOutputEnv != "" {
output := strings.ToLower(formatOutputEnv)
correct := false
for _, stlye := range formatTypes {
if stlye == output {
formatStyle = stlye
correct = true
break
}
}
if !correct {
return "", fmt.Errorf("Format type %s is not supported, only support %v\n", formatOutputEnv, formatTypes)
}
}
return formatStyle, nil
}
type jsonOutput struct {
LayerCount int
Vulnerabilities map[string][]*clair.Vulnerability
}
type config struct {
ClairAddr string
ClairOutput string
Threshold int
JSONOutput bool
FormatStyle string
ClairTimeout time.Duration
DockerConfig docker.Config
WhiteListFile string
IgnoreUnfixed bool
}
func newConfig(args []string) (*config, error) {
clairAddr := os.Getenv(optionClairAddress)
if clairAddr == "" {
return nil, fmt.Errorf("Clair address must be provided\n")
}
if os.Getenv(optionKlarTrace) != "" {
utils.Trace = true
}
clairOutput, err := parseOutputPriority()
if err != nil {
return nil, err
}
clairTimeout := parseIntOption(optionClairTimeout)
if clairTimeout == 0 {
clairTimeout = 1
}
dockerTimeout := parseIntOption(optionDockerTimeout)
if dockerTimeout == 0 {
dockerTimeout = 1
}
formatStyle, err := parseFormatTypes()
if err != nil {
return nil, err
}
return &config{
ClairAddr: clairAddr,
ClairOutput: clairOutput,
Threshold: parseIntOption(optionClairThreshold),
JSONOutput: formatStyle == "json",
FormatStyle: formatStyle,
IgnoreUnfixed: parseBoolOption(optionIgnoreUnfixed),
ClairTimeout: time.Duration(clairTimeout) * time.Minute,
WhiteListFile: os.Getenv(optionWhiteListFile),
DockerConfig: docker.Config{
ImageName: args[1],
User: os.Getenv(optionDockerUser),
Password: os.Getenv(optionDockerPassword),
Token: os.Getenv(optionDockerToken),
InsecureTLS: parseBoolOption(optionDockerInsecure),
InsecureRegistry: parseBoolOption(optionRegistryInsecure),
Timeout: time.Duration(dockerTimeout) * time.Minute,
PlatformOS: os.Getenv(optionDockerPlatformOS),
PlatformArch: os.Getenv(optionDockerPlatformArch),
},
}, nil
}
//Parse the whitelist file
func parseWhitelistFile(whitelistFile string) (*vulnerabilitiesWhitelist, error) {
whitelistYAML := vulnerabilitiesWhitelistYAML{}
whitelist := vulnerabilitiesWhitelist{}
//read the whitelist file
whitelistBytes, err := ioutil.ReadFile(whitelistFile)
if err != nil {
return nil, fmt.Errorf("could not read file %v", err)
}
if err = yaml.Unmarshal(whitelistBytes, &whitelistYAML); err != nil {
return nil, fmt.Errorf("could not unmarshal %v", err)
}
//Initialize the whitelist maps
whitelist.General = make(map[string]bool)
whitelist.Images = make(map[string]map[string]bool)
//Populate the maps
for _, cve := range whitelistYAML.General {
whitelist.General[cve] = true
}
for image, cveList := range whitelistYAML.Images {
whitelist.Images[image] = make(map[string]bool)
for _, cve := range cveList {
whitelist.Images[image][cve] = true
}
}
return &whitelist, nil
}