From a1026712043c036eadafa8e9503ad589973b8bbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Comb=C3=BCchen?= Date: Thu, 25 Apr 2024 16:51:08 +0200 Subject: [PATCH 1/2] fix(sbom): Retain error code during SBOM generation --- test/jest/acceptance/snyk-sbom/sbom.spec.ts | 22 +++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/jest/acceptance/snyk-sbom/sbom.spec.ts b/test/jest/acceptance/snyk-sbom/sbom.spec.ts index 56ff85546d..64d93430d9 100644 --- a/test/jest/acceptance/snyk-sbom/sbom.spec.ts +++ b/test/jest/acceptance/snyk-sbom/sbom.spec.ts @@ -1,7 +1,11 @@ -import { createProjectFromWorkspace } from '../../util/createProject'; +import * as fs from 'fs'; + +import { + createProject, + createProjectFromWorkspace, +} from '../../util/createProject'; import { runSnykCLI } from '../../util/runSnykCLI'; import { fakeServer } from '../../../acceptance/fake-server'; -import * as fs from 'fs'; jest.setTimeout(1000 * 60 * 5); @@ -169,4 +173,18 @@ describe('snyk sbom (mocked server only)', () => { ]), ); }); + + test('`sbom` retains the exit error code of the underlying SCA process', async () => { + const project = await createProject('empty'); + + const { code } = await runSnykCLI( + `sbom --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --format cyclonedx1.5+json --debug`, + { + cwd: project.path(), + env, + }, + ); + + expect(code).toBe(3); + }); }); From 32aec37af1de0a433c342ea9e18adf1884258e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Comb=C3=BCchen?= Date: Fri, 26 Apr 2024 08:59:13 +0200 Subject: [PATCH 2/2] fix(cli): Print message of errors wrapping exec.ExitError --- cliv2/cmd/cliv2/main.go | 3 +-- cliv2/cmd/cliv2/main_test.go | 14 ++++++++++++++ cliv2/go.mod | 4 ++-- cliv2/go.sum | 8 ++++---- test/jest/acceptance/snyk-sbom/sbom.spec.ts | 5 ++++- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cliv2/cmd/cliv2/main.go b/cliv2/cmd/cliv2/main.go index 17032084de..eb95141d86 100644 --- a/cliv2/cmd/cliv2/main.go +++ b/cliv2/cmd/cliv2/main.go @@ -370,9 +370,8 @@ func handleError(err error) HandleError { func displayError(err error, output io.Writer, config configuration.Configuration) { if err != nil { - var exitError *exec.ExitError var exitCode *cli_errors.ErrorWithExitCode - isExitError := errors.As(err, &exitError) + _, isExitError := err.(*exec.ExitError) isErrorWithCode := errors.As(err, &exitCode) if isExitError || isErrorWithCode { return diff --git a/cliv2/cmd/cliv2/main_test.go b/cliv2/cmd/cliv2/main_test.go index a8f74f4072..2dd8960009 100644 --- a/cliv2/cmd/cliv2/main_test.go +++ b/cliv2/cmd/cliv2/main_test.go @@ -400,4 +400,18 @@ func Test_displayError(t *testing.T) { assert.Equal(t, "", b.String()) }) } + + t.Run("prints messages of error wrapping exec.ExitError", func(t *testing.T) { + var b bytes.Buffer + config := configuration.NewInMemory() + err := &wrErr{wraps: &exec.ExitError{}} + displayError(err, &b, config) + + assert.Equal(t, "something went wrong\n", b.String()) + }) } + +type wrErr struct{ wraps error } + +func (e *wrErr) Error() string { return "something went wrong" } +func (e *wrErr) Unwrap() error { return e.wraps } diff --git a/cliv2/go.mod b/cliv2/go.mod index a97f7db980..ce26ed5de2 100644 --- a/cliv2/go.mod +++ b/cliv2/go.mod @@ -9,9 +9,9 @@ require ( github.com/google/uuid v1.6.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.32.0 - github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73 + github.com/snyk/cli-extension-dep-graph v0.0.0-20240426125928-8d56ac52821e github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672 - github.com/snyk/cli-extension-sbom v0.0.0-20240426072511-94652d584413 + github.com/snyk/cli-extension-sbom v0.0.0-20240426130148-b83c2ebc75c4 github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 github.com/snyk/go-application-framework v0.0.0-20240426112848-21dc9ca24101 github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 diff --git a/cliv2/go.sum b/cliv2/go.sum index 0da886be06..da8aea827a 100644 --- a/cliv2/go.sum +++ b/cliv2/go.sum @@ -721,12 +721,12 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= -github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73 h1:rwdJzpPWkWWf7csbv871GoKgRGwJ4GVNXMSsO0cRuLk= -github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73/go.mod h1:QF3v8HBpOpyudYNCuR8LqfULutO76c91sBdLzD+pBJU= +github.com/snyk/cli-extension-dep-graph v0.0.0-20240426125928-8d56ac52821e h1:j17Ujw51/2SC3m1hbNCUwxFc8aNIFyfpnwFAszgEM8c= +github.com/snyk/cli-extension-dep-graph v0.0.0-20240426125928-8d56ac52821e/go.mod h1:QF3v8HBpOpyudYNCuR8LqfULutO76c91sBdLzD+pBJU= github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672 h1:AkLej8Lk//vFex1fiygSYFrQTUd0xP+GyRbsI+m2kwQ= github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672/go.mod h1:2vKTUsW73sVbDcyD19iNLfN0so2GSu9BE3k/fqG0mjA= -github.com/snyk/cli-extension-sbom v0.0.0-20240426072511-94652d584413 h1:DrieS0CPi1GEnAv8xdUJ7ceqgqUaFJyJAXIZ4I24KCI= -github.com/snyk/cli-extension-sbom v0.0.0-20240426072511-94652d584413/go.mod h1:lqmQT+QdzLdfi7qsqIH4qvCsSWu+P09GDFwQcmFfC0g= +github.com/snyk/cli-extension-sbom v0.0.0-20240426130148-b83c2ebc75c4 h1:nLbplrdYB4ljdFSnpmw2rCqmBVxHcvZOFEqc8JF9OQc= +github.com/snyk/cli-extension-sbom v0.0.0-20240426130148-b83c2ebc75c4/go.mod h1:lqmQT+QdzLdfi7qsqIH4qvCsSWu+P09GDFwQcmFfC0g= github.com/snyk/code-client-go v1.5.3 h1:EnHogM2uNnvkrfIJbkOGa9X1bOkU6bJR/QRQCwbK9gQ= github.com/snyk/code-client-go v1.5.3/go.mod h1:Kkr7pQc8ItsBZSYd6A1S4r4VHO6HNyTWZsqi18sAtwQ= github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 h1:9RKY9NdX5DrJAoVXDP0JiqrXT+4Nb9NH8pjEcA0NsLA= diff --git a/test/jest/acceptance/snyk-sbom/sbom.spec.ts b/test/jest/acceptance/snyk-sbom/sbom.spec.ts index 64d93430d9..23277dc87c 100644 --- a/test/jest/acceptance/snyk-sbom/sbom.spec.ts +++ b/test/jest/acceptance/snyk-sbom/sbom.spec.ts @@ -177,7 +177,7 @@ describe('snyk sbom (mocked server only)', () => { test('`sbom` retains the exit error code of the underlying SCA process', async () => { const project = await createProject('empty'); - const { code } = await runSnykCLI( + const { code, stdout } = await runSnykCLI( `sbom --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --format cyclonedx1.5+json --debug`, { cwd: project.path(), @@ -186,5 +186,8 @@ describe('snyk sbom (mocked server only)', () => { ); expect(code).toBe(3); + expect(stdout).toContain( + 'An error occurred while running the underlying analysis needed to generate the SBOM.', + ); }); });