feat(request): add service accounts and unified authorization
This commit is contained in:
@@ -0,0 +1,377 @@
|
||||
# Implementation Report — TT-008 Service Accounts
|
||||
# Отчёт о реализации — TT-008 Service Accounts
|
||||
|
||||
## Общая информация / General Information
|
||||
|
||||
| Поле / Field | Значение / Value |
|
||||
|--------------|------------------|
|
||||
| Номер задачи / Task number | TT-008 |
|
||||
| Дата выполнения / Date | 2026-08-06 |
|
||||
| Исполнитель / Author | AI agent |
|
||||
| Ветка / Branch | feature/modular-platform |
|
||||
| Статус / Status | READY FOR REVIEW |
|
||||
|
||||
---
|
||||
|
||||
## 1. Implemented Architecture / Реализованная архитектура
|
||||
|
||||
### Principal Model
|
||||
|
||||
```text
|
||||
Principal
|
||||
├── User (human, email/password)
|
||||
└── ServiceAccount (machine, credential-based)
|
||||
```
|
||||
|
||||
Service Account — отдельная сущность, НЕ подтип User.
|
||||
Service Account is a separate entity, NOT a User subtype.
|
||||
|
||||
### Selected Principal → Role Implementation
|
||||
|
||||
Выбран вариант с отдельной таблицей `service_account_roles`:
|
||||
- ServiceAccountRole связывает ServiceAccount с Role
|
||||
- Role, Permission, RolePermission переиспользованы без изменений
|
||||
- compute_effective_policy() работает с generic Principal через isinstance()
|
||||
|
||||
Selected approach with separate `service_account_roles` table:
|
||||
- ServiceAccountRole links ServiceAccount to Role
|
||||
- Role, Permission, RolePermission reused without changes
|
||||
- compute_effective_policy() works with generic Principal via isinstance()
|
||||
|
||||
---
|
||||
|
||||
## 2. Credential Mechanism / Механизм credentials
|
||||
|
||||
### Selected: API Key with bcrypt hashing
|
||||
|
||||
- Identifier: stable string (not email)
|
||||
- Secret: cryptographically random (secrets.token_urlsafe(48))
|
||||
- Storage: bcrypt hash only
|
||||
- Format: `identifier:secret` in Authorization header
|
||||
|
||||
### Why replaceable
|
||||
|
||||
- Credential validation is isolated in `service_account_auth.py`
|
||||
- Hash function is configurable
|
||||
- Secret generation is configurable
|
||||
- No hardcoded protocol assumptions
|
||||
|
||||
---
|
||||
|
||||
## 3. Lifecycle
|
||||
|
||||
```text
|
||||
CREATED → ACTIVE → DISABLED → REVOKED
|
||||
```
|
||||
|
||||
| State | Description | Terminal |
|
||||
|-------|-------------|----------|
|
||||
| CREATED | Initial state | No |
|
||||
| ACTIVE | Can authenticate | No |
|
||||
| DISABLED | Temporarily blocked | No |
|
||||
| REVOKED | Permanently blocked | Yes |
|
||||
|
||||
Credential lifecycle is SEPARATE from Principal lifecycle.
|
||||
|
||||
---
|
||||
|
||||
## 4. Authorization Changes
|
||||
|
||||
### Generic Principal support
|
||||
|
||||
`compute_effective_policy()` updated to accept both User and ServiceAccount:
|
||||
|
||||
```python
|
||||
def compute_effective_policy(principal: Union[User, ServiceAccount], db: Session):
|
||||
if isinstance(principal, User):
|
||||
role_ids = [ur.role_id for ur in principal.user_roles]
|
||||
elif isinstance(principal, ServiceAccount):
|
||||
role_ids = [sar.role_id for sar in principal.roles]
|
||||
```
|
||||
|
||||
### Same authorization engine
|
||||
|
||||
- User and ServiceAccount use the same `compute_effective_policy()`
|
||||
- No separate ServiceAccount permission engine
|
||||
- `require_permission()` works for User
|
||||
- `require_sa_permission()` added for ServiceAccount
|
||||
|
||||
---
|
||||
|
||||
## 5. API / Управление
|
||||
|
||||
| Endpoint | Method | Description |
|
||||
|----------|--------|-------------|
|
||||
| `/service-accounts` | POST | Create SA (returns secret once) |
|
||||
| `/service-accounts` | GET | List all SAs |
|
||||
| `/service-accounts/{id}` | GET | Get SA metadata |
|
||||
| `/service-accounts/{id}/disable` | POST | Disable SA |
|
||||
| `/service-accounts/{id}/enable` | POST | Enable SA |
|
||||
| `/service-accounts/{id}/revoke` | POST | Revoke SA (terminal) |
|
||||
| `/service-accounts/{id}/rotate-credential` | POST | Rotate credential |
|
||||
| `/service-accounts/{id}/roles` | PUT | Update roles |
|
||||
|
||||
Secret returned only at creation. List/Get never return credentials.
|
||||
|
||||
---
|
||||
|
||||
## 6. Audit
|
||||
|
||||
### Events tracked
|
||||
|
||||
- created, activated, disabled, enabled, revoked
|
||||
- credential_issued, credential_rotated, credential_revoked
|
||||
- role_assigned, role_removed
|
||||
- authentication_success, authentication_failure
|
||||
|
||||
### Security
|
||||
|
||||
- NEVER logs plaintext secrets
|
||||
- NEVER logs passwords
|
||||
- NEVER logs sensitive token material
|
||||
- Audit is accountability, NOT tamper protection
|
||||
|
||||
---
|
||||
|
||||
## 7. Migration
|
||||
|
||||
### Tables created
|
||||
|
||||
| Table | Purpose |
|
||||
|-------|---------|
|
||||
| service_accounts | Service Account Principal |
|
||||
| service_account_credentials | Credential storage (hashed) |
|
||||
| service_account_roles | SA → Role mapping |
|
||||
| service_account_audit_events | Security audit trail |
|
||||
|
||||
### Migration file
|
||||
|
||||
`migrations/alembic/versions/002_add_service_accounts.py`
|
||||
|
||||
- down_revision = '001_add_permissions'
|
||||
- Reversible (upgrade/downgrade)
|
||||
- Existing tables unaffected
|
||||
|
||||
---
|
||||
|
||||
## 8. Platform Boundary
|
||||
|
||||
### Platform can:
|
||||
- See SA metadata/status via Module API
|
||||
- Initiate management operations via approved contract
|
||||
|
||||
### Platform cannot:
|
||||
- Directly create SA in Module DB
|
||||
- Directly assign Module roles
|
||||
- Directly modify credentials
|
||||
- Directly modify lifecycle
|
||||
|
||||
ServiceAccount remains Module-owned.
|
||||
|
||||
---
|
||||
|
||||
## 9. Platform Outage Behavior
|
||||
|
||||
Service Account authentication works WITHOUT Platform:
|
||||
|
||||
```text
|
||||
Platform unavailable
|
||||
↓
|
||||
local credential validation (bcrypt)
|
||||
↓
|
||||
local Principal (ServiceAccount)
|
||||
↓
|
||||
local authorization (DB-driven)
|
||||
↓
|
||||
ALLOW / DENY
|
||||
```
|
||||
|
||||
No Platform API calls required for SA authentication.
|
||||
|
||||
---
|
||||
|
||||
## 10. Security Limitations
|
||||
|
||||
### Security-sensitive DB fields
|
||||
|
||||
| Field | Table | Risk |
|
||||
|-------|-------|------|
|
||||
| state | service_accounts | Lifecycle bypass |
|
||||
| secret_hash | service_account_credentials | Credential theft |
|
||||
| role_id | service_account_roles | Permission escalation |
|
||||
|
||||
### Current protection
|
||||
|
||||
- Application authorization on all management endpoints
|
||||
- bcrypt hashing for credentials
|
||||
- Audit trail for accountability
|
||||
|
||||
### Future requirements
|
||||
|
||||
- Authorization artifact for tamper-proof authority
|
||||
- NOT implemented in this TT
|
||||
|
||||
---
|
||||
|
||||
## 11. Explicitly Deferred
|
||||
|
||||
- Vendor Break-Glass / Emergency Authority
|
||||
- Vendor Authorization artifact
|
||||
- Emergency cryptography
|
||||
- Corporate licensing gate
|
||||
- LDAP/AD integration
|
||||
- Offline authorization with TTL
|
||||
- Policy distribution
|
||||
- Remote deployment
|
||||
|
||||
---
|
||||
|
||||
## 12. Tests and Results
|
||||
|
||||
### New tests (22 tests)
|
||||
|
||||
| Category | Tests | Status |
|
||||
|----------|-------|--------|
|
||||
| Model existence | 4 | ✅ PASSED |
|
||||
| Lifecycle | 4 | ✅ PASSED |
|
||||
| Credential | 8 | ✅ PASSED |
|
||||
| Authorization | 4 | ✅ PASSED |
|
||||
| Platform outage | 1 | ✅ PASSED |
|
||||
| Same engine | 1 | ✅ PASSED |
|
||||
|
||||
### Regression
|
||||
|
||||
| Suite | Tests | Status |
|
||||
|-------|-------|--------|
|
||||
| Request Module | 50 | ✅ ALL PASSED |
|
||||
| Platform Backend | 27 | ✅ ALL PASSED |
|
||||
| Frontend build | - | ✅ OK (1.01s) |
|
||||
|
||||
---
|
||||
|
||||
## 13. Files Changed / Изменённые файлы
|
||||
|
||||
### New files
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `request_app/models/service_account.py` | ServiceAccount model |
|
||||
| `request_app/models/service_account_role.py` | SA → Role mapping |
|
||||
| `request_app/models/service_account_credential.py` | Credential storage |
|
||||
| `request_app/models/service_account_audit.py` | Audit events |
|
||||
| `request_app/core/service_account_auth.py` | Credential operations |
|
||||
| `request_app/api/v1/routes/service_accounts.py` | Management API |
|
||||
| `migrations/alembic/versions/002_add_service_accounts.py` | Migration |
|
||||
| `tests/smoke/test_service_accounts.py` | Tests |
|
||||
|
||||
### Modified files
|
||||
|
||||
| File | Change |
|
||||
|------|--------|
|
||||
| `request_app/models/__init__.py` | Added new model imports |
|
||||
| `request_app/api/v1/routes/permissions.py` | Generic Principal support |
|
||||
| `request_app/core/auth.py` | Added SA authentication |
|
||||
| `module.py` | Registered service_accounts router |
|
||||
|
||||
---
|
||||
|
||||
## 14. Known Follow-up Tasks
|
||||
|
||||
1. Alembic migration execution on real DB
|
||||
2. Frontend management UI for Service Accounts
|
||||
3. Vendor Break-Glass / Emergency Authority (separate TT)
|
||||
4. Authorization artifact for tamper-proof authority
|
||||
5. LDAP/AD integration
|
||||
6. Remote deployment support
|
||||
|
||||
---
|
||||
|
||||
## Agent Review Checklist
|
||||
|
||||
### Principal
|
||||
- [x] ServiceAccount is NOT User subtype
|
||||
- [x] No `User.source="service_account"` as primary design
|
||||
- [x] Generic Principal boundary exists
|
||||
|
||||
### Authorization
|
||||
- [x] Role/Permission/RolePermission reused
|
||||
- [x] No second authorization engine
|
||||
- [x] `compute_effective_policy()` generic
|
||||
- [x] Existing User authorization preserved
|
||||
|
||||
### Credentials
|
||||
- [x] Plaintext secret NEVER stored
|
||||
- [x] Secret shown only at creation
|
||||
- [x] Rotation works
|
||||
- [x] Revocation works
|
||||
- [x] Disabled/Revoked cannot authenticate
|
||||
|
||||
### Lifecycle
|
||||
- [x] CREATED exists
|
||||
- [x] ACTIVE exists
|
||||
- [x] DISABLED exists
|
||||
- [x] REVOKED exists (terminal)
|
||||
- [x] Credential lifecycle separated
|
||||
|
||||
### Platform
|
||||
- [x] Platform does NOT own ServiceAccount
|
||||
- [x] No direct DB access
|
||||
- [x] Authentication does NOT require Platform
|
||||
|
||||
### Break-Glass
|
||||
- [x] Nothing implemented
|
||||
- [x] No emergency_admin role
|
||||
- [x] No Vendor root token
|
||||
|
||||
### Security
|
||||
- [x] Secrets absent from list/get
|
||||
- [x] Secrets absent from logs/audit
|
||||
- [x] Security-sensitive fields documented
|
||||
- [x] SQL tampering limitation documented
|
||||
|
||||
### Migration
|
||||
- [x] Alembic migration created
|
||||
- [x] Downgrade defined
|
||||
- [x] Existing tables unaffected
|
||||
|
||||
### Tests
|
||||
- [x] New tests pass (22)
|
||||
- [x] Existing Request Module tests pass (50)
|
||||
- [x] Platform Backend tests pass (27)
|
||||
- [x] Frontend build passes
|
||||
|
||||
---
|
||||
|
||||
## Final Status
|
||||
|
||||
```text
|
||||
TT-008 Implementation completed
|
||||
|
||||
Status: READY FOR REVIEW
|
||||
|
||||
Changed files:
|
||||
- New: 8 files (models, auth, API, migration, tests)
|
||||
- Modified: 4 files (init, permissions, auth, module)
|
||||
|
||||
Tests:
|
||||
- New: 22 tests (all passed)
|
||||
- Regression: 77 tests (all passed)
|
||||
|
||||
Migration:
|
||||
- 002_add_service_accounts.py
|
||||
- 4 tables: service_accounts, credentials, roles, audit
|
||||
|
||||
Security notes:
|
||||
- Plaintext secrets never stored
|
||||
- bcrypt hashing
|
||||
- Audit trail for accountability
|
||||
- SQL tampering not fully prevented (documented)
|
||||
|
||||
Deferred:
|
||||
- Break-Glass / Emergency Authority
|
||||
- Authorization artifact
|
||||
- LDAP/AD integration
|
||||
```
|
||||
|
||||
Final commit выполняет только человек — финальный приёмщик проекта.
|
||||
Final commit is performed only by the human final reviewer.
|
||||
Reference in New Issue
Block a user