diff --git a/docs/SECRETS_MANAGEMENT.md b/docs/SECRETS_MANAGEMENT.md index f757148..7cc4af1 100644 --- a/docs/SECRETS_MANAGEMENT.md +++ b/docs/SECRETS_MANAGEMENT.md @@ -1,33 +1,49 @@ # Secrets Management — BIT-4603 -**Status:** ✅ Consolidated, standardized (v0.1) +**Status:** ✅ Consolidated, standardized (v0.2) **Owner:** BitReport DevOps -**Last Updated:** 2026-04-24 +**Last Updated:** 2026-05-09 ## Overview This document describes the unified secrets management strategy for **ai-news-writer**. All sensitive data (API keys, credentials, tokens) follow a single, standardized approach: 1. **Centralized loading** via `src/config/secrets.js` -2. **Priority-based resolution** (deployed secrets > development .env > environment) +2. **Layered resolution** (later sources override earlier ones — see precedence below) 3. **Single source of truth** across all environments --- ## Architecture -### Load Priority (highest to lowest) +### Load order (later sources override earlier — implementation in `src/config/secrets.js`) + +`SecretsManager.load()` performs three `Object.assign` calls in this exact order, so the **last source to define a key wins**: ``` -1. /etc/ai-news-writer/secrets.env - ↓ (injected by deployment tooling — Gitea Actions, lxc-secrets-inject.sh) -2. .env (local development — not in git) - ↓ (loaded via fs.readFileSync) -3. process.env (already in shell environment) - ↓ (e.g., PM2 env config, Docker env, CI pipeline) -4. Defaults (where safe: timeouts, log levels, ports) +1. process.env + ↓ (baseline — shell exports, PM2 env config, Docker, CI pipeline) +2. /etc/ai-news-writer/secrets.env + ↓ (production injection target — root:root 600, written by tools/deploy/lxc-secrets-inject.sh) +3. .env at the repo root + ↓ (local development override — `.env` is git-ignored) + +Effective precedence (highest to lowest): + .env > /etc/ai-news-writer/secrets.env > process.env ``` +> **Production note.** `.env` must not exist in production deployments. If it does, it +> **silently overrides** `/etc/ai-news-writer/secrets.env`. The intent of putting `.env` +> last is to let a developer override a single key on their workstation without rewriting +> the system file. Deployment tooling (`tools/deploy/lxc-secrets-inject.sh`) writes only +> the system file and never copies a `.env`; the `.gitignore` keeps `.env` out of any +> image built from the repo. + +> **Defaults.** Built-in defaults for non-secret tunables (timeouts, log levels, ports, +> retry caps) live in `src/config/loader.js`, `src/config/listen-defaults.js`, and the +> per-module `process.env.X || ` reads. They are applied only when none of the +> three sources above provide a value. See `.env.example` for the canonical list. + ### Components | Component | Purpose | Location | diff --git a/src/config/secrets.test.js b/src/config/secrets.test.js index 4a28c93..8a7f2a7 100644 --- a/src/config/secrets.test.js +++ b/src/config/secrets.test.js @@ -1,6 +1,8 @@ 'use strict'; const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); const { describe, it, beforeEach, afterEach } = require('node:test'); describe('SecretsManager', () => { @@ -134,4 +136,70 @@ describe('SecretsManager', () => { assert.strictEqual(parsed.CONNECTION_STRING, 'user=admin;pass=secret'); }); }); + + // BIT-4294 / BIT-4602: lock the documented precedence so that the runtime + // implementation in src/config/secrets.js and docs/SECRETS_MANAGEMENT.md + // cannot silently drift. Effective precedence (highest → lowest): + // 1. repo .env (developer override) + // 2. /etc/ai-news-writer/secrets.env (production injection target) + // 3. process.env (shell / PM2 / CI baseline) + describe('load() — precedence (BIT-4294 contract)', () => { + it('repo .env overrides /etc/.../secrets.env which overrides process.env', () => { + // Stub fs reads so the singleton load() executes against in-memory file + // contents rather than the real /etc/ai-news-writer/secrets.env path. + // This proves the merge order in SecretsManager.load() itself, not just + // the parser, so a future refactor of load() can't break the contract. + const SYSTEM_FILE = '/etc/ai-news-writer/secrets.env'; + const REPO_FILE = path.join(__dirname, '../../.env'); + + const fakeFiles = { + [SYSTEM_FILE]: [ + 'PREC_KEY_SHARED=from-system-file', + 'PREC_KEY_SYSTEM_ONLY=system-only-value', + 'PREC_KEY_SHELL_ONLY=system-overrides-shell' + ].join('\n') + '\n', + [REPO_FILE]: [ + 'PREC_KEY_SHARED=from-repo-env', + 'PREC_KEY_REPO_ONLY=repo-only-value', + 'PREC_KEY_SYSTEM_ONLY=repo-overrides-system' + ].join('\n') + '\n' + }; + + const realExistsSync = fs.existsSync; + const realReadFileSync = fs.readFileSync; + fs.existsSync = (p) => Object.prototype.hasOwnProperty.call(fakeFiles, p) || realExistsSync(p); + fs.readFileSync = (p, enc) => + Object.prototype.hasOwnProperty.call(fakeFiles, p) ? fakeFiles[p] : realReadFileSync(p, enc); + + try { + process.env.PREC_KEY_SHARED = 'from-process-env'; + process.env.PREC_KEY_SHELL_ONLY = 'shell-only-value'; + manager._clearCache(); + + const loaded = manager.load(); + + // Repo .env wins for keys defined in all three sources. + assert.strictEqual(loaded.PREC_KEY_SHARED, 'from-repo-env', + '.env should override /etc/ai-news-writer/secrets.env and process.env'); + // System file wins over shell when .env is silent on a key. + assert.strictEqual(loaded.PREC_KEY_SHELL_ONLY, 'system-overrides-shell', + '/etc/ai-news-writer/secrets.env should override process.env'); + // Repo .env wins over system file when shell is silent on a key. + assert.strictEqual(loaded.PREC_KEY_SYSTEM_ONLY, 'repo-overrides-system', + '.env should override /etc/ai-news-writer/secrets.env'); + // Source-only keys are still readable. + assert.strictEqual(loaded.PREC_KEY_REPO_ONLY, 'repo-only-value'); + + // get() honours the same precedence (sanity check on the public API). + assert.strictEqual(manager.get('PREC_KEY_SHARED'), 'from-repo-env'); + assert.strictEqual(manager.get('PREC_KEY_SHELL_ONLY'), 'system-overrides-shell'); + } finally { + fs.existsSync = realExistsSync; + fs.readFileSync = realReadFileSync; + delete process.env.PREC_KEY_SHARED; + delete process.env.PREC_KEY_SHELL_ONLY; + manager._clearCache(); + } + }); + }); });