Skip to content
Merged
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
47 changes: 28 additions & 19 deletions packages/core/src/config/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ const fileExists = async (filePath: string): Promise<boolean> => {
}
};

const CONFIG_FILE_NAMES = [
"docker-doctor.config.ts",
"docker-doctor.config.js",
"docker-doctor.config.mjs",
"docker-doctor.config.cjs",
"docker-doctor.config.json",
"docker-doctor.config.yaml",
"docker-doctor.config.yml",
];

const findFirstConfigFile = async (
rootDir: string
): Promise<string | undefined> => {
const candidates = CONFIG_FILE_NAMES.map((name) => path.join(rootDir, name));

const exists = await Promise.all(
candidates.map(async (candidate) => await fileExists(candidate))
);

const first = exists.indexOf(true);

return first === -1 ? undefined : candidates[first];
};

const describeError = (cause: unknown): string =>
cause instanceof Error ? cause.message : String(cause);

Expand Down Expand Up @@ -194,26 +218,11 @@ export const loadConfig = async (

configObject = await importConfig(fullPath);
} else {
const candidates = [
"docker-doctor.config.ts",
"docker-doctor.config.js",
"docker-doctor.config.mjs",
"docker-doctor.config.cjs",
"docker-doctor.config.json",
"docker-doctor.config.yaml",
"docker-doctor.config.yml",
];

/* eslint-disable no-await-in-loop */
for (const cand of candidates) {
const fullPath = path.join(rootDir, cand);

if (await fileExists(fullPath)) {
configObject = await importConfig(fullPath);
break;
}
const configPath = await findFirstConfigFile(rootDir);

if (configPath !== undefined) {
configObject = await importConfig(configPath);
}
/* eslint-enable no-await-in-loop */

configObject ??= await readPackageConfig(rootDir);
}
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/rules/compose-mounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import path from "node:path";
import {
composeField,
isComposeMapping,
isComposeSequence,
isComposeString,
} from "../parsers/compose-value";
import { hasText } from "../text";
Expand Down Expand Up @@ -121,8 +120,7 @@ export const volumeMount = (
};
}

// A sequence here is invalid Compose. It has none of these keys.
if (isComposeMapping(volume) || isComposeSequence(volume)) {
if (isComposeMapping(volume)) {
return {
readOnly: isComposeTrue(composeField(volume, "read_only")),
source: stringOrUndefined(composeField(volume, "source")),
Expand Down Expand Up @@ -251,7 +249,7 @@ export const classifyHostPath = (hostPath: string): HostPathScope => {
return normalized.startsWith("~/.") ? "home-dotfile" : "host";
}

if (normalized === "." || normalized.startsWith("./")) {
if (normalized === ".") {
return "project";
}

Expand Down
20 changes: 20 additions & 0 deletions packages/core/test/compose-mounts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, test } from "bun:test";

import type { HostPathScope } from "../src/rules/compose-mounts";
import { classifyHostPath } from "../src/rules/compose-mounts";

describe("classifyHostPath", () => {
const cases: [string, HostPathScope][] = [
[".", "project"],
["./", "project"],
[".//", "project"],
["./src", "project"],
["src/data", "project"],
["./../shared", "parent"],
["../shared", "parent"],
];

test.each(cases)("%s is %s", (hostPath, scope) => {
expect(classifyHostPath(hostPath)).toBe(scope);
});
});
18 changes: 18 additions & 0 deletions packages/core/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ describe("loadConfig", () => {
expect(config).toEqual({});
});

test("reads the first config file in lookup order", async () => {
await fs.writeFile(
path.join(dir, "docker-doctor.config.yaml"),
"rules:\n docker-doctor/no-root-user: info\n"
);
await fs.writeFile(
path.join(dir, "docker-doctor.config.json"),
JSON.stringify({ rules: { "docker-doctor/no-root-user": "error" } })
);
await fs.writeFile(
path.join(dir, "docker-doctor.config.mjs"),
'export default { rules: { "docker-doctor/no-root-user": "warning" } };\n'
);

const config = await loadConfig(dir);
expect(config.rules).toEqual({ "docker-doctor/no-root-user": "warning" });
});

test("warns about unknown rule and category keys", async () => {
await fs.writeFile(
path.join(dir, "docker-doctor.config.json"),
Expand Down
22 changes: 22 additions & 0 deletions packages/core/test/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,28 @@ describe("Compose Security Rules", () => {
);
});

test("compose mount rules skip a volume entry written as a sequence", () => {
const source = `services:
agent:
image: myagent:1.0
volumes:
- [/var/run/docker.sock, /var/run/docker.sock]
- [/, /host]
`;

const composeContent = parseCompose(source, "compose.yml");

expect(
noDockerSocketMount.check(composeContent, "compose.yml")
).toHaveLength(0);
expect(noBroadBindMount.check(composeContent, "compose.yml")).toHaveLength(
0
);
expect(
preferReadOnlyBindMount.check(composeContent, "compose.yml")
).toHaveLength(0);
});

test("prefer-read-only-bind-mount: writable host paths in short and long syntax", () => {
const source = `services:
web:
Expand Down
Loading