Skip to content

Commit

Permalink
feat: support --all-projects for sbom command
Browse files Browse the repository at this point in the history
Co-authored-by: Max <max.combuchen@snyk.io>
  • Loading branch information
xzhou-snyk and mcombuechen committed May 26, 2023
1 parent 6989e20 commit 849268c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cliv2/go.mod
Expand Up @@ -10,8 +10,8 @@ require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.29.1
github.com/snyk/cli-extension-iac-rules v0.0.0-20230523125328-59db55ecc135
github.com/snyk/cli-extension-sbom v0.0.0-20230331093938-3d6a5dfdae22
github.com/snyk/go-application-framework v0.0.0-20230519090414-ce080e28fec1
github.com/snyk/cli-extension-sbom v0.0.0-20230526074203-8198a7341fbc
github.com/snyk/go-application-framework v0.0.0-20230526065140-1fabe799e3f9
github.com/snyk/go-httpauth v0.0.0-20230512081507-800aedece3cb
github.com/snyk/snyk-iac-capture v0.6.0
github.com/spf13/cobra v1.7.0
Expand Down
12 changes: 8 additions & 4 deletions cliv2/go.sum
Expand Up @@ -576,10 +576,14 @@ github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/snyk/cli-extension-iac-rules v0.0.0-20230523125328-59db55ecc135 h1:b0QaMf9Wq6Tlpv2Rpbc9115S7sLraaNWv74O/IKZFYY=
github.com/snyk/cli-extension-iac-rules v0.0.0-20230523125328-59db55ecc135/go.mod h1:5/IYYTgf32pST7St4GhS3KNz32WE17Ys+Hdb5Pqxex0=
github.com/snyk/cli-extension-sbom v0.0.0-20230331093938-3d6a5dfdae22 h1:ucnmZwoo1gGU+YjmbYZAix5HKIZ1FYBNDau5RPwCSS8=
github.com/snyk/cli-extension-sbom v0.0.0-20230331093938-3d6a5dfdae22/go.mod h1:83CWQ4Oy3mL8cVkj/etP+bh7I8I1xb+n2bpsE6URuPs=
github.com/snyk/go-application-framework v0.0.0-20230519090414-ce080e28fec1 h1:pwHKIc2+NMoseY3hH5gr7okrc39DdYA6xTFsGKik0mg=
github.com/snyk/go-application-framework v0.0.0-20230519090414-ce080e28fec1/go.mod h1:Aun65T/AmzxjZe9jZZBqia6RHwoS7oq8QB2UfQIcPjU=
github.com/snyk/cli-extension-sbom v0.0.0-20230526064837-a45a2564bc54 h1:bUlOtaYRWyiaUoV1QC8cW7z932GJlD+rK88JGx6yyNA=
github.com/snyk/cli-extension-sbom v0.0.0-20230526064837-a45a2564bc54/go.mod h1:iq3uCtvc71ay9Y1tHuKwQ9feUwAi/6CjKfSFKxFaLeQ=
github.com/snyk/cli-extension-sbom v0.0.0-20230526074203-8198a7341fbc h1:hJzxC5ievJ+lwudvkDwzvifRUtLtima7vTXs8ePDrJE=
github.com/snyk/cli-extension-sbom v0.0.0-20230526074203-8198a7341fbc/go.mod h1:m0SL0IQ33c9uC5TI+Xo6bx0Dj8L1GgHxCaO/ZyGwhhw=
github.com/snyk/go-application-framework v0.0.0-20230525093644-7c923f48a33d h1:TUgvGsj1C7yRAkzlnITX6DqA5l/xDvTdRbdLlw81QAI=
github.com/snyk/go-application-framework v0.0.0-20230525093644-7c923f48a33d/go.mod h1:Aun65T/AmzxjZe9jZZBqia6RHwoS7oq8QB2UfQIcPjU=
github.com/snyk/go-application-framework v0.0.0-20230526065140-1fabe799e3f9 h1:FS0NA9lcbrW4Oha217+DppFXlECeWcHa8RBIKIK3+Qs=
github.com/snyk/go-application-framework v0.0.0-20230526065140-1fabe799e3f9/go.mod h1:Aun65T/AmzxjZe9jZZBqia6RHwoS7oq8QB2UfQIcPjU=
github.com/snyk/go-httpauth v0.0.0-20230512081507-800aedece3cb h1:UwbUBfe1u5MYLhtCNOsFEM98tfEUWqgmaXam/UxU88Q=
github.com/snyk/go-httpauth v0.0.0-20230512081507-800aedece3cb/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg=
github.com/snyk/policy-engine v0.22.0 h1:od9pduGrXyfWO791X+8M1qmnvWUxaIXh0gBzGKqeseA=
Expand Down
7 changes: 6 additions & 1 deletion cliv2/pkg/basic_workflows/legacycli.go
Expand Up @@ -61,6 +61,7 @@ func legacycliWorkflow(
oauthIsAvailable := config.GetBool(configuration.FF_OAUTH_AUTH_FLOW_ENABLED)
args := config.GetStringSlice(configuration.RAW_CMD_ARGS)
useStdIo := config.GetBool(configuration.WORKFLOW_USE_STDIO)
isDebug := config.GetBool(configuration.DEBUG)
cacheDirectory := config.GetString(configuration.CACHE_PATH)
insecure := config.GetBool(configuration.INSECURE_HTTPS)
proxyAuthenticationMechanismString := config.GetString(configuration.PROXY_AUTHENTICATION_MECHANISM)
Expand Down Expand Up @@ -132,7 +133,11 @@ func legacycliWorkflow(
proxyInfo := wrapperProxy.ProxyInfo()
err = cli.Execute(proxyInfo, FilteredArgs(args))

if useStdIo == false {
if !useStdIo {
if isDebug {
debugLogger.Println(errBuffer.String())
}

data := workflow.NewData(DATATYPEID_LEGACY_CLI_STDOUT, "text/plain", outBuffer.Bytes())
output = append(output, data)
}
Expand Down
20 changes: 17 additions & 3 deletions test/acceptance/fake-server.ts
Expand Up @@ -515,9 +515,23 @@ export const fakeServer = (basePath: string, snykToken: string): FakeServer => {
res.status(200).send({});
});

app.post(basePath.replace('v1', 'hidden') + '/orgs/:org/sbom', (req, res) => {
res.status(200).send({});
});
app.post(
basePath.replace('v1', 'hidden') + '/orgs/:org/sbom',
express.json(),
(req, res) => {
let bom: Record<string, unknown> = { bomFormat: 'CycloneDX' };

if (Array.isArray(req.body.depGraphs) && req.body.subject) {
// Return a fixture of an all-projects SBOM.
bom = {
...bom,
metadata: { component: { name: req.body.subject.name } },
};
}

res.status(200).send(bom);
},
);

app.get(basePath + '/download/driftctl', (req, res) => {
const fixturePath = getFixturePath('iac');
Expand Down
59 changes: 59 additions & 0 deletions test/jest/acceptance/snyk-sbom/all-projects.spec.ts
@@ -0,0 +1,59 @@
import { createProjectFromWorkspace } from '../../util/createProject';
import { runSnykCLI } from '../../util/runSnykCLI';
import { fakeServer } from '../../../acceptance/fake-server';
import { isCLIV2 } from '../../util/isCLIV2';

jest.setTimeout(1000 * 60 * 5);

describe('snyk sbom --all-projects (mocked server only)', () => {
let server;
let env: Record<string, string>;

beforeAll((done) => {
const port = process.env.PORT || process.env.SNYK_PORT || '58585';
const baseApi = '/api/v1';
env = {
...process.env,
SNYK_API: 'http://localhost:' + port + baseApi,
SNYK_HOST: 'http://localhost:' + port,
SNYK_TOKEN: '123456789',
SNYK_DISABLE_ANALYTICS: '1',
};
server = fakeServer(baseApi, env.SNYK_TOKEN);
server.listen(port, () => {
done();
});
});

afterEach(() => {
jest.resetAllMocks();
server.restore();
});

afterAll((done) => {
server.close(() => {
done();
});
});

test('`sbom mono-repo-project` generates an SBOM for multiple projects', async () => {
const project = await createProjectFromWorkspace('mono-repo-project');

if (isCLIV2()) {
const { code, stdout } = await runSnykCLI(
`sbom --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --format cyclonedx1.4+json --debug --all-projects`,
{
cwd: project.path(),
env,
},
);

expect(code).toEqual(0);
expect(stdout).toEqual(
expect.stringContaining(
'{"bomFormat":"CycloneDX","metadata":{"component":{"name":"mono-repo-project"}}}',
),
);
}
});
});

0 comments on commit 849268c

Please sign in to comment.