docs(secrets): align SECRETS_MANAGEMENT precedence with secrets.js
ai-news-writer-ci / test (pull_request) Failing after 38s
ai-news-writer-ci / test (pull_request) Failing after 38s
PR #15 (commit 0ab7e7e) fixed the merge-order comment inside
src/config/secrets.js but the standalone docs/SECRETS_MANAGEMENT.md
still claimed the inverse precedence (system file > .env > process.env),
which contradicts the runtime where SecretsManager.load() applies
process.env then /etc/ai-news-writer/secrets.env then the repo .env so
the last assignment wins.
Changes:
- Update docs/SECRETS_MANAGEMENT.md to reflect the true precedence
(.env > /etc/ai-news-writer/secrets.env > process.env) plus a
production-safety note explaining why .env must not exist on
deployed hosts and how lxc-secrets-inject.sh enforces that.
- Add a load() precedence test in src/config/secrets.test.js that
stubs fs.existsSync/fs.readFileSync to exercise the singleton end
to end, locking the contract so future refactors fail loudly if the
merge order changes without a docs update.
Closes the documentation drift left over from BIT-4294 PR #15. The
runtime semantics are unchanged.
Paperclip-Agent: David Santos (id 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376)
Paperclip-Issue: BIT-4294
Paperclip-Run: d5bfe2e3-56d0-418f-a8fd-80817aaa40f2
This commit is contained in:
1 parent
9991284d78
commit
50ed210353
2 files changed
+95
-11
No files matched your search
+27
-11
@@ -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 || <default>` 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 |
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user