-
Notifications
You must be signed in to change notification settings - Fork 124
/
verify.go
217 lines (184 loc) · 6.02 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
// 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-2020, 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 (
"fmt"
"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/spf13/cobra"
)
var (
sifGroupID uint32 // -g groupid specification
sifDescID uint32 // -i id specification
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'",
}
// -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(&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
// Set keyserver option, if applicable.
if !localVerify {
co, err := getKeyserverClientOpts(keyServerURI, endpoint.KeyserverVerifyOp)
if err != nil {
sylog.Fatalf("Error while getting keyserver client config: %v", err)
}
opts = append(opts, apptainer.OptVerifyUseKeyServer(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: %s", verifyErr)
}
} else {
opts = append(opts, apptainer.OptVerifyCallback(outputVerify))
fmt.Printf("Verifying image: %s\n", cpath)
if err := apptainer.Verify(cmd.Context(), cpath, opts...); err != nil {
sylog.Fatalf("Failed to verify container: %s", err)
}
fmt.Printf("Container verified: %s\n", cpath)
}
}