Skip to content

Commit

Permalink
fix: detect platform from image config file UNIFY-253
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-citiriga-snyk committed Sep 6, 2024
1 parent 894b262 commit af0973a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/extractor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export function getRootFsLayersFromConfig(imageConfig: ImageConfig): string[] {
export function getPlatformFromConfig(
imageConfig: ImageConfig,
): string | undefined {
return imageConfig.os && imageConfig.architecture
return imageConfig?.os && imageConfig?.architecture
? `${imageConfig.os}/${imageConfig.architecture}`
: undefined;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/extractor/oci-archive/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as gunzip from "gunzip-maybe";
import { normalize as normalizePath, sep as pathSeparator } from "path";
import { PassThrough } from "stream";
import { extract, Extract } from "tar-stream";
import { InvalidArchiveError } from "..";
import { getPlatformFromConfig, InvalidArchiveError } from "..";
import { streamToJson } from "../../stream-utils";
import { PluginOptions } from "../../types";
import { extractImageLayer } from "../layer";
Expand Down Expand Up @@ -133,7 +133,9 @@ function getLayersContentAndArchiveManifest(
manifest: OciArchiveManifest;
imageConfig: ImageConfig;
} {
const platform = options?.platform || "linux/amd64";
const platform =
options?.platform ||
(configs.length === 1 ? getPlatformFromConfig(configs[0]) : "linux/amd64");
const platformInfo = getOciPlatformInfoFromOptionString(platform as string);

// get manifest file first
Expand Down
Binary file added test/fixtures/docker-archives/alpine-arm64.tar
Binary file not shown.
13 changes: 13 additions & 0 deletions test/system/platforms/arm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { scan } from "../../../lib/index";
import { execute } from "../../../lib/sub-process";
import { getFixture } from "../../util";

describe("ARM platform tests", () => {
afterAll(async () => {
Expand All @@ -21,6 +22,18 @@ describe("ARM platform tests", () => {

expect(pluginResult).toMatchSnapshot();
});
test("should correctly scan an arm64 image when platform flag is missing", async () => {
const fixturePath = getFixture("docker-archives/alpine-arm64.tar");
const imageNameAndTag = `docker-archive:${fixturePath}`;
try {
const result = await scan({
path: imageNameAndTag,
});
expect(result).toBeDefined();
} catch (error) {
expect(error.message).not.toBe("Invalid OCI Archive");
}
});

it.todo(
"should correctly scan an ARM image when the user provides --platform=arm and return platform: arm",
Expand Down

0 comments on commit af0973a

Please sign in to comment.