Skip to content

Commit

Permalink
Merge pull request #286 from snyk/fix/read-docker-host-env-var
Browse files Browse the repository at this point in the history
Fix/read docker host env var
  • Loading branch information
ivanstanev authored Oct 23, 2020
2 parents d37aa0e + edf6726 commit 701cf5c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 89 deletions.
18 changes: 7 additions & 11 deletions lib/analyzer/image-inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ImageDetails,
} from "./types";

export { detect, getImageArchive, extractImageDetails, pullIfNotLocal };
export { getImageArchive, extractImageDetails, pullIfNotLocal };

const debug = Debug("snyk");

Expand All @@ -23,14 +23,6 @@ async function getInspectResult(
return JSON.parse(info.stdout)[0];
}

async function detect(
targetImage: string,
options?: DockerOptions,
): Promise<DockerInspectOutput> {
const docker = new Docker(options);
return getInspectResult(docker, targetImage);
}

function cleanupCallback(imagePath: string, imageName: string) {
return () => {
const fullImagePath = path.join(imagePath, imageName);
Expand Down Expand Up @@ -60,7 +52,11 @@ async function pullWithDockerBinary(
await docker.save(targetImage, saveLocation);
return (pullAndSaveSuccessful = true);
} catch (err) {
debug(`couldn't pull ${targetImage} using docker binary: ${err}`);
debug(
`couldn't pull ${targetImage} using docker binary: ${JSON.stringify(
err,
)}`,
);

if (
err.stderr &&
Expand Down Expand Up @@ -285,7 +281,7 @@ function isLocalImageSameArchitecture(
// Note: this is using the same flag/input pattern as the new Docker buildx: eg. linux/arm64/v8
platformArchitecture = platformOption.split("/")[1];
} catch (error) {
debug(`Error parsing platform flag: '${error}'`);
debug(`Error parsing platform flag: '${JSON.stringify(error)}'`);
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/analyzer/static-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export async function analyze(
dockerfileAnalysis,
);
} catch (err) {
debug(err);
debug(`Could not detect OS release: ${JSON.stringify(err)}`);
throw new Error("Failed to detect OS release");
}

Expand All @@ -123,7 +123,7 @@ export async function analyze(
aptDistrolessAnalyze(targetImage, distrolessAptFiles),
]);
} catch (err) {
debug(err);
debug(`Could not detect installed OS packages: ${JSON.stringify(err)}`);
throw new Error("Failed to detect installed OS packages");
}

Expand Down
44 changes: 2 additions & 42 deletions lib/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import * as Debug from "debug";
import * as Modem from "docker-modem";
import { createWriteStream } from "fs";
import { platform } from "os";
import { Stream } from "stream";
import * as subProcess from "./sub-process";

Expand Down Expand Up @@ -34,41 +33,6 @@ class Docker {
}
}

private static createOptionsList(options: any) {
const opts: string[] = [];
if (!options) {
return opts;
}
if (options.host) {
opts.push(`--host=${options.host}`);
}
if (options.tlscert) {
opts.push(`--tlscert=${options.tlscert}`);
}
if (options.tlscacert) {
opts.push(`--tlscacert=${options.tlscacert}`);
}
if (options.tlskey) {
opts.push(`--tlskey=${options.tlskey}`);
}
if (options.tlsverify) {
opts.push(`--tlsverify=${options.tlsverify}`);
}
return opts;
}

private optionsList: string[];
private socketPath: string;

constructor(options?: DockerOptions) {
this.optionsList = Docker.createOptionsList(options);
this.socketPath =
options?.socketPath ||
(platform() === "win32"
? "\\\\.\\pipe\\docker_engine"
: "/var/run/docker.sock");
}

public async pull(
registry: string,
repo: string,
Expand Down Expand Up @@ -124,7 +88,7 @@ class Docker {
`Docker.save: targetImage: ${targetImage}, destination: ${destination}`,
);

const modem: Modem = new Modem({ socketPath: this.socketPath });
const modem = new Modem();

return new Promise((resolve, reject) => {
modem.dial(request, (err, stream: Stream) => {
Expand Down Expand Up @@ -153,10 +117,6 @@ class Docker {
}

public async inspectImage(targetImage: string) {
return subProcess.execute("docker", [
...this.optionsList,
"inspect",
targetImage,
]);
return subProcess.execute("docker", ["inspect", targetImage]);
}
}
4 changes: 3 additions & 1 deletion lib/extractor/docker-archive/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export async function extractArchive(
);
} catch (error) {
debug(
`Error getting layers and manifest content from docker archive: '${error}'`,
`Error getting layers and manifest content from docker archive: '${JSON.stringify(
error,
)}'`,
);
reject(new Error("Invalid Docker archive"));
}
Expand Down
4 changes: 3 additions & 1 deletion lib/extractor/oci-archive/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export async function extractArchive(
);
} catch (error) {
debug(
`Error getting layers and manifest content from oci archive: '${error}'`,
`Error getting layers and manifest content from oci archive: '${JSON.stringify(
error,
)}'`,
);
reject(new Error("Invalid OCI archive"));
}
Expand Down
7 changes: 5 additions & 2 deletions lib/inputs/rpm/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ export async function getRpmDbFileContent(
}
return parserResponse.response;
} catch (error) {
debug("An error occurred while analysing RPM packages");
debug(error);
debug(
`An error occurred while analysing RPM packages: ${JSON.stringify(
error,
)}`,
);
return "";
}
}
30 changes: 0 additions & 30 deletions test/lib/analyzer/image-inspector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,6 @@ function rmdirRecursive(customPath: string[]): void {
rmdirRecursive(next);
}

test("image id", async (t) => {
const expectedId =
"sha256:93f518ec2c41722d6c21e55f96cef4dc4c9ba521cab51a757b1d7272b393902f";
const expectedLayers = [
"sha256:93f518ec2c41722d6c21e55f96cef4dc4c9ba521cab51a757b1d7272b3939021",
"sha256:93f518ec2c41722d6c21e55f96cef4dc4c9ba521cab51a757b1d7272b3939022",
"sha256:93f518ec2c41722d6c21e55f96cef4dc4c9ba521cab51a757b1d7272b3939023",
];

const stubbedData = [
{
Id: expectedId,
RootFS: {
Layers: expectedLayers,
},
MoreStuff: "stuff",
},
];

const execStub = sinon.stub(subProcess, "execute");
execStub
.withArgs("docker", ["inspect", "alpine:2.6"])
.resolves({ stdout: JSON.stringify(stubbedData), stderr: "" });
t.teardown(() => execStub.restore());

const imageData = await imageInspector.detect("alpine:2.6");
t.same(imageData.Id, expectedId, "id as expected");
t.same(imageData.RootFS.Layers, expectedLayers, "layers as expected");
});

test("extract image details", async (t) => {
const tests = {
"hello-world": {
Expand Down

0 comments on commit 701cf5c

Please sign in to comment.