Skip to content

Commit

Permalink
Use common step format for pr and tr attestation
Browse files Browse the repository at this point in the history
Signed-off-by: Luiz Carvalho <lucarval@redhat.com>
  • Loading branch information
lcarva authored and HACBS EC Robot committed May 20, 2022
1 parent 19fb4d8 commit fdeb5f1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 51 deletions.
7 changes: 4 additions & 3 deletions pkg/chains/formats/intotoite6/intotoite6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/tektoncd/chains/pkg/chains/formats"
"github.com/tektoncd/chains/pkg/chains/formats/intotoite6/taskrun"
"github.com/tektoncd/chains/pkg/chains/formats/intotoite6/util"
"github.com/tektoncd/chains/pkg/config"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestCreatePayload1(t *testing.T) {
},
BuildType: "https://tekton.dev/attestations/chains@v2",
BuildConfig: taskrun.BuildConfig{
Steps: []taskrun.Step{
Steps: []util.StepAttestation{
{
Arguments: []string(nil),
Environment: map[string]interface{}{
Expand Down Expand Up @@ -139,7 +140,7 @@ func TestCreatePayload2(t *testing.T) {
},
BuildType: "https://tekton.dev/attestations/chains@v2",
BuildConfig: taskrun.BuildConfig{
Steps: []taskrun.Step{
Steps: []util.StepAttestation{
{
Arguments: []string(nil),
Environment: map[string]interface{}{
Expand Down Expand Up @@ -218,7 +219,7 @@ func TestMultipleSubjects(t *testing.T) {
Parameters: map[string]string{},
},
BuildConfig: taskrun.BuildConfig{
Steps: []taskrun.Step{
Steps: []util.StepAttestation{
{
Arguments: []string(nil),
Environment: map[string]interface{}{
Expand Down
27 changes: 8 additions & 19 deletions pkg/chains/formats/intotoite6/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@ type BuildConfig struct {
}

type Task struct {
Name string `json:"name,omitempty"`
StartedAt time.Time `json:"startedAt,omitempty"`
FinishedAt time.Time `json:"finishedAt,omitempty"`
Status string `json:"status,omitempty"`
Steps []Step `json:"steps,omitempty"`
}

type Step struct {
StepState v1beta1.StepState `json:"stepState,omitempty"`
Command []string `json:"command,omitempty"`
Arguments []string `json:"arguments,omitempty"`
Script string `json:"script,omitempty"`
Name string `json:"name,omitempty"`
StartedAt time.Time `json:"startedAt,omitempty"`
FinishedAt time.Time `json:"finishedAt,omitempty"`
Status string `json:"status,omitempty"`
Steps []util.StepAttestation `json:"steps,omitempty"`
}

func GenerateAttestation(builderID string, pr *v1beta1.PipelineRun, logger *zap.SugaredLogger) (interface{}, error) {
Expand Down Expand Up @@ -108,14 +101,10 @@ func buildConfig(pr *v1beta1.PipelineRun) BuildConfig {
// Ignore Tasks that did not execute during the PipelineRun.
continue
}
steps := []Step{}
steps := []util.StepAttestation{}
for i, step := range trStatus.Status.Steps {
steps = append(steps, Step{
StepState: step,
Command: trStatus.Status.TaskSpec.Steps[i].Command,
Arguments: trStatus.Status.TaskSpec.Steps[i].Args,
Script: trStatus.Status.TaskSpec.Steps[i].Script,
})
stepState := trStatus.Status.TaskSpec.Steps[i]
steps = append(steps, util.AttestStep(&stepState, &step))
}
task := Task{
Name: trStatus.PipelineTaskName,
Expand Down
38 changes: 10 additions & 28 deletions pkg/chains/formats/intotoite6/taskrun/buildconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ limitations under the License.
package taskrun

import (
"strings"

"github.com/tektoncd/chains/pkg/chains/formats/intotoite6/util"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
)

// BuildConfig is the custom Chains format to fill out the
// "buildConfig" section of the slsa-provenance predicate
type BuildConfig struct {
Steps []Step `json:"steps"`
Steps []util.StepAttestation `json:"steps"`
}

// Step corresponds to one step in the TaskRun
Expand All @@ -37,38 +36,21 @@ type Step struct {
}

func buildConfig(tr *v1beta1.TaskRun) BuildConfig {
steps := []Step{}
for _, step := range tr.Status.Steps {
s := Step{}
c := container(step, tr)
// get the entrypoint
entrypoint := strings.Join(c.Command, " ")
if c.Script != "" {
entrypoint = c.Script
}
s.EntryPoint = entrypoint
s.Arguments = c.Args

// env comprises of:
env := map[string]interface{}{}
env["image"] = step.ImageID
env["container"] = step.Name
s.Environment = env

// append to all of the steps
steps = append(steps, s)
attestations := []util.StepAttestation{}
for _, stepState := range tr.Status.Steps {
step := stepFromTaskRun(stepState.Name, tr)
attestations = append(attestations, util.AttestStep(step, &stepState))
}
return BuildConfig{Steps: steps}
return BuildConfig{Steps: attestations}
}

func container(stepState v1beta1.StepState, tr *v1beta1.TaskRun) v1beta1.Step {
name := stepState.Name
func stepFromTaskRun(name string, tr *v1beta1.TaskRun) *v1beta1.Step {
if tr.Status.TaskSpec != nil {
for _, s := range tr.Status.TaskSpec.Steps {
if s.Name == name {
return s
return &s
}
}
}
return v1beta1.Step{}
return &v1beta1.Step{}
}
3 changes: 2 additions & 1 deletion pkg/chains/formats/intotoite6/taskrun/buildconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/ghodss/yaml"
"github.com/google/go-cmp/cmp"
"github.com/tektoncd/chains/pkg/chains/formats/intotoite6/util"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
)

Expand Down Expand Up @@ -62,7 +63,7 @@ status:
}

expected := BuildConfig{
Steps: []Step{
Steps: []util.StepAttestation{
{
EntryPoint: "",
Environment: map[string]interface{}{
Expand Down
25 changes: 25 additions & 0 deletions pkg/chains/formats/intotoite6/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,28 @@ func SpdxGit(url, revision string) string {
}
return prefix + url + fmt.Sprintf("@%s", revision)
}

type StepAttestation struct {
EntryPoint string `json:"entryPoint"`
Arguments interface{} `json:"arguments,omitempty"`
Environment interface{} `json:"environment,omitempty"`
Annotations map[string]string `json:"annotations"`
}

func AttestStep(step *v1beta1.Step, stepState *v1beta1.StepState) StepAttestation {
attestation := StepAttestation{}

entrypoint := strings.Join(step.Command, " ")
if step.Script != "" {
entrypoint = step.Script
}
attestation.EntryPoint = entrypoint
attestation.Arguments = step.Args

env := map[string]interface{}{}
env["image"] = stepState.ImageID
env["container"] = stepState.Name
attestation.Environment = env

return attestation
}

0 comments on commit fdeb5f1

Please sign in to comment.