-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtuftool_manual_tuf_repo_test.go
296 lines (255 loc) · 9.57 KB
/
tuftool_manual_tuf_repo_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package tuftool
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/securesign/sigstore-e2e/pkg/api"
"github.com/securesign/sigstore-e2e/pkg/clients"
"github.com/securesign/sigstore-e2e/test/testsupport"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
)
var workdir string
var root string
var input string
var keyDir string
var tufRepo string
var rootDir string
const ctlogPubkeyPath string = "targets/ctfe.pub"
const fulcioCertPath string = "targets/fulcio_v1.crt.pem"
const rekorPubkeyPath string = "targets/rekor.pub"
const tsaCertPath string = "targets/tsa.certchain.pem"
const expirationWeeks52 string = "in 52 weeks"
const expirationDays10 string = "in 10 days"
var _ = Describe("TUF manual repo test", Ordered, func() {
var (
err error
tuftool *clients.Tuftool
)
BeforeAll(func() {
tuftool = clients.NewTuftool()
openshiftStrategyActive := api.GetValueFor(api.CliStrategy) == "openshift"
if openshiftStrategyActive && (runtime.GOOS != "linux" || runtime.GOARCH != "amd64") {
logrus.Info("Skipping tuftool download test: openshift strategy is only supported on linux/amd64")
Skip("Skipping tuftool download test: openshift strategy is only supported on linux/amd64")
}
Expect(testsupport.InstallPrerequisites(tuftool)).To(Succeed())
DeferCleanup(func() {
if err := testsupport.DestroyPrerequisites(); err != nil {
logrus.Warn("Env was not cleaned-up" + err.Error())
}
})
workdir, err = os.MkdirTemp("", "trustroot_example")
Expect(err).ToNot(HaveOccurred())
logrus.Infof("Created temporary directory: %s", workdir)
})
It("should setup repository via tuftool", func() {
setupManualTufRepo(tuftool)
})
It("should verify workdir structure", func() {
verifyWorkdirStructure(workdir)
})
})
func setupManualTufRepo(tuftool *clients.Tuftool) {
// "Tuf repo directory"
root = filepath.Join(workdir, "root", "root.json")
rootDir = filepath.Join(workdir, "root")
input = filepath.Join(workdir, "input")
keyDir = filepath.Join(workdir, "keys")
tufRepo = filepath.Join(workdir, "tuf-repo")
err := os.MkdirAll(rootDir, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = os.MkdirAll(keyDir, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = os.MkdirAll(input, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = os.MkdirAll(tufRepo, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
Expect(tuftool.Command(testsupport.TestContext, "root", "init", root).Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "root", "expire", root, expirationWeeks52).Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "root", "set-threshold", root, "root", "1").Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "root", "set-threshold", root, "snapshot", "1").Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "root", "set-threshold", root, "targets", "1").Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "root", "set-threshold", root, "timestamp", "1").Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "root", "gen-rsa-key", root, keyDir+"/root.pem", "--role", "root").Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "root", "gen-rsa-key", root, keyDir+"/snapshot.pem", "--role", "snapshot").Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "root", "gen-rsa-key", root, keyDir+"/targets.pem", "--role", "targets").Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "root", "gen-rsa-key", root, keyDir+"/timestamp.pem", "--role", "timestamp").Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "root", "sign", root, "-k", keyDir+"/root.pem").Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "create",
"--root", root,
"--key", keyDir+"/root.pem",
"--key", keyDir+"/snapshot.pem",
"--key", keyDir+"/targets.pem",
"--key", keyDir+"/timestamp.pem",
"--add-targets", input,
"--targets-expires", expirationWeeks52,
"--targets-version", "1",
"--snapshot-expires", expirationWeeks52,
"--snapshot-version", "1",
"--timestamp-expires", expirationDays10,
"--timestamp-version", "1",
"--outdir", tufRepo).Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "rhtas",
"--root", root,
"--key", keyDir+"/root.pem",
"--key", keyDir+"/snapshot.pem",
"--key", keyDir+"/targets.pem",
"--key", keyDir+"/timestamp.pem",
"--set-ctlog-target", ctlogPubkeyPath,
"--ctlog-uri", "https://ctlog.rhtas",
"--targets-expires", expirationWeeks52,
"--targets-version", "1",
"--snapshot-expires", expirationWeeks52,
"--snapshot-version", "1",
"--timestamp-expires", expirationDays10,
"--timestamp-version", "1",
"--force-version",
"--outdir", tufRepo,
"--metadata-url", "file://"+tufRepo).Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "rhtas",
"--root", root,
"--key", keyDir+"/root.pem",
"--key", keyDir+"/snapshot.pem",
"--key", keyDir+"/targets.pem",
"--key", keyDir+"/timestamp.pem",
"--set-fulcio-target", fulcioCertPath,
"--fulcio-uri", "https://fulcio.rhtas",
"--targets-expires", expirationWeeks52,
"--targets-version", "1",
"--snapshot-expires", expirationWeeks52,
"--snapshot-version", "1",
"--timestamp-expires", expirationDays10,
"--timestamp-version", "1",
"--force-version",
"--outdir", tufRepo,
"--metadata-url", "file://"+tufRepo).Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "rhtas",
"--root", root,
"--key", keyDir+"/root.pem",
"--key", keyDir+"/snapshot.pem",
"--key", keyDir+"/targets.pem",
"--key", keyDir+"/timestamp.pem",
"--set-rekor-target", rekorPubkeyPath,
"--rekor-uri", "https://rekor.rhtas",
"--targets-expires", expirationWeeks52,
"--targets-version", "1",
"--snapshot-expires", expirationWeeks52,
"--snapshot-version", "1",
"--timestamp-expires", expirationDays10,
"--timestamp-version", "1",
"--force-version",
"--outdir", tufRepo,
"--metadata-url", "file://"+tufRepo).Run()).To(Succeed())
Expect(tuftool.Command(testsupport.TestContext, "rhtas",
"--root", root,
"--key", keyDir+"/root.pem",
"--key", keyDir+"/snapshot.pem",
"--key", keyDir+"/targets.pem",
"--key", keyDir+"/timestamp.pem",
"--set-tsa-target", tsaCertPath,
"--tsa-uri", "https://tsa.rhtas",
"--targets-expires", expirationWeeks52,
"--targets-version", "1",
"--snapshot-expires", expirationWeeks52,
"--snapshot-version", "1",
"--timestamp-expires", expirationDays10,
"--timestamp-version", "1",
"--force-version",
"--outdir", tufRepo,
"--metadata-url", "file://"+tufRepo).Run()).To(Succeed())
}
func verifyWorkdirStructure(rootPath string) {
expectedDirs := map[string]bool{
"input": true,
"keys": true,
"root": true,
"tuf-repo": true,
"tuf-repo/targets": true,
}
expectedFiles := map[string]bool{
"keys/root.pem": true,
"keys/snapshot.pem": true,
"keys/targets.pem": true,
"keys/timestamp.pem": true,
"root/root.json": true,
"tuf-repo/1.root.json": true,
"tuf-repo/1.snapshot.json": true,
"tuf-repo/1.targets.json": true,
"tuf-repo/root.json": true,
"tuf-repo/timestamp.json": true,
}
targetSuffixes := []string{
".tsa.certchain.pem",
".trusted_root.json",
".fulcio_v1.crt.pem",
".ctfe.pub",
".rekor.pub",
}
foundSuffixesCount := make(map[string]int)
for _, suffix := range targetSuffixes {
foundSuffixesCount[suffix] = 0 // Initialize count for each suffix
}
err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
Expect(err).ToNot(HaveOccurred())
relPath, err := filepath.Rel(rootPath, path)
Expect(err).ToNot(HaveOccurred())
// skip root directory
if relPath == "." {
return nil
}
if info.IsDir() {
_, exists := expectedDirs[relPath]
Expect(exists).To(BeTrue(), "unexpected directory: "+filepath.Join(rootPath, relPath))
delete(expectedDirs, relPath)
} else {
if strings.HasPrefix(relPath, "tuf-repo/targets/") {
validSuffix := false
for _, suffix := range targetSuffixes {
if strings.HasSuffix(relPath, suffix) {
foundSuffixesCount[suffix]++
validSuffix = true
break
}
}
Expect(validSuffix).To(BeTrue(), "unexpected file in targets: "+filepath.Join(rootPath, relPath))
} else {
_, exists := expectedFiles[relPath]
Expect(exists).To(BeTrue(), "unexpected file: "+filepath.Join(rootPath, relPath))
delete(expectedFiles, relPath)
}
}
return nil
})
Expect(err).ToNot(HaveOccurred())
if len(expectedDirs) > 0 {
missingDirs := []string{}
for dir := range expectedDirs {
missingDirs = append(missingDirs, filepath.Join(workdir, dir))
}
Expect(missingDirs).To(BeEmpty(), fmt.Sprintf("missing directories: %v", missingDirs))
}
if len(expectedFiles) > 0 {
missingFiles := []string{}
for file := range expectedFiles {
missingFiles = append(missingFiles, filepath.Join(workdir, file))
}
Expect(missingFiles).To(BeEmpty(), fmt.Sprintf("missing files: %v", missingFiles))
}
for suffix, count := range foundSuffixesCount {
if suffix == ".trusted_root.json" {
// Allow multiple .trusted_root.json files
Expect(count).To(BeNumerically(">=", 1), fmt.Sprintf("Expected at least one .trusted_root.json file, found %d", count))
} else {
// Ensure only one file for other suffixes
Expect(count).To(Equal(1), fmt.Sprintf("Expected exactly one file with suffix %s, found %d", suffix, count))
}
}
}
var _ = AfterSuite(func() {
// Cleanup shared resources after all tests have run.
Expect(os.RemoveAll(workdir)).To(Succeed())
})