Skip to content

Commit

Permalink
feat: set target-reference in the monitor request UNIFY-124
Browse files Browse the repository at this point in the history
  • Loading branch information
adrobuta committed Jun 5, 2024
1 parent d33a773 commit 08727f4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions help/cli-commands/container-monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ For more detailed advice, include the path to the Dockerfile for the image.

Specify a custom Snyk project name.

### `--target-reference=<TARGET_REFERENCE>`

Specify a reference that differentiates this project, for example, a branch name or version. Projects having the same reference can be grouped based on that reference.

For more information see [Group projects by branch or version for monitoring](https://docs.snyk.io/snyk-cli/scan-and-maintain-projects-using-the-cli/group-projects-by-branch-or-version-for-monitoring)

### `--policy-path=<PATH_TO_POLICY_FILE>`

Manually pass a path to a `.snyk` policy file.
Expand Down
1 change: 1 addition & 0 deletions src/lib/ecosystems/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export async function generateMonitorDependenciesRequest(
// WARNING! This mutates the payload. The project name logic should be handled in the plugin.
scanResult.name =
options['project-name'] || config.PROJECT_NAME || scanResult.name;
scanResult.targetReference = options['target-reference'];
// WARNING! This mutates the payload. Policy logic should be in the plugin.
const policy = await findAndLoadPolicyForScanResult(scanResult, options);
if (policy !== undefined) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export interface MonitorOptions {
'project-name'?: string;
'print-deps'?: boolean;
'print-dep-paths'?: boolean;
'target-reference'?: string;
scanAllUnmanaged?: boolean;
allProjects?: boolean;
// An experimental flag to allow monitoring of bigtrees (with degraded deps info and remediation advice).
Expand Down
50 changes: 50 additions & 0 deletions test/jest/acceptance/snyk-container/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { startSnykCLI, TestCLI } from '../../util/startSnykCLI';
import { runSnykCLI } from '../../util/runSnykCLI';
import { FakeServer, fakeServer } from '../../../acceptance/fake-server';
import { RunCommandOptions, RunCommandResult } from '../../util/runCommand';
import { getServerPort } from '../../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand Down Expand Up @@ -304,6 +305,55 @@ DepGraph end`,
});
});

describe('snyk container monitor supports --target-reference', () => {
let server: ReturnType<typeof fakeServer>;
let env: Record<string, string>;

beforeAll((done) => {
const port = getServerPort(process);
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',
DEBUG: 'snyk*',
};
server = fakeServer(baseApi, env.SNYK_TOKEN);
server.listen(port, () => {
done();
});
});

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

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

it('forwards value of target-reference to monitor-dependencies endpoint', async () => {
const { code } = await runSnykCLI(
`container monitor ${TEST_DISTROLESS_STATIC_IMAGE} --target-reference=test-target-ref`,
{
env,
},
);
expect(code).toEqual(0);

const monitorRequests = server
.getRequests()
.filter((request) => request.url?.includes('/monitor-dependencies'));

expect(monitorRequests.length).toBeGreaterThanOrEqual(1);
monitorRequests.forEach((request) => {
expect(request.body.scanResult.targetReference).toBe('test-target-ref');
});
});
});

function assertCliExitCode(
code: number,
expectedCode: number,
Expand Down

0 comments on commit 08727f4

Please sign in to comment.