Skip to content

Commit d0c2020

Browse files
renzodavid9chitrangpatel
authored andcommitted
Calculate subjects per formatter (tektoncd#1132)
* Calculate subjects per formatter * Tests for new retrieve full uris in grafeas
1 parent 9640b1f commit d0c2020

File tree

8 files changed

+353
-12
lines changed

8 files changed

+353
-12
lines changed

pkg/chains/formats/format.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Payloader interface {
2525
CreatePayload(ctx context.Context, obj interface{}) (interface{}, error)
2626
Type() config.PayloadType
2727
Wrap() bool
28+
RetrieveAllArtifactURIs(ctx context.Context, obj interface{}) ([]string, error)
2829
}
2930

3031
const (

pkg/chains/formats/simple/simple.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,8 @@ func (i SimpleContainerImage) ImageName() string {
6969
func (i *SimpleSigning) Type() config.PayloadType {
7070
return formats.PayloadTypeSimpleSigning
7171
}
72+
73+
// RetrieveAllArtifactURIs returns always an error, feature not available for simplesigning formatter.
74+
func (i *SimpleSigning) RetrieveAllArtifactURIs(_ context.Context, _ interface{}) ([]string, error) {
75+
return nil, fmt.Errorf("RetrieveAllArtifactURIs not supported for simeplesining formatter")
76+
}

pkg/chains/formats/slsa/v1/intotoite6.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222

2323
"github.com/tektoncd/chains/pkg/chains/formats"
24+
"github.com/tektoncd/chains/pkg/chains/formats/slsa/extract"
2425
"github.com/tektoncd/chains/pkg/chains/formats/slsa/internal/slsaconfig"
2526
"github.com/tektoncd/chains/pkg/chains/formats/slsa/v1/pipelinerun"
2627
"github.com/tektoncd/chains/pkg/chains/formats/slsa/v1/taskrun"
@@ -94,3 +95,12 @@ func (i *InTotoIte6) CreatePayload(ctx context.Context, obj interface{}) (interf
9495
func (i *InTotoIte6) Type() config.PayloadType {
9596
return formats.PayloadTypeSlsav1
9697
}
98+
99+
// RetrieveAllArtifactURIs returns the full URI of all artifacts detected as subjects.
100+
func (i *InTotoIte6) RetrieveAllArtifactURIs(ctx context.Context, obj interface{}) ([]string, error) {
101+
tkObj, ok := obj.(objects.TektonObject)
102+
if !ok {
103+
return nil, fmt.Errorf("intoto does not support type")
104+
}
105+
return extract.RetrieveAllArtifactURIs(ctx, tkObj, i.slsaConfig.DeepInspectionEnabled), nil
106+
}

pkg/chains/formats/slsa/v2alpha3/slsav2.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222

2323
"github.com/tektoncd/chains/pkg/chains/formats"
24+
"github.com/tektoncd/chains/pkg/chains/formats/slsa/extract"
2425
"github.com/tektoncd/chains/pkg/chains/formats/slsa/internal/slsaconfig"
2526
"github.com/tektoncd/chains/pkg/chains/formats/slsa/v2alpha3/internal/pipelinerun"
2627
"github.com/tektoncd/chains/pkg/chains/formats/slsa/v2alpha3/internal/taskrun"
@@ -68,3 +69,12 @@ func (s *Slsa) CreatePayload(ctx context.Context, obj interface{}) (interface{},
6869
func (s *Slsa) Type() config.PayloadType {
6970
return formats.PayloadTypeSlsav2alpha3
7071
}
72+
73+
// RetrieveAllArtifactURIs returns the full URI of all artifacts detected as subjects.
74+
func (s *Slsa) RetrieveAllArtifactURIs(ctx context.Context, obj interface{}) ([]string, error) {
75+
tkObj, ok := obj.(objects.TektonObject)
76+
if !ok {
77+
return nil, fmt.Errorf("intoto does not support type")
78+
}
79+
return extract.RetrieveAllArtifactURIs(ctx, tkObj, s.slsaConfig.DeepInspectionEnabled), nil
80+
}

pkg/chains/formats/slsa/v2alpha4/internal/pipelinerun/pipelinerun.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func GenerateAttestation(ctx context.Context, pro *objects.PipelineRunObjectV1,
4646
return nil, err
4747
}
4848

49-
sub := subjectDigests(ctx, pro, slsaconfig)
49+
sub := SubjectDigests(ctx, pro, slsaconfig)
5050

5151
return provenance.GetSLSA1Statement(pro, sub, &bd, bp, slsaconfig)
5252
}
@@ -73,7 +73,8 @@ func byproducts(pro *objects.PipelineRunObjectV1, slsaconfig *slsaconfig.SlsaCon
7373
return byProd, nil
7474
}
7575

76-
func subjectDigests(ctx context.Context, pro *objects.PipelineRunObjectV1, slsaconfig *slsaconfig.SlsaConfig) []*intoto.ResourceDescriptor {
76+
// SubjectDigests calculates the subjects associated with the given PipelineRun.
77+
func SubjectDigests(ctx context.Context, pro *objects.PipelineRunObjectV1, slsaconfig *slsaconfig.SlsaConfig) []*intoto.ResourceDescriptor {
7778
subjects := extract.SubjectsFromBuildArtifact(ctx, pro.GetResults())
7879

7980
if !slsaconfig.DeepInspectionEnabled {

pkg/chains/formats/slsa/v2alpha4/slsav2.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222

23+
intoto "github.com/in-toto/attestation/go/v1"
2324
"github.com/tektoncd/chains/pkg/chains/formats"
2425
"github.com/tektoncd/chains/pkg/chains/formats/slsa/internal/slsaconfig"
2526
"github.com/tektoncd/chains/pkg/chains/formats/slsa/v2alpha4/internal/pipelinerun"
@@ -74,3 +75,25 @@ func (s *Slsa) CreatePayload(ctx context.Context, obj interface{}) (interface{},
7475
func (s *Slsa) Type() config.PayloadType {
7576
return payloadTypeSlsav2alpha4
7677
}
78+
79+
// RetrieveAllArtifactURIs returns the full URI of all artifacts detected as subjects.
80+
func (s *Slsa) RetrieveAllArtifactURIs(ctx context.Context, obj interface{}) ([]string, error) {
81+
var subjects []*intoto.ResourceDescriptor
82+
var fullURIs []string
83+
84+
switch v := obj.(type) {
85+
case *objects.TaskRunObjectV1:
86+
subjects = taskrun.SubjectDigests(ctx, v)
87+
case *objects.PipelineRunObjectV1:
88+
subjects = pipelinerun.SubjectDigests(ctx, v, s.slsaConfig)
89+
default:
90+
return nil, fmt.Errorf("intoto does not support type: %s", v)
91+
}
92+
93+
for _, s := range subjects {
94+
for algo, digest := range s.Digest {
95+
fullURIs = append(fullURIs, fmt.Sprintf("%s@%s:%s", s.Name, algo, digest))
96+
}
97+
}
98+
return fullURIs, nil
99+
}

pkg/chains/storage/grafeas/grafeas.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func (b *Backend) createOccurrence(ctx context.Context, obj objects.TektonObject
253253
}
254254

255255
// create Occurrence_Build for TaskRun
256-
allURIs := extract.RetrieveAllArtifactURIs(ctx, obj, b.cfg.Artifacts.PipelineRuns.DeepInspectionEnabled)
256+
allURIs := b.getAllArtifactURIs(ctx, opts.PayloadFormat, obj)
257257
for _, uri := range allURIs {
258258
occ, err := b.createBuildOccurrence(ctx, obj, payload, signature, uri)
259259
if err != nil {
@@ -264,6 +264,22 @@ func (b *Backend) createOccurrence(ctx context.Context, obj objects.TektonObject
264264
return occs, nil
265265
}
266266

267+
func (b *Backend) getAllArtifactURIs(ctx context.Context, payloadFormat config.PayloadType, obj objects.TektonObject) []string {
268+
logger := logging.FromContext(ctx)
269+
payloader, err := formats.GetPayloader(payloadFormat, b.cfg)
270+
if err != nil {
271+
logger.Infof("couldn't get payloader for %v format, will use extract.RetrieveAllArtifactURIs method instead", payloadFormat)
272+
return extract.RetrieveAllArtifactURIs(ctx, obj, b.cfg.Artifacts.PipelineRuns.DeepInspectionEnabled)
273+
}
274+
275+
if uris, err := payloader.RetrieveAllArtifactURIs(ctx, obj); err == nil {
276+
return uris
277+
}
278+
279+
logger.Infof("couldn't get URIs from payloader %v, will use extract.RetrieveAllArtifactURIs method instead", payloadFormat)
280+
return extract.RetrieveAllArtifactURIs(ctx, obj, b.cfg.Artifacts.PipelineRuns.DeepInspectionEnabled)
281+
}
282+
267283
func (b *Backend) createAttestationOccurrence(ctx context.Context, payload []byte, signature string, uri string) (*pb.Occurrence, error) {
268284
occurrenceDetails := &pb.Occurrence_Attestation{
269285
Attestation: &pb.AttestationOccurrence{
@@ -364,7 +380,7 @@ func (b *Backend) getBuildNotePath(obj objects.TektonObject) string {
364380
func (b *Backend) getAllOccurrences(ctx context.Context, obj objects.TektonObject, opts config.StorageOpts) ([]*pb.Occurrence, error) {
365381
result := []*pb.Occurrence{}
366382
// step 1: get all resource URIs created under the taskrun
367-
uriFilters := extract.RetrieveAllArtifactURIs(ctx, obj, b.cfg.Artifacts.PipelineRuns.DeepInspectionEnabled)
383+
uriFilters := b.getAllArtifactURIs(ctx, opts.PayloadFormat, obj)
368384

369385
// step 2: find all build occurrences
370386
if _, ok := formats.IntotoAttestationSet[opts.PayloadFormat]; ok {

0 commit comments

Comments
 (0)