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
6.6 KiB
Secrets Management — BIT-4603
Status: ✅ Consolidated, standardized (v0.2)
Owner: BitReport DevOps
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:
- Centralized loading via
src/config/secrets.js - Layered resolution (later sources override earlier ones — see precedence below)
- Single source of truth across all environments
Architecture
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. 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.
.envmust not exist in production deployments. If it does, it silently overrides/etc/ai-news-writer/secrets.env. The intent of putting.envlast 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.gitignorekeeps.envout 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-moduleprocess.env.X || <default>reads. They are applied only when none of the three sources above provide a value. See.env.examplefor the canonical list.
Components
| Component | Purpose | Location |
|---|---|---|
| SecretsManager | Centralized loader, parser, validator | src/config/secrets.js |
| Injection Script | Deploy-time secret injection (Gitea → LXC) | tools/deploy/lxc-secrets-inject.sh |
| Config Loader | Application config (env-dependent settings) | src/config/loader.js |
| HTTP Auth | UI token validation | src/http/ui-auth.js |
Usage
In Application Code
Centralized (PREFERRED):
const secrets = require('./config/secrets');
const openaiApiKey = secrets.get('OPENAI_API_KEY'); // throws if missing
const wpPassword = secrets.get('WP_PASSWORD', { required: false }); // optional
const defaultValue = secrets.get('OPTIONAL_TOKEN', {
required: false,
default: 'fallback'
});
// Check if a secret exists
if (secrets.has('TWITTER_BEARER_TOKEN')) {
// Twitter integration enabled
}
Deployment Flow
Local Development
-
Create
.envin repo root (not committed):cp .env.example .env # Edit .env with your local keys -
Run application:
npm start # or npm run dev
SecretsManager loads .env automatically.
Production Deployment (LXC Container)
-
Prepare Gitea Repository Secrets (one-time setup):
# Via Gitea UI: Settings → Actions → Secrets (repository secrets) # Prefer BIT-4619 names (Gitea) — tools/deploy/lxc-secrets-inject.sh maps to runtime keys: # WORDPRESS_USER → WP_USER, WORDPRESS_PASSWORD → WP_PASSWORD, APP_ADMIN_SECRET → ADMIN_TOKEN # Also add: OPENAI_API_KEY, ANTHROPIC_API_KEY (optional), TWITTER_BEARER_TOKEN, TELEGRAM_BOT_TOKEN, # CODECOV_TOKEN (optional, for CI Codecov upload). # Equivalent Gitea names for WordPress/admin if you prefer: WP_USER, WP_PASSWORD, ADMIN_TOKEN -
Inject at Deploy Time:
# In CI/deployment script or manually: export GITEA_API_TOKEN="ghp_xxxxxxxxxxxx" ./tools/deploy/lxc-secrets-inject.sh \ --lxc-id 223 \ --secrets OPENAI_API_KEY,WP_USER,WP_PASSWORD,TWITTER_BEARER_TOKEN # Results in: /etc/ai-news-writer/secrets.env (root:root, 600) -
Start Application:
# In container: npm install pm2 start ecosystem.config.cjs
SecretsManager loads /etc/ai-news-writer/secrets.env with highest priority.
Secrets Reference
Core API Keys
| Secret | Required | Description |
|---|---|---|
OPENAI_API_KEY |
Yes (if using OpenAI) | OpenAI API key |
ANTHROPIC_API_KEY |
No (if using Anthropic) | Anthropic API key |
WordPress
| Secret | Required | Description |
|---|---|---|
WP_URL |
Yes | WordPress site URL |
WP_USER |
Yes | WordPress username |
WP_PASSWORD |
Yes | WordPress password |
Social / External APIs
| Secret | Required | Description |
|---|---|---|
TWITTER_BEARER_TOKEN |
No | X (Twitter) API token |
TELEGRAM_BOT_TOKEN |
No | Telegram bot token |
Application
| Secret | Required | Description |
|---|---|---|
ADMIN_TOKEN |
Conditional | UI admin access token |
NODE_ENV |
No | Node.js environment (default: production) |
Security Considerations
✅ Best Practices
- Never commit secrets to git (
.envis in.gitignore) - Use
/etc/ai-news-writer/secrets.envin production (file permissions:600, owner:root:root) - Rotate secrets regularly via Gitea → re-inject → restart app
- Audit secret access (logs should not contain secret values)
- Use strong ADMIN_TOKEN (random 32+ character string)
⚠️ Anti-Patterns (AVOID)
- ❌ Embedding secrets in code or configs
- ❌ Committing
.envto git - ❌ Storing secrets in Docker image layers
- ❌ Logging secret values (even with masking)
- ❌ Using weak ADMIN_TOKEN (e.g., "admin", "123456")
Troubleshooting
"Missing required secret: OPENAI_API_KEY"
Cause: OPENAI_API_KEY not set in any source (process.env, .env, /etc/ai-news-writer/secrets.env)
Fix:
- Local development:
echo 'OPENAI_API_KEY=sk-...' >> .env - Production: Re-run
lxc-secrets-inject.shand restart app
Roadmap
| Phase | Status | Description |
|---|---|---|
| 1 | ✅ Done | Audit current mechanisms (env vars, .env files) |
| 2 | ✅ Done | Centralized loader module + documentation |
| 3 | ✅ Done | LXC injection refinement |
| 4 | ⏳ Future | Vault integration (if needed for multi-region) |
| 5 | ⏳ Future | Automated secret rotation (CI integration) |