docs(secrets): BIT-4294 — align SECRETS_MANAGEMENT precedence with secrets.js #17

Merged
spark merged 2 commits from task/BIT-4294-secrets-doc-precedence into main 2026-05-09 12:22:29 +01:00
3 changed files with 115 additions and 15 deletions

No files matched your search

+27 -11
View File
@@ -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 |
+20 -4
View File
@@ -1100,9 +1100,9 @@
"license": "MIT"
},
"node_modules/fast-xml-builder": {
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/fast-xml-builder/-/fast-xml-builder-1.1.5.tgz",
"integrity": "sha512-4TJn/8FKLeslLAH3dnohXqE3QSoxkhvaMzepOIZytwJXZO69Bfz0HBdDHzOTOon6G59Zrk6VQ2bEiv1t61rfkA==",
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/fast-xml-builder/-/fast-xml-builder-1.2.0.tgz",
"integrity": "sha512-00aAWieqff+ZJhsXA4g1g7M8k+7AYoMUUHF+/zFb5U6Uv/P0Vl4QZo84/IcufzYalLuEj9928bXN9PbbFzMF0Q==",
"funding": [
{
"type": "github",
@@ -1111,7 +1111,8 @@
],
"license": "MIT",
"dependencies": {
"path-expression-matcher": "^1.1.3"
"path-expression-matcher": "^1.5.0",
"xml-naming": "^0.1.0"
}
},
"node_modules/fast-xml-parser": {
@@ -2793,6 +2794,21 @@
"dev": true,
"license": "ISC"
},
"node_modules/xml-naming": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/xml-naming/-/xml-naming-0.1.0.tgz",
"integrity": "sha512-k8KO9hrMyNk6tUWqUfkTEZbezRRpONVOzUTnc97VnCvyj6Tf9lyUR9EDAIeiVLv56jsMcoXEwjW8Kv5yPY52lw==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/NaturalIntelligence"
}
],
"license": "MIT",
"engines": {
"node": ">=16.0.0"
}
},
"node_modules/xml2js": {
"version": "0.5.0",
"license": "MIT",
+68
View File
@@ -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();
}
});
});
});