-
Notifications
You must be signed in to change notification settings - Fork 500
/
verify.go
282 lines (222 loc) · 10.8 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
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cli
import (
"fmt"
"github.com/spf13/cobra"
"github.com/sigstore/cosign/cmd/cosign/cli/options"
"github.com/sigstore/cosign/cmd/cosign/cli/verify"
)
func Verify() *cobra.Command {
o := &options.VerifyOptions{}
cmd := &cobra.Command{
Use: "verify",
Short: "Verify a signature on the supplied container image",
Long: `Verify signature and annotations on an image by checking the claims
against the transparency log.`,
Example: ` cosign verify --key <key path>|<key url>|<kms uri> <image uri> [<image uri> ...]
# verify cosign claims and signing certificates on the image
cosign verify <IMAGE>
# verify multiple images
cosign verify <IMAGE_1> <IMAGE_2> ...
# additionally verify specified annotations
cosign verify -a key1=val1 -a key2=val2 <IMAGE>
# (experimental) additionally, verify with the transparency log
COSIGN_EXPERIMENTAL=1 cosign verify <IMAGE>
# verify image with an on-disk public key
cosign verify --key cosign.pub <IMAGE>
# verify image with an on-disk public key, manually specifying the
# signature digest algorithm
cosign verify --key cosign.pub --signature-digest-algorithm sha512 <IMAGE>
# verify image with an on-disk signed image from 'cosign save'
cosign verify --key cosign.pub --local-image <PATH>
# verify image with local certificate and certificate chain
cosign verify --cert cosign.crt --cert-chain chain.crt <IMAGE>
# verify image with public key provided by URL
cosign verify --key https://host.for/[FILE] <IMAGE>
# verify image with a key stored in an environment variable
cosign verify --key env://[ENV_VAR] <IMAGE>
# verify image with public key stored in Google Cloud KMS
cosign verify --key gcpkms://projects/[PROJECT]/locations/global/keyRings/[KEYRING]/cryptoKeys/[KEY] <IMAGE>
# verify image with public key stored in Hashicorp Vault
cosign verify --key hashivault://[KEY] <IMAGE>
# verify image with public key stored in a Kubernetes secret
cosign verify --key k8s://[NAMESPACE]/[KEY] <IMAGE>
# verify image with public key stored in GitLab with project name
cosign verify --key gitlab://[OWNER]/[PROJECT_NAME] <IMAGE>
# verify image with public key stored in GitLab with project id
cosign verify --key gitlab://[PROJECT_ID] <IMAGE>`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
annotations, err := o.AnnotationsMap()
if err != nil {
return err
}
hashAlgorithm, err := o.SignatureDigest.HashAlgorithm()
if err != nil {
return err
}
v := verify.VerifyCommand{
RegistryOptions: o.Registry,
CheckClaims: o.CheckClaims,
KeyRef: o.Key,
CertRef: o.CertVerify.Cert,
CertEmail: o.CertVerify.CertEmail,
CertOidcIssuer: o.CertVerify.CertOidcIssuer,
CertGithubWorkflowTrigger: o.CertVerify.CertGithubWorkflowTrigger,
CertGithubWorkflowSha: o.CertVerify.CertGithubWorkflowSha,
CertGithubWorkflowName: o.CertVerify.CertGithubWorkflowName,
CertGithubWorkflowRepository: o.CertVerify.CertGithubWorkflowRepository,
CertGithubWorkflowRef: o.CertVerify.CertGithubWorkflowRef,
CertChain: o.CertVerify.CertChain,
EnforceSCT: o.CertVerify.EnforceSCT,
Sk: o.SecurityKey.Use,
Slot: o.SecurityKey.Slot,
Output: o.Output,
RekorURL: o.Rekor.URL,
Attachment: o.Attachment,
Annotations: annotations,
HashAlgorithm: hashAlgorithm,
SignatureRef: o.SignatureRef,
LocalImage: o.LocalImage,
}
return v.Exec(cmd.Context(), args)
},
}
o.AddFlags(cmd)
return cmd
}
func VerifyAttestation() *cobra.Command {
o := &options.VerifyAttestationOptions{}
cmd := &cobra.Command{
Use: "verify-attestation",
Short: "Verify an attestation on the supplied container image",
Long: `Verify an attestation on an image by checking the claims
against the transparency log.`,
Example: ` cosign verify-attestation --key <key path>|<key url>|<kms uri> <image uri> [<image uri> ...]
# verify cosign attestations on the image
cosign verify-attestation <IMAGE>
# verify multiple images
cosign verify-attestation <IMAGE_1> <IMAGE_2> ...
# additionally verify specified annotations
cosign verify-attestation -a key1=val1 -a key2=val2 <IMAGE>
# (experimental) additionally, verify with the transparency log
COSIGN_EXPERIMENTAL=1 cosign verify-attestation <IMAGE>
# verify image with public key
cosign verify-attestation --key cosign.pub <IMAGE>
# verify image attestations with an on-disk signed image from 'cosign save'
cosign verify-attestation --key cosign.pub --local-image <PATH>
# verify image with public key provided by URL
cosign verify-attestation --key https://host.for/<FILE> <IMAGE>
# verify image with public key stored in Google Cloud KMS
cosign verify-attestation --key gcpkms://projects/<PROJECT>/locations/global/keyRings/<KEYRING>/cryptoKeys/<KEY> <IMAGE>
# verify image with public key stored in Hashicorp Vault
cosign verify-attestation --key hashivault:///<KEY> <IMAGE>
# verify image with public key stored in GitLab with project name
cosign verify-attestation --key gitlab://[OWNER]/[PROJECT_NAME] <IMAGE>
# verify image with public key stored in GitLab with project id
cosign verify-attestation --key gitlab://[PROJECT_ID] <IMAGE>
# verify image with public key and validate attestation based on Rego policy
cosign verify-attestation --key cosign.pub --type <PREDICATE_TYPE> --policy <REGO_POLICY> <IMAGE>
# verify image with public key and validate attestation based on CUE policy
cosign verify-attestation --key cosign.pub --type <PREDICATE_TYPE> --policy <CUE_POLICY> <IMAGE>`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
v := verify.VerifyAttestationCommand{
RegistryOptions: o.Registry,
CheckClaims: o.CheckClaims,
CertRef: o.CertVerify.Cert,
CertEmail: o.CertVerify.CertEmail,
CertOidcIssuer: o.CertVerify.CertOidcIssuer,
CertChain: o.CertVerify.CertChain,
CertGithubWorkflowTrigger: o.CertVerify.CertGithubWorkflowTrigger,
CertGithubWorkflowSha: o.CertVerify.CertGithubWorkflowSha,
CertGithubWorkflowName: o.CertVerify.CertGithubWorkflowName,
CertGithubWorkflowRepository: o.CertVerify.CertGithubWorkflowRepository,
CertGithubWorkflowRef: o.CertVerify.CertGithubWorkflowRef,
EnforceSCT: o.CertVerify.EnforceSCT,
KeyRef: o.Key,
Sk: o.SecurityKey.Use,
Slot: o.SecurityKey.Slot,
Output: o.Output,
RekorURL: o.Rekor.URL,
PredicateType: o.Predicate.Type,
Policies: o.Policies,
LocalImage: o.LocalImage,
}
return v.Exec(cmd.Context(), args)
},
}
o.AddFlags(cmd)
return cmd
}
func VerifyBlob() *cobra.Command {
o := &options.VerifyBlobOptions{}
cmd := &cobra.Command{
Use: "verify-blob",
Short: "Verify a signature on the supplied blob",
Long: `Verify a signature on the supplied blob input using the specified key reference.
You may specify either a key, a certificate or a kms reference to verify against.
If you use a key or a certificate, you must specify the path to them on disk.
The signature may be specified as a path to a file or a base64 encoded string.
The blob may be specified as a path to a file or - for stdin.`,
Example: ` cosign verify-blob (--key <key path>|<key url>|<kms uri>)|(--certificate <cert>) --signature <sig> <blob>
# Verify a simple blob and message
cosign verify-blob --key cosign.pub --signature sig msg
# Verify a simple blob with remote signature URL, both http and https schemes are supported
cosign verify-blob --key cosign.pub --signature http://host/my.sig
# Verify a signature from an environment variable
cosign verify-blob --key cosign.pub --signature $sig msg
# verify a signature with public key provided by URL
cosign verify-blob --key https://host.for/<FILE> --signature $sig msg
# Verify a signature against a payload from another process using process redirection
cosign verify-blob --key cosign.pub --signature $sig <(git rev-parse HEAD)
# Verify a signature against Azure Key Vault
cosign verify-blob --key azurekms://[VAULT_NAME][VAULT_URI]/[KEY] --signature $sig <blob>
# Verify a signature against AWS KMS
Ccosign verify-blob --key awskms://[ENDPOINT]/[ID/ALIAS/ARN] --signature $sig <blob>
# Verify a signature against Google Cloud KMS
cosign verify-blob --key gcpkms://projects/[PROJECT ID]/locations/[LOCATION]/keyRings/[KEYRING]/cryptoKeys/[KEY] --signature $sig <blob>
# Verify a signature against Hashicorp Vault
cosign verify-blob --key hashivault://[KEY] --signature $sig <blob>
# Verify a signature against GitLab with project name
cosign verify-blob --key gitlab://[OWNER]/[PROJECT_NAME] --signature $sig <blob>
# Verify a signature against GitLab with project id
cosign verify-blob --key gitlab://[PROJECT_ID] --signature $sig <blob>
# Verify a signature against a certificate
COSIGN_EXPERIMENTAL=1 cosign verify-blob --certificate <cert> --signature $sig <blob>
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ko := options.KeyOpts{
KeyRef: o.Key,
Sk: o.SecurityKey.Use,
Slot: o.SecurityKey.Slot,
RekorURL: o.Rekor.URL,
BundlePath: o.BundlePath,
}
if err := verify.VerifyBlobCmd(cmd.Context(), ko, o.CertVerify.Cert,
o.CertVerify.CertEmail, o.CertVerify.CertOidcIssuer, o.CertVerify.CertChain,
o.Signature, args[0], o.CertVerify.CertGithubWorkflowTrigger, o.CertVerify.CertGithubWorkflowSha,
o.CertVerify.CertGithubWorkflowName, o.CertVerify.CertGithubWorkflowRepository, o.CertVerify.CertGithubWorkflowRef,
o.CertVerify.EnforceSCT); err != nil {
return fmt.Errorf("verifying blob %s: %w", args, err)
}
return nil
},
}
o.AddFlags(cmd)
return cmd
}