/
main.go
90 lines (78 loc) · 1.9 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
package main
import (
"context"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/asraa/slsa-on-github/pkg/provenance"
"github.com/google/go-github/v40/github"
"golang.org/x/oauth2"
)
func main() {
// get org and repo flags
repoStr := flag.String("repository", "", "owner and repository to fetch build logs, e.g. ossf/scorecard")
digestStr := flag.String("digest", "", "sha256 digest of the input binary being produced")
flag.Parse()
if *repoStr == "" {
flag.Usage()
return
}
if *digestStr == "" {
flag.Usage()
return
}
// Check for GITHUB env variables
ghRunIdStr, ok := os.LookupEnv("GITHUB_RUN_ID")
if !ok {
fmt.Fprintln(os.Stderr, "Environment variable GITHUB_RUN_ID not present")
os.Exit(1)
}
ghRunId, err := strconv.ParseInt(ghRunIdStr, 10, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid github run ID string: %v", err)
os.Exit(1)
}
digest := *digestStr
if _, err := hex.DecodeString(digest); err != nil && len(digest) != 64 {
fmt.Fprintln(os.Stderr, "sha256 digest is not valid")
os.Exit(1)
}
// split string
z := strings.SplitN(*repoStr, "/", 2)
if z == nil || len(z) != 2 {
flag.Usage()
return
}
org := z[0]
repo := z[1]
// make github client
token, ok := os.LookupEnv("GITHUB_TOKEN")
if !ok {
fmt.Printf("%s", "missing GITHUB_TOKEN")
}
ctx := context.Background()
// Requires a token with repo scope
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
workflow, run, job, err := provenance.GetCurrentWorkflowRunAndBuildJob(ctx, client, org, repo, ghRunId)
if err != nil {
log.Fatal(err)
}
att, err := provenance.GenerateAttestation(workflow, run, job, digest)
if err != nil {
log.Fatal(err)
}
attBytes, err := json.MarshalIndent(att, "", "\t")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(attBytes))
}