Commit Graph

932 Commits

Author SHA1 Message Date
Agent Zero a922408e49 fix: add Origin header to login test in deploy.py 2026-08-01 21:23:52 +02:00
Agent Zero b3f40bacd2 fix: deploy.py rewrite — everything via Coolify API, no manual docker 2026-08-01 21:22:55 +02:00
Agent Zero a7b3424eee docs: Vollständige Installationsanleitung (INSTALL.md)
- Komplette Schritt-für-Schritt-Installation von Grund auf
- Alle DB-Rollen und Bootstrap-Reihenfolge dokumentiert
- Alle ENV-Variablen für API, Worker, DB dokumentiert
- Vollständige docker-compose.yml Referenz
- Coolify-Setup-Anleitung
- prestart.sh Startup-Ablauf
- seed_admin.py Admin-Erstellung
- Verifikationsschritte (Health, Login, Alembic, RLS, DDL)
- SMTP-Konfiguration
- Backup und Restore mit Grant-Hinweis
- Häufige Probleme und Lösungen
- Architektur-Übersicht und Datei-Struktur
2026-08-01 20:37:53 +02:00
Agent Zero be20a8545e docs: Abschlussbericht Phase 0+1 und vollständiger Sanierungsplan
- Kompletter Statusbericht mit allen 5 Gates
- Datenbankrollen-Architektur dokumentiert
- RLS-Architektur dokumentiert
- Verifizierte Sicherheitsnachweise
- Durchgeführte Code-Änderungen und Migrationen
- Offene Risiken
- Vollständiger Sanierungsplan Phase 2-10
- Gesamtschätzung: 120-210h verbleibend
- Empfohlene Reihenfolge
2026-08-01 07:28:11 +02:00
Agent Zero 733fa1c807 docs: Gate 3 acceptance — restore test verified
Gate 3 (Restore-Test) bestanden:
- Backup aus Forgejo-Release heruntergeladen, MD5 verifiziert
- pg_restore in separate Test-DB (crm_restore_test)
- alembic upgrade head: 0086 → 0090
- Datenintegrität: 9 Contacts, 2 Tenants, 1 User, 479 Sessions
- RLS: 0 rows ohne Kontext, 8 rows Tenant B, 2 rows Tenant A
- Cross-Tenant INSERT blockiert, DDL blockiert
- 108 RLS-Tabellen, 112 Policies, 0 Legacy Policies
2026-08-01 00:27:12 +02:00
Agent Zero 9b4ee3b8ca docs: Gate 5 acceptance — worker event handlers verified
Gate 5 (Worker und Eventhandler) bestanden:
- Worker healthy, verarbeitet Outbox-Jobs und enqueued Jobs
- 18 Worker-Funktionen registriert
- Plugin-Eventhandler nur für aktive Plugins
- Per-Tenant Outbox-Processing mit RLS-Kontext
- Worker verwendet crm_worker (get_worker_session_factory)
- Keine Plugin-Router im Worker
2026-07-31 23:15:32 +02:00
Agent Zero 94847ea515 fix: PluginModel.is_active → PluginModel.active (worker crash fix) 2026-07-31 23:12:05 +02:00
Agent Zero cea21ff576 fix: Gate 5 — worker event handlers and per-tenant outbox processing
Worker fixes:
- registry.initialize uses get_migration_engine() for DDL (not worker_engine)
- Worker session uses get_worker_session_factory() (crm_worker, not crm_api)
- Event handlers only registered for active plugins (is_active check)
- Outbox processing per-tenant with set_config(app.current_tenant_id)
- process_outbox_job uses get_worker_session_factory() and loads tenant_ids
- Removed unused get_engine import

Outbox fixes:
- process_outbox_batch iterates over tenants, sets RLS context per tenant
- _process_single_outbox_event extracted for clarity
- Events claimed per-tenant (RLS-compatible, no BYPASSRLS needed)
- Commit after each tenant to release locks

Gate 5 requirements met:
- Plugin event handlers registered for active plugins only
- No plugin routers registered in worker
- Outbox events without handlers marked as no_handlers
- Failed consumers trigger retry with exponential backoff
- Processing is idempotent (consumer_inbox check)
- Every worker DB access sets app.current_tenant_id
- Worker cannot read/write other tenant data (RLS enforced)
2026-07-31 23:09:25 +02:00
Agent Zero 89fe7a4750 docs: Gate 2 acceptance — fresh DB install verified
Gate 2 (Neuinstallation auf leerer Datenbank) bestanden:
- Alembic-Head 0090, 124 Tabellen, 47 RLS-Tabellen
- 0 legacy app.tenant_id policies
- Alle 4 DB-Rollen korrekt (NOSUPERUSER, crm_migration BYPASSRLS)
- RLS fail-closed: 0 rows ohne Kontext
- Cross-Tenant INSERT blockiert
- crm_api DDL blockiert
- seed_admin.py funktioniert
- Login erfolgreich (200 OK)
- Keine manuellen Schemaänderungen
2026-07-31 22:33:07 +02:00
Agent Zero 89b775b9ef fix: legacy app.tenant_id policies on _old tables + seed_admin.py rewrite
- Migration 0090: Drop legacy tenant_isolation policies on companies_old,
  company_contacts_old, contacts_old that used app.tenant_id variable.
  Create new policies using app.current_tenant_id for crm_api/crm_worker.
