Skip to content

feat: loading secrets not from toml but from env files#261

Merged
HardMax71 merged 3 commits intomainfrom
fix/more-issues
Mar 3, 2026
Merged

feat: loading secrets not from toml but from env files#261
HardMax71 merged 3 commits intomainfrom
fix/more-issues

Conversation

@HardMax71
Copy link
Owner

@HardMax71 HardMax71 commented Mar 3, 2026


Summary by cubic

Switched secrets from secrets.toml to environment variables with dev defaults (including a safe fallback for SECRET_KEY). This streamlines local dev, CI, and production by passing secrets via env vars and Docker Compose.

  • Refactors

    • Settings reads SECRET_KEY, MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD from env vars (with dev defaults), builds MONGODB_URL, and drops secrets_path.
    • Removed secrets.example.toml and all secrets.toml mounts/copy steps in Compose and workflows (e2e-ready, docs, stack-tests); added a shared x-backend-secrets env anchor in Compose.
    • Release deploy maps JWT_SECRET_KEY to SECRET_KEY and exports all secrets to the remote host.
    • Docs updated (README, getting-started, operations, configuration, AGENTS) to reflect env-based secrets.
  • Migration

    • Stop using backend/secrets.toml; remove any copies/mounts.
    • In production, set env vars: SECRET_KEY, MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD.
    • Ensure CI/CD provides these vars; release workflow already maps and exports the required secrets.

Written for commit ded545c. Summary will update on new commits.

Summary by CodeRabbit

  • Documentation

    • Updated guides and reference docs to describe environment-variable-based secrets (with dev defaults) and removed instructions to copy a secrets template.
  • Chores

    • Switched secrets handling from file-based to env vars across deployments, CI, and local dev.
    • Centralized secret provisioning in compose setup and removed per-service secret file mounts.

Copilot AI review requested due to automatic review settings March 3, 2026 21:05
@coderabbitai
Copy link

coderabbitai bot commented Mar 3, 2026

📝 Walkthrough

Walkthrough

This PR switches secrets provisioning from a file-based approach (backend/secrets.toml) to environment-variable-based secrets (SECRET_KEY, MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD), removing secrets file usage across CI workflows, docker-compose, backend settings, and documentation.

Changes

Cohort / File(s) Summary
GitHub Actions & CI
\.github/actions/e2e-ready/action.yml, \.github/workflows/docs.yml, \.github/workflows/release-deploy.yml, \.github/workflows/stack-tests.yml
Removed steps that copied backend/secrets.example.toml/secrets.toml; release-deploy.yml now propagates SECRET_KEY through the SSH/deploy env.
Backend Settings & Config
backend/app/settings.py, backend/config.toml, backend/config.test.toml, backend/secrets.example.toml
Settings.init signature changed (removed secrets_path); secrets now read from environment variables with dev defaults; updated TOML files to document env-var secrets; example secrets file deleted.
Docker Compose
docker-compose.yaml
Added x-backend-secrets YAML anchor (SECRET_KEY, MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD) and merged it into multiple services; removed secrets.toml volume mounts and per-service secret entries.
Documentation
AGENTS.md, README.md, docs/getting-started.md, docs/operations/cicd.md, docs/operations/deployment.md, docs/reference/configuration.md
Replaced secrets.toml-oriented guidance with environment-variable-based secret instructions and dev defaults; removed CI copy steps and updated troubleshooting/docs accordingly.

Sequence Diagram(s)

sequenceDiagram
  participant Dev/CI
  participant GitHub_Actions
  participant Docker_Compose
  participant Backend_App
  participant MongoDB

  Dev/CI->>GitHub_Actions: Provide env vars (SECRET_KEY, MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD)
  GitHub_Actions->>Docker_Compose: Start services with merged env (x-backend-secrets)
  Docker_Compose->>Backend_App: Process environment on start
  Backend_App->>Backend_App: Settings.load() reads env vars (no secrets.toml)
  Backend_App->>MongoDB: Connect using MONGO_USER/MONGO_PASSWORD -> authenticate
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 I hopped from TOML to env with glee,
No secret files beneath the tree,
Keys in the air, dev defaults in sight,
Services start up snug and bright,
A little rabbit cheers: secrets set, all right! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: switching secret management from TOML files to environment variables, which is the core transformation across all modified files.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/more-issues

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 15 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="AGENTS.md">

<violation number="1" location="AGENTS.md:451">
P2: This added env-var guidance conflicts with adjacent section text that still says environment variables must never be used, leaving contradictory setup instructions.</violation>
</file>

<file name="backend/app/settings.py">

<violation number="1" location="backend/app/settings.py:42">
P0: Do not use a hardcoded fallback for `SECRET_KEY`; require it from environment/config so JWT signing cannot default to a known value.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
AGENTS.md (1)

438-452: ⚠️ Potential issue | 🟡 Minor

Fix the section title to match the new guidance.

