Skip to content

Commit 80eb6ee

Browse files
authored
Merge pull request #6 from williamlin-suse/main
NVSHAS-6216: Integrate and support sigstore cosign for image signing …
2 parents c3ce72e + 5101b98 commit 80eb6ee

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

main.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,33 @@ type SignatureData struct {
6666
func main() {
6767
config, err := loadConfiguration()
6868
if err != nil {
69-
log.Fatalf("error loading config: %s", err.Error())
69+
log.Fatalf("ERROR: error loading config: %s", err.Error())
7070
}
7171

7272
imageDigestHash, err := v1.NewHash(config.ImageDigest)
7373
if err != nil {
74-
log.Fatalf("error hashing image digest: %s", err.Error())
74+
log.Fatalf("ERROR: error hashing image digest: %s", err.Error())
7575
}
7676

7777
signatures, err := generateCosignSignatureObjects(config.SignatureData)
7878
if err != nil {
79-
log.Fatalf("error generating objects for signature data: %s", err.Error())
79+
log.Fatalf("ERROR: error generating objects for signature data: %s", err.Error())
8080
}
8181

8282
allSatisfiedVerifiers := []string{}
8383
for _, rootOfTrust := range config.RootsOfTrust {
84+
fmt.Printf("checking root of trust: %s\n", rootOfTrust.Name)
8485
satisfiedVerifiers, err := verify(imageDigestHash, rootOfTrust, signatures)
8586
if err != nil {
86-
log.Fatalf("error verifying signatures: %s", err.Error())
87+
// line with prefix "ERROR: " is recognized by scanner for error encounted when verifying against a verifier
88+
fmt.Printf("ERROR: %s\n", err.Error())
8789
} else if len(satisfiedVerifiers) > 0 {
8890
allSatisfiedVerifiers = append(allSatisfiedVerifiers, satisfiedVerifiers...)
8991
}
9092
}
9193

92-
fmt.Println("satisfied verifiers")
93-
fmt.Println(strings.Join(allSatisfiedVerifiers, ", "))
94+
// line with prefix "Satisfied verifiers: " is recognized by scanner for all the satisfied verifiers separated by ", "
95+
fmt.Printf("Satisfied verifiers: %s\n", strings.Join(allSatisfiedVerifiers, ", "))
9496
}
9597

9698
func loadConfiguration() (config Configuration, err error) {
@@ -129,24 +131,25 @@ func verify(imgDigest v1.Hash, rootOfTrust RootOfTrust, sigs []oci.Signature) (s
129131
cosignOptions := cosign.CheckOpts{ClaimVerifier: cosign.SimpleClaimVerifier}
130132
err = setRootOfTrustCosignOptions(&cosignOptions, rootOfTrust, ctx)
131133
if err != nil {
132-
return satisfiedVerifiers, fmt.Errorf("could not set root of trust cosign check options: %s", err.Error())
134+
return satisfiedVerifiers, fmt.Errorf("could not set root of trust %s cosign check options: %s", rootOfTrust.Name, err.Error())
133135
}
134136
for _, verifier := range rootOfTrust.Verifiers {
135137
fmt.Printf("checking verifier %s\n", verifier.Name)
136138
err = setVerifierCosignOptions(&cosignOptions, verifier, rootOfTrust, ctx)
137139
if err != nil {
138-
return satisfiedVerifiers, fmt.Errorf("could not set cosign options for verifier %s: %s", verifier.Name, err.Error())
139-
}
140-
for i, signature := range sigs {
141-
fmt.Printf("verifying signature %d\n", i)
142-
_, err := cosign.VerifyImageSignature(ctx, signature, imgDigest, &cosignOptions)
143-
if err != nil {
144-
fmt.Printf("signature not verified: %s\n", err.Error())
145-
}
146-
if err == nil {
147-
fmt.Printf("signature %d satisfies verifier %s\n", i, verifier.Name)
148-
satisfiedVerifiers = append(satisfiedVerifiers, fmt.Sprintf("%s/%s", rootOfTrust.Name, verifier.Name))
149-
break
140+
fmt.Printf("ERROR: %s\n", err.Error())
141+
} else {
142+
for i, signature := range sigs {
143+
fmt.Printf("verifying signature %d\n", i)
144+
_, err := cosign.VerifyImageSignature(ctx, signature, imgDigest, &cosignOptions)
145+
if err != nil {
146+
// the image is not signed by this verifier
147+
fmt.Printf("signature not verified: %s\n", err.Error())
148+
} else {
149+
fmt.Printf("signature %d satisfies verifier %s\n", i, verifier.Name)
150+
satisfiedVerifiers = append(satisfiedVerifiers, fmt.Sprintf("%s/%s", rootOfTrust.Name, verifier.Name))
151+
break
152+
}
150153
}
151154
}
152155
}
@@ -221,7 +224,7 @@ func setVerifierCosignOptions(cosignOptions *cosign.CheckOpts, verifier Verifier
221224
case "keypair":
222225
cosignOptions.SigVerifier, err = sig.LoadPublicKeyRaw([]byte(verifier.KeyPairOptions.PublicKey), crypto.SHA256)
223226
if err != nil {
224-
return fmt.Errorf("could not load verifier's pem encoded public key: %s", err.Error())
227+
return fmt.Errorf("could not load PEM encoded public key of verifier %s under %s: %s", verifier.Name, rootOfTrust.Name, err.Error())
225228
}
226229
case "keyless":
227230
cosignOptions.Identities = []cosign.Identity{
@@ -231,7 +234,8 @@ func setVerifierCosignOptions(cosignOptions *cosign.CheckOpts, verifier Verifier
231234
},
232235
}
233236
default:
234-
return fmt.Errorf("invalid verification type in config file, must be either \"keypair\" or \"keyless\", got \"%s\"", verifier.Type)
237+
// verifier.Type must be "keypair" or "keyless"
238+
return fmt.Errorf("invalid verification type in config file: %s", verifier.Type)
235239
}
236240
if !rootOfTrust.IsPublic() {
237241
if rootOfTrust.RekorPublicKey == "" {

0 commit comments

Comments
 (0)