-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathverify-bundle.go
159 lines (134 loc) · 4.18 KB
/
verify-bundle.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
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
var bundlePath = "bundle.tar.gz"
var OrasPush = []string{"--artifact-type", "application/vnd.cncf.openpolicyagent.config.v1+json", fmt.Sprintf("%s:application/vnd.cncf.openpolicyagent.layer.v1.tar+gzip", bundlePath)}
var supportedTrivyVersions = []string{"0.57.1", "0.58.1", "latest", "canary"} // TODO: add more versions
func createRegistryContainer(ctx context.Context) (testcontainers.Container, string) {
reqReg := testcontainers.ContainerRequest{
Image: "registry:2",
ExposedPorts: []string{"5111:5000/tcp"},
WaitingFor: wait.ForExposedPort(),
}
regC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: reqReg,
Started: true,
})
if err != nil {
panic(err)
}
regIP, _ := regC.Host(ctx)
fmt.Println(regIP)
return regC, regIP
}
func createOrasContainer(ctx context.Context, regIP string, bundlePath string) testcontainers.Container {
reqOras := testcontainers.ContainerRequest{
Image: "bitnami/oras:latest",
Cmd: append([]string{"push", fmt.Sprintf("%s:5111/defsec-test:latest", regIP)}, OrasPush...),
HostConfigModifier: func(config *container.HostConfig) {
config.NetworkMode = "host"
config.Mounts = []mount.Mount{
{
Type: mount.TypeBind,
Source: bundlePath,
Target: "/bundle.tar.gz",
}}
},
WaitingFor: wait.ForLog("Pushed [registry] localhost:5111/defsec-test:latest"),
}
orasC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: reqOras,
Started: true,
})
if err != nil {
panic(err)
}
return orasC
}
func createTrivyContainer(ctx context.Context, trivyVersion string, regIP string) testcontainers.Container {
testDataPath, err := filepath.Abs("testdata")
if err != nil {
panic(err)
}
reqTrivy := testcontainers.ContainerRequest{
Image: fmt.Sprintf("aquasec/trivy:%s", trivyVersion),
AlwaysPullImage: true,
Cmd: []string{"--debug", "config", "--include-deprecated-checks=false", fmt.Sprintf("--checks-bundle-repository=%s:5111/defsec-test:latest", regIP), "/testdata"},
HostConfigModifier: func(config *container.HostConfig) {
config.NetworkMode = "host"
config.Mounts = []mount.Mount{
{
Type: mount.TypeBind,
Source: testDataPath,
Target: "/testdata",
},
}
},
WaitingFor: wait.ForLog("OS is not detected."),
}
trivyC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: reqTrivy,
Started: true,
})
if err != nil {
panic(err)
}
return trivyC
}
func debugLogsForContainer(ctx context.Context, c testcontainers.Container) string {
r, err := c.Logs(ctx)
if err != nil {
panic(err)
}
b, _ := io.ReadAll(r)
return string(b)
}
func LoadAndVerifyBundle() {
ctx := context.Background()
bundlePath, err := filepath.Abs("bundle.tar.gz")
if err != nil {
panic(err)
}
regC, regIP := createRegistryContainer(ctx)
defer func() {
if err = regC.Terminate(ctx); err != nil {
panic(err)
}
}()
orasC := createOrasContainer(ctx, regIP, bundlePath)
defer func() {
if err = orasC.Terminate(ctx); err != nil {
panic(err)
}
}()
fmt.Println(debugLogsForContainer(ctx, regC))
fmt.Println(debugLogsForContainer(ctx, orasC))
for _, trivyVersion := range supportedTrivyVersions {
fmt.Println("=======Testing version: ", trivyVersion, "==========")
trivyC := createTrivyContainer(ctx, trivyVersion, regIP)
fmt.Println(debugLogsForContainer(ctx, trivyC))
if !assertInLogs(debugLogsForContainer(ctx, trivyC), `Tests: 1 (SUCCESSES: 0, FAILURES: 1)`) {
panic("asserting Trivy logs for misconfigurations failed, check Trivy log output")
}
if err = trivyC.Terminate(ctx); err != nil {
panic(err)
}
}
}
func assertInLogs(containerLogs, assertion string) bool {
return strings.Contains(containerLogs, assertion)
}
func main() {
os.Setenv("TESTCONTAINERS_RYUK_DISABLED", "true")
LoadAndVerifyBundle()
}