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

(fix) syft, fixing cyclone dx version to sbom version 1.4 for syft #4991

Merged
merged 5 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions cmd/kanikoExecute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func TestRunKanikoExecute(t *testing.T) {
assert.Equal(t, "https://index.docker.io", commonPipelineEnvironment.container.registryURL)

assert.Equal(t, "/tmp/syfttest/syft", execRunner.Calls[2].Exec)
assert.Equal(t, []string{"scan", "registry:index.docker.io/myImage:tag", "-o", "cyclonedx-xml=bom-docker-0.xml", "-q"}, execRunner.Calls[2].Params)
assert.Equal(t, []string{"scan", "registry:index.docker.io/myImage:tag", "-o", "cyclonedx-xml@1.4=bom-docker-0.xml", "-q"}, execRunner.Calls[2].Params)
})

t.Run("success case - multi image build with root image", func(t *testing.T) {
Expand Down Expand Up @@ -518,7 +518,7 @@ func TestRunKanikoExecute(t *testing.T) {
found := false
for _, expected := range expectedParams {
if expected[0] == "scan" {
expected = append(expected, fmt.Sprintf("cyclonedx-xml=bom-docker-%d.xml", index-3), "-q")
expected = append(expected, fmt.Sprintf("cyclonedx-xml@1.4=bom-docker-%d.xml", index-3), "-q")
}
if strings.Join(call.Params, " ") == strings.Join(expected, " ") {
found = true
Expand Down Expand Up @@ -670,7 +670,7 @@ func TestRunKanikoExecute(t *testing.T) {
found := false
for _, expected := range expectedParams {
if expected[0] == "scan" {
expected = append(expected, fmt.Sprintf("cyclonedx-xml=bom-docker-%d.xml", index-2), "-q")
expected = append(expected, fmt.Sprintf("cyclonedx-xml@1.4=bom-docker-%d.xml", index-2), "-q")
}
if strings.Join(call.Params, " ") == strings.Join(expected, " ") {
found = true
Expand Down
4 changes: 3 additions & 1 deletion pkg/syft/syft.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type SyftScanner struct {
additionalArgs []string
}

const cyclonedxFormatForSyft = "@1.4"

func GenerateSBOM(syftDownloadURL, dockerConfigDir string, execRunner command.ExecRunner, fileUtils piperutils.FileUtils, httpClient piperhttp.Sender, registryURL string, images []string) error {
scanner, err := CreateSyftScanner(syftDownloadURL, fileUtils, httpClient)
if err != nil {
Expand Down Expand Up @@ -64,7 +66,7 @@ func (s *SyftScanner) ScanImages(dockerConfigDir string, execRunner command.Exec
return errors.New("syft: image name must not be empty")
}
// TrimPrefix needed as syft needs containerRegistry name only
args := []string{"scan", fmt.Sprintf("registry:%s/%s", strings.TrimPrefix(registryURL, "https://"), image), "-o", fmt.Sprintf("cyclonedx-xml=bom-docker-%v.xml", index), "-q"}
args := []string{"scan", fmt.Sprintf("registry:%s/%s", strings.TrimPrefix(registryURL, "https://"), image), "-o", fmt.Sprintf("cyclonedx-xml%s=bom-docker-%v.xml", cyclonedxFormatForSyft, index), "-q"}
args = append(args, s.additionalArgs...)
err := execRunner.RunExecutable(s.syftFile, args...)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/syft/syft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ func TestGenerateSBOM(t *testing.T) {
assert.Len(t, execMock.Calls, 2)
firstCall := execMock.Calls[0]
assert.Equal(t, firstCall.Exec, "/tmp/syfttest/syft")
assert.Equal(t, firstCall.Params, []string{"scan", "registry:my-registry/image:latest", "-o", "cyclonedx-xml=bom-docker-0.xml", "-q"})
assert.Equal(t, firstCall.Params, []string{"scan", "registry:my-registry/image:latest", "-o", "cyclonedx-xml@1.4=bom-docker-0.xml", "-q"})

secondCall := execMock.Calls[1]
assert.Equal(t, secondCall.Exec, "/tmp/syfttest/syft")
assert.Equal(t, secondCall.Params, []string{"scan", "registry:my-registry/image:1.2.3", "-o", "cyclonedx-xml=bom-docker-1.xml", "-q"})
assert.Equal(t, secondCall.Params, []string{"scan", "registry:my-registry/image:1.2.3", "-o", "cyclonedx-xml@1.4=bom-docker-1.xml", "-q"})
})

t.Run("error case: syft execution failed", func(t *testing.T) {
execMock = mock.ExecMockRunner{}
execMock.ShouldFailOnCommand = map[string]error{
"/tmp/syfttest/syft scan registry:my-registry/image:latest -o cyclonedx-xml=bom-docker-0.xml -q": errors.New("failed"),
"/tmp/syfttest/syft scan registry:my-registry/image:latest -o cyclonedx-xml@1.4=bom-docker-0.xml -q": errors.New("failed"),
}

err := syft.GenerateSBOM("http://test-syft-gh-release.com/syft.tar.gz", "", &execMock, &fileMock, client, "https://my-registry", []string{"image:latest"})
Expand Down
Loading