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: change signature and pull node and python data #641

Open
wants to merge 1 commit into
base: expose_image_content_extraction
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
UpdateDockerfileBaseImageNameErrorCode,
} from "./dockerfile/types";
import * as facts from "./facts";
import { extractContent, scan } from "./scan";
import { collectApplicationFiles, scan } from "./scan";
import {
AutoDetectedUserInstructions,
ContainerTarget,
Expand All @@ -28,7 +28,7 @@ export {
scan,
display,
dockerFile,
extractContent,
collectApplicationFiles,
facts,
ScanResult,
PluginResponse,
Expand Down
62 changes: 56 additions & 6 deletions lib/scan.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import * as fs from "fs";
import * as path from "path";

import { getApplicationFiles } from "./analyzer/applications/runtime-common";
import { getImageArchive } from "./analyzer/image-inspector";
import { readDockerfileAndAnalyse } from "./dockerfile";
import { DockerFileAnalysis } from "./dockerfile/types";
import { extractImageContent } from "./extractor";
import { ImageName } from "./extractor/image";
import { ExtractAction, ExtractionResult } from "./extractor/types";
import { ImageIdFact } from "./facts";
import { fullImageSavePath } from "./image-save-path";
import { getArchivePath, getImageType } from "./image-type";
import { getFileContent } from "./inputs";
import { getNodeJsTsAppFileContentAction } from "./inputs/node/static";
import { getPythonAppFileContentAction } from "./inputs/python/static";
import { isNumber, isTrue } from "./option-utils";
import * as staticModule from "./static";
import { ImageType, PluginOptions, PluginResponse } from "./types";

import { ImageType, PluginOptions, PluginResponse, ScanResult } from "./types";

// Registry credentials may also be provided by env vars. When both are set, flags take precedence.
export function mergeEnvVarsIntoCredentials(
Expand Down Expand Up @@ -213,10 +218,9 @@ export function appendLatestTagIfMissing(targetImage: string): string {
return targetImage;
}

export async function extractContent(
extractActions: ExtractAction[],
export async function collectApplicationFiles(
options?: Partial<PluginOptions>,
): Promise<ExtractionResult> {
): Promise<PluginResponse> {
nozik marked this conversation as resolved.
Show resolved Hide resolved
const {
targetImage,
imageType,
Expand Down Expand Up @@ -245,10 +249,56 @@ export async function extractContent(
throw new Error("Unhandled image type for image " + targetImage);
}

return extractImageContent(
const extractActions = [
getNodeJsTsAppFileContentAction,
getPythonAppFileContentAction,
];

const content = await extractImageContent(
imageType,
imagePath,
extractActions,
updatedOptions,
);

const nodeApplicationFilesScanResults = getApplicationFiles(
getFileContent(
content.extractedLayers,
getNodeJsTsAppFileContentAction.actionName,
),
"node",
"npm",
);

const pythonApplicationFilesScanResults = getApplicationFiles(
getFileContent(
content.extractedLayers,
getPythonAppFileContentAction.actionName,
),
"python",
"python",
);

const imageIdFact: ImageIdFact = {
type: "imageId",
data: content.imageId,
};

const applicationDependenciesScanResults: ScanResult[] = [
...nodeApplicationFilesScanResults,
...pythonApplicationFilesScanResults,
].map((appDepsScanResult) => {
appDepsScanResult.facts.push(imageIdFact);

return {
...appDepsScanResult,
target: {
image: targetImage,
},
};
});

return {
scanResults: applicationDependenciesScanResults,
};
}
15 changes: 1 addition & 14 deletions test/system/application-scans/gomodules.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as elf from "elfy";

import { extractContent, scan } from "../../../lib";
import { collectApplicationFiles, scan } from "../../../lib";
import { getGoModulesContentAction } from "../../../lib/go-parser";
import { getFixture } from "../../util";

Expand All @@ -24,19 +24,6 @@ describe("gomodules binaries scanning", () => {
expect(pluginResult).toMatchSnapshot();
});

it("should extract image content successfully", async () => {
const fixturePath = getFixture(
"docker-archives/docker-save/testgo-1.17.tar",
);
const imageNameAndTag = `docker-archive:${fixturePath}`;
const result = await extractContent([getGoModulesContentAction], {
path: imageNameAndTag,
});
const testgoBinary = result.extractedLayers["/testgo"];
expect(testgoBinary).toBeTruthy();
expect("gomodules" in testgoBinary).toBeTruthy();
});

it("return plugin result when Go binary cannot be parsed do not break layer iterator", async () => {
const elfParseMock = jest.spyOn(elf, "parse").mockImplementation(() => {
throw new Error("Cannot read property 'type' of undefined");
Expand Down
30 changes: 17 additions & 13 deletions test/system/application-scans/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { legacy } from "@snyk/dep-graph";
import * as lockFileParser from "snyk-nodejs-lockfile-parser";
import { NodeLockfileVersion } from "snyk-nodejs-lockfile-parser";
import * as resolveDeps from "snyk-resolve-deps";
import { extractContent, scan } from "../../../lib";
import { collectApplicationFiles, scan } from "../../../lib";
import {
getLockFileVersion,
shouldBuildDepTree,
} from "../../../lib/analyzer/applications/node";
import * as nodeUtils from "../../../lib/analyzer/applications/node-modules-utils";
import { getAppFilesRootDir } from "../../../lib/analyzer/applications/runtime-common";
import { FilePathToContent } from "../../../lib/analyzer/applications/types";
import { getNodeAppFileContentAction } from "../../../lib/inputs/node/static";
import { ApplicationFilesFact } from "../../../lib/facts";
import { getFixture, getObjFromFixture } from "../../util";

describe("node application scans", () => {
Expand Down Expand Up @@ -216,21 +216,25 @@ describe("node application scans", () => {
expect(depGraphNpmFromGlobalNodeModules.rootPkg.name).toEqual("lib");
});

it("should extract image content successfully", async () => {
it("should collect application files successfully", async () => {
const fixturePath = getFixture("npm/multi-project-image.tar");
const imageNameAndTag = `docker-archive:${fixturePath}`;
const result = await extractContent([getNodeAppFileContentAction], {
const result = await collectApplicationFiles({
path: imageNameAndTag,
});
expect(Object.keys(result.extractedLayers).length).toEqual(608);
Object.keys(result.extractedLayers).forEach((fileName) => {
expect(
fileName.endsWith("/package.json") ||
fileName.endsWith("/package-lock.json") ||
fileName.endsWith("/yarn.lock"),
).toBeTruthy();
expect("node-app-files" in result.extractedLayers[fileName]).toBeTruthy();
});
const applicationFilesFact: ApplicationFilesFact =
result.scanResults[0].facts.find(
(fact) => fact.type === "applicationFiles",
)!.data;

const fileList = applicationFilesFact[0].fileHierarchy.map((fh) => fh.path);
expect(fileList.length).toEqual(4);
prsnca marked this conversation as resolved.
Show resolved Hide resolved
expect(fileList).toEqual([
"bin/yarn.js",
"lib/cli.js",
"lib/v8-compile-cache.js",
"package.json",
]);
});

it("should generate a scanResult that contains a npm7 depGraph generated from node modules manifest files", async () => {
Expand Down
18 changes: 11 additions & 7 deletions test/system/application-scans/python/pip.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extractContent, scan } from "../../../../lib";
import { collectApplicationFiles, scan } from "../../../../lib";
import { getAppFilesRootDir } from "../../../../lib/analyzer/applications/runtime-common";
import { getPythonAppFileContentAction } from "../../../../lib/inputs/python/static";
import { ApplicationFilesFact } from "../../../../lib/facts";
import { getFixture } from "../../../util";

describe("pip application scan", () => {
Expand Down Expand Up @@ -61,15 +61,19 @@ describe("pip application scan", () => {
expect(pluginResultExcludeAppVulnsTrueBoolean.scanResults).toHaveLength(1);
});

it("should extract image content successfully", async () => {
it("should collect application files content successfully", async () => {
const fixturePath = getFixture("docker-archives/docker-save/pip-flask.tar");
const imageNameAndTag = `docker-archive:${fixturePath}`;
const result = await extractContent([getPythonAppFileContentAction], {
const result = await collectApplicationFiles({
path: imageNameAndTag,
});
const serverPyFile = result.extractedLayers["/app/server.py"];
expect(serverPyFile).toBeTruthy();
expect("python-app-files" in serverPyFile).toBeTruthy();
const applicationFilesFact: ApplicationFilesFact =
result.scanResults[0].facts.find(
(fact) => fact.type === "applicationFiles",
)!.data;
const fileList = applicationFilesFact[0].fileHierarchy.map((fh) => fh.path);
expect(fileList.length).toBe(1);
expect(fileList.includes("server.py")).toBeTruthy();
});

it("should handle --collect-application-files", async () => {
Expand Down