Line 438 says “No Env Vars”, but Line 451 documents env vars for secrets. This contradiction will confuse contributors.

✏️ Proposed doc fix
-## Backend: Settings (TOML — No Env Vars)
+## Backend: Settings (TOML + Env Vars for Secrets)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@AGENTS.md` around lines 438 - 452, The section title "Backend: Settings (TOML
— No Env Vars)" is contradictory with the later lines that allow environment
variables for secrets; update the heading to reflect that env vars are allowed
for secrets (for example "Backend: Settings (TOML — env vars allowed for
secrets)") so it aligns with the examples referencing DATABASE_URL/DATABASE_URL
usage and the documented environment variables SECRET_KEY, MONGO_USER,
MONGO_PASSWORD, REDIS_PASSWORD and the DI example `settings:
FromDishka[Settings]`.
backend/app/settings.py (1)

31-47: ⚠️ Potential issue | 🟠 Major

Reintroduce TOML-based secret loading in Settings initialization.

This constructor now reads secrets directly from environment variables and defaults, which breaks the current settings contract and makes behavior depend on ambient process env.

Proposed direction
 def __init__(
     self,
     config_path: str = "config.toml",
     override_path: str | None = None,
+    secrets_path: str = "secrets.toml",
 ) -> None:
     with open(config_path, "rb") as f:
         data = tomllib.load(f)
     if override_path:
         with open(override_path, "rb") as f:
             data |= tomllib.load(f)
-    for key, default in (
-        ("SECRET_KEY", "CHANGE_ME_min_32_chars_long_!!!!"),
-        ("MONGO_USER", "root"),
-        ("MONGO_PASSWORD", "rootpassword"),
-        ("REDIS_PASSWORD", "redispassword"),
-    ):
-        data[key] = os.environ.get(key) or data.get(key) or default
+    with open(secrets_path, "rb") as f:
+        data |= tomllib.load(f)

