Skip to content
Merged
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 47 additions & 43 deletions app/controlplane/api/workflowcontract/v1/crafting_schema.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ message CraftingSchema {
GITHUB_ACTION = 1;
GITLAB_PIPELINE = 2;
AZURE_PIPELINE = 3;
JENKINS_JOB = 4;
}
}

Expand Down
2 changes: 2 additions & 0 deletions internal/attestation/crafter/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func NewRunner(t schemaapi.CraftingSchema_Runner_RunnerType) supportedRunner {
return &runners.GitlabPipeline{}
case schemaapi.CraftingSchema_Runner_AZURE_PIPELINE:
return &runners.AzurePipeline{}
case schemaapi.CraftingSchema_Runner_JENKINS_JOB:
return &runners.JenkinsJob{}
default:
return &runners.Generic{}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/attestation/crafter/runners/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (r *Generic) CheckEnv() bool {
return true
}

// Returns a list of environment variables names. This list is used to
// automatically inject environment variables into the attestation.
func (r *Generic) ListEnvVars() []string {
return []string{}
}
Expand Down
67 changes: 67 additions & 0 deletions internal/attestation/crafter/runners/jenkinsjob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Copyright 2023 The Chainloop Authors.
//
// 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 runners

import "os"

type JenkinsJob struct{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to your change, since the other runners have the same issue, but it's something that we could tackle in another patch.

It seems that the "factory functions" are not being used outside of tests, when they could also be used in runner.go in the switch clause.


const JenkinsJobID = "jenkins-job"

func NewJenkinsJob() *JenkinsJob {
return &JenkinsJob{}
}

// Checks whether we are within a Jenkins job
func (r *JenkinsJob) CheckEnv() bool {
for _, envVarName := range []string{"JENKINS_HOME", "BUILD_URL"} {
if os.Getenv(envVarName) == "" {
return false
}
}

return true
}

func (r *JenkinsJob) ListEnvVars() []string {
return []string{
// Some info about the job
"JOB_NAME",
"BUILD_URL",

// Some info about the commit (Jenkins Git Plugin)
// NOTE: Commenting these vars out because they are not always present
// and current chainloop behavior requires these to be set.
// "GIT_BRANCH",
// "GIT_COMMIT",

// Some info about the agent
"AGENT_WORKDIR",
"NODE_NAME",
}
}

func (r *JenkinsJob) ResolveEnvVars() map[string]string {
return resolveEnvVars(r.ListEnvVars())
}

func (r *JenkinsJob) String() string {
return JenkinsJobID
}

func (r *JenkinsJob) RunURI() string {
return os.Getenv("BUILD_URL")
}
119 changes: 119 additions & 0 deletions internal/attestation/crafter/runners/jenkinsjob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//
// Copyright 2023 The Chainloop Authors.
//
// 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 runners

import (
"os"
"testing"

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

type jenkinsJobSuite struct {
suite.Suite
runner *JenkinsJob
}

func (s *jenkinsJobSuite) TestCheckEnv() {
testCases := []struct {
name string
env map[string]string
want bool
}{
{
name: "empty",
env: map[string]string{},
want: false,
},
{
name: "missing JENKINS_HOME",
env: map[string]string{
"BUILD_URL": "http://some-build-url/",
},
want: false,
},
{
name: "missing BUILD_URL",
env: map[string]string{
"JENKINS_HOME": "http://some-jenkins-home/",
},
want: false,
},
{
name: "all present",
env: map[string]string{
"BUILD_URL": "http://some-build-url/",
"JENKINS_HOME": "http://some-jenkins-home/",
},
want: true,
},
}

for _, tc := range testCases {
s.T().Run(tc.name, func(t *testing.T) {
os.Unsetenv("BUILD_URL")
os.Unsetenv("JENKINS_HOME")

for k, v := range tc.env {
t.Setenv(k, v)
}

s.Equal(tc.want, s.runner.CheckEnv())
})
}
}

func (s *jenkinsJobSuite) TestListEnvVars() {
s.Equal([]string{
"JOB_NAME",
"BUILD_URL",
"AGENT_WORKDIR",
"NODE_NAME",
}, s.runner.ListEnvVars())
}

func (s *jenkinsJobSuite) TestResolveEnvVars() {
s.Equal(jenkinsJobTestingEnvVars, s.runner.ResolveEnvVars())
}

func (s *jenkinsJobSuite) TestRunURI() {
s.Equal("http://some-build-url/", s.runner.RunURI())
}

func (s *jenkinsJobSuite) TestRunnerName() {
s.Equal("jenkins-job", s.runner.String())
}

// Run before each test
func (s *jenkinsJobSuite) SetupTest() {
s.runner = NewJenkinsJob()
t := s.T()
for k, v := range jenkinsJobTestingEnvVars {
t.Setenv(k, v)
}
}

var jenkinsJobTestingEnvVars = map[string]string{
"JOB_NAME": "some-jenkins-job",
"BUILD_URL": "http://some-build-url/",
"AGENT_WORKDIR": "/home/sample/agent",
"NODE_NAME": "some-node",
}

// Run the tests
func TestJenkinsJobRunner(t *testing.T) {
suite.Run(t, new(jenkinsJobSuite))
}