Setting ENVIRONMENT_NAME (and optionally ENVIRONMENT_COLOR, a name or hex value) gives the sidebar that colour, adds a name badge next to the title and on the sign-in page, and prefixes the browser tab title, so a dev copy can't be taken for production. Unset, nothing changes. The colour is validated to a plain #rrggbb before it reaches the page's styles. A public GET /api/environment lets the sign-in page use it too. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
109 lines
5.0 KiB
TypeScript
109 lines
5.0 KiB
TypeScript
/** Colours that can be asked for by name; all dark enough to carry white text. Anything else must be a hex colour. */
|
|
export const ENVIRONMENT_COLORS: Record<string, string> = {
|
|
red: "#b91c1c",
|
|
orange: "#c2410c",
|
|
amber: "#b45309",
|
|
green: "#15803d",
|
|
teal: "#0f766e",
|
|
blue: "#1d4ed8",
|
|
purple: "#7e22ce",
|
|
pink: "#be185d",
|
|
gray: "#475569",
|
|
grey: "#475569",
|
|
};
|
|
const DEFAULT_ENVIRONMENT_COLOR = ENVIRONMENT_COLORS.orange;
|
|
|
|
export interface EnvironmentLook {
|
|
/** Shown as a badge and in the browser tab; may be empty when only a colour was set. */
|
|
name: string;
|
|
/** A #rrggbb colour. */
|
|
color: string;
|
|
}
|
|
|
|
/**
|
|
* How this installation says what it is — so a dev copy can't be mistaken for production. Both values come from the environment
|
|
* (ENVIRONMENT_NAME, ENVIRONMENT_COLOR). Neither set means a normal look; a name alone gets a default colour. The colour is only
|
|
* ever a validated #rrggbb value, because it is placed into the page's styles.
|
|
*/
|
|
export function parseEnvironment(nameRaw: string | undefined, colorRaw: string | undefined): { look: EnvironmentLook | null; warning?: string } {
|
|
const name = (nameRaw ?? "").replace(/\p{Cc}/gu, "").trim().slice(0, 24);
|
|
const wanted = (colorRaw ?? "").trim().toLowerCase();
|
|
|
|
let color: string | null = null;
|
|
let warning: string | undefined;
|
|
if (wanted) {
|
|
if (ENVIRONMENT_COLORS[wanted]) color = ENVIRONMENT_COLORS[wanted];
|
|
else if (/^#[0-9a-f]{6}$/.test(wanted)) color = wanted;
|
|
else if (/^#[0-9a-f]{3}$/.test(wanted)) color = `#${wanted[1]}${wanted[1]}${wanted[2]}${wanted[2]}${wanted[3]}${wanted[3]}`;
|
|
else warning = `ENVIRONMENT_COLOR "${colorRaw}" isn't a colour name (${Object.keys(ENVIRONMENT_COLORS).join(", ")}) or a hex value like #b45309 — using the default.`;
|
|
}
|
|
if (!name && !color) return { look: null, warning };
|
|
return { look: { name, color: color ?? DEFAULT_ENVIRONMENT_COLOR }, warning };
|
|
}
|
|
|
|
const environmentLook = parseEnvironment(process.env.ENVIRONMENT_NAME, process.env.ENVIRONMENT_COLOR);
|
|
|
|
export const env = {
|
|
port: Number(process.env.PORT ?? 3000),
|
|
nodeEnv: process.env.NODE_ENV ?? "development",
|
|
appBaseUrl: process.env.APP_BASE_URL ?? "http://localhost:3000",
|
|
sessionSecret: process.env.SESSION_SECRET ?? "dev-insecure-session-secret-change-me",
|
|
sessionDir: process.env.SESSION_DIR ?? "../data/sessions",
|
|
databasePath: process.env.DATABASE_PATH ?? "../data/homelab-manager.sqlite",
|
|
credentialsEncryptionKey: process.env.CREDENTIALS_ENCRYPTION_KEY ?? "",
|
|
authentik: {
|
|
issuerUrl: process.env.AUTHENTIK_ISSUER_URL ?? "",
|
|
clientId: process.env.AUTHENTIK_CLIENT_ID ?? "",
|
|
clientSecret: process.env.AUTHENTIK_CLIENT_SECRET ?? "",
|
|
},
|
|
environment: environmentLook.look,
|
|
environmentWarning: environmentLook.warning,
|
|
get authEnabled() {
|
|
return Boolean(this.authentik.issuerUrl && this.authentik.clientId && this.authentik.clientSecret);
|
|
},
|
|
get credentialsEncryptionEnabled() {
|
|
return this.credentialsEncryptionKey.length === 64;
|
|
},
|
|
};
|
|
|
|
/** What .env.example ships with. Anyone who read the repository knows it. */
|
|
export const SESSION_SECRET_PLACEHOLDER = "change-me-to-a-random-64-char-hex-string";
|
|
|
|
/**
|
|
* Whether the session secret is fit to run production with. Missing is fatal (the app would fall back to a value in its own
|
|
* source code); the placeholder from .env.example or something short is a warning, not a refusal, so an existing install
|
|
* that has been running with one isn't locked out by an upgrade.
|
|
*/
|
|
export function checkSessionSecret(value: string | undefined, nodeEnv: string): { fatal?: string; warning?: string } {
|
|
if (nodeEnv !== "production") return {};
|
|
if (!value) {
|
|
return {
|
|
fatal:
|
|
"SESSION_SECRET is not set. Sessions would be signed with a value that is public (it is in the source code). " +
|
|
"Set it to a long random string, e.g. node -e \"console.log(require('crypto').randomBytes(32).toString('hex'))\" " +
|
|
"(everyone is signed out when it changes).",
|
|
};
|
|
}
|
|
if (value === SESSION_SECRET_PLACEHOLDER) {
|
|
return { warning: "SESSION_SECRET is still the placeholder from .env.example, which is public. Replace it with a random value (everyone is signed out when it changes)." };
|
|
}
|
|
if (value.length < 32) return { warning: "SESSION_SECRET is shorter than 32 characters. Use a long random value." };
|
|
return {};
|
|
}
|
|
|
|
export function warnIfAuthNotConfigured() {
|
|
if (!env.authEnabled) {
|
|
console.warn(
|
|
"AUTHENTIK_ISSUER_URL / AUTHENTIK_CLIENT_ID / AUTHENTIK_CLIENT_SECRET are not fully set. " +
|
|
"The app will boot, but /auth/login and /auth/logout will fail until they are configured.",
|
|
);
|
|
}
|
|
if (!env.credentialsEncryptionEnabled) {
|
|
console.warn(
|
|
"CREDENTIALS_ENCRYPTION_KEY is not set to a 64-character hex string. " +
|
|
"Saving integration credentials (Proxmox/Synology/etc API tokens) will fail until it is configured, and the " +
|
|
"notification channels' credentials (Gotify/ntfy tokens, SMTP password, webhook secret) are stored unencrypted.",
|
|
);
|
|
}
|
|
}
|