Skip to content

Commit

Permalink
test: add jenkins and slsa attestor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdabosh committed Aug 16, 2024
1 parent 35d6257 commit 9575031
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
38 changes: 38 additions & 0 deletions attestation/jenkins/jenkins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 jenkins

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSubjects(t *testing.T) {
attestor := &Attestor{}

subjects := attestor.Subjects()
assert.NotNil(t, subjects)
assert.Equal(t, 2, len(subjects))

expectedSubjects := []string{"pipelineurl:" + attestor.PipelineUrl, "jenkinsurl:" + attestor.JenkinsUrl}
for _, expectedSubject := range expectedSubjects {
_, ok := subjects[expectedSubject]
assert.True(t, ok, "Expected subject not found: %s", expectedSubject)
}
m := attestor.BackRefs()
assert.NotNil(t, m)
assert.Equal(t, 1, len(m))
}
46 changes: 46 additions & 0 deletions attestation/slsa/slsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func TestAttest(t *testing.T) {
gl.Data().JWT.Claims["sha"] = "abc123"
gl.Data().PipelineUrl = "https://github.com/testifysec/swf/actions/runs/7879307166"

// Setup Jenkins
jks := attestors.NewTestJenkinsAttestor()
jks.Data().JenkinsUrl = "https://localhost:8000/"
jks.Data().PipelineUrl = "https://github.com/testifysec/swf/actions/runs/7879307166"

// Setup Materials
m := attestors.NewTestMaterialAttestor()

Expand All @@ -135,6 +140,7 @@ func TestAttest(t *testing.T) {
}{
{"github", []attestation.Attestor{e, g, gh, m, c, p, o}, testGHProvJSON},
{"gitlab", []attestation.Attestor{e, g, gl, m, c, p, o}, testGLProvJSON},
{"jenkins", []attestation.Attestor{e, g, jks, m, c, p, o}, testJKSProvJSON},
}

for _, test := range tests {
Expand Down Expand Up @@ -316,3 +322,43 @@ const testGLProvJSON = `{
}
}
}`

const testJKSProvJSON = `{
"build_definition": {
"build_type": "https://witness.dev/[email protected]",
"external_parameters": {
"command": "touch test.txt"
},
"internal_parameters": {
"env": {
"SHELL": "/bin/zsh",
"TERM": "xterm-256color",
"TERM_PROGRAM": "iTerm.app"
}
},
"resolved_dependencies": [
{
"name": "[email protected]:in-toto/witness.git",
"digest": {
"sha1": "abc123"
}
}
]
},
"run_details": {
"builder": {
"id": "https://witness.dev/[email protected]"
},
"metadata": {
"invocation_id": "https://github.com/testifysec/swf/actions/runs/7879307166",
"started_on": {
"seconds": 1711199861,
"nanos": 560152000
},
"finished_on": {
"seconds": 1711199861,
"nanos": 560152000
}
}
}
}`
65 changes: 65 additions & 0 deletions internal/attestors/jenkins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 attestors

import (
"github.com/in-toto/go-witness/attestation"
"github.com/in-toto/go-witness/attestation/jenkins"
"github.com/in-toto/go-witness/cryptoutil"
"github.com/invopop/jsonschema"
)

var _ jenkins.JenkinsAttestor = &TestJenkinsAttestor{}

type TestJenkinsAttestor struct {
jenkinsAtt jenkins.Attestor
}

func NewTestJenkinsAttestor() *TestJenkinsAttestor {
att := jenkins.Attestor{}
return &TestJenkinsAttestor{jenkinsAtt: att}
}

func (t *TestJenkinsAttestor) Name() string {
return t.jenkinsAtt.Name()
}

func (t *TestJenkinsAttestor) Type() string {
return t.jenkinsAtt.Type()
}

func (t *TestJenkinsAttestor) RunType() attestation.RunType {
return t.jenkinsAtt.RunType()
}

func (t *TestJenkinsAttestor) Schema() *jsonschema.Schema {
return jsonschema.Reflect(&t)
}

func (t *TestJenkinsAttestor) Attest(ctx *attestation.AttestationContext) error {
return nil
}

func (t *TestJenkinsAttestor) Data() *jenkins.Attestor {
return &t.jenkinsAtt
}

func (t *TestJenkinsAttestor) Subjects() map[string]cryptoutil.DigestSet {
return nil
}

func (t *TestJenkinsAttestor) BackRefs() map[string]cryptoutil.DigestSet {
return nil
}

0 comments on commit 9575031

Please sign in to comment.