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
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user