Repository navigation
fix(access-control): keep app db, log setup - #269
Conversation
Replace setup.py and the requirements*.txt files with PEP 621 metadata in pyproject.toml (setuptools backend). Distribution name, import package and the `dev` and `docs` extras are unchanged. - declare `requires-python = ">=3.10"` (3.9 is end of life) - ship `py.typed` inside the package so the "Typed" classifier holds - package only `foca*`, so `tests` is no longer installed into site-packages; configure pytest `pythonpath` so tests still import - declare `typing_extensions` as a runtime dependency (it is imported by `foca.models.config`) - drop the obsolete `typing` backport and the unused `toml`, `mypy-extensions`, `types-urllib3`, `types-setuptools`, `pylint`, `python-semantic-release` and `readthedocs-sphinx-ext` - drop `setup_requires` and the setuptools-git based MANIFEST.in - remove the stale semantic-release config from setup.cfg; move the mypy config to pyproject.toml (flake8 config stays in setup.cfg until ruff replaces it) - install FOCA directly from the source tree in the Docker image Closes #232 Co-Authored-By: Claude Opus 5.5 <[email protected]>
FOCA now requires Python >= 3.10. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Update runtime, dev and docs dependencies while staying on Connexion 2. Ranges now allow minor/patch updates within the current major version. - connexion >=2.15.1,<3: makes explicit what was already resolved (2.15 is the only 2.x line compatible with Werkzeug 3) - Flask >=3.1.3,<4 (fixes CVE-2026-27205); Flask-PyMongo 3.x - cryptography >=50 (fixes six advisories in 44.x) - flask-authz <2.8 (2.8+ hard-pins a vulnerable Flask) - swagger-ui-bundle >=1.1 (Swagger UI 4; required by connexion 2.15) - raise floors of celery, Flask-Cors, PyJWT, requests and dev/docs tools (Sphinx >=8, sphinx-rtd-theme >=3) pip-audit reports no known vulnerabilities for the updated set. BREAKING CHANGE: FOCA now requires Flask 3. Apps using APIs removed in Flask 3 (e.g. `flask.Markup`, `_app_ctx_stack`, custom JSON encoders) need to be updated. Closes #216 Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Docker: default to python:3.13-slim-trixie (Debian 13), use trixie-based images in CI, fix Dockerfile lint (`AS`, `ENV K=V`) - examples: MongoDB 8.0, Python 3.13 images, drop obsolete compose `version` key - Read the Docs: Ubuntu 24.04 and Python 3.13; install only the `docs` extra Co-Authored-By: Claude Opus 5.5 <[email protected]>
- update actions (Node 24 versions) and pin them by commit SHA - split into lint/build, unit test, integration test and image jobs - test Python 3.10-3.14 against MongoDB 8.0 (plus 3.13 on 7.0) instead of 16 jobs with EOL MongoDB versions; upload coverage and run the petstore integration tests once - check that sdist and wheel build and pass `twine check` - wait for the petstore app instead of sleeping; show logs on failure - push Docker images only for pushes to `dev`, add Python 3.13/3.14 images and move `latest` to 3.13; skip image builds on PRs (the integration job already builds one) - restrict token permissions, cancel superseded PR runs, set job timeouts, cache pip downloads - replace the blanket `-W ignore::DeprecationWarning` with targeted pytest filters for connexion's use of deprecated jsonschema APIs - add Python 3.13 and 3.14 classifiers Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build sdist and wheel when a GitHub release is published, check that the release tag matches the package version, and upload to PyPI via trusted publishing from the `pypi` environment (no API token). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Weekly grouped updates for Python dependencies, GitHub Actions and the Docker base image, targeting `dev`. Connexion major updates are ignored until the Connexion 3 migration (#188). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Configure ruff for linting and formatting (line length 79, as used by the existing code) with pycodestyle, Pyflakes, isort, pyupgrade, pydocstyle (Google convention), Pylint, bugbear, simplify, comprehensions and Ruff-specific rules. Complexity limits are left disabled for now. Tests are exempt from docstring and magic-value rules. Run `ruff check` and `ruff format --check` in CI instead of flake8, and add a pre-commit config with ruff and basic file hygiene hooks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Automatic, behaviour-preserving fixes by `ruff check --fix`: PEP 585 and PEP 604 annotations (closes #231), sorted imports, docstring whitespace, f-strings, and simplified conditionals and with-blocks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
One-off reformat of all Python files with `ruff format` (Black style, line length 79). No functional changes; one `type: ignore` comment was moved back to the line it applies to. Closes #237 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Docstrings (closes #239): - add package, class and method docstrings where missing - shorten multi-line summaries to one line (Google convention) Linting (closes #238): - chain exceptions raised in `except` blocks (`raise ... from exc`) - replace mutable argument default in `_validate_jwt_public_key()` - replace attribute probe with `hasattr()` and try/except/pass with `contextlib.suppress()` in exception config validation - merge nested `with` statements in tests; minor simplifications Disable magic-value comparisons (PLR2004), which only flag HTTP status codes and Casbin rule lengths, and docstring rules for docs and examples. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # .github/workflows/checks.yml # pyproject.toml
If `security.access_control.db_name` named one of the app's databases, registering access control replaced that database's config, dropping the app's collections (and their clients and indexes) from `config.foca.db`. Add the policy collection to the existing database instead, and refuse to start if the policy collection would share a collection with the app. Also log when the Casbin enforcer, the permission management API and access control as a whole have been registered, and correct the default collection name in the access control docs. Closes #114 Closes #250 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThis maintenance PR fixes access-control registration so shared application databases retain their existing collections and clients, rejects policy collection collisions, supports repeated app creation, and adds setup INFO logs and corresponding documentation. It also substantially refreshes project tooling and delivery infrastructure through formatting/type modernization, dependency updates, new CI/release workflows, container updates, Dependabot, and pre-commit configuration. Sequence diagram for access-control setup registration and logssequenceDiagram
participant Foca
participant Register as register_access_control
participant Casbin as register_casbin_enforcer
participant API as register_permission_specs
participant Logger
Foca->>Register: register_access_control
Register->>Casbin: register_casbin_enforcer
Casbin-->>Logger: Casbin enforcer registered.
Register->>API: register_permission_specs
API-->>Logger: Permission management API registered from spec_path.
Register-->>Foca: registered access-control configuration
Foca-->>Logger: Access control registered.
Flow diagram for shared access-control database registrationflowchart TD
A[register_access_control] --> B[Look up access_control_db]
B -->|database absent| C[Create DBConfig with policy_rules]
C --> D[add_new_database]
B -->|database exists| E[Keep existing DBConfig and app collections]
E --> F{Policy collection clashes?}
F -->|yes| G[Raise ValueError]
F -->|no| H[Add policy CollectionConfig]
H --> I{Database client exists?}
I -->|no| J[add_new_database]
I -->|yes| K[Attach policy collection client]
D --> L[register_casbin_enforcer]
J --> L
K --> L
L --> M[register_permission_specs]
M --> N[Store _registered_collection for repeated registration]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #269 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 22 22
Lines 1101 1130 +29
=========================================
+ Hits 1101 1130 +29
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Exclude the deliberately malformed YAML fixture from `check-yaml` and keep Markdown hard line breaks in `trailing-whitespace`, so that `pre-commit run --all-files` passes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
One-off fixes by the `end-of-file-fixer` and `trailing-whitespace` pre-commit hooks. No content changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Repeated app creation fails, and the YAML hook rejects an intentional invalid fixture.
2 open issues
What changed in this PR
Preserves application collections when sharing the access-control database and adds startup confirmation logs. The stacked changes also modernize dependencies, tooling, and delivery workflows.
Changes:
- Preserve shared database configuration and reject collection-name conflicts.
- Add setup logs, regression tests, and shared-database documentation.
- Update dependencies, CI, release automation, and Python formatting.
| File | Description |
|---|---|
| tests/utils/test_misc.py | Clean up test docstrings. |
| tests/utils/test_logging.py | Format logging tests. |
| tests/utils/test_db.py | Normalize quoting. |
| tests/test_foca.py | Format imports and paths. |
| tests/test_files/models_petstore.py | Modernize type annotations. |
| tests/test_files/model_valid.py | Format model fixture. |
| tests/security/test_cors.py | Normalize quoting. |
| tests/security/test_auth.py | Format authentication tests. |
| tests/security/access_control/test_register_access_control.py | Add database preservation, collision, and logging tests. |
| tests/security/access_control/test_enforcement.py | Modernize enforcement tests. |
| tests/security/access_control/test_access_control_server.py | Format permission-controller tests. |
| tests/security/access_control/foca_casbin_adapter/test_casbin_rule.py | Format rule tests. |
| tests/security/access_control/foca_casbin_adapter/test_adapter.py | Modernize adapter tests. |
| tests/models/test_config.py | Format configuration tests. |
| tests/mock_data.py | Format shared fixtures. |
| tests/factories/test_connexion_app.py | Normalize imports and quoting. |
| tests/factories/test_celery_app.py | Simplify imports. |
| tests/errors/test_errors.py | Format error-handler tests. |
| tests/database/test_register_mongodb.py | Format database tests. |
| tests/config/test_config_parser.py | Normalize formatting and type comparisons. |
| tests/api/test_register_openapi.py | Format OpenAPI tests. |
| setup.cfg | Remove obsolete Flake8 configuration. |
| README.md | Format Python examples. |
| pyproject.toml | Update dependencies and configure Ruff. |
| foca/version.py | Normalize quoting; version unchanged. |
| foca/utils/misc.py | Format identifier utility. |
| foca/utils/logging.py | Modernize annotations and formatting. |
| foca/utils/db.py | Modernize database utilities. |
| foca/utils/__init__.py | Add package docstring. |
| foca/security/cors.py | Clarify docstring and formatting. |
| foca/security/auth.py | Modernize typing, defaults, and exception chaining. |
| foca/security/access_control/register_access_control.py | Preserve app databases, reject clashes, and log setup. |
| foca/security/access_control/foca_casbin_adapter/casbin_rule.py | Modernize typing and docstrings. |
| foca/security/access_control/foca_casbin_adapter/adapter.py | Modernize adapter typing and documentation. |
| foca/security/access_control/foca_casbin_adapter/__init__.py | Add package docstring. |
| foca/security/access_control/enforcement.py | Modernize typing and formatting. |
| foca/security/access_control/constants.py | Format module docstring. |
| foca/security/access_control/api/__init__.py | Add package docstring. |
| foca/security/access_control/access_control_server.py | Modernize controllers and chain exceptions. |
| foca/security/__init__.py | Add package docstring. |
| foca/models/config.py | Modernize annotations and validator syntax. |
| foca/models/__init__.py | Add package docstring. |
| foca/foca.py | Log completed access-control registration. |
| foca/factories/connexion_app.py | Modernize factory formatting and typing. |
| foca/factories/celery_app.py | Clarify task documentation and formatting. |
| foca/factories/__init__.py | Add package docstring. |
| foca/errors/exceptions.py | Modernize error-handler typing and formatting. |
| foca/errors/__init__.py | Add package docstring. |
| foca/database/register_mongodb.py | Format database registration code. |
| foca/database/__init__.py | Add package docstring. |
| foca/config/config_parser.py | Modernize typing and chain import errors. |
| foca/config/__init__.py | Add package docstring. |
| foca/api/register_openapi.py | Modernize OpenAPI registration formatting. |
| foca/api/__init__.py | Add package docstring. |
| examples/petstore/docker-compose.yaml | Update Python and MongoDB images. |
| examples/petstore/controllers.py | Format example controllers. |
| examples/petstore/app.py | Simplify application bootstrap formatting. |
| examples/petstore-access-control/exceptions.py | Sort imports. |
| examples/petstore-access-control/docker-compose.yaml | Update Python and MongoDB images. |
| examples/petstore-access-control/controllers.py | Format protected controllers. |
| examples/petstore-access-control/app.py | Simplify application bootstrap formatting. |
| docs/api/conf.py | Format Sphinx configuration and close version file. |
| docs/access_control/README.md | Document database sharing and correct collection default. |
| docker/Dockerfile | Update base image and Dockerfile syntax. |
| .readthedocs.yaml | Update documentation build environment. |
| .pre-commit-config.yaml | Add Ruff and file-validation hooks. |
| .github/workflows/release.yml | Add trusted PyPI publishing. |
| .github/workflows/checks.yml | Expand validation and restrict image publishing. |
| .github/dependabot.yml | Add scheduled dependency updates. |
Review effort: Balanced
Calling `Foca.create_app()` twice on the same instance failed: the policy collection added by the first call was taken for an app collection with the same name. Remember which collection (and database) access control registration added and replace it on repeated registration. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Hey - I've reviewed your changes and they look great!
Sourcery assessment
Needs a human reviewer. Access-control registration now selects or adds the MongoDB collection that stores Casbin policies, so a routing mistake could make authorization use the wrong persisted rules; reverting would not undo policy records already written. The new release workflow also publishes distributions externally, which a revert cannot retract.
# Conflicts: # .github/workflows/checks.yml # pyproject.toml
Remember the policy collection added by access control registration only once registration of the database succeeded, so that retrying a rejected registration (collection clash) does not remove the app's collection config. Also cover app databases without collections. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # .github/workflows/checks.yml # .github/workflows/release.yml # foca/foca.py # foca/models/config.py # foca/security/access_control/register_access_control.py # tests/security/access_control/test_register_access_control.py


