-
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.
Implement first draft to generate provenance for containers
Signed-off-by: Marco Franssen <[email protected]>
- Loading branch information
1 parent
9366640
commit 0e46eaa
Showing
16 changed files
with
932 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/docker/docker/client" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/philips-labs/slsa-provenance-action/cmd/slsa-provenance/cli/options" | ||
"github.com/philips-labs/slsa-provenance-action/lib/github" | ||
"github.com/philips-labs/slsa-provenance-action/lib/oci" | ||
) | ||
|
||
// OCI creates an instance of *cobra.Command to generate oci provenance | ||
func OCI() *cobra.Command { | ||
o := &options.OCIOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "container", | ||
Short: "Generate provenance on container assets", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
outputPath, err := o.GetOutputPath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
gh, err := o.GetGitHubContext() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
runner, err := o.GetRunnerContext() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
materials, err := o.GetExtraMaterials() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
repo, err := o.GetRepository() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
digest, err := o.GetDigest() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tags, err := o.GetTags() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||
if err != nil { | ||
return err | ||
} | ||
subjecter := oci.NewContainerSubjecter(cli, repo, digest, tags...) | ||
|
||
env := &github.Environment{ | ||
Context: gh, | ||
Runner: runner, | ||
} | ||
stmt, err := env.GenerateProvenanceStatement(cmd.Context(), subjecter) | ||
if err != nil { | ||
return fmt.Errorf("failed to generate provenance: %w", err) | ||
} | ||
|
||
stmt.Predicate.Materials = append(stmt.Predicate.Materials, materials...) | ||
|
||
fmt.Fprintf(cmd.OutOrStdout(), "Saving provenance to %s\n", outputPath) | ||
|
||
return env.PersistProvenanceStatement(cmd.Context(), stmt, outputPath) | ||
}, | ||
} | ||
|
||
o.AddFlags(cmd) | ||
|
||
return cmd | ||
} |
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
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ func Generate() *cobra.Command { | |
cmd.AddCommand( | ||
Files(), | ||
GitHubRelease(), | ||
OCI(), | ||
) | ||
|
||
return cmd | ||
|
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,44 @@ | ||
package options | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
// OCIOptions Commandline flags used for the generate oci command. | ||
type OCIOptions struct { | ||
GenerateOptions | ||
Repository string | ||
Digest string | ||
Tags []string | ||
} | ||
|
||
// GetRepository The oci repository to search for the given tags. | ||
func (o *OCIOptions) GetRepository() (string, error) { | ||
if o.Repository == "" { | ||
return "", RequiredFlagError("repository") | ||
} | ||
return o.Repository, nil | ||
} | ||
|
||
// GetDigest The digest to validate the tag digests against. | ||
func (o *OCIOptions) GetDigest() (string, error) { | ||
if o.Digest == "" { | ||
return "", RequiredFlagError("repository") | ||
} | ||
return o.Digest, nil | ||
} | ||
|
||
// GetTags The tags to add as provenance subjects. | ||
func (o *OCIOptions) GetTags() ([]string, error) { | ||
if len(o.Tags) == 0 { | ||
return []string{"latest"}, nil | ||
} | ||
|
||
return o.Tags, nil | ||
} | ||
|
||
// AddFlags Registers the flags with the cobra.Command. | ||
func (o *OCIOptions) AddFlags(cmd *cobra.Command) { | ||
o.GenerateOptions.AddFlags(cmd) | ||
cmd.PersistentFlags().StringVar(&o.Repository, "repository", "", "The repository of the oci artifact.") | ||
cmd.PersistentFlags().StringVar(&o.Digest, "digest", "", "The digest for the oci artifact.") | ||
cmd.PersistentFlags().StringSliceVar(&o.Tags, "tags", nil, "The given tags for this oci release.") | ||
} |
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
Oops, something went wrong.