- seed_admin.py: Rewrite to use migration engine (crm_migration) for
  bootstrap, set tenant context, create Tenant + Role + User + UserTenant.
  No longer passes tenant_id as User parameter.

Fixes: 3 legacy app.tenant_id policies found in Gate 2 verification.
Fixes: seed_admin.py incompatible with current User model.
2026-07-31 22:23:38 +02:00
Agent Zero b5191f0d11 gate2: migration 0089 — add updated_at to sessions table (model uses TimestampMixin but table was missing column) 2026-07-31 22:15:29 +02:00
Agent Zero 569476b993 gate2: fix prestart.sh shell quote conflict — use temp Python file instead of python3 -c 2026-07-31 21:57:04 +02:00
Agent Zero 68db50544c gate2: fix shell quote conflict in prestart.sh — use string concat instead of f-string for ALTER ROLE 2026-07-31 21:49:45 +02:00
Agent Zero 2a7412e49f gate2: fix prestart.sh — inline password for ALTER ROLE (prepared statements dont work with ALTER ROLE) 2026-07-31 21:42:55 +02:00
Agent Zero 9124b17a8e gate2: prestart.sh sets passwords for all DB roles (crm_api, crm_auth, crm_worker, crm_migration) after migration
Migration 0070 creates roles without passwords. On fresh DB, API cannot authenticate.
prestart.sh now extracts password from MIGRATION_DATABASE_URL and sets it for all roles.
2026-07-31 21:31:18 +02:00
Agent Zero 10296137e9 gate2: fix migration 0085 — revoke default privileges before dropping crm_runtime, handle dependent_objects_still_exist 2026-07-31 21:20:59 +02:00
Agent Zero 48ddd78e9e gate2: fix mail plugin migration 0009 — guard UPDATE for missing deleted_at column on fresh DB
Check Cross-Plugin Imports / check (push) Has been cancelled
2026-07-31 20:56:06 +02:00
Agent Zero 4a5c905934 P0-fix: plugin migrations use migration engine (crm_migration) instead of API engine (crm_api)
Check Cross-Plugin Imports / check (push) Has been cancelled
- main.py: registry.initialize(get_migration_engine()) instead of get_engine()
- main.py: plugin migrations run via get_migration_session_factory() not async_session()
- registry.py: upgrade_plugin, install_plugin, uninstall_plugin all use migration session for DDL
- db/__init__.py: get_migration_engine() raises RuntimeError if MIGRATION_DATABASE_URL missing (no fallback)
- Fixes fresh-install failure: crm_api has no DDL rights, plugin migrations need crm_migration
2026-07-31 20:45:16 +02:00
Agent Zero 010ef448e7 gate2: fix all migrations for fresh DB installation 2026-07-31 19:16:11 +02:00
Agent Zero d37388423d gate2: fix ix_contacts_tenant_id conflict — drop old index before recreate in 0021 2026-07-31 18:51:15 +02:00
Agent Zero e43a906cde gate2: fix ix_contacts_tenant_id duplicate (index=True in 0021 vs create_index in 0002) 2026-07-31 18:34:35 +02:00
Agent Zero dd7ad461d8 gate2: fix duplicate column/index in migrations for fresh DB installation 2026-07-31 18:20:09 +02:00
Agent Zero 224a5ea9af gate2: fix migration 0019 duplicate deleted_at on roles (IF NOT EXISTS) 2026-07-31 17:50:07 +02:00
Agent Zero 3f3ef28264 gate: final acceptance report — Gate 1 + Gate 4 passed, Gate 2/3/5 open 2026-07-31 12:07:04 +02:00
Agent Zero 3032ad2cbf gate4: migration 0088 — auth RLS policies for password_reset_tokens and audit_log 2026-07-31 12:04:27 +02:00
Agent Zero a303a4e455 gate4: use separate API session for audit log in confirm_password_reset 2026-07-31 12:01:11 +02:00
Agent Zero a721db5214 gate4: set tenant context before audit log in confirm_password_reset 2026-07-31 11:58:11 +02:00
Agent Zero ce0e9ab12a gate4: fix SMTP TLS mode for port 465 (implicit TLS instead of STARTTLS) 2026-07-31 11:37:11 +02:00
Agent Zero 31408670e6 gate4: register app.core.jobs in worker for send_password_reset_email 2026-07-31 11:32:47 +02:00
Agent Zero ebc63beeb4 gate1: fix npm peer dependency conflict with --legacy-peer-deps 2026-07-31 11:19:53 +02:00
Agent Zero f1ce130a45 gate1: fix Dockerfile npm ci error suppression to show build errors 2026-07-31 11:18:29 +02:00
Agent Zero 044336a56d gate: final acceptance report for Phase 0 + Phase 1 with all gate items 2026-07-31 09:45:45 +02:00
Agent Zero fa96466a50 gate: fresh session per plugin activation to isolate RLS errors 2026-07-31 09:43:29 +02:00
Agent Zero ab8d878bc7 gate: db.expunge_all() after rollback to clear pending objects from failed INSERTs 2026-07-31 09:41:32 +02:00
Agent Zero d114fd7d4c gate: wrap db.commit() in try/except after plugin activation 2026-07-31 09:39:45 +02:00
Agent Zero 79d132b66d gate: fully resilient plugin activation in API startup 2026-07-31 09:37:44 +02:00
Agent Zero 01aa31a3e0 gate: API startup resilient to RLS errors, dont fail on duplicate cron job inserts 2026-07-31 09:33:41 +02:00
Agent Zero 31d11efd33 gate: API main.py flush+rollback after plugin activation for RLS error handling 2026-07-31 09:32:15 +02:00
Agent Zero 9d7b160e2a gate: fix password_reset RLS policy for crm_auth, set tenant context before token creation 2026-07-31 09:24:04 +02:00
Agent Zero 437c107ee8 gate: migration 0087 add timestamps to password_reset_tokens, backup uploaded to Forgejo 2026-07-31 09:23:34 +02:00
Agent Zero 1deb852ff3 gate: worker skips plugin activation, only registers event handlers
Check Cross-Plugin Imports / check (push) Has been cancelled
phase1-backup
2026-07-31 09:20:54 +02:00
Agent Zero ab61c81d2b gate: worker flush after plugin activation to detect swallowed RLS errors 2026-07-31 09:19:45 +02:00
Agent Zero ec0cf6f588 gate: worker resilient to RLS errors during plugin activation, use crm_worker engine 2026-07-31 09:12:42 +02:00
Agent Zero 5ce85f4324 gate: fix worker on_startup to set tenant context per-tenant for plugin activation 2026-07-31 09:09:23 +02:00
Agent Zero 1a980ba9d8 gate: migration 0086, crm_migration BYPASSRLS, audit_log fix, CI test for app.tenant_id
- Migration 0086: Remove FORCE RLS from 5 global tables
- Migration 0085: crm_migration keeps BYPASSRLS for data migrations
- Migration 0085: Remove audit_log from crm_auth grants
- auth_service.py: Audit log via separate API session (crm_api with tenant context)
- tests/test_no_legacy_tenant_var.py: CI test for app.tenant_id in policies
2026-07-31 09:02:40 +02:00
Agent Zero 94318aaa4d phase1: acceptance report for Phase 0 + Phase 1 2026-07-31 02:29:32 +02:00
Agent Zero 15f0a07d4e phase1: fix auth_service tenant context for audit_log, add sessions+audit_log to crm_auth grants
- auth_service.py: set tenant context before audit log write in login
- migration 0085: add sessions and audit_log to AUTH_TABLES for crm_auth
- Login now works on production with RLS enabled
2026-07-31 02:28:29 +02:00
Agent Zero 100b9f705c phase1: separate DB roles, RLS restoration, login on crm_auth
Check Cross-Plugin Imports / check (push) Has been cancelled
- config.py: add auth_database_url, worker_database_url, migration_database_url
- db/__init__.py: separate engines for auth/worker/migration + get_auth_db/get_worker_db
- auth.py: all auth endpoints use get_auth_db (crm_auth role)
- auth_service.py: remove login fallback, require active membership, check status
- auth_service.py: switch_tenant checks active membership status
- alembic/env.py: use migration_database_url for Alembic
- docker-compose.yml: add AUTH_DATABASE_URL, WORKER_DATABASE_URL
- .env.example: add all 4 DB URLs with separate roles
- migration 0085: transfer ownership to crm_migration, fix BYPASSRLS,
  enable RLS+FORCE on all tenant tables, drop old policies, create new
  fail-closed policies scoped to crm_api+crm_worker, revoke excessive grants,
  grant minimal crm_auth access, drop crm_runtime, set default privileges
- tests/test_rls_coverage.py: automated RLS coverage check (13 tests)
- tests/test_cross_tenant_security_v2.py: RLS tests with unprivileged role
2026-07-31 02:05:16 +02:00
Agent Zero cdbbc1b6f0 phase0: frozen error list with 21 findings (10 P0, 7 P1 open, 4 P1 fixed) 2026-07-31 01:58:43 +02:00
Agent Zero 032a7e80a8 phase0: fix cross-plugin import, remove app.tenant_id, create cross-tenant v2 tests
- Fix report_generator/jobs.py: use DmsContract instead of direct DMS import
- Remove app.tenant_id from set_tenant_context (only app.current_tenant_id)
- Create tests/test_cross_tenant_security_v2.py with real RLS tests using
  unprivileged crm_api role (NOSUPERUSER, NOBYPASSRLS)
- Fix existing tests referencing app.tenant_id
- Git baseline tag v-phase0-baseline at 11d6faa
- Production DB backup at /tmp/crm_backup_20260731_015514.dump
2026-07-31 01:57:51 +02:00