Skip to content

Commit

Permalink
Refactor additional materials to the environment
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Franssen <marco.franssen@philips.com>
  • Loading branch information
marcofranssen committed Nov 30, 2021
1 parent dca7236 commit 0d8b360
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 1 addition & 3 deletions cmd/slsa-provenance/cli/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ func Files() *cobra.Command {
Runner: runner,
}

stmt, err := env.GenerateProvenanceStatement(cmd.Context(), artifactPath)
stmt, err := env.GenerateProvenanceStatement(cmd.Context(), artifactPath, materials...)
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)
Expand Down
10 changes: 6 additions & 4 deletions lib/github/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// GenerateProvenanceStatement generates provenance from the provided artifactPath
//
// The artifactPath can be a file or a directory.
func (e *Environment) GenerateProvenanceStatement(ctx context.Context, artifactPath string) (*intoto.Statement, error) {
func (e *Environment) GenerateProvenanceStatement(ctx context.Context, artifactPath string, materials ...intoto.Item) (*intoto.Statement, error) {
subjects, err := intoto.Subjects(artifactPath)
if os.IsNotExist(err) {
return nil, fmt.Errorf("resource path not found: [provided=%s]", artifactPath)
Expand Down Expand Up @@ -45,7 +45,9 @@ func (e *Environment) GenerateProvenanceStatement(ctx context.Context, artifactP
[]intoto.Item{
{URI: "git+" + repoURI, Digest: intoto.DigestSet{"sha1": e.Context.SHA}},
},
))
),
intoto.WithMaterials(materials),
)

return stmt, nil
}
Expand Down Expand Up @@ -92,7 +94,7 @@ func NewReleaseEnvironment(gh Context, runner RunnerContext, tagName string, rc
// Release assets will be downloaded to the given artifactPath
//
// The artifactPath has to be a directory.
func (e *ReleaseEnvironment) GenerateProvenanceStatement(ctx context.Context, artifactPath string) (*intoto.Statement, error) {
func (e *ReleaseEnvironment) GenerateProvenanceStatement(ctx context.Context, artifactPath string, materials ...intoto.Item) (*intoto.Statement, error) {
err := os.MkdirAll(artifactPath, 0755)
if err != nil {
return nil, err
Expand All @@ -117,7 +119,7 @@ func (e *ReleaseEnvironment) GenerateProvenanceStatement(ctx context.Context, ar
return nil, err
}

return e.Environment.GenerateProvenanceStatement(ctx, artifactPath)
return e.Environment.GenerateProvenanceStatement(ctx, artifactPath, materials...)
}

// PersistProvenanceStatement writes the provenance statement at the given path and uploads it to the GitHub release
Expand Down
7 changes: 7 additions & 0 deletions lib/intoto/intoto.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func WithInvocation(buildType, entryPoint string, environment json.RawMessage, p
}
}

// WithMaterials adds additional materials to the predicate
func WithMaterials(materials []Item) StatementOption {
return func(s *Statement) {
s.Predicate.Materials = append(s.Predicate.Materials, materials...)
}
}

// Statement The Statement is the middle layer of the attestation, binding it to a particular subject and unambiguously identifying the types of the predicate.
type Statement struct {
Type string `json:"_type"`
Expand Down

0 comments on commit 0d8b360

Please sign in to comment.