-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsecurityscan_test.go
229 lines (206 loc) · 7.4 KB
/
securityscan_test.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
package securityscanutils_test
import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"sort"
"github.com/solo-io/go-utils/fileutils"
"github.com/Masterminds/semver/v3"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/solo-io/go-utils/securityscanutils"
)
const (
repoName = "gloo"
gatewayOwnerName = "solo-io"
)
var _ = Describe("Security Scan Suite", func() {
var (
outputDir string
)
BeforeEach(func() {
var err error
outputDir, err = ioutil.TempDir("", "")
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
err := os.RemoveAll(outputDir)
Expect(err).NotTo(HaveOccurred())
})
Context("Security Scanner", func() {
It("works", func() {
verConstraint, err := semver.NewConstraint("=v1.14.0 || =v1.15.1")
Expect(err).NotTo(HaveOccurred())
fmt.Println("Output dir:", outputDir)
secScanner := &SecurityScanner{
Repos: []*SecurityScanRepo{{
Repo: repoName,
Owner: gatewayOwnerName,
Opts: &SecurityScanOpts{
OutputDir: outputDir,
OutputResultLocally: true,
ImagesPerVersion: map[string][]string{
"v1.14.0": {"gloo"},
// Scan should continue in the case an image cannot be found
"v1.15.1": {"thisimagecannotbefound", "gloo", "discovery"},
},
VersionConstraint: verConstraint,
ImageRepo: "quay.io/solo-io",
},
}},
}
// Run security scan
err = secScanner.GenerateSecurityScans(context.TODO())
Expect(err).NotTo(HaveOccurred())
ExpectDirToHaveFiles(outputDir, "gloo")
// Have a markdown file for each version we scanned
glooDir := path.Join(outputDir, "gloo")
ExpectDirToHaveFiles(glooDir, "issue_results", "markdown_results")
githubIssueDir := path.Join(glooDir, "issue_results")
ExpectDirToHaveFiles(githubIssueDir, "1.14.0.md", "1.15.1.md")
// Have a directory for each repo we scanned
markdownDir := path.Join(outputDir, "gloo", "markdown_results")
// Have a directory for each version we scanned
ExpectDirToHaveFiles(markdownDir, "1.14.0", "1.15.1")
// Expect there to be a generated docgen file for each image per version
ExpectDirToHaveFiles(path.Join(markdownDir, "1.14.0"), "gloo_cve_report.docgen")
ExpectDirToHaveFiles(path.Join(markdownDir, "1.15.1"), "discovery_cve_report.docgen", "gloo_cve_report.docgen")
})
It("scans all images from all constraints matched", func() {
verConstraint, err := semver.NewConstraint("=v1.15.0")
Expect(err).NotTo(HaveOccurred())
fmt.Println("Output dir:", outputDir)
secScanner := &SecurityScanner{
Repos: []*SecurityScanRepo{{
Repo: repoName,
Owner: gatewayOwnerName,
Opts: &SecurityScanOpts{
OutputDir: outputDir,
// Specify redundant constraints
ImagesPerVersion: map[string][]string{
">v1.14.0": {"gloo", "discovery"},
">=v1.15.0": {"glooGreaterThan17"},
},
VersionConstraint: verConstraint,
ImageRepo: "quay.io/solo-io",
},
}},
}
imagesToScan, err := secScanner.Repos[0].GetImagesToScan(semver.MustParse("v1.15.7"))
Expect(imagesToScan).To(ContainElements("gloo", "discovery", "glooGreaterThan17"))
})
It("errors if no constraint is matched", func() {
verConstraint, err := semver.NewConstraint("=v1.15.0")
Expect(err).NotTo(HaveOccurred())
fmt.Println("Output dir:", outputDir)
secScanner := &SecurityScanner{
Repos: []*SecurityScanRepo{{
Repo: repoName,
Owner: gatewayOwnerName,
Opts: &SecurityScanOpts{
OutputDir: outputDir,
ImagesPerVersion: map[string][]string{
"v1.14.0": {"gloo", "discovery"},
},
VersionConstraint: verConstraint,
ImageRepo: "quay.io/solo-io",
},
}},
}
err = secScanner.GenerateSecurityScans(context.TODO())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("version 1.15.0 matched no constraints and has no images to scan"))
})
When("scan has unrecoverable error", func() {
It("short-circuits", func() {
verConstraint, err := semver.NewConstraint("=v1.13.0 || =v1.14.0")
Expect(err).NotTo(HaveOccurred())
fmt.Println("Output dir:", outputDir)
secScanner := &SecurityScanner{
Repos: []*SecurityScanRepo{{
Repo: repoName,
Owner: gatewayOwnerName,
Opts: &SecurityScanOpts{
OutputDir: outputDir,
OutputResultLocally: true,
ImagesPerVersion: map[string][]string{
"v1.14.0": {"gloo; $(poorly formatted image name to force UnrecoverableError)"},
},
VersionConstraint: verConstraint,
ImageRepo: "quay.io/solo-io",
},
}},
}
// Run security scan
err = secScanner.GenerateSecurityScans(context.TODO())
Expect(err).To(MatchError(UnrecoverableErr))
ExpectDirToHaveFiles(outputDir, "gloo")
// No images scanned; no files should exist
glooDir := path.Join(outputDir, "gloo")
ExpectDirToHaveFiles(glooDir, "issue_results", "markdown_results")
localIssueDir := path.Join(glooDir, "issue_results")
ExpectDirToHaveFiles(localIssueDir)
// Have a directory for each repo we scanned
markdownDir := path.Join(outputDir, "gloo", "markdown_results")
// Have a directory for each version we scanned
ExpectDirToHaveFiles(markdownDir, "1.14.0")
ExpectDirToHaveFiles(path.Join(markdownDir, "1.14.0"))
})
})
When("scan has recoverable error", func() {
It("contains error in generated file", func() {
verConstraint, err := semver.NewConstraint("=v1.15.0")
Expect(err).NotTo(HaveOccurred())
fmt.Println("Output dir:", outputDir)
secScanner := &SecurityScanner{
Repos: []*SecurityScanRepo{{
Repo: repoName,
Owner: gatewayOwnerName,
Opts: &SecurityScanOpts{
OutputDir: outputDir,
OutputResultLocally: true,
ImagesPerVersion: map[string][]string{
"v1.15.0": {"thisimagedoesnotexist"},
},
VersionConstraint: verConstraint,
ImageRepo: "quay.io/solo-io",
},
}},
}
// Run security scan
err = secScanner.GenerateSecurityScans(context.TODO())
Expect(err).NotTo(HaveOccurred())
ExpectDirToHaveFiles(outputDir, "gloo")
// No images scanned; no files should exist
glooDir := path.Join(outputDir, "gloo")
ExpectDirToHaveFiles(glooDir, "issue_results", "markdown_results")
localIssueDir := path.Join(glooDir, "issue_results")
ExpectDirToHaveFiles(localIssueDir, "1.15.0.md")
contents, err := fileutils.ReadFileString(path.Join(localIssueDir, "1.15.0.md"))
Expect(err).NotTo(HaveOccurred())
Expect(contents).To(ContainSubstring(ImageNotFoundError.Error()))
// Have a directory for each repo we scanned
markdownDir := path.Join(outputDir, "gloo", "markdown_results")
// Have a directory for each version we scanned
ExpectDirToHaveFiles(markdownDir, "1.15.0")
ExpectDirToHaveFiles(path.Join(markdownDir, "1.15.0"))
})
})
})
})
// Accepts a list of file names and passes all tests only if the directory path passed in
// as dir includes all fileNames passed in.
func ExpectDirToHaveFiles(dir string, fileNames ...string) {
dirResults, err := ioutil.ReadDir(dir)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, dirResults).To(HaveLen(len(fileNames)))
var dirFiles []string
for _, res := range dirResults {
dirFiles = append(dirFiles, res.Name())
}
sort.Strings(dirFiles)
sort.Strings(fileNames)
ExpectWithOffset(1, dirFiles).To(Equal(fileNames))
}