Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work in progress - Add SLSA Provenance #3148

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ require (
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/in-toto/attestation v1.0.2
github.com/jellydator/ttlcache/v3 v3.2.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/in-toto/attestation v1.0.2 h1:ICqV41bfaDC3ixVUzAtFxFu+Dy56EPcjiIrJQe+4LVM=
github.com/in-toto/attestation v1.0.2/go.mod h1:3uRayZSKuCHDDZOxLm5UfYulqqd1L1NdzYvxX/jyZEM=
github.com/jellydator/ttlcache/v3 v3.2.0 h1:6lqVJ8X3ZaUwvzENqPAobDsXNExfUJd61u++uW8a3LE=
github.com/jellydator/ttlcache/v3 v3.2.0/go.mod h1:hi7MGFdMAwZna5n2tuvh63DvFLzVKySzCVW6+0gA2n4=
github.com/jmhodges/clock v1.2.0 h1:eq4kys+NI0PLngzaHEe7AmPT90XMGIEySD1JfV1PDIs=
Expand Down
1 change: 1 addition & 0 deletions src/BUILD.plz
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_binary(
deps = [
"///third_party/go/github.com_thought-machine_go-flags//:go-flags",
"///third_party/go/go.uber.org_automaxprocs//maxprocs",
"//src/attestor",
"//src/assets",
"//src/build",
"//src/cache",
Expand Down
9 changes: 9 additions & 0 deletions src/attestor/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
go_library(
name = "attestor",
srcs = ["attestor.go"],
visibility = ["PUBLIC"],
deps = [
"//src/cli",
"//src/core",
],
)
118 changes: 118 additions & 0 deletions src/attestor/attestor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package attestor

import (
"encoding/hex"
"encoding/json"

prov "github.com/in-toto/attestation/go/predicates/provenance/v1"
attestation "github.com/in-toto/attestation/go/v1"
// v1 "github.com/in-toto/attestation/go/v1"
"google.golang.org/protobuf/types/known/structpb"

"github.com/thought-machine/please/src/cli"
"github.com/thought-machine/please/src/core"
)

const (
Name = "please-build"
Type = "https://slsa.dev/provenance/v1.0"
BuildType = "https://please.build/buildtypes/[email protected]"
DefaultBuilderId = "https://please.build/[email protected]"

Check warning on line 20 in src/attestor/attestor.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: const DefaultBuilderId should be DefaultBuilderID (revive)
)


Check failure on line 23 in src/attestor/attestor.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/thought-machine/please) (gci)

type Provenance struct {
PbProvenance prov.Provenance
subjects []*attestation.ResourceDescriptor

Check failure on line 27 in src/attestor/attestor.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/thought-machine/please) (gci)
}

func New() *Provenance {
return &Provenance{}
}

func (p *Provenance) Name() string {
return Name
}

func (p *Provenance) Type() string {
return Type
}

func (p *Provenance) Attest(targets, preTargets []core.BuildLabel, state *core.BuildState, config *core.Configuration, arch cli.Arch) error {
builder := prov.Builder{}
metadata := prov.BuildMetadata{}
p.PbProvenance.BuildDefinition = &prov.BuildDefinition{}
p.PbProvenance.RunDetails = &prov.RunDetails{Builder: &builder, Metadata: &metadata}

p.PbProvenance.BuildDefinition.BuildType = BuildType
p.PbProvenance.RunDetails.Builder.Id = DefaultBuilderId

// Internal Parameters
internalParam := make(map[string]interface{})
internalParam["version"] = config.Please.Version.VersionString()

var err error
p.PbProvenance.BuildDefinition.InternalParameters, err = structpb.NewStruct(internalParam)
if err != nil {
return err
}

// External Parameters
externalParam := make(map[string]interface{})

Check failure on line 63 in src/attestor/attestor.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/thought-machine/please) (gci)
targetNames := make([]interface{}, 0)
for _, v := range targets {
targetNames = append(targetNames, v.String())
}
externalParam["targets"] = targetNames

p.PbProvenance.BuildDefinition.ExternalParameters, err = structpb.NewStruct(externalParam)
if err != nil {
return err
}

// Resolved Dependencies


// Run Details


// Subjects
p.subjects, err = p.Subjects(targets, state)
if err != nil {
return err
}

return nil
}

func (p *Provenance) MarshalJSON() ([]byte, error) {
return json.Marshal(&p.PbProvenance)
}

func (p *Provenance) Subjects(targets []core.BuildLabel, state *core.BuildState) ([]*attestation.ResourceDescriptor, error) {
subjects := []*attestation.ResourceDescriptor{}

for _, label := range targets {
p := state.SyncParsePackage(label)
outputs := p.Target(label.Name).FullOutputs()

for _, outputItem := range outputs {
hash, err := state.PathHasher.Hash(outputItem, false, false, false)
if err != nil {
return nil, err
}

subject := &attestation.ResourceDescriptor{}
subject.Name = outputItem
subject.Digest = map[string]string{
state.PathHasher.AlgoName(): hex.EncodeToString(hash),
}

subjects = append(subjects, subject)
}

Check failure on line 115 in src/attestor/attestor.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}
return subjects, nil
}
20 changes: 20 additions & 0 deletions src/plz/plz.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"github.com/peterebden/go-cli-init/v5/flags"

"github.com/thought-machine/please/src/attestor"
"github.com/thought-machine/please/src/build"
"github.com/thought-machine/please/src/cli"
"github.com/thought-machine/please/src/cli/logging"
Expand Down Expand Up @@ -89,6 +90,25 @@
log.Info("Total remote RPC data in: %d out: %d", in, out)
}
state.CloseResults()

prov := attestor.New()
err := prov.Attest(targets, preTargets, state, config, arch)
if err != nil {
log.Errorf("%v", err)
}

provenanceJson, err := prov.MarshalJSON()

Check warning on line 100 in src/plz/plz.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var provenanceJson should be provenanceJSON (revive)
if err != nil {
log.Errorf("%v", err)
}

log.Infof("%s", provenanceJson)

// TODO: Provenance implementation
// - Sign provenance
// - Get config for output location
// - Write to output file

metrics.Push(config)
}

Expand Down
6 changes: 6 additions & 0 deletions third_party/go/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,9 @@ go_repo(
version = "v1.2.0",
licences = ["Apache-2.0"],
)

go_repo(
module = "github.com/in-toto/attestation",
version = "v1.0.2",
licences = ["Apache-2.0"],
)
Loading