-
Notifications
You must be signed in to change notification settings - Fork 132
/
verify.go
320 lines (276 loc) · 9.59 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2017-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 cli
import (
"crypto"
"os"
"github.com/apptainer/apptainer/docs"
"github.com/apptainer/apptainer/internal/app/apptainer"
"github.com/apptainer/apptainer/internal/pkg/remote/endpoint"
"github.com/apptainer/apptainer/pkg/cmdline"
"github.com/apptainer/apptainer/pkg/sylog"
"github.com/sigstore/sigstore/pkg/signature"
"github.com/spf13/cobra"
)
var (
sifGroupID uint32 // -g groupid specification
sifDescID uint32 // -i id specification
certificatePath string // --certificate flag
certificateIntermediatesPath string // --certificate-intermediates flag
certificateRootsPath string // --certificate-roots flag
ocspVerify bool // --ocsp-verify flag
pubKeyPath string // --key flag
localVerify bool // -l flag
jsonVerify bool // -j flag
verifyAll bool
verifyLegacy bool
)
// -u|--url
var verifyServerURIFlag = cmdline.Flag{
ID: "verifyServerURIFlag",
Value: &keyServerURI,
DefaultValue: "",
Name: "url",
ShortHand: "u",
Usage: "specify a URL for a key server",
EnvKeys: []string{"URL"},
}
// -g|--group-id
var verifySifGroupIDFlag = cmdline.Flag{
ID: "verifySifGroupIDFlag",
Value: &sifGroupID,
DefaultValue: uint32(0),
Name: "group-id",
ShortHand: "g",
Usage: "verify objects with the specified group ID",
}
// --groupid (deprecated)
var verifyOldSifGroupIDFlag = cmdline.Flag{
ID: "verifyOldSifGroupIDFlag",
Value: &sifGroupID,
DefaultValue: uint32(0),
Name: "groupid",
Usage: "verify objects with the specified group ID",
Deprecated: "use '--group-id'",
}
// -i|--sif-id
var verifySifDescSifIDFlag = cmdline.Flag{
ID: "verifySifDescSifIDFlag",
Value: &sifDescID,
DefaultValue: uint32(0),
Name: "sif-id",
ShortHand: "i",
Usage: "verify object with the specified ID",
}
// --id (deprecated)
var verifySifDescIDFlag = cmdline.Flag{
ID: "verifySifDescIDFlag",
Value: &sifDescID,
DefaultValue: uint32(0),
Name: "id",
Usage: "verify object with the specified ID",
Deprecated: "use '--sif-id'",
}
// --certificate
var verifyCertificateFlag = cmdline.Flag{
ID: "certificateFlag",
Value: &certificatePath,
DefaultValue: "",
Name: "certificate",
Usage: "path to the certificate",
EnvKeys: []string{"VERIFY_CERTIFICATE"},
}
// --certificate-intermediates
var verifyCertificateIntermediatesFlag = cmdline.Flag{
ID: "certificateIntermediatesFlag",
Value: &certificateIntermediatesPath,
DefaultValue: "",
Name: "certificate-intermediates",
Usage: "path to pool of intermediate certificates",
EnvKeys: []string{"VERIFY_INTERMEDIATES"},
}
// --certificate-roots
var verifyCertificateRootsFlag = cmdline.Flag{
ID: "certificateRootsFlag",
Value: &certificateRootsPath,
DefaultValue: "",
Name: "certificate-roots",
Usage: "path to pool of root certificates",
EnvKeys: []string{"VERIFY_ROOTS"},
}
// --ocsp-verify
var verifyOCSPFlag = cmdline.Flag{
ID: "ocspVerifyFlag",
Value: &ocspVerify,
DefaultValue: false,
Name: "ocsp-verify",
Usage: "enable online revocation check for certificates",
EnvKeys: []string{"VERIFY_OCSP"},
}
// --key
var verifyPublicKeyFlag = cmdline.Flag{
ID: "publicKeyFlag",
Value: &pubKeyPath,
DefaultValue: "",
Name: "key",
Usage: "path to the public key file",
EnvKeys: []string{"VERIFY_KEY"},
}
// -l|--local
var verifyLocalFlag = cmdline.Flag{
ID: "verifyLocalFlag",
Value: &localVerify,
DefaultValue: false,
Name: "local",
ShortHand: "l",
Usage: "only verify with local key(s) in keyring",
EnvKeys: []string{"LOCAL_VERIFY"},
}
// -j|--json
var verifyJSONFlag = cmdline.Flag{
ID: "verifyJsonFlag",
Value: &jsonVerify,
DefaultValue: false,
Name: "json",
ShortHand: "j",
Usage: "output json",
}
// -a|--all
var verifyAllFlag = cmdline.Flag{
ID: "verifyAllFlag",
Value: &verifyAll,
DefaultValue: false,
Name: "all",
ShortHand: "a",
Usage: "verify all objects",
}
// --legacy-insecure
var verifyLegacyFlag = cmdline.Flag{
ID: "verifyLegacyFlag",
Value: &verifyLegacy,
DefaultValue: false,
Name: "legacy-insecure",
Usage: "enable verification of (insecure) legacy signatures",
}
func init() {
addCmdInit(func(cmdManager *cmdline.CommandManager) {
cmdManager.RegisterCmd(VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyServerURIFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifySifGroupIDFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyOldSifGroupIDFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifySifDescSifIDFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifySifDescIDFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyCertificateFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyCertificateIntermediatesFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyCertificateRootsFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyOCSPFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyPublicKeyFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyLocalFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyJSONFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyAllFlag, VerifyCmd)
cmdManager.RegisterFlagForCmd(&verifyLegacyFlag, VerifyCmd)
})
}
// VerifyCmd apptainer verify
var VerifyCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// args[0] contains image path
doVerifyCmd(cmd, args[0])
},
Use: docs.VerifyUse,
Short: docs.VerifyShort,
Long: docs.VerifyLong,
Example: docs.VerifyExample,
}
func doVerifyCmd(cmd *cobra.Command, cpath string) {
var opts []apptainer.VerifyOpt
switch {
case cmd.Flag(verifyCertificateFlag.Name).Changed:
sylog.Infof("Verifying image with key material from certificate '%v'", certificatePath)
c, err := loadCertificate(certificatePath)
if err != nil {
sylog.Fatalf("Failed to load certificate: %v", err)
}
opts = append(opts, apptainer.OptVerifyWithCertificate(c))
if cmd.Flag(verifyCertificateIntermediatesFlag.Name).Changed {
p, err := loadCertificatePool(certificateIntermediatesPath)
if err != nil {
sylog.Fatalf("Failed to load intermediate certificates: %v", err)
}
opts = append(opts, apptainer.OptVerifyWithIntermediates(p))
}
if cmd.Flag(verifyCertificateRootsFlag.Name).Changed {
p, err := loadCertificatePool(certificateRootsPath)
if err != nil {
sylog.Fatalf("Failed to load root certificates: %v", err)
}
opts = append(opts, apptainer.OptVerifyWithRoots(p))
}
if cmd.Flag(verifyOCSPFlag.Name).Changed {
opts = append(opts, apptainer.OptVerifyWithOCSP())
}
case cmd.Flag(verifyPublicKeyFlag.Name).Changed:
sylog.Infof("Verifying image with key material from '%v'", pubKeyPath)
v, err := signature.LoadVerifierFromPEMFile(pubKeyPath, crypto.SHA256)
if err != nil {
sylog.Fatalf("Failed to load key material: %v", err)
}
opts = append(opts, apptainer.OptVerifyWithVerifier(v))
default:
sylog.Infof("Verifying image with PGP key material")
// Set keyserver option, if applicable.
if localVerify {
opts = append(opts, apptainer.OptVerifyWithPGP())
} else {
co, err := getKeyserverClientOpts(keyServerURI, endpoint.KeyserverVerifyOp)
if err != nil {
sylog.Fatalf("Error while getting keyserver client config: %v", err)
}
opts = append(opts, apptainer.OptVerifyWithPGP(co...))
}
}
// Set group option, if applicable.
if cmd.Flag(verifySifGroupIDFlag.Name).Changed || cmd.Flag(verifyOldSifGroupIDFlag.Name).Changed {
opts = append(opts, apptainer.OptVerifyGroup(sifGroupID))
}
// Set object option, if applicable.
if cmd.Flag(verifySifDescSifIDFlag.Name).Changed || cmd.Flag(verifySifDescIDFlag.Name).Changed {
opts = append(opts, apptainer.OptVerifyObject(sifDescID))
}
// Set all option, if applicable.
if verifyAll {
opts = append(opts, apptainer.OptVerifyAll())
}
// Set legacy option, if applicable.
if verifyLegacy {
opts = append(opts, apptainer.OptVerifyLegacy())
}
// Set callback option.
if jsonVerify {
var kl keyList
opts = append(opts, apptainer.OptVerifyCallback(getJSONCallback(&kl)))
verifyErr := apptainer.Verify(cmd.Context(), cpath, opts...)
// Always output JSON.
if err := outputJSON(os.Stdout, kl); err != nil {
sylog.Fatalf("Failed to output JSON: %v", err)
}
if verifyErr != nil {
sylog.Fatalf("Failed to verify container: %v", verifyErr)
}
} else {
opts = append(opts, apptainer.OptVerifyCallback(outputVerify))
if err := apptainer.Verify(cmd.Context(), cpath, opts...); err != nil {
sylog.Fatalf("Failed to verify container: %v", err)
}
sylog.Infof("Verified signature(s) from image '%v'", cpath)
}
}