Summary
Second phase-2 PR of the v0.14.0 maintenance series. Builds on #267 (and #266, #265); the new commits are the two
fix(access-control)commits. Independent of #268.Keep app databases when sharing the access control database (#114)
If
security.access_control.db_namewas the name of one of the app's databases,register_access_control()replaced that database'sDBConfig, silently dropping the app's collections (including their clients and index config) fromconfig.foca.db. Now:ValueErrorexplaining which setting to change (policies and app documents would otherwise share a collection)Foca.create_app()twice on the same instance) reuses the collection/database FOCA added before (thanks Copilot for catching that the first version broke this)Setup logs (#250)
INFO log lines confirm that the Casbin enforcer, the permission management API (with spec path) and access control as a whole were registered.
Docs
The access control docs describe the shared-database behaviour and now state the correct default collection name (
policy_rules, notpermission_rules).Ideas for the log lines come from #256/#257 (thanks @minhpham1810).
Closes #114, closes #250
Not included: configurable permission IDs (#142), which you rated low priority on the issue.
Verification
Test-first: new
TestRegisterAccessControlSetuptests (separate DB, shared DB with and without a registered client, collection clash, no DB config, log lines, repeated registration) andtest_foca_create_app_twice; the tests for the shared DB, clash, logs and repeated registration failed before the respective fixes.Locally (Python 3.13): unit tests without MongoDB: 255 passed;
ruff check,ruff format --check,mypy focaclean.CI: all checks pass (run): ruff, mypy, build, unit tests incl. MongoDB-dependent tests on Python 3.10–3.14, petstore integration tests, Read the Docs.
Open questions
None.
🤖 Generated with Claude Code
Summary by Sourcery
Preserve application database configuration during access-control setup and improve registration visibility and reliability.
Bug Fixes:
Enhancements:
policy_rules.Documentation:
Tests: