forked from sylabs/singularity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.go
209 lines (196 loc) · 7.44 KB
/
verify.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
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package verify
import (
"os"
"path/filepath"
"testing"
"github.com/sylabs/singularity/e2e/internal/e2e"
"github.com/sylabs/singularity/e2e/internal/testhelper"
)
type ctx struct {
e2e.TestEnv
}
func (c *ctx) verify(t *testing.T) {
keyPath := filepath.Join("..", "test", "keys", "ed25519-public.pem")
certPath := filepath.Join("..", "test", "certs", "leaf.pem")
intPath := filepath.Join("..", "test", "certs", "intermediate.pem")
rootPath := filepath.Join("..", "test", "certs", "root.pem")
tests := []struct {
name string
envs []string
flags []string
imagePath string
expectCode int
expectOps []e2e.SingularityCmdResultOp
}{
{
name: "Help",
flags: []string{"--help"},
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ContainMatch, "Verify digital signature(s) within an image"),
},
},
{
name: "OK",
imagePath: filepath.Join("..", "test", "images", "one-group-signed-pgp.sif"),
flags: []string{"--local"},
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with PGP key material"),
e2e.ExpectOutput(e2e.ContainMatch, "Signing entity: SingularityCE Tests <[email protected]>"),
e2e.ExpectOutput(e2e.ContainMatch, "Fingerprint: F34371D0ACD5D09EB9BD853A80600A5FA11BBD29"),
e2e.ExpectError(e2e.ContainMatch, "Verified signature(s) from image"),
},
},
{
name: "LegacyObjectIDFlag",
flags: []string{"--local", "--legacy-insecure", "--sif-id", "2"},
imagePath: filepath.Join("..", "test", "images", "one-group-signed-legacy.sif"),
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with PGP key material"),
e2e.ExpectOutput(e2e.ContainMatch, "Signing entity: SingularityCE Tests <[email protected]>"),
e2e.ExpectOutput(e2e.ContainMatch, "Fingerprint: F34371D0ACD5D09EB9BD853A80600A5FA11BBD29"),
e2e.ExpectError(e2e.ContainMatch, "Verified signature(s) from image"),
},
},
{
name: "LegacyObjectIDFlagNotFound",
flags: []string{"--local", "--legacy-insecure", "--sif-id", "9"},
imagePath: filepath.Join("..", "test", "images", "one-group-signed-legacy.sif"),
expectCode: 255,
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with PGP key material"),
e2e.ExpectError(e2e.ContainMatch, "integrity: object not found"),
},
},
{
name: "LegacyGroupIDFlag",
flags: []string{"--local", "--legacy-insecure", "--group-id", "1"},
imagePath: filepath.Join("..", "test", "images", "one-group-signed-legacy-group.sif"),
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with PGP key material"),
e2e.ExpectOutput(e2e.ContainMatch, "Signing entity: SingularityCE Tests <[email protected]>"),
e2e.ExpectOutput(e2e.ContainMatch, "Fingerprint: F34371D0ACD5D09EB9BD853A80600A5FA11BBD29"),
e2e.ExpectError(e2e.ContainMatch, "Verified signature(s) from image"),
},
},
{
name: "LegacyGroupIDFlagNotFound",
flags: []string{"--local", "--legacy-insecure", "--group-id", "5"},
imagePath: filepath.Join("..", "test", "images", "one-group-signed-legacy-group.sif"),
expectCode: 255,
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with PGP key material"),
e2e.ExpectError(e2e.ContainMatch, "integrity: group not found"),
},
},
{
name: "LegacyAllFlag",
flags: []string{"--local", "--legacy-insecure", "--all"},
imagePath: filepath.Join("..", "test", "images", "one-group-signed-legacy-all.sif"),
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with PGP key material"),
e2e.ExpectOutput(e2e.ContainMatch, "Signing entity: SingularityCE Tests <[email protected]>"),
e2e.ExpectOutput(e2e.ContainMatch, "Fingerprint: F34371D0ACD5D09EB9BD853A80600A5FA11BBD29"),
e2e.ExpectError(e2e.ContainMatch, "Verified signature(s) from image"),
},
},
{
name: "JSONFlag",
imagePath: filepath.Join("..", "test", "images", "one-group-signed-pgp.sif"),
flags: []string{"--local", "--json"},
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with PGP key material"),
},
},
{
name: "KeyFlag",
flags: []string{"--key", keyPath},
imagePath: filepath.Join("..", "test", "images", "one-group-signed-dsse.sif"),
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with key material from '"+keyPath+"'"),
e2e.ExpectError(e2e.ContainMatch, "Verified signature(s) from image"),
},
},
{
name: "KeyEnvVar",
envs: []string{"SINGULARITY_VERIFY_KEY=" + keyPath},
imagePath: filepath.Join("..", "test", "images", "one-group-signed-dsse.sif"),
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with key material from '"+keyPath+"'"),
e2e.ExpectError(e2e.ContainMatch, "Verified signature(s) from image"),
},
},
{
name: "CertificateFlags",
flags: []string{
"--certificate", certPath,
"--certificate-intermediates", intPath,
"--certificate-roots", rootPath,
},
imagePath: filepath.Join("..", "test", "images", "one-group-signed-dsse.sif"),
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with key material from certificate '"+certPath+"'"),
e2e.ExpectError(e2e.ContainMatch, "Verified signature(s) from image"),
},
},
{
name: "CertificateEnvVars",
envs: []string{
"SINGULARITY_VERIFY_CERTIFICATE=" + certPath,
"SINGULARITY_VERIFY_INTERMEDIATES=" + intPath,
"SINGULARITY_VERIFY_ROOTS=" + rootPath,
},
imagePath: filepath.Join("..", "test", "images", "one-group-signed-dsse.sif"),
expectOps: []e2e.SingularityCmdResultOp{
e2e.ExpectError(e2e.ContainMatch, "Verifying image with key material from certificate '"+certPath+"'"),
e2e.ExpectError(e2e.ContainMatch, "Verified signature(s) from image"),
},
},
}
for _, tt := range tests {
c.RunSingularity(t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(e2e.UserProfile),
e2e.WithEnv(tt.envs),
e2e.WithCommand("verify"),
e2e.WithArgs(append(tt.flags, tt.imagePath)...),
e2e.ExpectExit(tt.expectCode, tt.expectOps...),
)
}
}
func (c *ctx) importPGPKeypairs(t *testing.T) {
c.RunSingularity(
t,
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("key import"),
e2e.WithArgs(filepath.Join("..", "test", "keys", "pgp-public.asc")),
e2e.ExpectExit(0),
)
}
// E2ETests is the main func to trigger the test suite
func E2ETests(env e2e.TestEnv) testhelper.Tests {
c := ctx{
TestEnv: env,
}
return testhelper.Tests{
"ordered": func(t *testing.T) {
var err error
// Create a temporary PGP keyring.
c.KeyringDir, err = os.MkdirTemp("", "e2e-sign-keyring-")
if err != nil {
t.Fatalf("failed to create temporary directory: %s", err)
}
defer func() {
err := os.RemoveAll(c.KeyringDir)
if err != nil {
t.Fatalf("failed to delete temporary directory: %s", err)
}
}()
c.importPGPKeypairs(t)
t.Run("Verify", c.verify)
},
}
}