Based on learnings: "Settings must load from TOML files (config.toml, secrets.toml, config..toml) via Settings(config_path=...) or Settings(override_path=...); never use environment variables directly; use model_config = ConfigDict(extra='forbid') to catch unknown keys as errors".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/app/settings.py` around lines 31 - 47, The Settings.__init__
currently pulls values from os.environ and defaults, violating the requirement
to load configuration strictly from TOML files; change Settings.__init__ to only
read config_path and override_path TOML data (including loading secrets.toml and
config.<worker>.toml variants when appropriate) and merge them deterministically
without consulting os.environ or runtime env vars, and remove the final
os.environ.get(...) logic that populates SECRET_KEY, MONGO_USER, MONGO_PASSWORD,
REDIS_PASSWORD; also enforce strict schema validation by setting model_config =
ConfigDict(extra='forbid') on the Settings model so unknown keys in the TOML
cause errors.
🧹 Nitpick comments (1)
backend/app/settings.py (1)

14-27: Use Google-style sections in the Settings docstring.

Please convert this class docstring to include explicit Args, Returns, and Raises sections.

As per coding guidelines: "Use Google-style docstrings with Args/Returns/Raises sections".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/app/settings.py` around lines 14 - 27, Update the Settings class
docstring to Google-style sections: replace the current narrative block with a
short summary line followed by "Args:" documenting config_path (str, path to
base TOML), override_path (str, optional per-worker overrides), and noting env
var secrets (SECRET_KEY, MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD); add a
"Returns:" section describing that it constructs and returns a Settings instance
(or the loaded configuration object); and add a "Raises:" section listing
possible exceptions (e.g., FileNotFoundError for missing config files,
TOMLDecodeError for parse errors, or ValueError for invalid/missing required env
vars). Ensure the docstring mentions the load order (config_path, override_path,
then env vars) and keep it inside the Settings class docstring.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@docker-compose.yaml`:
- Around line 2-5: The compose file uses fallback defaults for sensitive env
vars (SECRET_KEY, MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD) which allows
fail‑open predictable credentials; change the parameter expansion from the
permissive ":-" form to the strict ":?" form so Docker Compose will fail fast
when any of these environment variables are unset or empty, enforcing explicit
secret configuration instead of using the provided defaults.

---

Outside diff comments:
In `@AGENTS.md`:
- Around line 438-452: The section title "Backend: Settings (TOML — No Env
Vars)" is contradictory with the later lines that allow environment variables
for secrets; update the heading to reflect that env vars are allowed for secrets
(for example "Backend: Settings (TOML — env vars allowed for secrets)") so it
aligns with the examples referencing DATABASE_URL/DATABASE_URL usage and the
documented environment variables SECRET_KEY, MONGO_USER, MONGO_PASSWORD,
REDIS_PASSWORD and the DI example `settings: FromDishka[Settings]`.

In `@backend/app/settings.py`:
- Around line 31-47: The Settings.__init__ currently pulls values from
os.environ and defaults, violating the requirement to load configuration
strictly from TOML files; change Settings.__init__ to only read config_path and
override_path TOML data (including loading secrets.toml and config.<worker>.toml
variants when appropriate) and merge them deterministically without consulting
os.environ or runtime env vars, and remove the final os.environ.get(...) logic
that populates SECRET_KEY, MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD; also
enforce strict schema validation by setting model_config =
ConfigDict(extra='forbid') on the Settings model so unknown keys in the TOML
cause errors.

---

Nitpick comments:
In `@backend/app/settings.py`:
- Around line 14-27: Update the Settings class docstring to Google-style
sections: replace the current narrative block with a short summary line followed
by "Args:" documenting config_path (str, path to base TOML), override_path (str,
optional per-worker overrides), and noting env var secrets (SECRET_KEY,
MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD); add a "Returns:" section describing
that it constructs and returns a Settings instance (or the loaded configuration
object); and add a "Raises:" section listing possible exceptions (e.g.,
FileNotFoundError for missing config files, TOMLDecodeError for parse errors, or
ValueError for invalid/missing required env vars). Ensure the docstring mentions
the load order (config_path, override_path, then env vars) and keep it inside
the Settings class docstring.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 00100f4 and 8b4582e.

📒 Files selected for processing (15)
  • .github/actions/e2e-ready/action.yml
  • .github/workflows/docs.yml
  • .github/workflows/release-deploy.yml
  • .github/workflows/stack-tests.yml
  • AGENTS.md
  • README.md
  • backend/app/settings.py
  • backend/config.test.toml
  • backend/config.toml
  • backend/secrets.example.toml
  • docker-compose.yaml
  • docs/getting-started.md
  • docs/operations/cicd.md
  • docs/operations/deployment.md
  • docs/reference/configuration.md
💤 Files with no reviewable changes (3)
  • .github/workflows/docs.yml
  • .github/workflows/stack-tests.yml
  • backend/secrets.example.toml

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="backend/app/settings.py">

<violation number="1" location="backend/app/settings.py:47">
P1: `SECRET_KEY` is only set when the environment variable is present. Unlike `MONGO_USER`, `MONGO_PASSWORD`, and `REDIS_PASSWORD` which retain dev defaults via the `or default` fallback, `SECRET_KEY` has no fallback at all. If the env var is unset and not present in TOML config, `data["SECRET_KEY"]` will be missing, likely causing a `KeyError` or broken auth at runtime. Consider adding a dev default or raising an explicit error when the key is missing.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
AGENTS.md (1)

438-452: Rename this section header to match the new behavior.

The title says “No Env Vars,” but the content now correctly uses env vars for secrets via Settings. Please rename the heading (for example, “TOML + Env Secrets via Settings”) to avoid policy confusion for contributors.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@AGENTS.md` around lines 438 - 452, The section header "Backend: Settings
(TOML — No Env Vars)" is now misleading because the content documents using
environment variables for secrets via the Settings object (see Settings and the
listed SECRET_KEY, MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD); update the
header to reflect the new behavior (e.g., "Backend: Settings (TOML + Env Secrets
via Settings)" or similar) so it aligns with the text referencing Settings and
env var secrets handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@AGENTS.md`:
- Around line 438-452: The section header "Backend: Settings (TOML — No Env
Vars)" is now misleading because the content documents using environment
variables for secrets via the Settings object (see Settings and the listed
SECRET_KEY, MONGO_USER, MONGO_PASSWORD, REDIS_PASSWORD); update the header to
reflect the new behavior (e.g., "Backend: Settings (TOML + Env Secrets via
Settings)" or similar) so it aligns with the text referencing Settings and env
var secrets handling.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b4582e and 41a926e.

📒 Files selected for processing (2)
  • AGENTS.md
  • backend/app/settings.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • backend/app/settings.py

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 3, 2026

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="backend/app/settings.py">

<violation number="1" location="backend/app/settings.py:42">
P0: Security: hardcoded default for `SECRET_KEY` means production can silently start with a well-known JWT secret if the env var is missing. The previous code would fail to start (fail-fast), which is far safer. Consider keeping a fail-fast behavior for `SECRET_KEY` in non-dev environments, or at minimum emitting a loud warning at startup when the default is used.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@HardMax71 HardMax71 merged commit 1205cf4 into main Mar 3, 2026
18 checks passed
@HardMax71 HardMax71 deleted the fix/more-issues branch March 3, 2026 21:25
@codecov-commenter
Copy link

codecov-commenter commented Mar 3, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Flag Coverage Δ
backend-e2e 83.22% <100.00%> (-0.01%) ⬇️
backend-unit 67.91% <100.00%> (-0.01%) ⬇️
frontend-unit 86.86% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
backend/app/settings.py 98.18% <100.00%> (-0.02%) ⬇️
Components Coverage Δ
Backend 90.07% <100.00%> (-0.01%) ⬇️
Frontend 86.86% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants