-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.go
231 lines (184 loc) · 5.56 KB
/
wrapper.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
package tunnel
import (
"encoding/json"
"fmt"
"io"
"log/slog"
"os/exec"
"strings"
"github.com/khulnasoft/harbor-scanner-tunnel/pkg/etc"
"github.com/khulnasoft/harbor-scanner-tunnel/pkg/ext"
)
const (
tunnelCmd = "tunnel"
)
type ImageRef struct {
Name string
Auth RegistryAuth
Insecure bool
}
// RegistryAuth wraps registry credentials.
type RegistryAuth interface {
}
type NoAuth struct {
}
type BasicAuth struct {
Username string
Password string
}
type BearerAuth struct {
Token string
}
type Wrapper interface {
Scan(imageRef ImageRef) ([]Vulnerability, error)
GetVersion() (VersionInfo, error)
}
type wrapper struct {
config etc.Tunnel
ambassador ext.Ambassador
}
func NewWrapper(config etc.Tunnel, ambassador ext.Ambassador) Wrapper {
return &wrapper{
config: config,
ambassador: ambassador,
}
}
func (w *wrapper) Scan(imageRef ImageRef) ([]Vulnerability, error) {
logger := slog.With(slog.String("image_ref", imageRef.Name))
logger.Debug("Started scanning")
reportFile, err := w.ambassador.TempFile(w.config.ReportsDir, "scan_report_*.json")
if err != nil {
return nil, err
}
logger.Debug("Saving scan report to tmp file", slog.String("path", reportFile.Name()))
defer func() {
logger.Debug("Removing scan report tmp file", slog.String("path", reportFile.Name()))
if err = w.ambassador.Remove(reportFile.Name()); err != nil {
logger.Warn("Error while removing scan report tmp file", slog.String("err", err.Error()))
}
}()
cmd, err := w.prepareScanCmd(imageRef, reportFile.Name())
if err != nil {
return nil, err
}
logger.Debug("Exec command with args", slog.String("path", cmd.Path),
slog.String("args", strings.Join(cmd.Args, " ")))
stdout, err := w.ambassador.RunCmd(cmd)
if err != nil {
logger.Error("Running tunnel failed",
slog.String("exit_code", fmt.Sprintf("%d", cmd.ProcessState.ExitCode())),
slog.String("std_out", string(stdout)),
)
return nil, fmt.Errorf("running tunnel: %v: %v", err, string(stdout))
}
logger.Debug("Running tunnel finished",
slog.String("exit_code", fmt.Sprintf("%d", cmd.ProcessState.ExitCode())),
slog.String("std_out", string(stdout)),
)
return w.parseVulnerabilities(reportFile)
}
func (w *wrapper) parseVulnerabilities(reportFile io.Reader) ([]Vulnerability, error) {
var scanReport ScanReport
if err := json.NewDecoder(reportFile).Decode(&scanReport); err != nil {
return nil, fmt.Errorf("decoding scan report from file: %w", err)
}
if scanReport.SchemaVersion != SchemaVersion {
return nil, fmt.Errorf("unsupported schema %d, expected %d", scanReport.SchemaVersion, SchemaVersion)
}
var vulnerabilities []Vulnerability
for _, scanResult := range scanReport.Results {
slog.Debug("Parsing vulnerabilities", slog.String("target", scanResult.Target))
vulnerabilities = append(vulnerabilities, scanResult.Vulnerabilities...)
}
return vulnerabilities, nil
}
func (w *wrapper) prepareScanCmd(imageRef ImageRef, outputFile string) (*exec.Cmd, error) {
args := []string{
"--no-progress",
"--severity", w.config.Severity,
"--vuln-type", w.config.VulnType,
"--scanners", w.config.SecurityChecks,
"--format", "json",
"--output", outputFile,
imageRef.Name,
}
if w.config.IgnoreUnfixed {
args = append([]string{"--ignore-unfixed"}, args...)
}
if w.config.SkipUpdate {
args = append([]string{"--skip-db-update"}, args...)
}
if w.config.SkipJavaDBUpdate {
args = append([]string{"--skip-java-db-update"}, args...)
}
if w.config.OfflineScan {
args = append([]string{"--offline-scan"}, args...)
}
if w.config.IgnorePolicy != "" {
args = append([]string{"--ignore-policy", w.config.IgnorePolicy}, args...)
}
name, err := w.ambassador.LookPath(tunnelCmd)
if err != nil {
return nil, err
}
globalArgs := []string{"--cache-dir", w.config.CacheDir}
if w.config.DebugMode {
globalArgs = append(globalArgs, "--debug")
}
globalArgs = append(globalArgs, "image")
args = append(globalArgs, args...)
cmd := exec.Command(name, args...)
cmd.Env = w.ambassador.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("TUNNEL_TIMEOUT=%s", w.config.Timeout.String()))
switch a := imageRef.Auth.(type) {
case NoAuth:
case BasicAuth:
cmd.Env = append(cmd.Env,
fmt.Sprintf("TUNNEL_USERNAME=%s", a.Username),
fmt.Sprintf("TUNNEL_PASSWORD=%s", a.Password))
case BearerAuth:
cmd.Env = append(cmd.Env,
fmt.Sprintf("TUNNEL_REGISTRY_TOKEN=%s", a.Token))
default:
return nil, fmt.Errorf("invalid auth type %T", a)
}
if imageRef.Insecure {
cmd.Env = append(cmd.Env, "TUNNEL_NON_SSL=true")
}
if strings.TrimSpace(w.config.GitHubToken) != "" {
cmd.Env = append(cmd.Env, fmt.Sprintf("GITHUB_TOKEN=%s", w.config.GitHubToken))
}
if w.config.Insecure {
cmd.Env = append(cmd.Env, "TUNNEL_INSECURE=true")
}
return cmd, nil
}
func (w *wrapper) GetVersion() (VersionInfo, error) {
cmd, err := w.prepareVersionCmd()
if err != nil {
return VersionInfo{}, fmt.Errorf("failed preparing tunnel version command: %w", err)
}
versionOutput, err := w.ambassador.RunCmd(cmd)
if err != nil {
return VersionInfo{}, fmt.Errorf("failed running tunnel version command: %w: %v", err, string(versionOutput))
}
var vi VersionInfo
err = json.Unmarshal(versionOutput, &vi)
if err != nil {
return VersionInfo{}, fmt.Errorf("failed parsing tunnel version output: %w", err)
}
return vi, nil
}
func (w *wrapper) prepareVersionCmd() (*exec.Cmd, error) {
args := []string{
"--cache-dir", w.config.CacheDir,
"version",
"--format", "json",
}
name, err := w.ambassador.LookPath(tunnelCmd)
if err != nil {
return nil, err
}
cmd := exec.Command(name, args...)
return cmd, nil
}