Let a secret be marked as never expiring
The expiry date is now optional: no date means the secret never expires, gets the status "no expiry" and never raises an alert or reminder. The Secrets form has a "Never expires" checkbox. Certificates checked against a host keep their live expiry and can't be set to never expire. Migration 0016 makes secrets.expiry_date nullable. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
75f0a3d881
commit
3daec601fd
11 files changed
+2069
-36
No files matched your search
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* Secrets that never expire: a secret with no expiry date has no status to warn about, and the API accepts and returns it.
|
||||
* The routes run for real against a scratch database; only the sign-in is stubbed (every request is an admin).
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import type { Server } from "node:http";
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), "hm-secrets-test-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, secrets } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { secretsRouter } = await import("../src/routes/secrets.js");
|
||||
const { collectAlerts } = await import("../src/services/alerts.js");
|
||||
const { computeSecretStatus } = await import("../src/services/secretStatus.js");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
describe("computeSecretStatus", () => {
|
||||
const now = new Date("2026-10-10T12:00:00Z");
|
||||
|
||||
test("a secret without an expiry date never expires", () => {
|
||||
assert.deepEqual(computeSecretStatus(null, 30, now), { status: "never", daysLeft: null });
|
||||
});
|
||||
|
||||
test("dated secrets keep their ok / expiring / expired meaning", () => {
|
||||
assert.deepEqual(computeSecretStatus("2026-12-31", 30, now), { status: "ok", daysLeft: 82 });
|
||||
assert.deepEqual(computeSecretStatus("2026-10-20", 30, now), { status: "expiring", daysLeft: 10 });
|
||||
assert.deepEqual(computeSecretStatus("2026-10-08", 30, now), { status: "expired", daysLeft: -2 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("secrets API", () => {
|
||||
let server: Server;
|
||||
let base = "";
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values({ oidcSub: "tester", name: "Tester", role: "admin" });
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: "tester" } };
|
||||
next();
|
||||
});
|
||||
app.use("/api/secrets", secretsRouter);
|
||||
server = await new Promise<Server>((resolve) => {
|
||||
const s = app.listen(0, "127.0.0.1", () => resolve(s));
|
||||
});
|
||||
base = `http://127.0.0.1:${(server.address() as AddressInfo).port}/api/secrets`;
|
||||
});
|
||||
|
||||
after(() => {
|
||||
server.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits; the folder is in the temp directory anyway.
|
||||
}
|
||||
});
|
||||
|
||||
async function send(method: string, path: string, body?: unknown) {
|
||||
const res = await fetch(`${base}${path}`, {
|
||||
method,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
});
|
||||
return { status: res.status, body: res.status === 204 ? null : await res.json() };
|
||||
}
|
||||
|
||||
test("a secret can be created without an expiry date and is reported as never expiring", async () => {
|
||||
const created = await send("POST", "/", { name: "Break-glass admin", type: "password", expiryDate: null });
|
||||
assert.equal(created.status, 201);
|
||||
assert.equal(created.body.secret.expiryDate, null);
|
||||
assert.equal(created.body.secret.status, "never");
|
||||
assert.equal(created.body.secret.daysLeft, null);
|
||||
});
|
||||
|
||||
test("leaving the date out is still an error — never expiring has to be said explicitly", async () => {
|
||||
const res = await send("POST", "/", { name: "Forgot the date", type: "generic" });
|
||||
assert.equal(res.status, 400);
|
||||
});
|
||||
|
||||
test("a dated secret can be switched to never expire and back", async () => {
|
||||
const created = await send("POST", "/", { name: "API token", type: "api_token", expiryDate: "2030-01-01" });
|
||||
const id = created.body.secret.id;
|
||||
assert.equal(created.body.secret.status, "ok");
|
||||
|
||||
const never = await send("PATCH", `/${id}`, { expiryDate: null });
|
||||
assert.equal(never.status, 200);
|
||||
assert.equal(never.body.secret.expiryDate, null);
|
||||
assert.equal(never.body.secret.status, "never");
|
||||
|
||||
const dated = await send("PATCH", `/${id}`, { expiryDate: "2030-06-01" });
|
||||
assert.equal(dated.body.secret.expiryDate, "2030-06-01");
|
||||
assert.equal(dated.body.secret.status, "ok");
|
||||
});
|
||||
|
||||
test("a certificate checked against a host cannot be set to never expire", async () => {
|
||||
const res = await send("POST", "/", {
|
||||
name: "Monitored cert",
|
||||
type: "ssl_certificate",
|
||||
expiryDate: null,
|
||||
checkHost: "nas.example.lan",
|
||||
});
|
||||
assert.equal(res.status, 400);
|
||||
});
|
||||
|
||||
test("the list puts secrets without a date last, after the dated ones in date order", async () => {
|
||||
await send("POST", "/", { name: "Soonest", type: "generic", expiryDate: "2026-11-01" });
|
||||
const list = await send("GET", "/");
|
||||
const dates = list.body.secrets.map((s: { expiryDate: string | null }) => s.expiryDate);
|
||||
const firstNever = dates.findIndex((d: string | null) => d === null);
|
||||
assert.ok(firstNever > 0);
|
||||
assert.ok(dates.slice(firstNever).every((d: string | null) => d === null), "every undated secret comes after every dated one");
|
||||
const dated = dates.slice(0, firstNever);
|
||||
assert.deepEqual(dated, [...dated].sort());
|
||||
});
|
||||
|
||||
test("a secret that never expires raises no alert, an expired one does", async () => {
|
||||
await db.delete(secrets);
|
||||
await send("POST", "/", { name: "Never", type: "generic", expiryDate: null });
|
||||
await send("POST", "/", { name: "Old", type: "generic", expiryDate: "2020-01-01" });
|
||||
const report = await collectAlerts();
|
||||
const messages = report.alerts.map((a: { message: string }) => a.message).filter((m: string) => m.startsWith("Secret"));
|
||||
assert.equal(messages.length, 1);
|
||||
assert.match(messages[0], /Old/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user