Skip to content

fix: make namespace dry runs read-only - #343

Merged
uniqueg merged 2 commits into
devfrom
fix/339-dry-run-read-only
Nov 3, 2026
Merged

uniqueg merged 2 commits into
devfrom
fix/339-dry-run-read-only

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Closes #339

Summary

Finding L3 of the 1.0.0rc2 pre-release review: python -m cloud_registry.namespace_subjects <namespace> --dry-run called create_app(), which creates the configured indexes, replaces the configured rules and changes the TTL index of invitations. Run with a newer image against a database that an older version still uses, the dry run left pods of that version unable to restart, and owners on them could get 403 Forbidden.

A dry run now reads the config (create_foca(), split from create_app()) and registers the collections with FOCA's register_mongodb(), without their indexes, and with the collection of the stored rules. It needs no write access at all.

The docs say what a dry run no longer predicts.

Verification

  • TDD: test_main_dry_run_read_only failed first. It sets up the indexes and rules of 1.0.0rc1, and compares all indexes and documents before and after the dry run.
  • Full suite before the review's fixes: mongomock 1953 passed, 11 skipped, 100% coverage; MongoDB 7.0.34 1964 passed.
  • After the fixes: tests/test_namespace_subjects.py and tests/test_issuers.py on both (32 passed each), namespace_subjects.py at 100%; CI runs the full suite.
  • pre-commit run --all-files and mkdocs build --strict: pass.
  • The adversarial reviewer ran the dry run on real MongoDB 7.0.34 with --auth, as a user with only the read role: exit 0 with correct counts. The same user fails on index creation without --dry-run.

Declined review findings

  • Dry run's counts differ from the real run (medium, adversarial): configured rules are counted as stored, not from the config. Documented in the module docs, --help and the upgrade guide instead of computing them from security.access_control.policies, as the migration leaves configured rules alone and only warns about them.
  • Services without a creator aren't counted (low, adversarial): only stored data from versions before the backfill is affected. Documented.
  • Exit 0 doesn't mean the migration can start (low, adversarial): a dry run counts, it doesn't validate startup. Documented. Moving the startup checks into a shared helper is more than this fix needs.
  • No direct test of create_foca() (low, quality): covered by every test that creates the app.

🤖 Generated with Claude Code

https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG

Summary by Sourcery

Make namespace migration dry runs safely inspect stored data without performing application startup changes.

Bug Fixes:

  • Make namespace-subject dry runs read-only so they no longer modify database indexes, rules, or other startup-managed state.

Enhancements:

  • Separate configuration loading from application startup and register collections without indexes for dry-run operations.
  • Document the data and validation limitations of dry-run counts.

Documentation:

  • Clarify dry-run behavior, limitations, and version requirements in the upgrade documentation.

Tests:

  • Add coverage proving dry runs preserve existing indexes and documents from older deployments.

--dry-run called create_app(), which creates the configured indexes,
replaces the configured rules and changes the TTL index of invitations.
With a newer image, that left pods of the older version unable to
restart. A dry run now only reads the config and registers the
collections, without indexes; the docs say what it doesn't predict.

Closes #339

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
@uniqueg uniqueg added this to the v1.0.0-rc2 milestone Nov 3, 2026
@uniqueg uniqueg added the type: bug Something isn't working label Nov 3, 2026
@uniqueg
uniqueg marked this pull request as ready for review November 3, 2026 11:18
@sourcery-ai

sourcery-ai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR makes namespace-subject dry runs genuinely read-only by separating FOCA config loading from startup, registering collections without index creation or rule replacement, documenting the resulting count and validation limitations, and testing preservation of legacy indexes and documents.

Sequence diagram for the read-only namespace-subject dry run

sequenceDiagram
    participant CLI as namespace_subjects CLI
    participant RO as _read_only_app
    participant FOCA as create_foca
    participant Mongo as register_mongodb
    participant DB as MongoDB
    participant Migration as namespace_subjects

    CLI->>RO: _read_only_app()
    RO->>FOCA: create_foca()
    FOCA-->>RO: validated config
    RO->>Mongo: register_mongodb(mongo_config)
    Mongo->>DB: register collections without indexes
    Mongo-->>RO: Flask app
    CLI->>Migration: namespace_subjects(namespace, dry_run=True)
    Migration->>DB: read stored subjects and rules
    DB-->>Migration: counts
    Migration-->>CLI: exit 0
Loading

Sequence diagram for the namespace-subject migration startup path

sequenceDiagram
    participant CLI as namespace_subjects CLI
    participant App as create_app
    participant FOCA as create_foca
    participant DB as MongoDB
    participant Migration as namespace_subjects

    CLI->>App: create_app()
    App->>FOCA: create_foca()
    FOCA-->>App: validated config
    App->>DB: create configured indexes and replace rules
    App-->>CLI: started app
    CLI->>Migration: namespace_subjects(namespace, dry_run=False)
    Migration->>DB: read and update namespace subjects
    DB-->>Migration: migration result
Loading

File-Level Changes

Change Details Files
Separate configuration loading from full application startup so dry runs avoid all database writes and startup side effects.
  • Add a reusable FOCA configuration factory that does not connect to or initialize the database.
  • Keep full application creation responsible for startup checks and tasks.
  • Build a minimal Flask app for dry runs, register configured collections without indexes, and preserve the stored access-control rules collection before registering MongoDB access.
cloud_registry/app.py
cloud_registry/namespace_subjects.py
Make namespace-subject dry-run behavior and its limitations explicit to operators.
  • Clarify that dry runs count stored data without changing the database or performing startup validation.
  • Document differences in rule and service counts compared with a real migration.
  • Update command help to describe read-only behavior.
cloud_registry/namespace_subjects.py
docs/upgrading.md
Verify that dry runs preserve legacy database state while still producing counts.
  • Seed older-version indexes and configured rules before the dry run.
  • Snapshot all collection indexes and documents and assert they are unchanged afterward.
  • Ensure the test fails if normal application startup is invoked.
tests/test_namespace_subjects.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Linked issues

#339 fix: make namespace dry runs read-only · 3 of 3 met

# Requirement Status Notes
1 With --dry-run, read the config and stored data without FOCA's app setup or the app's startup tasks. ✅ Met Dry runs use _read_only_app(), which loads the config and registers collections without indexes instead of calling create_app().
2 Add a regression test showing that a dry run on 1.0.0rc1 data preserves indexes and rules and still counts subjects. ✅ Met The test sets up older-style indexes and a configured rule, asserts the dry-run count, and compares database indexes and documents before and after.
3 Say in the command documentation that dry runs are read-only. ✅ Met The module documentation and --dry-run help say it makes no database changes; the upgrade guide explains its limitations.

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (c9d09b5) to head (6471b11).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #343   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         2406      2426   +20     
=========================================
+ Hits          2406      2426   +20     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg merged commit 03bb612 into dev Nov 3, 2026
32 checks passed
@uniqueg
uniqueg deleted the fix/339-dry-run-read-only branch November 3, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: make namespace dry runs read-only

1 participant