|
| 1 | +package webhook |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/x509" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/google/go-containerregistry/pkg/name" |
| 13 | + v1 "github.com/google/go-containerregistry/pkg/v1" |
| 14 | + "github.com/google/go-containerregistry/pkg/v1/remote" |
| 15 | + |
| 16 | + "github.com/sigstore/sigstore-go/pkg/bundle" |
| 17 | + "github.com/sigstore/sigstore-go/pkg/root" |
| 18 | + "github.com/sigstore/sigstore-go/pkg/verify" |
| 19 | +) |
| 20 | + |
| 21 | +type VerifiedBundle struct { |
| 22 | + SGBundle *bundle.Bundle |
| 23 | + Result *verify.VerificationResult |
| 24 | + Hash v1.Hash |
| 25 | +} |
| 26 | + |
| 27 | +// VerifiedBundle implements Signature |
| 28 | +var _ Signature = &VerifiedBundle{} |
| 29 | + |
| 30 | +func (vb *VerifiedBundle) Digest() (v1.Hash, error) { |
| 31 | + return vb.Hash, nil |
| 32 | +} |
| 33 | + |
| 34 | +func (vb *VerifiedBundle) Payload() ([]byte, error) { |
| 35 | + // todo: this should return the json-serialized dsse envelope |
| 36 | + envelope := vb.SGBundle.GetDsseEnvelope() |
| 37 | + if envelope == nil { |
| 38 | + return nil, fmt.Errorf("no dsse envelope found") |
| 39 | + } |
| 40 | + return json.Marshal(envelope) |
| 41 | +} |
| 42 | + |
| 43 | +func (vb *VerifiedBundle) Signature() ([]byte, error) { |
| 44 | + // TODO: implement this |
| 45 | + return []byte{}, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (vb *VerifiedBundle) Cert() (*x509.Certificate, error) { |
| 49 | + vc, err := vb.SGBundle.VerificationContent() |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + cert := vc.GetCertificate() |
| 54 | + if cert != nil { |
| 55 | + return cert, nil |
| 56 | + } |
| 57 | + return nil, errors.New("bundle does not contain a certificate") |
| 58 | +} |
| 59 | + |
| 60 | +func VerifiedBundles(ref name.Reference, trustedMaterial root.TrustedMaterial, remoteOpts []remote.Option, policyOptions []verify.PolicyOption, verifierOptions []verify.VerifierOption) ([]Signature, error) { |
| 61 | + sev, err := verify.NewSignedEntityVerifier(trustedMaterial, verifierOptions...) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + bundles, hash, err := getBundles(ref, remoteOpts) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + digestBytes, err := hex.DecodeString(hash.Hex) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + artifactPolicy := verify.WithArtifactDigest(hash.Algorithm, digestBytes) |
| 76 | + policy := verify.NewPolicy(artifactPolicy, policyOptions...) |
| 77 | + |
| 78 | + verifiedBundles := make([]Signature, 0) |
| 79 | + for _, b := range bundles { |
| 80 | + // TODO: should these be done in parallel? (as is done in cosign?) |
| 81 | + result, err := sev.Verify(b, policy) |
| 82 | + if err == nil { |
| 83 | + verifiedBundles = append(verifiedBundles, &VerifiedBundle{SGBundle: b, Result: result, Hash: *hash}) |
| 84 | + } |
| 85 | + } |
| 86 | + return verifiedBundles, nil |
| 87 | +} |
| 88 | + |
| 89 | +func getBundles(ref name.Reference, remoteOpts []remote.Option) ([]*bundle.Bundle, *v1.Hash, error) { |
| 90 | + desc, err := remote.Get(ref, remoteOpts...) |
| 91 | + if err != nil { |
| 92 | + return nil, nil, fmt.Errorf("error getting image descriptor: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + digest := ref.Context().Digest(desc.Digest.String()) |
| 96 | + |
| 97 | + referrers, err := remote.Referrers(digest, remoteOpts...) |
| 98 | + if err != nil { |
| 99 | + return nil, nil, fmt.Errorf("error getting referrers: %w", err) |
| 100 | + } |
| 101 | + refManifest, err := referrers.IndexManifest() |
| 102 | + if err != nil { |
| 103 | + return nil, nil, fmt.Errorf("error getting referrers manifest: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + bundles := make([]*bundle.Bundle, 0) |
| 107 | + |
| 108 | + for _, refDesc := range refManifest.Manifests { |
| 109 | + if !strings.HasPrefix(refDesc.ArtifactType, "application/vnd.dev.sigstore.bundle") { |
| 110 | + continue |
| 111 | + } |
| 112 | + |
| 113 | + refImg, err := remote.Image(ref.Context().Digest(refDesc.Digest.String()), remoteOpts...) |
| 114 | + if err != nil { |
| 115 | + return nil, nil, fmt.Errorf("error getting referrer image: %w", err) |
| 116 | + } |
| 117 | + layers, err := refImg.Layers() |
| 118 | + if err != nil { |
| 119 | + return nil, nil, fmt.Errorf("error getting referrer image: %w", err) |
| 120 | + } |
| 121 | + layer0, err := layers[0].Uncompressed() |
| 122 | + if err != nil { |
| 123 | + return nil, nil, fmt.Errorf("error getting referrer image: %w", err) |
| 124 | + } |
| 125 | + bundleBytes, err := io.ReadAll(layer0) |
| 126 | + if err != nil { |
| 127 | + return nil, nil, fmt.Errorf("error getting referrer image: %w", err) |
| 128 | + } |
| 129 | + b := &bundle.Bundle{} |
| 130 | + err = b.UnmarshalJSON(bundleBytes) |
| 131 | + if err != nil { |
| 132 | + return nil, nil, fmt.Errorf("error unmarshalling bundle: %w", err) |
| 133 | + } |
| 134 | + bundles = append(bundles, b) |
| 135 | + } |
| 136 | + if len(bundles) == 0 { |
| 137 | + return nil, nil, fmt.Errorf("no bundle found in referrers") |
| 138 | + } |
| 139 | + return bundles, &desc.Digest, nil |
| 140 | +} |
0 commit comments