This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cosign.go
55 lines (44 loc) · 1.46 KB
/
cosign.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
package main
import (
"context"
"fmt"
ecrlogin "github.com/awslabs/amazon-ecr-credential-helper/ecr-login"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/api"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/sigstore/cosign/pkg/cosign"
ociremote "github.com/sigstore/cosign/pkg/oci/remote"
sigs "github.com/sigstore/cosign/pkg/signature"
"log"
)
func Verify(containerImage, keyID string) (bool, error) {
log.Printf("[INFO] Veriying Container Image: %v", containerImage)
ctx := context.TODO()
pubKey, err := sigs.LoadPublicKey(ctx, fmt.Sprintf("awskms:///%s", keyID))
if err != nil {
return false, err
}
ref, err := name.ParseReference(containerImage)
if err != nil {
return false, err
}
ecrHelper := ecrlogin.ECRHelper{ClientFactory: api.DefaultClientFactory{}}
opts := []remote.Option{
remote.WithAuthFromKeychain(authn.NewKeychainFromHelper(ecrHelper)),
remote.WithContext(ctx),
}
co := &cosign.CheckOpts{
ClaimVerifier: cosign.SimpleClaimVerifier,
RegistryClientOpts: []ociremote.Option{ociremote.WithRemoteOptions(opts...)},
SigVerifier: pubKey,
}
//Verify Image
log.Println("[INFO] COSIGN Verifying sig")
verifiedSigs, _, err := cosign.VerifyImageSignatures(ctx, ref, co)
if err != nil {
log.Printf("[ERROR] COSIGN error: %v", err)
return false, err
}
return len(verifiedSigs) > 0, err
}