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

Link & SLSA attestor #149

Merged
merged 43 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
63410d4
Initial link attestor
jkjell Feb 3, 2024
7da776c
refactor: move gitoid code to cyrptoutil, use digestvalue everywhere …
mikhailswift Jan 29, 2024
924eb1f
chore: bump actions/upload-artifact from 4.2.0 to 4.3.0 (#142)
dependabot[bot] Jan 29, 2024
856b500
chore: bump github/codeql-action from 3.23.1 to 3.23.2 (#143)
dependabot[bot] Jan 30, 2024
315793e
Adding job to auto cut releases (#141)
ChaosInTheCRD Jan 30, 2024
ad61b8a
fixing error in github actions workflow (#147)
ChaosInTheCRD Feb 1, 2024
ed1dfef
RunAttestors refactor (#131)
ChaosInTheCRD Feb 2, 2024
ed519d1
Adding workaround due to failing workflows (#145)
ChaosInTheCRD Feb 3, 2024
04a8ef4
Checking policy signature against cert constraints (#144)
ChaosInTheCRD Feb 3, 2024
3cff01c
[StepSecurity] ci: Harden GitHub Actions (#148)
step-security-bot Feb 3, 2024
3d7747b
Add import for init and export variables
jkjell Feb 3, 2024
7a1a1f7
Add mulitple results to run to allow exporting attestors to indivudal…
jkjell Feb 4, 2024
fb27f55
Add collection to result array
jkjell Feb 4, 2024
af0470f
Replace export parameters in run with attestor option
jkjell Feb 5, 2024
8e2aaa4
Fix golang lint isues
jkjell Feb 5, 2024
bb8a962
Merge branch 'main' into link-attestor
jkjell Feb 5, 2024
62057c3
Update link attestor testing
jkjell Feb 12, 2024
bb035a0
Merge branch 'main' into link-attestor
jkjell Mar 22, 2024
b11d528
Add SLSA attestor
jkjell Mar 23, 2024
ae52a37
Add interface for product attestor
jkjell Mar 23, 2024
8f016d9
Add more attestor interfaces
jkjell Mar 23, 2024
885a436
Address some review feedback, licenses, and golanglint
jkjell Mar 25, 2024
0bf0842
More golangcilint errors
jkjell Mar 25, 2024
21006a2
WIP - Improve testing interfaces for exposing data fields
jkjell Apr 2, 2024
74a58da
Merge branch 'main' into link-attestor
jkjell Apr 2, 2024
420a746
Merge branch 'link-attestor' of github.com:in-toto/go-witness into li…
ChaosInTheCRD Apr 4, 2024
61e8165
added changes
ChaosInTheCRD Apr 4, 2024
b8923d2
adding changes to merge into main PR
ChaosInTheCRD Apr 4, 2024
4d86ee9
Link attestor proposed changes (#204)
ChaosInTheCRD Apr 4, 2024
1d19081
Merge branch 'link-attestor' of github.com:in-toto/go-witness into li…
ChaosInTheCRD Apr 5, 2024
690505e
Merge branch 'main' into link-attestor
jkjell Apr 6, 2024
450a306
Passing SLSA Attest tests for GitHub and GitLab
jkjell Apr 6, 2024
33f3905
Clean up
jkjell Apr 6, 2024
dba3c39
Add attestation test for link attestor
jkjell Apr 7, 2024
4e37a04
Add data function for git interface and remove unused code
jkjell Apr 10, 2024
f6b9f69
Merge branch 'link-attestor' of github.com:in-toto/go-witness into li…
ChaosInTheCRD Apr 10, 2024
bb842ee
adding warning mesage for slsa attestor
ChaosInTheCRD Apr 10, 2024
ec4f58a
Try to gracefully handle gitlab jwt
jkjell Apr 19, 2024
b2322d9
Merge branch 'main' into link-attestor
jkjell May 2, 2024
5ce8543
Merge branch 'main' into link-attestor
ChaosInTheCRD May 8, 2024
0f6805d
ran go mod tidy
ChaosInTheCRD May 8, 2024
86d4e22
ensuring link and slsa attestation exporting is optional
ChaosInTheCRD May 8, 2024
0afae1b
Merge branch 'main' into link-attestor
jkjell May 9, 2024
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
154 changes: 154 additions & 0 deletions attestation/link/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright 2024 The Witness Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package link

import (
"encoding/json"
"fmt"

v0 "github.com/in-toto/attestation/go/predicates/link/v0"
v1 "github.com/in-toto/attestation/go/v1"
"github.com/in-toto/go-witness/attestation"
"github.com/in-toto/go-witness/attestation/commandrun"
"github.com/in-toto/go-witness/attestation/environment"
"github.com/in-toto/go-witness/attestation/material"
"github.com/in-toto/go-witness/attestation/product"
"github.com/in-toto/go-witness/cryptoutil"
"github.com/in-toto/go-witness/registry"
"google.golang.org/protobuf/types/known/structpb"
)

const (
Name = "link"
Type = "https://in-toto.io/attestation/link/v0.3"
RunType = attestation.PostProductRunType

defaultExport = false
)

// This is a hacky way to create a compile time error in case the attestor
// doesn't implement the expected interfaces.
var (
_ attestation.Attestor = &Link{}
_ attestation.Subjecter = &Link{}
)

func init() {
attestation.RegisterAttestation(Name, Type, RunType,
func() attestation.Attestor { return New() },
registry.BoolConfigOption(
"export",
"Export the link attestation to its own file",
defaultExport,
func(a attestation.Attestor, export bool) (attestation.Attestor, error) {
linkAttestor, ok := a.(*Link)
if !ok {
return a, fmt.Errorf("unexpected attestor type: %T is not a link attestor", a)
}
WithExport(export)(linkAttestor)
return linkAttestor, nil
},
),
)
}

type Option func(*Link)

func WithExport(export bool) Option {
return func(l *Link) {
l.export = export
}
}

type Link struct {
PbLink v0.Link
products map[string]attestation.Product
export bool
}

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

func (l *Link) Name() string {
return Name
}

func (l *Link) Type() string {
return Type
}

func (l *Link) RunType() attestation.RunType {
return RunType
}

func (l *Link) Export() bool {
return l.export
}

func (l *Link) Attest(ctx *attestation.AttestationContext) error {
l.PbLink.Name = "stepNameHere"
for _, attestor := range ctx.CompletedAttestors() {
ChaosInTheCRD marked this conversation as resolved.
Show resolved Hide resolved
jkjell marked this conversation as resolved.
Show resolved Hide resolved
switch name := attestor.Attestor.Name(); name {
case commandrun.Name:
l.PbLink.Command = attestor.Attestor.(*commandrun.CommandRun).Cmd
case material.Name:
mats := attestor.Attestor.(*material.Attestor).Materials()
for name, digestSet := range mats {
digests, _ := digestSet.ToNameMap()
l.PbLink.Materials = append(l.PbLink.Materials, &v1.ResourceDescriptor{
Name: name,
Digest: digests,
})
}
case environment.Name:
envs := attestor.Attestor.(*environment.Attestor).Variables
pbEnvs := make(map[string]interface{}, len(envs))
for name, value := range envs {
pbEnvs[name] = value
}

var err error
l.PbLink.Environment, err = structpb.NewStruct(pbEnvs)
if err != nil {
return err
}
case product.Name:
l.products = attestor.Attestor.(*product.Attestor).Products()
}
}
return nil
}

func (l *Link) MarshalJSON() ([]byte, error) {
return json.Marshal(&l.PbLink)
}

func (l *Link) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &l.PbLink); err != nil {
return err
}

return nil
}

func (l *Link) Subjects() map[string]cryptoutil.DigestSet {
subjects := make(map[string]cryptoutil.DigestSet)
for productName, product := range l.products {
subjects[fmt.Sprintf("file:%v", productName)] = product.Digest
}

return subjects
}
15 changes: 15 additions & 0 deletions attestation/link/link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 The Witness Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package link
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/edwarnicke/gitoid v0.0.0-20220710194850-1be5bfda1f9d
github.com/go-git/go-git/v5 v5.11.0
github.com/in-toto/archivista v0.2.0
github.com/in-toto/attestation v1.0.1
github.com/mattn/go-isatty v0.0.20
github.com/open-policy-agent/opa v0.49.2
github.com/owenrumney/go-sarif v1.1.1
Expand Down Expand Up @@ -93,7 +94,7 @@ require (
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a // indirect
google.golang.org/protobuf v1.32.0 // indirect
google.golang.org/protobuf v1.32.0
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ github.com/honeycombio/libhoney-go v1.16.0 h1:kPpqoz6vbOzgp7jC6SR7SkNj7rua7rgxvz
github.com/honeycombio/libhoney-go v1.16.0/go.mod h1:izP4fbREuZ3vqC4HlCAmPrcPT9gxyxejRjGtCYpmBn0=
github.com/in-toto/archivista v0.2.0 h1:FViuHMVVETborvOqlmSYdROY8RmX3CO0V0MOhU/Rl20=
github.com/in-toto/archivista v0.2.0/go.mod h1:qt9uN4TkHWUgR5A2wxRqQIBizSl32P2nI2AjESskkr0=
github.com/in-toto/attestation v1.0.1 h1:DgX1XuBkryTpj1Piq8AiMK3CMfEcec3Qv6+Ku+uI3WY=
github.com/in-toto/attestation v1.0.1/go.mod h1:hCR5COCuENh5+VfojEkJnt7caOymbEgvyZdKifD6pOw=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
Expand Down
1 change: 1 addition & 0 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
_ "github.com/in-toto/go-witness/attestation/github"
_ "github.com/in-toto/go-witness/attestation/gitlab"
_ "github.com/in-toto/go-witness/attestation/jwt"
_ "github.com/in-toto/go-witness/attestation/link"
_ "github.com/in-toto/go-witness/attestation/maven"
_ "github.com/in-toto/go-witness/attestation/oci"
_ "github.com/in-toto/go-witness/attestation/sarif"
Expand Down
42 changes: 37 additions & 5 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/in-toto/go-witness/attestation"
"github.com/in-toto/go-witness/attestation/environment"
"github.com/in-toto/go-witness/attestation/git"
"github.com/in-toto/go-witness/attestation/link"
"github.com/in-toto/go-witness/cryptoutil"
"github.com/in-toto/go-witness/dsse"
"github.com/in-toto/go-witness/intoto"
Expand Down Expand Up @@ -60,9 +61,24 @@ func RunWithTimestampers(ts ...timestamp.Timestamper) RunOption {
type RunResult struct {
Collection attestation.Collection
SignedEnvelope dsse.Envelope
AttestorName string
}

// Should this be deprecated?
// Deprecated: Use RunWithExports instead
func Run(stepName string, signer cryptoutil.Signer, opts ...RunOption) (RunResult, error) {
results, err := run(stepName, signer, opts)
if len(results) > 1 {
return RunResult{}, errors.New("expected a single result, got multiple")
}
return results[0], err
}

func RunWithExports(stepName string, signer cryptoutil.Signer, opts ...RunOption) ([]RunResult, error) {
ChaosInTheCRD marked this conversation as resolved.
Show resolved Hide resolved
return run(stepName, signer, opts)
}

func run(stepName string, signer cryptoutil.Signer, opts []RunOption) ([]RunResult, error) {
ro := runOptions{
stepName: stepName,
signer: signer,
Expand All @@ -73,7 +89,7 @@ func Run(stepName string, signer cryptoutil.Signer, opts ...RunOption) (RunResul
opt(&ro)
}

result := RunResult{}
result := []RunResult{}
if err := validateRunOpts(ro); err != nil {
return result, err
}
Expand All @@ -91,6 +107,20 @@ func Run(stepName string, signer cryptoutil.Signer, opts ...RunOption) (RunResul
for _, r := range runCtx.CompletedAttestors() {
if r.Error != nil {
errs = append(errs, r.Error)
} else if r.Attestor.Name() == link.Name {
// TODO: Find a better way to set stepName
r.Attestor.(*link.Link).PbLink.Name = ro.stepName

// TODO: Add Exporter interface to attestors
if r.Attestor.(*link.Link).Export() {
if subjecter, ok := r.Attestor.(attestation.Subjecter); ok {
linkEnvelope, err := createAndSignEnvelope(r.Attestor, r.Attestor.Type(), subjecter.Subjects(), dsse.SignWithSigners(ro.signer), dsse.SignWithTimestampers(ro.timestampers...))
if err != nil {
return result, fmt.Errorf("failed to sign envelope: %w", err)
}
result = append(result, RunResult{SignedEnvelope: linkEnvelope, AttestorName: r.Attestor.Name()})
}
}
}
}

Expand All @@ -99,11 +129,13 @@ func Run(stepName string, signer cryptoutil.Signer, opts ...RunOption) (RunResul
return result, errors.Join(errs...)
}

result.Collection = attestation.NewCollection(ro.stepName, runCtx.CompletedAttestors())
result.SignedEnvelope, err = signCollection(result.Collection, dsse.SignWithSigners(ro.signer), dsse.SignWithTimestampers(ro.timestampers...))
var collectionResult RunResult
collectionResult.Collection = attestation.NewCollection(ro.stepName, runCtx.CompletedAttestors())
collectionResult.SignedEnvelope, err = createAndSignEnvelope(collectionResult.Collection, attestation.CollectionType, collectionResult.Collection.Subjects(), dsse.SignWithSigners(ro.signer), dsse.SignWithTimestampers(ro.timestampers...))
if err != nil {
return result, fmt.Errorf("failed to sign collection: %w", err)
}
result = append(result, collectionResult)

return result, nil
}
Expand All @@ -120,13 +152,13 @@ func validateRunOpts(ro runOptions) error {
return nil
}

func signCollection(collection attestation.Collection, opts ...dsse.SignOption) (dsse.Envelope, error) {
func createAndSignEnvelope(collection interface{}, predType string, subjects map[string]cryptoutil.DigestSet, opts ...dsse.SignOption) (dsse.Envelope, error) {
jkjell marked this conversation as resolved.
Show resolved Hide resolved
data, err := json.Marshal(&collection)
if err != nil {
return dsse.Envelope{}, err
}

stmt, err := intoto.NewStatement(attestation.CollectionType, data, collection.Subjects())
stmt, err := intoto.NewStatement(predType, data, subjects)
if err != nil {
return dsse.Envelope{}, err
}
Expand Down
Loading