Repository navigation
Expand file tree
/
Copy pathattestations.go
More file actions
102 lines (93 loc) · 4.38 KB
/
Copy pathattestations.go
File metadata and controls
102 lines (93 loc) · 4.38 KB
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
package forges
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
)
// sha256DigestLength is the number of hexadecimal characters in a SHA-256
// digest.
const sha256DigestLength = 64
// ErrInvalidDigest is returned when an artifact digest is not of the form
// "sha256:<64 hex digits>".
var ErrInvalidDigest = errors.New("artifact digest must be sha256:<64 hex digits>")
// AttestationService discovers and retrieves artifact attestations, such as
// SLSA provenance or SBOMs signed as Sigstore bundles, that a forge stores for
// an artifact digest. Fetching is kept separate from verification: callers
// verify the returned bundles themselves.
type AttestationService interface {
// List returns every attestation the repository holds for the artifact
// with the given digest, following pagination. digest has the form
// "sha256:<hex>". An empty result with a nil error means the lookup
// succeeded and the forge holds no attestations for the artifact; request
// failures, including authentication and rate limit errors, are returned
// as errors. Implementations return ErrNotFound when the repository does
// not exist and ErrNotSupported when the forge has no attestation API.
List(ctx context.Context, owner, repo, digest string, opts ListAttestationOpts) ([]Attestation, error)
// Download returns the original Sigstore bundle JSON for an attestation
// returned by List. When the listing already included the bundle it is
// returned without a request.
Download(ctx context.Context, owner, repo string, att Attestation) (json.RawMessage, error)
}
// Attestation holds normalized metadata about an artifact attestation.
type Attestation struct {
// ID identifies the attestation within the repository. GitLab sets the
// attestation IID; GitHub has no such ID and leaves it empty.
ID string `json:"id,omitempty"`
SubjectDigest string `json:"subject_digest"` // sha256:<hex>
// PredicateType is the in-toto predicate type URI. It is empty when the
// forge only reports it inside the bundle, as GitHub does.
PredicateType string `json:"predicate_type,omitempty"`
Status string `json:"status,omitempty"` // forge-native, e.g. GitLab's success or error
// BundleURL is where Download fetches the bundle from. GitHub's is a
// signed blob storage link that expires shortly after listing.
BundleURL string `json:"bundle_url,omitempty"`
Bundle json.RawMessage `json:"bundle,omitempty"` // set when the listing includes the bundle
CreatedAt time.Time `json:"created_at,omitzero"`
ExpiresAt time.Time `json:"expires_at,omitzero"`
}
// ListAttestationOpts holds options for listing attestations.
type ListAttestationOpts struct {
// PredicateType keeps only attestations of this predicate type. It
// accepts a full predicate type URI or the short names "provenance" and
// "sbom".
PredicateType string
Limit int // max total results; 0 = unlimited
}
// ParseSHA256Digest validates an artifact digest of the form "sha256:<hex>"
// and returns its hex value in lowercase. Any other form returns
// ErrInvalidDigest.
func ParseSHA256Digest(digest string) (string, error) {
hex, ok := strings.CutPrefix(digest, "sha256:")
if !ok || len(hex) != sha256DigestLength {
return "", fmt.Errorf("%w: %q", ErrInvalidDigest, digest)
}
for _, char := range hex {
if (char < '0' || char > '9') && (char < 'a' || char > 'f') && (char < 'A' || char > 'F') {
return "", fmt.Errorf("%w: %q", ErrInvalidDigest, digest)
}
}
return strings.ToLower(hex), nil
}
// ListAttestations lists the attestations for the artifact with the given
// digest in the repository at repoURL, routing to the forge for that URL the
// same way ForgeForURL does.
func (c *Client) ListAttestations(ctx context.Context, repoURL, digest string, opts ListAttestationOpts) ([]Attestation, error) {
f, owner, repo, err := c.ForgeForURL(ctx, repoURL)
if err != nil {
return nil, err
}
return f.Attestations().List(ctx, owner, repo, digest, opts)
}
// DownloadAttestation returns the Sigstore bundle for an attestation that
// ListAttestations returned for the repository at repoURL, routing the same
// way ForgeForURL does.
func (c *Client) DownloadAttestation(ctx context.Context, repoURL string, att Attestation) (json.RawMessage, error) {
f, owner, repo, err := c.ForgeForURL(ctx, repoURL)
if err != nil {
return nil, err
}
return f.Attestations().Download(ctx, owner, repo, att)
}