-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split files and release asset cli commands
Signed-off-by: Marco Franssen <marco.franssen@philips.com>
- Loading branch information
1 parent
3125396
commit b8149b4
Showing
6 changed files
with
503 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/peterbourgon/ff/v3/ffcli" | ||
|
||
"github.com/philips-labs/slsa-provenance-action/lib/github" | ||
"github.com/philips-labs/slsa-provenance-action/lib/intoto" | ||
) | ||
|
||
// Files creates an instance of *ffcli.Command to manage file provenance | ||
func Files(w io.Writer) *ffcli.Command { | ||
var ( | ||
flagset = flag.NewFlagSet("slsa-provenance generate files", flag.ExitOnError) | ||
artifactPath = flagset.String("artifact_path", "", "The file or dir path of the artifacts for which provenance should be generated.") | ||
outputPath = flagset.String("output_path", "build.provenance", "The path to which the generated provenance should be written.") | ||
githubContext = flagset.String("github_context", "", "The '${github}' context value.") | ||
runnerContext = flagset.String("runner_context", "", "The '${runner}' context value.") | ||
extraMaterials = []string{} | ||
) | ||
flagset.Func("extra_materials", "paths to files containing SLSA v0.1 formatted materials (JSON array) in to include in the provenance", func(s string) error { | ||
extraMaterials = append(extraMaterials, strings.Fields(s)...) | ||
return nil | ||
}) | ||
|
||
flagset.SetOutput(w) | ||
|
||
return &ffcli.Command{ | ||
Name: "files", | ||
ShortUsage: "slsa-provenance generate files", | ||
ShortHelp: "Generates slsa provenance for file(s)", | ||
FlagSet: flagset, | ||
Exec: func(ctx context.Context, args []string) error { | ||
if *artifactPath == "" { | ||
flagset.Usage() | ||
return RequiredFlagError("-artifact_path") | ||
} | ||
if *outputPath == "" { | ||
flagset.Usage() | ||
return RequiredFlagError("-output_path") | ||
} | ||
if *githubContext == "" { | ||
flagset.Usage() | ||
return RequiredFlagError("-github_context") | ||
} | ||
if *runnerContext == "" { | ||
flagset.Usage() | ||
return RequiredFlagError("-runner_context") | ||
} | ||
|
||
var gh github.Context | ||
if err := json.Unmarshal([]byte(*githubContext), &gh); err != nil { | ||
return fmt.Errorf("failed to unmarshal github context json: %w", err) | ||
} | ||
|
||
var runner github.RunnerContext | ||
if err := json.Unmarshal([]byte(*runnerContext), &runner); err != nil { | ||
return fmt.Errorf("failed to unmarshal runner context json: %w", err) | ||
} | ||
|
||
env := &github.Environment{ | ||
Context: &gh, | ||
Runner: &runner, | ||
} | ||
|
||
stmt, err := env.GenerateProvenanceStatement(ctx, *artifactPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to generate provenance: %w", err) | ||
} | ||
|
||
for _, extra := range extraMaterials { | ||
content, err := ioutil.ReadFile(extra) | ||
if err != nil { | ||
return fmt.Errorf("could not load extra materials from %s: %w", extra, err) | ||
} | ||
var materials []intoto.Item | ||
if err = json.Unmarshal(content, &materials); err != nil { | ||
return fmt.Errorf("invalid JSON in extra materials file %s: %w", extra, err) | ||
} | ||
for _, material := range materials { | ||
if material.URI == "" { | ||
return fmt.Errorf("empty or missing \"uri\" field in %s", extra) | ||
} | ||
if len(material.Digest) == 0 { | ||
return fmt.Errorf("empty or missing \"digest\" in %s", extra) | ||
} | ||
} | ||
stmt.Predicate.Materials = append(stmt.Predicate.Materials, materials...) | ||
} | ||
|
||
fmt.Fprintf(w, "Saving provenance to %s\n", *outputPath) | ||
|
||
return env.PersistProvenanceStatement(ctx, stmt, *outputPath) | ||
}, | ||
} | ||
} |
Oops, something went wrong.