From ebb19c84440357c448d405dabcd24dd143adda26 Mon Sep 17 00:00:00 2001 From: Juan Denis Date: Sun, 22 Nov 2026 02:41:56 -0500 Subject: [PATCH] Read through services in the scoped routes The workspace-scoped mobile and shared-variable routes queried models in the controller, which the boundary inventory counts as new crossings. They now go through WorkspaceService and ManagedDatabaseService; the baseline drops the two crossings the mobile routes no longer make. --- backend/app/api/mobile.py | 12 +++++------- backend/app/api/proxy.py | 3 ++- backend/app/api/shared_resources.py | 4 ++-- backend/tests/api_controller_boundary_baseline.json | 4 +--- docs/MIGRATION_INVENTORY.md | 4 ++-- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/backend/app/api/mobile.py b/backend/app/api/mobile.py index 74ec231f..a3d9d5f1 100644 --- a/backend/app/api/mobile.py +++ b/backend/app/api/mobile.py @@ -9,6 +9,9 @@ from app.services.workspace_service import WorkspaceService mobile_bp = Blueprint('mobile', __name__) +# How many servers the offline cache carries. +OFFLINE_CACHE_SERVERS = 50 + @mobile_bp.route('/push/register', methods=['POST']) @jwt_required() @@ -99,12 +102,10 @@ def get_mobile_summary(): except Exception: metrics = {} - from app.models.server import Server from app.models.security_alert import SecurityAlert # Only the servers this caller can reach — the workspace rule /servers applies. - server_count = WorkspaceService.filter_accessible_servers( - Server.query, get_current_user()).count() + server_count = len(WorkspaceService.accessible_servers(get_current_user())) active_alerts = SecurityAlert.query.filter_by(resolved=False).count() if hasattr(SecurityAlert, 'resolved') else 0 # MonitoringService returns nested sections; a section that could not be @@ -124,10 +125,7 @@ def get_mobile_summary(): @jwt_required() def get_offline_data(): """Get data for offline caching.""" - from app.models.server import Server - - servers = WorkspaceService.filter_accessible_servers( - Server.query, get_current_user()).limit(50).all() + servers = WorkspaceService.accessible_servers(get_current_user())[:OFFLINE_CACHE_SERVERS] return jsonify({ 'servers': [{'id': s.id, 'name': s.name, 'hostname': s.hostname, 'status': s.status} for s in servers], 'cached_at': __import__('datetime').datetime.utcnow().isoformat(), diff --git a/backend/app/api/proxy.py b/backend/app/api/proxy.py index 7493c64b..ae3fda10 100644 --- a/backend/app/api/proxy.py +++ b/backend/app/api/proxy.py @@ -32,7 +32,8 @@ def _require_server(server_id): # reads the proxy state but may not change it. if (request.method not in ('GET', 'HEAD', 'OPTIONS') and server.workspace_id and not WorkspaceService.can_write_in_workspace(user, server.workspace_id)): - return None, (jsonify({'error': 'You have read-only access to this workspace'}), 403) + from app.exceptions import PermissionDeniedError + raise PermissionDeniedError('You have read-only access to this workspace') return server, None diff --git a/backend/app/api/shared_resources.py b/backend/app/api/shared_resources.py index 7ff133c7..b26558f6 100644 --- a/backend/app/api/shared_resources.py +++ b/backend/app/api/shared_resources.py @@ -189,9 +189,9 @@ def _check_resource_read(resource_type, resource_id): return _bad('access denied', 403) return None if resource_type == 'database': - from app.models.managed_database import ManagedDatabase + from app.services.managed_database_service import ManagedDatabaseService try: - row = ManagedDatabase.query_active().filter_by(id=int(resource_id)).first() + row = ManagedDatabaseService.get(int(resource_id)) except (TypeError, ValueError): row = None if row is None: diff --git a/backend/tests/api_controller_boundary_baseline.json b/backend/tests/api_controller_boundary_baseline.json index d651fe08..f1ce16e6 100644 --- a/backend/tests/api_controller_boundary_baseline.json +++ b/backend/tests/api_controller_boundary_baseline.json @@ -3,7 +3,7 @@ "policy": "Exact reviewed inventory of direct persistence, subprocess, and filesystem mutation calls in Flask API controllers. New calls are forbidden; remove entries as controllers delegate to services.", "counts": { "filesystem": 8, - "persistence": 445 + "persistence": 443 }, "owners": { "api/admin.py": { @@ -501,8 +501,6 @@ "api/manifests.py::reset_bootstrap::persistence::db.session.commit()::1", "api/manifests.py::scaffold_manifest::persistence::Application.query_active().filter_by(id=app_id).first()::1", "api/mobile.py::get_mobile_summary::persistence::SecurityAlert.query.filter_by(resolved=False).count()::1", - "api/mobile.py::get_mobile_summary::persistence::Server.query.count()::1", - "api/mobile.py::get_offline_data::persistence::Server.query.limit(50).all()::1", "api/mobile.py::register_push::persistence::User.query.get(user_id)::1", "api/mobile.py::register_push::persistence::db.session.commit()::1", "api/notifications.py::get_user_preferences::persistence::User.query.get(current_user_id)::1", diff --git a/docs/MIGRATION_INVENTORY.md b/docs/MIGRATION_INVENTORY.md index 951bc903..760d1798 100644 --- a/docs/MIGRATION_INVENTORY.md +++ b/docs/MIGRATION_INVENTORY.md @@ -18,8 +18,8 @@ debt. Regenerate with `python scripts/generate-migration-inventory.py`. | API crashes swallowed without recording | app.error_reporting | 0 | 0 | INVARIANT at 0 | | Hand-shaped {'error': ...} bodies in app/api | typed errors + the global handler | 849 | 850 | migrate when touched; new endpoints raise | | Raw subprocess calls outside the runners | app/utils/system.py runners | 23 | 23 | migrate when touched | -| Controller-boundary violations (routes doing service work) | service layer extraction | 453 | 453 | migrate when touched (first-wave ratchet) | -| raw api.* calls in pages/ | E1: useServerQuery/useServerMutation | 363 | 371 | migrate when touched | +| Controller-boundary violations (routes doing service work) | service layer extraction | 451 | 451 | migrate when touched (first-wave ratchet) | +| raw api.* calls in pages/ | E1: useServerQuery/useServerMutation | 364 | 371 | migrate when touched | | per-page toast.error extractions in pages/ | E1: query-layer error presentation | 185 | 206 | migrate when touched | | hand-rolled form-group blocks | F2: FormField/useForm | 206 | 206 | migrate when touched | | unencoded ?k=${v} query interpolations in services/api | C4: buildQuery/encoding template | 0 | 0 | INVARIANT at 0 |