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:
bobbanandClaude Sonnet 5.5 committed 2026-10-11 01:44:40 +02:00
1 parent 75f0a3d881
commit 3daec601fd
11 files changed
+2069 -36

No files matched your search

+1 -1
View File
@@ -94,7 +94,7 @@ export const secrets = sqliteTable("secrets", {
name: text("name").notNull(),
type: text("type").$type<SecretType>().notNull().default("generic"),
description: text("description"),
expiryDate: text("expiry_date").notNull(), // ISO date, e.g. 2026-03-01
expiryDate: text("expiry_date"), // ISO date, e.g. 2026-03-01. NULL = never expires (no status, no reminders).
warnDays: integer("warn_days").notNull().default(30),
notes: text("notes"),
// ssl_certificate secrets only: when set, expiryDate is read from the live certificate on this host:port instead of typed in.
+20 -7
View File
@@ -1,5 +1,5 @@
import { Router } from "express";
import { eq } from "drizzle-orm";
import { eq, sql } from "drizzle-orm";
import { z } from "zod";
import { db } from "../db/client.js";
import { secrets, secretTypes } from "../db/schema.js";
@@ -14,7 +14,7 @@ export const secretsRouter = Router();
secretsRouter.use(requireAuth);
secretsRouter.get("/", asyncHandler(async (_req, res) => {
const rows = await db.select().from(secrets).orderBy(secrets.expiryDate);
const rows = await db.select().from(secrets).orderBy(sql`${secrets.expiryDate} IS NULL`, secrets.expiryDate);
res.json({
secrets: rows.map((s) => ({ ...s, ...computeSecretStatus(s.expiryDate, s.warnDays) })),
});
@@ -24,8 +24,8 @@ const secretInput = z.object({
name: z.string().min(1).max(200),
type: z.enum(secretTypes),
description: z.string().max(2000).optional(),
// Optional only because a monitored certificate gets its date from the live cert; validated below.
expiryDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Expected YYYY-MM-DD").optional(),
// Optional only because a monitored certificate gets its date from the live cert; validated below. null means the secret never expires.
expiryDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Expected YYYY-MM-DD").nullable().optional(),
warnDays: z.number().int().min(0).max(3650).default(30),
notes: z.string().max(4000).optional(),
checkHost: z.string().min(1).max(253).regex(/^[A-Za-z0-9._:-]+$/, "Invalid host").nullable().optional(),
@@ -43,7 +43,11 @@ secretsRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) =
return res.status(400).json({ error: "invalid_body", message: "Only SSL certificates can be checked against a host." });
}
let expiryDate = manualExpiry;
if (checkHost && manualExpiry === null) {
return res.status(400).json({ error: "invalid_body", message: "A certificate that is checked against a host has the expiry of the live certificate; it can't be set to never expire." });
}
let expiryDate: string | null | undefined = manualExpiry;
let lastCheckedAt: string | null = null;
let lastCheckError: string | null = null;
if (checkHost) {
@@ -60,8 +64,8 @@ secretsRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) =
}
}
}
if (!expiryDate) {
return res.status(400).json({ error: "invalid_body", message: "An expiry date is required unless a host to check is given." });
if (expiryDate === undefined) {
return res.status(400).json({ error: "invalid_body", message: "An expiry date is required unless the secret never expires or a host to check is given." });
}
const [created] = await db
@@ -101,6 +105,9 @@ secretsRouter.patch("/:id", requireRole("operator"), asyncHandler(async (req, re
// Changing a monitored secret to a non-certificate type quietly ends the monitoring rather than leaving a stale check behind.
const nextHost = nextType !== "ssl_certificate" ? null : checkHost !== undefined ? checkHost : existing.checkHost;
const nextPort = checkPort !== undefined ? checkPort : existing.checkPort;
if (nextHost && rest.expiryDate === null) {
return res.status(400).json({ error: "invalid_body", message: "A certificate that is checked against a host has the expiry of the live certificate; it can't be set to never expire." });
}
const now = new Date().toISOString();
const changes: Partial<typeof secrets.$inferInsert> = { ...rest, updatedAt: now };
@@ -117,6 +124,12 @@ secretsRouter.patch("/:id", requireRole("operator"), asyncHandler(async (req, re
} catch (err) {
// Keep whatever date we already have (a manually supplied one, else the existing one) and record why the check failed.
changes.lastCheckError = err instanceof Error ? err.message : String(err);
if (!changes.expiryDate && !existing.expiryDate) {
return res.status(400).json({
error: "tls_check_failed",
message: `Couldn't read the certificate from ${nextHost}:${port} (${changes.lastCheckError}), and this secret has no expiry date to fall back on. Fix the host, or enter an expiry date first.`,
});
}
}
} else {
// Same host as before: the live certificate stays the source of truth, so ignore a hand-typed date.
+13 -10
View File
@@ -1,19 +1,22 @@
export type SecretStatus = "ok" | "expiring" | "expired";
export type SecretStatus = "ok" | "expiring" | "expired" | "never";
/** Computes status + days-left for a secret, matching Sloth Manager's original semantics. */
export type SecretStatusResult =
| { status: "ok" | "expiring" | "expired"; daysLeft: number }
| { status: "never"; daysLeft: null };
/** Computes status + days-left for a secret, matching Sloth Manager's original semantics. A secret with no expiry date never expires. */
export function computeSecretStatus(
expiryDate: string,
expiryDate: string | null,
warnDays: number,
now: Date = new Date(),
): { status: SecretStatus; daysLeft: number } {
): SecretStatusResult {
if (!expiryDate) return { status: "never", daysLeft: null };
const expiry = new Date(`${expiryDate}T00:00:00Z`);
const today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
const daysLeft = Math.round((expiry.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
let status: SecretStatus;
if (daysLeft < 0) status = "expired";
else if (daysLeft <= warnDays) status = "expiring";
else status = "ok";
return { status, daysLeft };
if (daysLeft < 0) return { status: "expired", daysLeft };
if (daysLeft <= warnDays) return { status: "expiring", daysLeft };
return { status: "ok", daysLeft };
}