|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 |
| - "crypto/sha256" |
6 |
| - "encoding/hex" |
7 |
| - "flag" |
| 4 | + "errors" |
8 | 5 | "fmt"
|
9 |
| - "io" |
10 | 6 | "os"
|
11 |
| - "strings" |
12 | 7 |
|
13 |
| - serrors "github.com/slsa-framework/slsa-verifier/errors" |
14 |
| - "github.com/slsa-framework/slsa-verifier/options" |
15 |
| - "github.com/slsa-framework/slsa-verifier/verifiers" |
16 |
| - "github.com/slsa-framework/slsa-verifier/verifiers/container" |
| 8 | + "github.com/spf13/cobra" |
17 | 9 | )
|
18 | 10 |
|
19 |
| -type workflowInputs struct { |
20 |
| - kv map[string]string |
21 |
| -} |
22 |
| - |
23 |
| -var ( |
24 |
| - provenancePath string |
25 |
| - builderID string |
26 |
| - artifactPath string |
27 |
| - artifactImage string |
28 |
| - source string |
29 |
| - branch string |
30 |
| - tag string |
31 |
| - versiontag string |
32 |
| - inputs workflowInputs |
33 |
| - printProvenance bool |
34 |
| -) |
35 |
| - |
36 |
| -func experimentalEnabled() bool { |
37 |
| - return os.Getenv("SLSA_VERIFIER_EXPERIMENTAL") == "1" |
38 |
| -} |
39 |
| - |
40 |
| -func (i *workflowInputs) String() string { |
41 |
| - return fmt.Sprintf("%v", i.kv) |
42 |
| -} |
43 |
| - |
44 |
| -func (i *workflowInputs) Set(value string) error { |
45 |
| - l := strings.Split(value, "=") |
46 |
| - if len(l) != 2 { |
47 |
| - return fmt.Errorf("%w: expected 'key=value' format, got '%s'", serrors.ErrorInvalidFormat, value) |
48 |
| - } |
49 |
| - i.kv[l[0]] = l[1] |
50 |
| - return nil |
51 |
| -} |
52 |
| - |
53 |
| -func (i *workflowInputs) AsMap() map[string]string { |
54 |
| - return i.kv |
55 |
| -} |
56 |
| - |
57 |
| -func main() { |
58 |
| - if experimentalEnabled() { |
59 |
| - flag.StringVar(&builderID, "builder-id", "", "EXPERIMENTAL: the unique builder ID who created the provenance") |
60 |
| - } |
61 |
| - flag.StringVar(&provenancePath, "provenance", "", "path to a provenance file") |
62 |
| - flag.StringVar(&artifactPath, "artifact-path", "", "path to an artifact to verify") |
63 |
| - flag.StringVar(&artifactImage, "artifact-image", "", "name of the OCI image to verify") |
64 |
| - flag.StringVar(&source, "source", "", |
65 |
| - "expected source repository that should have produced the binary, e.g. github.com/some/repo") |
66 |
| - flag.StringVar(&branch, "branch", "", "[optional] expected branch the binary was compiled from") |
67 |
| - flag.StringVar(&tag, "tag", "", "[optional] expected tag the binary was compiled from") |
68 |
| - flag.StringVar(&versiontag, "versioned-tag", "", |
69 |
| - "[optional] expected version the binary was compiled from. Uses semantic version to match the tag") |
70 |
| - flag.BoolVar(&printProvenance, "print-provenance", false, |
71 |
| - "print the verified provenance to std out") |
72 |
| - inputs.kv = make(map[string]string) |
73 |
| - flag.Var(&inputs, "workflow-input", |
74 |
| - "[optional] a workflow input provided by a user at trigger time in the format 'key=value'. (Only for 'workflow_dispatch' events).") |
75 |
| - flag.Parse() |
76 |
| - |
77 |
| - if (provenancePath == "" || artifactPath == "") && artifactImage == "" { |
78 |
| - fmt.Fprintf(os.Stderr, "either 'provenance' and 'artifact-path' or 'artifact-image' must be specified\n") |
79 |
| - flag.Usage() |
80 |
| - os.Exit(1) |
81 |
| - } |
82 |
| - |
83 |
| - if artifactImage != "" && (provenancePath != "" || artifactPath != "") { |
84 |
| - fmt.Fprintf(os.Stderr, "'provenance' and 'artifact-path' should not be specified when 'artifact-image' is provided\n") |
85 |
| - flag.Usage() |
86 |
| - os.Exit(1) |
87 |
| - } |
88 |
| - |
89 |
| - if source == "" { |
90 |
| - flag.Usage() |
91 |
| - os.Exit(1) |
92 |
| - } |
93 |
| - |
94 |
| - var pbuilderID, pbranch, ptag, pversiontag *string |
95 |
| - |
96 |
| - // Note: nil tag, version-tag and builder-id means we ignore them during verification. |
97 |
| - if isFlagPassed("tag") { |
98 |
| - ptag = &tag |
99 |
| - } |
100 |
| - if isFlagPassed("versioned-tag") { |
101 |
| - pversiontag = &versiontag |
102 |
| - } |
103 |
| - if experimentalEnabled() && isFlagPassed("builder-id") { |
104 |
| - pbuilderID = &builderID |
105 |
| - } |
106 |
| - if isFlagPassed("branch") { |
107 |
| - pbranch = &branch |
108 |
| - } |
109 |
| - |
110 |
| - if ptag != nil && pversiontag != nil { |
111 |
| - fmt.Fprintf(os.Stderr, "'version' and 'tag' options cannot be used together\n") |
112 |
| - os.Exit(1) |
113 |
| - } |
114 |
| - |
115 |
| - verifiedProvenance, _, err := runVerify(artifactImage, artifactPath, provenancePath, source, |
116 |
| - pbranch, pbuilderID, ptag, pversiontag, inputs.AsMap()) |
| 11 | +func check(err error) { |
117 | 12 | if err != nil {
|
118 |
| - fmt.Fprintf(os.Stderr, "FAILED: SLSA verification failed: %v\n", err) |
119 |
| - os.Exit(2) |
120 |
| - } |
121 |
| - |
122 |
| - fmt.Fprintf(os.Stderr, "PASSED: Verified SLSA provenance\n") |
123 |
| - if printProvenance { |
124 |
| - fmt.Fprintf(os.Stdout, "%s\n", string(verifiedProvenance)) |
| 13 | + fmt.Fprintln(os.Stderr, err) |
| 14 | + os.Exit(1) |
125 | 15 | }
|
126 | 16 | }
|
127 | 17 |
|
128 |
| -func isFlagPassed(name string) bool { |
129 |
| - found := false |
130 |
| - flag.Visit(func(f *flag.Flag) { |
131 |
| - if f.Name == name { |
132 |
| - found = true |
133 |
| - } |
134 |
| - }) |
135 |
| - return found |
| 18 | +func ExperimentalEnabled() bool { |
| 19 | + return os.Getenv("SLSA_VERIFIER_EXPERIMENTAL") == "1" |
136 | 20 | }
|
137 | 21 |
|
138 |
| -func runVerify(artifactImage, artifactPath, provenancePath, source string, |
139 |
| - branch, builderID, ptag, pversiontag *string, inputs map[string]string, |
140 |
| -) ([]byte, string, error) { |
141 |
| - ctx := context.Background() |
142 |
| - |
143 |
| - // Artifact hash retrieval depends on the artifact type. |
144 |
| - artifactHash, err := getArtifactHash(artifactImage, artifactPath) |
145 |
| - if err != nil { |
146 |
| - return nil, "", err |
147 |
| - } |
148 |
| - |
149 |
| - provenanceOpts := &options.ProvenanceOpts{ |
150 |
| - ExpectedSourceURI: source, |
151 |
| - ExpectedBranch: branch, |
152 |
| - ExpectedDigest: artifactHash, |
153 |
| - ExpectedVersionedTag: pversiontag, |
154 |
| - ExpectedTag: ptag, |
155 |
| - ExpectedWorkflowInputs: inputs, |
156 |
| - } |
157 |
| - |
158 |
| - builderOpts := &options.BuilderOpts{ |
159 |
| - ExpectedID: builderID, |
160 |
| - } |
161 |
| - |
162 |
| - var provenance []byte |
163 |
| - if provenancePath != "" { |
164 |
| - provenance, err = os.ReadFile(provenancePath) |
165 |
| - if err != nil { |
166 |
| - return nil, "", err |
167 |
| - } |
| 22 | +func rootCmd() *cobra.Command { |
| 23 | + c := &cobra.Command{ |
| 24 | + Use: "slsa-verifier", |
| 25 | + Short: "Verify SLSA provenance for Github Actions", |
| 26 | + Long: `Verify SLSA provenance for Github Actions. |
| 27 | +For more information on SLSA, visit https://slsa.dev`, |
| 28 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 29 | + return errors.New("expected command") |
| 30 | + }, |
168 | 31 | }
|
169 |
| - |
170 |
| - return verifiers.Verify(ctx, artifactImage, provenance, artifactHash, provenanceOpts, builderOpts) |
| 32 | + c.AddCommand(versionCmd()) |
| 33 | + c.AddCommand(verifyArtifactCmd()) |
| 34 | + c.AddCommand(verifyImageCmd()) |
| 35 | + // We print our own errors and usage in the check function. |
| 36 | + c.SilenceErrors = true |
| 37 | + return c |
171 | 38 | }
|
172 | 39 |
|
173 |
| -func getArtifactHash(artifactImage, artifactPath string) (string, error) { |
174 |
| - if artifactPath != "" { |
175 |
| - f, err := os.Open(artifactPath) |
176 |
| - if err != nil { |
177 |
| - return "", err |
178 |
| - } |
179 |
| - defer f.Close() |
180 |
| - h := sha256.New() |
181 |
| - if _, err := io.Copy(h, f); err != nil { |
182 |
| - return "", err |
183 |
| - } |
184 |
| - return hex.EncodeToString(h.Sum(nil)), nil |
185 |
| - } |
186 |
| - // Retrieve image digest |
187 |
| - return container.GetImageDigest(artifactImage) |
| 40 | +func main() { |
| 41 | + check(rootCmd().Execute()) |
188 | 42 | }
|
0 commit comments