Skip to content

Commit

Permalink
Merge pull request #602 from snyk/fix/detect-arm64-architecture
Browse files Browse the repository at this point in the history
fix: detect platform from image config file UNIFY-253
  • Loading branch information
stefan-citiriga-snyk authored Sep 6, 2024
2 parents 894b262 + af0973a commit 8a15b8b
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 8a15b8b

Please sign in to comment.