Skip to content

feat: add MONGO_URI environment variable support (#253)#256

Open
minhpham1810 wants to merge 4 commits intoelixir-cloud-aai:devfrom
minhpham1810:dev
Open

feat: add MONGO_URI environment variable support (#253)#256
minhpham1810 wants to merge 4 commits intoelixir-cloud-aai:devfrom
minhpham1810:dev

Conversation

@minhpham1810
Copy link

@minhpham1810 minhpham1810 commented Mar 17, 2026

Description

Add support for a MONGO_URI environment variable that, when set, is used as the complete MongoDB connection string, taking precedence over individual component variables (MONGO_HOST, MONGO_PORT, MONGO_USERNAME, MONGO_PASSWORD, MONGO_DBNAME). This enables advanced connection features such as replica sets, SRV records, TLS options, and URIs from external secret managers.

A warning is logged when MONGO_URI is set alongside individual variables to help users avoid configuration confusion. Full backward compatibility is maintained.

Fixes #253

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation (or documentation does not need to be updated)
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not reduced the existing code coverage

Summary by Sourcery

Add support for configuring the MongoDB client via a single MONGO_URI environment variable that takes precedence over individual MongoDB settings while preserving existing behavior as a fallback.

New Features:

  • Allow MongoDB connection to be configured using a complete MONGO_URI environment variable that overrides individual MongoDB environment variables and config values.

Enhancements:

  • Log a warning when MONGO_URI is used alongside individual MongoDB environment variables to clarify which settings are ignored.

Documentation:

  • Document MONGO_URI and related MongoDB environment variable overrides in the README and configuration template.

Tests:

  • Add unit tests covering MONGO_URI usage, precedence over individual variables, empty-value fallback behavior, logging of warnings, and full registration flow.

Add support for a MONGO_URI environment variable that, when set, is used as the complete MongoDB connection string, taking precedence over individual component variables (MONGO_HOST, MONGO_PORT, MONGO_USERNAME, MONGO_PASSWORD, MONGO_DBNAME). This enables advanced connection features such as replica sets, SRV records, TLS options, and URIs from external secret managers.

A warning is logged when MONGO_URI is set alongside individual variables to help users avoid configuration confusion. Full backward compatibility is maintained.
@sourcery-ai
Copy link

sourcery-ai bot commented Mar 17, 2026

Reviewer's Guide

Adds support for a MONGO_URI environment variable that, when set and non-empty, is used as the complete MongoDB connection string with precedence over individual MongoDB env vars and config, along with tests, logging, and documentation/template updates describing this behavior.

Sequence diagram for Flask app initialization with MONGO_URI support

sequenceDiagram
    participant Env as Environment
    participant App as FlaskApp
    participant Reg as register_mongodb
    participant Mongo as PyMongo

    App->>Reg: _create_mongo_client(app, host, port, db)
    Reg->>Env: get MONGO_URI
    Env-->>Reg: MONGO_URI value or null

    alt MONGO_URI set and non-empty
        Reg->>Env: get MONGO_HOST, MONGO_PORT,
        Env-->>Reg: individual env values
        Reg->>Reg: build list of set individual vars
        alt any individual vars set
            Reg->>App: logger.warning(MONGO_URI overrides individual vars)
        end
        Reg->>App: set app.config[MONGO_URI]
        Reg->>Mongo: create PyMongo(app)
        Mongo-->>Reg: PyMongo client
        Reg->>App: logger.info(registered via MONGO_URI)
        Reg-->>App: PyMongo client
    else MONGO_URI not set or empty
        Reg->>Env: get MONGO_USERNAME, MONGO_PASSWORD,
        Env-->>Reg: individual env values
        Reg->>Reg: build connection URI from env or config
        Reg->>Mongo: create PyMongo(app)
        Mongo-->>Reg: PyMongo client
        Reg-->>App: PyMongo client
    end
Loading

File-Level Changes

Change Details Files
Add MONGO_URI-based connection string handling in Mongo client creation, including precedence logic and warnings when mixed with individual env vars.
  • Read MONGO_URI from the environment and, when non-empty, configure Flask's MONGO_URI directly and initialize PyMongo from it.
  • Collect individual MongoDB-related environment variables and log a warning listing those that will be ignored when MONGO_URI is used.
  • Short-circuit the previous component-based URI construction when MONGO_URI is handled, preserving legacy auth and URI-building logic as the fallback path.
foca/database/register_mongodb.py
Extend MongoDB registration tests to cover MONGO_URI usage, precedence, empty-string fallback, warning logging, and auth handling.
  • Add tests ensuring _create_mongo_client uses MONGO_URI as-is when set, including variants with credentials.
  • Add tests verifying MONGO_URI takes precedence over individual component env vars and that an empty MONGO_URI falls back to component-based URI construction.
  • Add a test checking that a warning is logged when MONGO_URI is set alongside individual env vars, and that register_mongodb works end-to-end with MONGO_URI configured.
tests/database/test_register_mongodb.py
Document MONGO_URI and environment-variable-based MongoDB overrides in user-facing docs and the default config template.
  • Update README to describe MONGO_URI as the preferred complete MongoDB connection string that overrides other Mongo-related env vars and config values, including typical use cases.
  • Annotate the sample config.yaml template with comments explaining MONGO_URI and the supported individual MongoDB environment variable overrides.
README.md
templates/config.yaml

Assessment against linked issues

Issue Objective Addressed Explanation
#253 Add support in the MongoDB registration code for a MONGO_URI environment variable that, when set and non-empty, is used directly as the complete MongoDB connection string.
#253 Ensure that when MONGO_URI is set it takes precedence over individual MongoDB environment variables (MONGO_HOST, MONGO_PORT, MONGO_USERNAME, MONGO_PASSWORD, MONGO_DBNAME) while preserving full backward compatibility when MONGO_URI is unset or empty.
#253 Document the new MONGO_URI environment variable behavior and precedence in the user-facing documentation and configuration templates.

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

Copy link

@sourcery-ai sourcery-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.

Hey - I've left some high level feedback:

  • In _create_mongo_client, consider normalizing MONGO_URI and the individual env vars (e.g., using .strip() and treating empty strings as unset) so that whitespace/empty-string values don’t unintentionally trigger URI usage or get included in the "ignored" warning list.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `_create_mongo_client`, consider normalizing `MONGO_URI` and the individual env vars (e.g., using `.strip()` and treating empty strings as unset) so that whitespace/empty-string values don’t unintentionally trigger URI usage or get included in the "ignored" warning list.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

- add INFO log after casbin enforcer registration in register_access_control.py
- add INFO log after permission spec registration in register_access_control.py
- add INFO log after access-control registration in foca.py
- add focused log-flow unit test in test_register_access_control.py
- update access-control tests to assert emitted logs via stderr capture in test_foca.py
- replace deprecated importlib.resources path usage with files/as_file in config.py and register_access_control.py
- suppress known third-party deprecation noise in pytest config in setup.cfg

Suggested PR title

Add readable access-control setup logs and clean up deprecation/test noise

Suggested PR description

Summary
This PR implements issue elixir-cloud-aai#250 by adding readable confirmation logs for the access-control setup flow, then updates tests and warning handling so CI output is reliable and low-noise.

What changed
1. Access-control setup logging
- Added log confirming enforcer registration in register_access_control.py.
- Added log confirming permission spec registration in register_access_control.py.
- Added top-level log confirming access-control registration completion in foca.py.

2. Test updates
- Added a focused unit test to validate setup-step logs in test_register_access_control.py.
- Updated access-control assertions in test_foca.py to use stderr capture, matching current project logging behavior.

3. Deprecation cleanup
- Replaced deprecated importlib.resources path usage with files/as_file in:
  - config.py
  - register_access_control.py
- Added pytest warning filters for known external deprecations in setup.cfg.

Why
- Improves operator visibility during bootstrap by clearly signaling successful access-control setup steps.
- Prevents flaky/false-negative log assertions caused by logger capture differences.
- Reduces warning noise from dependency internals so real regressions are easier to spot.

Validation
- Ran: pytest -q test_foca.py
- Result: 15 passed

Issue
- Closes elixir-cloud-aai#250
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.

Add MONGO_URI Environment Variable Support

1 participant