Skip to content

Commit 48e3995

Browse files
committed
fix: use correct projectName in container monitor JSON output
The projectName field in 'snyk container monitor --json' output was incorrectly set to monitorResult.id (the monitor's public ID) instead of monitorResult.projectName (the actual project name). This caused the JSON output to display a UUID instead of the project's display name (e.g., the image name or --project-name value). Changes: - Fixed src/lib/ecosystems/monitor.ts line 214 to use monitorResult.projectName - Added unit test with mocked registry response to validate the fix - Updated acceptance tests with correct expected projectName values
1 parent 65dfdb1 commit 48e3995

File tree

5 files changed

+105
-3
lines changed

5 files changed

+105
-3
lines changed

src/lib/ecosystems/monitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export async function getFormattedMonitorOutput(
218218
ok: true,
219219
data: monOutput,
220220
path: monitorResult.path,
221-
projectName: monitorResult.id,
221+
projectName: monitorResult.projectName,
222222
});
223223
}
224224
for (const monitorError of errors) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"identity": {
3+
"type": "deb"
4+
},
5+
"facts": [
6+
{
7+
"type": "depGraph",
8+
"data": {
9+
"schemaVersion": "1.2.0",
10+
"pkgManager": {
11+
"name": "deb",
12+
"repositories": [{"alias": "debian:11"}]
13+
},
14+
"pkgs": [{"id": "alpine@3.18", "info": {"name": "alpine", "version": "3.18"}}],
15+
"graph": {
16+
"rootNodeId": "root-node",
17+
"nodes": [{"nodeId": "root-node", "pkgId": "alpine@3.18", "deps": []}]
18+
}
19+
}
20+
}
21+
],
22+
"target": {
23+
"image": "alpine:latest"
24+
},
25+
"name": "my-custom-project-name"
26+
}
27+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"ok": true,
3+
"org": "test-org",
4+
"id": "7c7305e2-fbcb-44d7-8fbf-8367371c509f",
5+
"isMonitored": true,
6+
"licensesPolicy": null,
7+
"uri": "https://app.snyk.io/org/test-org/project/3dda9b21-ca42-4de6-be7a-85696fa6e866/history/f60dce17-8a72-4cca-8a76-e9c88df546aa",
8+
"trialStarted": false,
9+
"path": "/srv",
10+
"projectName": "my-custom-project-name"
11+
}
12+

test/jest/acceptance/snyk-container/container.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ describe('snyk container', () => {
1818
});
1919
}
2020

21-
const TEST_DISTROLESS_STATIC_IMAGE =
22-
'gcr.io/distroless/static@sha256:7198a357ff3a8ef750b041324873960cf2153c11cc50abb9d8d5f8bb089f6b4e';
21+
const TEST_DISTROLESS_STATIC_IMAGE_NAME = 'gcr.io/distroless/static';
22+
const TEST_DISTROLESS_STATIC_IMAGE = `${TEST_DISTROLESS_STATIC_IMAGE_NAME}@sha256:7198a357ff3a8ef750b041324873960cf2153c11cc50abb9d8d5f8bb089f6b4e`;
2323
const TEST_DISTROLESS_STATIC_IMAGE_DEPGRAPH = {
2424
schemaVersion: '1.3.0',
2525
pkgManager: {
@@ -612,6 +612,7 @@ DepGraph end`,
612612
expect.objectContaining({
613613
ok: true,
614614
packageManager: 'deb',
615+
projectName: `docker-image|${TEST_DISTROLESS_STATIC_IMAGE_NAME}`,
615616
manageUrl: expect.stringContaining('://'),
616617
scanResult: expect.objectContaining({
617618
facts: expect.arrayContaining([
@@ -646,6 +647,7 @@ DepGraph end`,
646647
expect.objectContaining({
647648
ok: true,
648649
packageManager: 'deb',
650+
projectName: 'docker-image|snyk/snyk',
649651
manageUrl: expect.stringContaining('://'),
650652
scanResult: expect.objectContaining({
651653
facts: expect.arrayContaining([
@@ -668,6 +670,7 @@ DepGraph end`,
668670
expect.objectContaining({
669671
ok: true,
670672
packageManager: 'gomodules',
673+
projectName: 'docker-image|snyk/snyk:/usr/local/bin/snyk',
671674
manageUrl: expect.stringContaining('://'),
672675
scanResult: expect.objectContaining({
673676
facts: expect.arrayContaining([
@@ -685,6 +688,18 @@ DepGraph end`,
685688
]),
686689
);
687690
});
691+
692+
it('snyk container monitor json returns custom projectName when --project-name is provided', async () => {
693+
const customProjectName = 'my-custom-project-name';
694+
const { code, stdout } = await runSnykCLI(
695+
`container monitor --platform=linux/amd64 --project-name=${customProjectName} --json ${TEST_DISTROLESS_STATIC_IMAGE}`,
696+
);
697+
expect(code).toEqual(0);
698+
const result = JSON.parse(stdout);
699+
700+
// projectName should match the --project-name flag value
701+
expect(result.projectName).toBe(customProjectName);
702+
});
688703
});
689704

690705
describe('snyk container monitor supports --target-reference', () => {

test/jest/unit/ecosystems-monitor-docker.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,52 @@ describe('monitorEcosystem docker/container', () => {
186186
makeRequestSpy.mock.calls[0][0].body.pruneRepeatedSubdependencies,
187187
).toBeUndefined();
188188
});
189+
190+
it('should return projectName from registry response in JSON output', async () => {
191+
const containerScanResult = readJsonFixture(
192+
'container-deb-scan-result.json',
193+
) as ScanResult;
194+
const monitorDependenciesResponse = readJsonFixture(
195+
'monitor-dependencies-response-with-project-name.json',
196+
) as ecosystemsTypes.MonitorDependenciesResponse;
197+
198+
jest
199+
.spyOn(dockerPlugin, 'scan')
200+
.mockResolvedValue({ scanResults: [containerScanResult] });
201+
jest
202+
.spyOn(request, 'makeRequest')
203+
.mockResolvedValue(monitorDependenciesResponse);
204+
205+
const results: Array<GoodResult | BadResult> = [];
206+
207+
const [monitorResults, monitorErrors] = await ecosystems.monitorEcosystem(
208+
'docker',
209+
['/srv'],
210+
{
211+
path: '/srv',
212+
docker: true,
213+
org: 'test-org',
214+
},
215+
);
216+
217+
const jsonOutput = await getFormattedMonitorOutput(
218+
results,
219+
monitorResults,
220+
monitorErrors,
221+
{
222+
path: '/srv',
223+
docker: true,
224+
org: 'test-org',
225+
json: true,
226+
} as Options,
227+
);
228+
229+
const parsedOutput = JSON.parse(jsonOutput);
230+
231+
// projectName should be the actual project name from the registry, not the id (UUID)
232+
expect(parsedOutput.projectName).toBe('my-custom-project-name');
233+
expect(parsedOutput.projectName).not.toBe(
234+
'7c7305e2-fbcb-44d7-8fbf-8367371c509f',
235+
);
236+
});
189237
});

0 commit comments

Comments
 (0)