forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
198 lines (165 loc) · 6.02 KB
/
main.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
// Copyright 2024 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This is a simple example of how to verify an artifact
// attestations hosted on GitHub using the sigstore-go library.
// This is a very barebones example drawn from the sigstore-go
// library's examples and should not be used in production.
package main
import (
"context"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"github.com/google/go-github/v69/github"
"github.com/sigstore/sigstore-go/pkg/bundle"
"github.com/sigstore/sigstore-go/pkg/root"
"github.com/sigstore/sigstore-go/pkg/verify"
)
var (
owner = flag.String("owner", "cli", "GitHub organization or user to scope attestation lookup by")
// You can use a utility like openssl or sha256sum to
// compute the digest.
artifactDigest = flag.String("artifact-digest", "", "The digest of the artifact")
// The algorithm used to compute the digest of the artifact.
// Note that the GitHub API only currently support querying
// by sha256 digest.
artifactDigestAlgorithm = flag.String("artifact-digest-algorithm", "sha256", "The algorithm used to compute the digest of the artifact")
// Attestations produced by GitHub Actions use ID token
// issued by GitHub.
expectedIssuer = flag.String("expected-issuer", "https://token.actions.githubusercontent.com", "Issuer of the OIDC token")
// Subject Alternative Name is set to the calling workflow file.
// This value will vary from repository to repository.
expectedSAN = flag.String("expected-san", "https://github.com/cli/cli/.github/workflows/deployment.yml@refs/heads/trunk", "The expected Subject Alternative Name (SAN) of the certificate used to sign the attestation")
// Attestations produced by GitHub Actions use the public
// good trust root maintained by Sigstore.
// A copy is included in this repo for convenience.
//
// https://github.com/sigstore/root-signing/raw/refs/heads/main/targets/trusted_root.json
trustedRootJSONPath = flag.String("trusted-root-json-path", "verifyartifact/trusted-root-public-good.json", "Path to the trusted root JSON file")
)
func usage() {
fmt.Fprintln(os.Stderr, "This is an example of how to verify the provenance of an artifact using GitHub Attestations and the sigstore-go library.")
fmt.Fprintf(os.Stderr, "\nUsage: %s [flags]\n", os.Args[0])
fmt.Fprint(os.Stderr, "\nThe flags are:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
Example:
Verifying a GitHub CLI artifact
%s -owner cli \
-artifact-digest 2ce2e480e3c3f7ca0af83418d3ebaeedacee135dbac94bd946d7d84edabcdb64 \
-expected-san https://github.com/cli/cli/.github/workflows/deployment.yml@refs/heads/trunk
See https://github.com/cli/cli/attestations/2543768 for a summary of the attestation.
`, os.Args[0])
}
func main() {
flag.Parse()
if *artifactDigest == "" {
fmt.Fprintln(os.Stderr, "artifact-digest is required.")
usage()
os.Exit(1)
}
token := os.Getenv("GITHUB_AUTH_TOKEN")
if token == "" {
log.Fatal("Unauthorized: No token present. Please set the GITHUB_AUTH_TOKEN environment variable to a valid token with `attestations:read` permission.")
}
ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
// Fetch attestations from the GitHub API.
// The attestations API doesn't differentiate between users and orgs,
// so we can use the OrganizationsService to fetch attestations for both.
attestations, _, err := client.Organizations.ListAttestations(ctx, *owner, fmt.Sprintf("%v:%v", *artifactDigestAlgorithm, *artifactDigest), nil)
if err != nil {
log.Fatal(err)
}
if len(attestations.Attestations) == 0 {
log.Fatal("No attestations found.")
}
sev, err := getSignedEntityVerifier()
if err != nil {
log.Fatal(err)
}
pb, err := getPolicyBuilder()
if err != nil {
log.Fatal(err)
}
var b *bundle.ProtobufBundle
for _, attestation := range attestations.Attestations {
if err := json.Unmarshal(attestation.Bundle, &b); err != nil {
log.Fatal(err)
}
err := runVerification(sev, pb, b)
if err != nil {
log.Fatal(err)
}
}
}
func getTrustedMaterial() (root.TrustedMaterialCollection, error) {
trustedRootJSON, err := os.ReadFile(*trustedRootJSONPath)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %w", *trustedRootJSONPath, err)
}
trustedRoot, err := root.NewTrustedRootFromJSON(trustedRootJSON)
if err != nil {
return nil, err
}
trustedMaterial := root.TrustedMaterialCollection{
trustedRoot,
}
return trustedMaterial, nil
}
func getIdentityPolicies() ([]verify.PolicyOption, error) {
certID, err := verify.NewShortCertificateIdentity(*expectedIssuer, "", *expectedSAN, "")
if err != nil {
return nil, err
}
return []verify.PolicyOption{
verify.WithCertificateIdentity(certID),
}, nil
}
func getSignedEntityVerifier() (*verify.SignedEntityVerifier, error) {
// Set up the verifier
verifierConfig := []verify.VerifierOption{
verify.WithSignedCertificateTimestamps(1),
verify.WithObserverTimestamps(1),
verify.WithTransparencyLog(1),
}
// Set up the trusted material
trustedMaterial, err := getTrustedMaterial()
if err != nil {
return nil, err
}
return verify.NewSignedEntityVerifier(trustedMaterial, verifierConfig...)
}
func getPolicyBuilder() (*verify.PolicyBuilder, error) {
// Set up the identity policy
identityPolicies, err := getIdentityPolicies()
if err != nil {
return nil, err
}
// Set up the artifact policy
artifactDigestBytes, err := hex.DecodeString(*artifactDigest)
if err != nil {
return nil, err
}
artifactPolicy := verify.WithArtifactDigest(*artifactDigestAlgorithm, artifactDigestBytes)
pb := verify.NewPolicy(artifactPolicy, identityPolicies...)
return &pb, nil
}
func runVerification(sev *verify.SignedEntityVerifier, pb *verify.PolicyBuilder, b *bundle.ProtobufBundle) error {
res, err := sev.Verify(b, *pb)
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, "Verification successful!\n")
marshaled, err := json.MarshalIndent(res, "", " ")
if err != nil {
return err
}
fmt.Println(string(marshaled))
return nil
}