From 3daec601fde674fb4db95ca2b0ed0e63145d9673 Mon Sep 17 00:00:00 2001 From: Bobban Rydh Date: Sun, 11 Oct 2026 01:44:40 +0200 Subject: [PATCH] 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 --- DATABASE.md | 2 +- README.md | 4 +- server/drizzle/0016_great_glorian.sql | 21 + server/drizzle/meta/0016_snapshot.json | 1820 ++++++++++++++++++++++++ server/drizzle/meta/_journal.json | 7 + server/src/db/schema.ts | 2 +- server/src/routes/secrets.ts | 27 +- server/src/services/secretStatus.ts | 23 +- server/test/secrets.test.ts | 139 ++ web/src/api/client.ts | 11 +- web/src/pages/Secrets.tsx | 49 +- 11 files changed, 2069 insertions(+), 36 deletions(-) create mode 100644 server/drizzle/0016_great_glorian.sql create mode 100644 server/drizzle/meta/0016_snapshot.json create mode 100644 server/test/secrets.test.ts diff --git a/DATABASE.md b/DATABASE.md index fdc3aa2..30b9965 100644 --- a/DATABASE.md +++ b/DATABASE.md @@ -361,7 +361,7 @@ tracks *when* something expires; it does not store the secret itself. | `name` | text NOT NULL | | | `type` | text NOT NULL, default `generic` | `api_token` \| `ssl_certificate` \| `password` \| `generic`. | | `description`, `notes` | text | | -| `expiry_date` | text NOT NULL | `YYYY-MM-DD`. For a certificate with a host to check, overwritten from the live certificate. | +| `expiry_date` | text | `YYYY-MM-DD`. NULL = never expires (no status, no alert). For a certificate with a host to check, overwritten from the live certificate, and never NULL. | | `warn_days` | integer NOT NULL, default 30 | How long before expiry to start reminding. | | `check_host`, `check_port` | text / integer | Certificates only: read the expiry from the live certificate at this host:port. | | `last_checked_at`, `last_check_error` | text | Result of the last live check. | diff --git a/README.md b/README.md index e04fba9..5f0767d 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,9 @@ All modules from the original plan are built: real TLS connection (daily, and on demand via "Check now"), reads the certificate's actual expiry, and keeps the date current — so a renewed cert is picked up automatically and an unreachable host is flagged instead of - silently going stale + silently going stale. A secret that doesn't expire (a break-glass password, + a key with no end date) can be ticked "Never expires": it is listed with the + status "no expiry" and never raises an alert or reminder - **IP Addresses (IPAM)** — inventory of IPs across vendors/locations, with "Sync from Tailscale", "Sync from Proxmox", and "Sync from phpIPAM" actions to pull in tailnet device IPs, VM/LXC IPs, and phpIPAM's own diff --git a/server/drizzle/0016_great_glorian.sql b/server/drizzle/0016_great_glorian.sql new file mode 100644 index 0000000..92c9d22 --- /dev/null +++ b/server/drizzle/0016_great_glorian.sql @@ -0,0 +1,21 @@ +PRAGMA foreign_keys=OFF;--> statement-breakpoint +CREATE TABLE `__new_secrets` ( + `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, + `name` text NOT NULL, + `type` text DEFAULT 'generic' NOT NULL, + `description` text, + `expiry_date` text, + `warn_days` integer DEFAULT 30 NOT NULL, + `notes` text, + `check_host` text, + `check_port` integer, + `last_checked_at` text, + `last_check_error` text, + `created_at` text DEFAULT (current_timestamp) NOT NULL, + `updated_at` text DEFAULT (current_timestamp) NOT NULL +); +--> statement-breakpoint +INSERT INTO `__new_secrets`("id", "name", "type", "description", "expiry_date", "warn_days", "notes", "check_host", "check_port", "last_checked_at", "last_check_error", "created_at", "updated_at") SELECT "id", "name", "type", "description", "expiry_date", "warn_days", "notes", "check_host", "check_port", "last_checked_at", "last_check_error", "created_at", "updated_at" FROM `secrets`;--> statement-breakpoint +DROP TABLE `secrets`;--> statement-breakpoint +ALTER TABLE `__new_secrets` RENAME TO `secrets`;--> statement-breakpoint +PRAGMA foreign_keys=ON; \ No newline at end of file diff --git a/server/drizzle/meta/0016_snapshot.json b/server/drizzle/meta/0016_snapshot.json new file mode 100644 index 0000000..636a240 --- /dev/null +++ b/server/drizzle/meta/0016_snapshot.json @@ -0,0 +1,1820 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "46bf4941-716d-4761-b1d1-a4f44bc28d4f", + "prevId": "81ecea99-a8ea-442b-890c-fb52b326c12c", + "tables": { + "alert_tickets": { + "name": "alert_tickets", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "alert_key": { + "name": "alert_key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "seen_count": { + "name": "seen_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 1 + }, + "integration_id": { + "name": "integration_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "ticket_number": { + "name": "ticket_number", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "grouped": { + "name": "grouped", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "severity": { + "name": "severity", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "message": { + "name": "message", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "first_seen_at": { + "name": "first_seen_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "last_seen_at": { + "name": "last_seen_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "ticketed_at": { + "name": "ticketed_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "cleared_at": { + "name": "cleared_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "alert_tickets_key_idx": { + "name": "alert_tickets_key_idx", + "columns": [ + "alert_key" + ], + "isUnique": false + } + }, + "foreignKeys": { + "alert_tickets_integration_id_integrations_id_fk": { + "name": "alert_tickets_integration_id_integrations_id_fk", + "tableFrom": "alert_tickets", + "tableTo": "integrations", + "columnsFrom": [ + "integration_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "audit_log": { + "name": "audit_log", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "actor_user_id": { + "name": "actor_user_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "actor_label": { + "name": "actor_label", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "category": { + "name": "category", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "action": { + "name": "action", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "target_type": { + "name": "target_type", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "target_id": { + "name": "target_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "detail": { + "name": "detail", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "audit_log_actor_user_id_users_id_fk": { + "name": "audit_log_actor_user_id_users_id_fk", + "tableFrom": "audit_log", + "tableTo": "users", + "columnsFrom": [ + "actor_user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "consistency_ignores": { + "name": "consistency_ignores", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "key": { + "name": "key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "reason": { + "name": "reason", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_by": { + "name": "created_by", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "consistency_ignores_key_unique": { + "name": "consistency_ignores_key_unique", + "columns": [ + "key" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "diag_log": { + "name": "diag_log", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "operation": { + "name": "operation", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "ok": { + "name": "ok", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "latency_ms": { + "name": "latency_ms", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "error": { + "name": "error", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "dns_providers": { + "name": "dns_providers", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "provider_type": { + "name": "provider_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "credential_id": { + "name": "credential_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "config": { + "name": "config", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "enabled": { + "name": "enabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "dns_providers_credential_id_integration_credentials_id_fk": { + "name": "dns_providers_credential_id_integration_credentials_id_fk", + "tableFrom": "dns_providers", + "tableTo": "integration_credentials", + "columnsFrom": [ + "credential_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "dns_records_cache": { + "name": "dns_records_cache", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "provider_id": { + "name": "provider_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "zone_id": { + "name": "zone_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "record_id": { + "name": "record_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "content": { + "name": "content", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "ttl": { + "name": "ttl", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "priority": { + "name": "priority", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "proxied": { + "name": "proxied", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": { + "dns_records_cache_provider_id_dns_providers_id_fk": { + "name": "dns_records_cache_provider_id_dns_providers_id_fk", + "tableFrom": "dns_records_cache", + "tableTo": "dns_providers", + "columnsFrom": [ + "provider_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "dns_zones_cache": { + "name": "dns_zones_cache", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "provider_id": { + "name": "provider_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "zone_id": { + "name": "zone_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "zone_name": { + "name": "zone_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "synced_at": { + "name": "synced_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "dns_zones_cache_provider_zone_idx": { + "name": "dns_zones_cache_provider_zone_idx", + "columns": [ + "provider_id", + "zone_id" + ], + "isUnique": true + } + }, + "foreignKeys": { + "dns_zones_cache_provider_id_dns_providers_id_fk": { + "name": "dns_zones_cache_provider_id_dns_providers_id_fk", + "tableFrom": "dns_zones_cache", + "tableTo": "dns_providers", + "columnsFrom": [ + "provider_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "domains": { + "name": "domains", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "origin": { + "name": "origin", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'manual'" + }, + "expires_at": { + "name": "expires_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "registrar": { + "name": "registrar", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "lookup_source": { + "name": "lookup_source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_checked_at": { + "name": "last_checked_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_checked_ok_at": { + "name": "last_checked_ok_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_check_error": { + "name": "last_check_error", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "domains_name_unique": { + "name": "domains_name_unique", + "columns": [ + "name" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "integration_credentials": { + "name": "integration_credentials", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "encrypted_secret": { + "name": "encrypted_secret", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "integrations": { + "name": "integrations", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "base_url": { + "name": "base_url", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "credential_id": { + "name": "credential_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "config": { + "name": "config", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "enabled": { + "name": "enabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "integrations_credential_id_integration_credentials_id_fk": { + "name": "integrations_credential_id_integration_credentials_id_fk", + "tableFrom": "integrations", + "tableTo": "integration_credentials", + "columnsFrom": [ + "credential_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "ipam_entries": { + "name": "ipam_entries", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "ip_address": { + "name": "ip_address", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "label": { + "name": "label", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "vendor": { + "name": "vendor", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "location": { + "name": "location", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "ipam_entries_ip_address_unique": { + "name": "ipam_entries_ip_address_unique", + "columns": [ + "ip_address" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "maintenance_windows": { + "name": "maintenance_windows", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "target_type": { + "name": "target_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "target_id": { + "name": "target_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "reason": { + "name": "reason", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "started_at": { + "name": "started_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "ends_at": { + "name": "ends_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_by": { + "name": "created_by", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "notification_queue": { + "name": "notification_queue", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "message": { + "name": "message", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "port_forwards": { + "name": "port_forwards", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "label": { + "name": "label", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "external_port": { + "name": "external_port", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "protocol": { + "name": "protocol", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'tcp'" + }, + "server_id": { + "name": "server_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "destination": { + "name": "destination", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "internal_port": { + "name": "internal_port", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "comment": { + "name": "comment", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "port_forwards_server_id_servers_id_fk": { + "name": "port_forwards_server_id_servers_id_fk", + "tableFrom": "port_forwards", + "tableTo": "servers", + "columnsFrom": [ + "server_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "scheduled_tasks": { + "name": "scheduled_tasks", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "server_id": { + "name": "server_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "schedule_type": { + "name": "schedule_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "origin": { + "name": "origin", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'agent'" + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "command": { + "name": "command", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "schedule_expression": { + "name": "schedule_expression", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "enabled": { + "name": "enabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": true + }, + "next_run_at": { + "name": "next_run_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "raw_metadata": { + "name": "raw_metadata", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "is_stale": { + "name": "is_stale", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "first_seen_at": { + "name": "first_seen_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "last_seen_at": { + "name": "last_seen_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "scheduled_tasks_server_id_servers_id_fk": { + "name": "scheduled_tasks_server_id_servers_id_fk", + "tableFrom": "scheduled_tasks", + "tableTo": "servers", + "columnsFrom": [ + "server_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "secrets": { + "name": "secrets", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'generic'" + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "expiry_date": { + "name": "expiry_date", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "warn_days": { + "name": "warn_days", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 30 + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "check_host": { + "name": "check_host", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "check_port": { + "name": "check_port", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_checked_at": { + "name": "last_checked_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_check_error": { + "name": "last_check_error", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "server_links": { + "name": "server_links", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "server_id": { + "name": "server_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "label": { + "name": "label", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": { + "server_links_server_id_servers_id_fk": { + "name": "server_links_server_id_servers_id_fk", + "tableFrom": "server_links", + "tableTo": "servers", + "columnsFrom": [ + "server_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "server_ports": { + "name": "server_ports", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "server_id": { + "name": "server_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "port": { + "name": "port", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "protocol": { + "name": "protocol", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'tcp'" + }, + "label": { + "name": "label", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "comment": { + "name": "comment", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "open": { + "name": "open", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "last_seen_open_at": { + "name": "last_seen_open_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "server_ports_unique": { + "name": "server_ports_unique", + "columns": [ + "server_id", + "port", + "protocol" + ], + "isUnique": true + } + }, + "foreignKeys": { + "server_ports_server_id_servers_id_fk": { + "name": "server_ports_server_id_servers_id_fk", + "tableFrom": "server_ports", + "tableTo": "servers", + "columnsFrom": [ + "server_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "servers": { + "name": "servers", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "hostname": { + "name": "hostname", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "os_type": { + "name": "os_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'linux'" + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "api_token_hash": { + "name": "api_token_hash", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "api_token_prefix": { + "name": "api_token_prefix", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "last_seen_at": { + "name": "last_seen_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "ip_addresses": { + "name": "ip_addresses", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "cpu_model": { + "name": "cpu_model", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "cpu_cores": { + "name": "cpu_cores", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "cpu_load_percent": { + "name": "cpu_load_percent", + "type": "real", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "mem_total_bytes": { + "name": "mem_total_bytes", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "mem_used_bytes": { + "name": "mem_used_bytes", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "disks": { + "name": "disks", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "listening_ports": { + "name": "listening_ports", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_port_scan": { + "name": "last_port_scan", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "tags": { + "name": "tags", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "proxmox_integration_id": { + "name": "proxmox_integration_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "proxmox_node": { + "name": "proxmox_node", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "proxmox_guest_type": { + "name": "proxmox_guest_type", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "proxmox_vmid": { + "name": "proxmox_vmid", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "hide_proxmox_link": { + "name": "hide_proxmox_link", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + } + }, + "indexes": {}, + "foreignKeys": { + "servers_proxmox_integration_id_integrations_id_fk": { + "name": "servers_proxmox_integration_id_integrations_id_fk", + "tableFrom": "servers", + "tableTo": "integrations", + "columnsFrom": [ + "proxmox_integration_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "settings": { + "name": "settings", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "tag_definitions": { + "name": "tag_definitions", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "color": { + "name": "color", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + } + }, + "indexes": { + "tag_definitions_name_unique": { + "name": "tag_definitions_name_unique", + "columns": [ + "name" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "users": { + "name": "users", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "oidc_sub": { + "name": "oidc_sub", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'viewer'" + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "(current_timestamp)" + }, + "last_login_at": { + "name": "last_login_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "users_oidc_sub_unique": { + "name": "users_oidc_sub_unique", + "columns": [ + "oidc_sub" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} \ No newline at end of file diff --git a/server/drizzle/meta/_journal.json b/server/drizzle/meta/_journal.json index a024f9c..a3181e9 100644 --- a/server/drizzle/meta/_journal.json +++ b/server/drizzle/meta/_journal.json @@ -113,6 +113,13 @@ "when": 1791407868932, "tag": "0015_yellow_scalphunter", "breakpoints": true + }, + { + "idx": 16, + "version": "6", + "when": 1791667275424, + "tag": "0016_great_glorian", + "breakpoints": true } ] } \ No newline at end of file diff --git a/server/src/db/schema.ts b/server/src/db/schema.ts index 5fdba9e..ecfd9b1 100644 --- a/server/src/db/schema.ts +++ b/server/src/db/schema.ts @@ -94,7 +94,7 @@ export const secrets = sqliteTable("secrets", { name: text("name").notNull(), type: text("type").$type().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. diff --git a/server/src/routes/secrets.ts b/server/src/routes/secrets.ts index 798ee49..bea35b4 100644 --- a/server/src/routes/secrets.ts +++ b/server/src/routes/secrets.ts @@ -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 = { ...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. diff --git a/server/src/services/secretStatus.ts b/server/src/services/secretStatus.ts index 7abf2c9..cbdcd92 100644 --- a/server/src/services/secretStatus.ts +++ b/server/src/services/secretStatus.ts @@ -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 }; } diff --git a/server/test/secrets.test.ts b/server/test/secrets.test.ts new file mode 100644 index 0000000..da2bf32 --- /dev/null +++ b/server/test/secrets.test.ts @@ -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((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/); + }); +}); diff --git a/web/src/api/client.ts b/web/src/api/client.ts index f4df9bb..5c079ff 100644 --- a/web/src/api/client.ts +++ b/web/src/api/client.ts @@ -78,20 +78,22 @@ export interface DiagLogResponse { } export type SecretType = "api_token" | "ssl_certificate" | "password" | "generic"; -export type SecretStatus = "ok" | "expiring" | "expired"; +export type SecretStatus = "ok" | "expiring" | "expired" | "never"; export interface SecretRecord { id: number; name: string; type: SecretType; description: string | null; - expiryDate: string; + /** null = never expires. */ + expiryDate: string | null; warnDays: number; notes: string | null; createdAt: string; updatedAt: string; status: SecretStatus; - daysLeft: number; + /** null when the secret never expires. */ + daysLeft: number | null; /** ssl_certificate only — when set, expiryDate is read from the live certificate here rather than typed in. */ checkHost: string | null; checkPort: number | null; @@ -104,7 +106,8 @@ export interface SecretInput { type: SecretType; description?: string; /** Omitted when a host to check is given — the date then comes from the live certificate. */ - expiryDate?: string; + /** null = never expires. */ + expiryDate?: string | null; warnDays: number; notes?: string; checkHost?: string | null; diff --git a/web/src/pages/Secrets.tsx b/web/src/pages/Secrets.tsx index e3eeabe..a10d24b 100644 --- a/web/src/pages/Secrets.tsx +++ b/web/src/pages/Secrets.tsx @@ -22,6 +22,14 @@ const STATUS_BADGE: Record = { ok: "bg-green-lt text-green", expiring: "bg-yellow-lt text-yellow", expired: "bg-red-lt text-red", + never: "bg-secondary-lt text-secondary", +}; + +const STATUS_LABEL: Record = { + ok: "ok", + expiring: "expiring", + expired: "expired", + never: "no expiry", }; const emptyForm: SecretInput = { @@ -45,6 +53,7 @@ export default function Secrets({ user }: { user: CurrentUser }) { const [editingId, setEditingId] = useState(null); const [form, setForm] = useState(emptyForm); const [saving, setSaving] = useState(false); + const [neverExpires, setNeverExpires] = useState(false); const selection = useSelection(); const [bulkDeleting, setBulkDeleting] = useState(false); const [checkingId, setCheckingId] = useState(null); @@ -76,11 +85,12 @@ export default function Secrets({ user }: { user: CurrentUser }) { function startEdit(s: SecretRecord) { setEditingId(s.id); + setNeverExpires(s.expiryDate === null); setForm({ name: s.name, type: s.type, description: s.description ?? "", - expiryDate: s.expiryDate, + expiryDate: s.expiryDate ?? "", warnDays: s.warnDays, notes: s.notes ?? "", checkHost: s.checkHost ?? "", @@ -90,6 +100,7 @@ export default function Secrets({ user }: { user: CurrentUser }) { function cancelEdit() { setEditingId(null); + setNeverExpires(false); setForm(emptyForm); } @@ -102,7 +113,7 @@ export default function Secrets({ user }: { user: CurrentUser }) { const host = form.type === "ssl_certificate" ? (form.checkHost ?? "").trim() : ""; const payload: SecretInput = { ...form, - expiryDate: host ? undefined : form.expiryDate, + expiryDate: host ? undefined : neverExpires ? null : form.expiryDate, checkHost: host || null, checkPort: host ? form.checkPort || 443 : null, }; @@ -173,10 +184,10 @@ export default function Secrets({ user }: { user: CurrentUser }) { s.name, TYPE_LABELS[s.type], s.description ?? "", - s.expiryDate, + s.expiryDate ?? "never expires", s.warnDays, - s.status, - s.daysLeft, + STATUS_LABEL[s.status], + s.daysLeft ?? "", s.notes ?? "", s.checkHost ? `${s.checkHost}:${s.checkPort ?? 443}` : "", ]), @@ -225,12 +236,24 @@ export default function Secrets({ user }: { user: CurrentUser }) { setForm({ ...form, expiryDate: e.target.value })} /> - {monitoredInForm &&
Read from the live certificate.
} + {monitoredInForm ? ( +
Read from the live certificate.
+ ) : ( + + )} {form.type === "ssl_certificate" && ( <> @@ -264,6 +287,7 @@ export default function Secrets({ user }: { user: CurrentUser }) { type="number" min={0} className="form-control" + disabled={neverExpires && !monitoredInForm} value={form.warnDays} onChange={(e) => setForm({ ...form, warnDays: Number(e.target.value) })} /> @@ -319,6 +343,7 @@ export default function Secrets({ user }: { user: CurrentUser }) { +