TT-007.2: establish DB-backed module authorization
This commit is contained in:
@@ -0,0 +1,434 @@
|
||||
# TT-007.2 Source Inspection — Module Authorization Data Model
|
||||
|
||||
## 1. Executive Summary
|
||||
|
||||
| Пункт | Значение |
|
||||
|-------|----------|
|
||||
| Дата | 2026-08-06 |
|
||||
| Baseline | dev-doc/security_authorization_architecture.md |
|
||||
| Предыдущие материалы | TT-007.1 implementation + correction |
|
||||
| Тип | READ-ONLY inspection |
|
||||
| Код изменен | НЕТ |
|
||||
|
||||
**Главный вывод:** Текущая hardcoded `ROLE_PERMISSIONS` может быть заменена на data-driven модель с минимальными изменениями. Существующие модели `User`, `Role`, `UserRole` уже подходят для локальной Module-owned RBAC.
|
||||
|
||||
---
|
||||
|
||||
## 2. Current Authorization Data Model
|
||||
|
||||
### 2.1 Entity Inventory
|
||||
|
||||
| Entity | File | Table | Owner | Purpose | Relations |
|
||||
|--------|------|-------|-------|---------|-----------|
|
||||
| User | `models/user.py` | `users` | Module | Local + platform users | user_roles, tickets, comments |
|
||||
| Role | `models/role.py` | `roles` | Module | Role definitions | user_roles |
|
||||
| UserRole | `models/user_role.py` | `user_roles` | Module | User → Role assignment | user, role |
|
||||
|
||||
### 2.2 Relationship Model
|
||||
|
||||
```
|
||||
User (1) ──→ (N) UserRole ──→ (1) Role
|
||||
```
|
||||
|
||||
- One user can have multiple roles
|
||||
- One role can be assigned to multiple users
|
||||
- UserRole is the junction table
|
||||
|
||||
---
|
||||
|
||||
## 3. Current Database Schema
|
||||
|
||||
### 3.1 users Table
|
||||
|
||||
| Column | Type | Nullable | Unique | Default |
|
||||
|--------|------|----------|--------|---------|
|
||||
| id | Integer PK | No | Yes | Auto |
|
||||
| email | String(255) | No | Yes | - |
|
||||
| first_name | String(100) | No | No | "" |
|
||||
| last_name | String(100) | No | No | "" |
|
||||
| phone | String(50) | Yes | No | - |
|
||||
| password_hash | String(255) | Yes | No | - |
|
||||
| is_active | Boolean | No | No | True |
|
||||
| source | String(20) | No | No | "local" |
|
||||
|
||||
### 3.2 roles Table
|
||||
|
||||
| Column | Type | Nullable | Unique | Default |
|
||||
|--------|------|----------|--------|---------|
|
||||
| id | Integer PK | No | Yes | Auto |
|
||||
| name | String(100) | No | Yes | - |
|
||||
| description | Text | Yes | No | - |
|
||||
| is_system | Boolean | No | No | False |
|
||||
|
||||
### 3.3 user_roles Table
|
||||
|
||||
| Column | Type | Nullable | Unique | Default |
|
||||
|--------|------|----------|--------|---------|
|
||||
| id | Integer PK | No | Yes | Auto |
|
||||
| user_id | Integer FK | No | No | - |
|
||||
| role_id | Integer FK | No | No | - |
|
||||
|
||||
### 3.4 Default Roles (from database.py:47-52)
|
||||
|
||||
| Role | Description | is_system |
|
||||
|------|-------------|-----------|
|
||||
| admin | Administrator with full access | True |
|
||||
| support_agent | Support agent - create and update tickets | True |
|
||||
| manager | Manager - view all tickets, create teams | True |
|
||||
| customer | Customer - read-only, create tickets | True |
|
||||
|
||||
---
|
||||
|
||||
## 4. Current Role/Permission Semantics
|
||||
|
||||
### 4.1 Role Definitions
|
||||
|
||||
**Source:** `request_app/api/v1/routes/permissions.py:27-43`
|
||||
|
||||
```python
|
||||
ROLE_PERMISSIONS = {
|
||||
"admin": MODULE_PERMISSION_CATALOG, # admin gets all
|
||||
"manager": [
|
||||
"request.read", "request.create", "request.update",
|
||||
"request.assign", "request.close", "request.comment", "request.report.view",
|
||||
],
|
||||
"user": [
|
||||
"request.read", "request.create", "request.comment",
|
||||
],
|
||||
}
|
||||
```
|
||||
|
||||
### 4.2 Permission Catalog
|
||||
|
||||
**Source:** `request_app/api/v1/routes/permissions.py:13-24`
|
||||
|
||||
```python
|
||||
MODULE_PERMISSION_CATALOG = [
|
||||
"request.read",
|
||||
"request.create",
|
||||
"request.update",
|
||||
"request.delete",
|
||||
"request.assign",
|
||||
"request.close",
|
||||
"request.comment",
|
||||
"request.admin",
|
||||
"request.settings.manage",
|
||||
"request.report.view",
|
||||
]
|
||||
```
|
||||
|
||||
### 4.3 Naming Convention
|
||||
|
||||
- Pattern: `{module}.{action}`
|
||||
- Module: `request`
|
||||
- Actions: `read`, `create`, `update`, `delete`, `assign`, `close`, `comment`, `admin`, `settings.manage`, `report.view`
|
||||
|
||||
### 4.4 Observations
|
||||
|
||||
- `ROLE_PERMISSIONS` uses "user" role but DB has "customer" and "support_agent"
|
||||
- `admin` role exists in both DB and mapping
|
||||
- `manager` role exists in both DB and mapping
|
||||
- `user` role does NOT exist in DB default roles
|
||||
|
||||
---
|
||||
|
||||
## 5. Current Authorization Engine
|
||||
|
||||
### 5.1 Flow
|
||||
|
||||
```
|
||||
require_permission(permission_id)
|
||||
↓
|
||||
compute_effective_policy(user)
|
||||
↓
|
||||
User.user_roles
|
||||
↓
|
||||
UserRole.role.name
|
||||
↓
|
||||
ROLE_PERMISSIONS[role_name]
|
||||
↓
|
||||
permission check
|
||||
```
|
||||
|
||||
**Files:**
|
||||
- `request_app/core/auth.py:120-142` — require_permission()
|
||||
- `request_app/api/v1/routes/permissions.py:46-60` — compute_effective_policy()
|
||||
|
||||
### 5.2 Key Properties
|
||||
|
||||
- Single authorization engine
|
||||
- Module-owned (not Platform)
|
||||
- Local computation (no HTTP calls)
|
||||
- Role-based (RBAC)
|
||||
|
||||
---
|
||||
|
||||
## 6. JIT Provisioning Impact
|
||||
|
||||
### 6.1 Current JIT Flow
|
||||
|
||||
**File:** `request_app/core/auth.py:27-52`
|
||||
|
||||
```python
|
||||
def _sync_platform_user_to_local(db: Session, payload: dict) -> User:
|
||||
email = payload.get("email", "")
|
||||
user = db.query(User).filter(User.email == email).first()
|
||||
if not user:
|
||||
user = User(email=email, source="platform")
|
||||
db.add(user)
|
||||
db.commit()
|
||||
return user
|
||||
```
|
||||
|
||||
### 6.2 Observations
|
||||
|
||||
- JIT creates user WITHOUT role assignment
|
||||
- User gets NO default role on first login
|
||||
- Role assignment must be done separately (via sync endpoint or manual)
|
||||
|
||||
### 6.3 Impact on DB-driven Model
|
||||
|
||||
- Need to define default role for JIT users
|
||||
- Or keep JIT as-is (no role = no permissions)
|
||||
|
||||
---
|
||||
|
||||
## 7. Platform Integration Impact
|
||||
|
||||
### 7.1 Data from Platform
|
||||
|
||||
| Data | Source | Used for |
|
||||
|------|--------|----------|
|
||||
| user_id | JWT `sub` | Identity |
|
||||
| email | JWT `email` | Identity |
|
||||
| first_name | JWT | Display |
|
||||
| last_name | JWT | Display |
|
||||
| is_active | JWT | Status |
|
||||
| roles | JWT | Platform roles |
|
||||
| module_roles | JWT | Module access |
|
||||
|
||||
### 7.2 Module Uses Only
|
||||
|
||||
- Identity (user_id, email, name)
|
||||
- Status (is_active)
|
||||
- Source tracking (source="platform")
|
||||
|
||||
### 7.3 Module Does NOT Use
|
||||
|
||||
- Platform roles for authorization
|
||||
- Platform module_roles for permission computation
|
||||
|
||||
---
|
||||
|
||||
## 8. Reusable Components
|
||||
|
||||
| Component | File | Can be reused for |
|
||||
|-----------|------|-------------------|
|
||||
| User model | `models/user.py` | User identity |
|
||||
| Role model | `models/role.py` | Role definitions |
|
||||
| UserRole | `models/user_role.py` | Role assignment |
|
||||
| compute_effective_policy() | `permissions.py` | Authorization engine |
|
||||
| require_permission() | `auth.py` | Permission checking |
|
||||
| init_db() | `database.py` | Bootstrap |
|
||||
|
||||
---
|
||||
|
||||
## 9. Data Model Gaps
|
||||
|
||||
### 9.1 Missing for DB-driven Authorization
|
||||
|
||||
| Gap | Required for | Priority |
|
||||
|-----|--------------|----------|
|
||||
| Permission table | Permission catalog in DB | HIGH |
|
||||
| RolePermission table | Role → Permission mapping | HIGH |
|
||||
| Default role for JIT | Bootstrap behavior | MEDIUM |
|
||||
|
||||
### 9.2 Missing for Future Features
|
||||
|
||||
| Gap | Required for | Priority |
|
||||
|-----|--------------|----------|
|
||||
| Scope/entity access | Fine-grained authorization | LOW |
|
||||
| Permission metadata | Display names, descriptions | LOW |
|
||||
| Custom roles | Local administration | MEDIUM |
|
||||
|
||||
---
|
||||
|
||||
## 10. Recommended Minimal Data Model
|
||||
|
||||
### 10.1 Entities for TT-007.2
|
||||
|
||||
| Entity | Table | Purpose | New/Existing |
|
||||
|--------|-------|---------|--------------|
|
||||
| User | `users` | User identity | EXISTING |
|
||||
| Role | `roles` | Role definitions | EXISTING |
|
||||
| UserRole | `user_roles` | User → Role | EXISTING |
|
||||
| Permission | `permissions` | Permission catalog | NEW |
|
||||
| RolePermission | `role_permissions` | Role → Permission | NEW |
|
||||
|
||||
### 10.2 Permission Table
|
||||
|
||||
| Column | Type | Nullable | Unique | Default |
|
||||
|--------|------|----------|--------|---------|
|
||||
| id | Integer PK | No | Yes | Auto |
|
||||
| name | String(100) | No | Yes | - |
|
||||
| description | Text | Yes | No | - |
|
||||
| module_id | String(50) | No | No | "request" |
|
||||
| is_system | Boolean | No | No | True |
|
||||
|
||||
### 10.3 RolePermission Table
|
||||
|
||||
| Column | Type | Nullable | Unique | Default |
|
||||
|--------|------|----------|--------|---------|
|
||||
| id | Integer PK | No | Yes | Auto |
|
||||
| role_id | Integer FK | No | No | - |
|
||||
| permission_id | Integer FK | No | No | - |
|
||||
|
||||
**Unique constraint:** (role_id, permission_id)
|
||||
|
||||
### 10.4 Relationships
|
||||
|
||||
```
|
||||
User (1) ──→ (N) UserRole ──→ (1) Role
|
||||
Role (1) ──→ (N) RolePermission ──→ (1) Permission
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 11. Migration Strategy
|
||||
|
||||
### 11.1 Phase 1: Add Tables
|
||||
|
||||
1. Create `permissions` table
|
||||
2. Create `role_permissions` table
|
||||
3. Seed permissions from MODULE_PERMISSION_CATALOG
|
||||
4. Seed role_permissions from ROLE_PERMISSIONS
|
||||
|
||||
### 11.2 Phase 2: Update Engine
|
||||
|
||||
1. Update `compute_effective_policy()` to read from DB
|
||||
2. Keep `ROLE_PERMISSIONS` as fallback during transition
|
||||
3. Add `get_permission_catalog()` to read from DB
|
||||
|
||||
### 11.3 Phase 3: Cleanup
|
||||
|
||||
1. Remove `ROLE_PERMISSIONS` from code
|
||||
2. Remove `MODULE_PERMISSION_CATALOG` from code
|
||||
3. All authorization data in DB
|
||||
|
||||
### 11.4 Safety Measures
|
||||
|
||||
- Keep Python constants as fallback during transition
|
||||
- Seed data on first startup (init_db)
|
||||
- No data loss for existing users
|
||||
|
||||
---
|
||||
|
||||
## 12. Future Compatibility
|
||||
|
||||
### 12.1 Access Profile / Snapshot
|
||||
|
||||
Current model supports:
|
||||
- User → Role → Permission chain
|
||||
- Can add `access_profiles` table later
|
||||
- Can add `permission_snapshots` table later
|
||||
|
||||
### 12.2 AD/LDAP
|
||||
|
||||
Current model supports:
|
||||
- Role is NOT tied to local User only
|
||||
- Can add AD/LDAP Group → Role mapping
|
||||
- Role can be assigned by external source
|
||||
|
||||
### 12.3 Standalone / Distributed
|
||||
|
||||
Current model supports:
|
||||
- All data in local DB
|
||||
- No Platform dependency for authorization
|
||||
- Can work standalone
|
||||
|
||||
---
|
||||
|
||||
## 13. Risks
|
||||
|
||||
| Risk | Description | Mitigation |
|
||||
|------|-------------|------------|
|
||||
| Data loss during migration | Existing users might lose roles | Seed data carefully |
|
||||
| Performance | DB query for each permission check | Add caching later |
|
||||
| Consistency | Code and DB might diverge | Single source of truth |
|
||||
|
||||
---
|
||||
|
||||
## 14. Recommended Implementation Sequence
|
||||
|
||||
### TT-007.2 Scope
|
||||
|
||||
1. Add Permission model
|
||||
2. Add RolePermission model
|
||||
3. Update init_db() to seed data
|
||||
4. Update compute_effective_policy() to read from DB
|
||||
5. Keep Python constants as fallback
|
||||
6. Add tests
|
||||
|
||||
### Future Scope
|
||||
|
||||
1. Custom roles (local admin)
|
||||
2. Permission metadata (display names)
|
||||
3. Scope/entity access
|
||||
4. Caching layer
|
||||
|
||||
---
|
||||
|
||||
## Appendix A: What We Keep
|
||||
|
||||
| Component | File | Reason |
|
||||
|-----------|------|--------|
|
||||
| User model | `models/user.py` | Identity storage |
|
||||
| Role model | `models/role.py` | Role definitions |
|
||||
| UserRole | `models/user_role.py` | Role assignment |
|
||||
| compute_effective_policy() | `permissions.py` | Authorization engine |
|
||||
| require_permission() | `auth.py` | Permission checking |
|
||||
|
||||
## Appendix B: What We Extend
|
||||
|
||||
| Component | File | Change |
|
||||
|-----------|------|--------|
|
||||
| Role model | `models/role.py` | Add relationship to RolePermission |
|
||||
| init_db() | `database.py` | Add permission seeding |
|
||||
| compute_effective_policy() | `permissions.py` | Read from DB |
|
||||
|
||||
## Appendix C: What We Add
|
||||
|
||||
| Component | File | Purpose |
|
||||
|-----------|------|---------|
|
||||
| Permission model | `models/permission.py` | Permission catalog |
|
||||
| RolePermission model | `models/role_permission.py` | Role → Permission mapping |
|
||||
|
||||
## Appendix D: What We Deprecate
|
||||
|
||||
| Component | File | When |
|
||||
|-----------|------|------|
|
||||
| ROLE_PERMISSIONS | `permissions.py` | After DB migration complete |
|
||||
| MODULE_PERMISSION_CATALOG | `permissions.py` | After DB migration complete |
|
||||
|
||||
## Appendix E: What We Don't Do
|
||||
|
||||
- Custom roles UI
|
||||
- Permission metadata
|
||||
- Scope/entity access
|
||||
- Caching layer
|
||||
- AD/LDAP integration
|
||||
- Service Accounts
|
||||
|
||||
## Appendix F: Open Questions
|
||||
|
||||
1. Should JIT users get a default role?
|
||||
2. Should we support permission categories?
|
||||
3. Should we add permission versioning?
|
||||
|
||||
---
|
||||
|
||||
## Final Verdict
|
||||
|
||||
**READY FOR TT-007.2 IMPLEMENTATION**
|
||||
|
||||
Нет блокирующих вопросов. Существующая модель `User → Role → UserRole` может быть расширена до `User → Role → RolePermission → Permission` с минимальными изменениями.
|
||||
Reference in New Issue
Block a user