mirror of
https://github.com/taylanbakircioglu/haproxy-openmanager.git
synced 2026-09-12 05:48:58 +00:00
eee0a4716a
Closes the follow-up filed during the v1.9.0 CSR review. The private key of a
PENDING CSR is now Fernet-encrypted in the database instead of being stored as
a raw PEM.
Why this key specifically: it is the one key in the system that sits idle. It
is generated at CSR creation, waits for an external CA to sign the request
(days to weeks), and is destroyed the moment the signed certificate is
imported. It is never transmitted to an agent and never leaves the server.
ssl_certificates.private_key_content and the ACME order keys are deliberately
NOT covered, because agents must receive those in plaintext on every poll, so
encrypting them at rest buys nothing without an end-to-end redesign.
Implementation follows the pattern already used for the VRRP secret, TOTP
secrets and DNS provider credentials: a new utils/csr_key_crypto.py with its
own CSR_ENCRYPTION_KEY env var and its own HKDF info string
("csr-private-key-v1"), so rotating one secret class never affects another.
No schema change and deliberately NO SCHEMA_VERSION bump: the Fernet token
replaces the PEM inside the existing ssl_csrs.private_key_pem TEXT column. A
bump would re-run the migration sequence and re-seed the four built-in roles to
their defaults, which is a needless side effect for a storage-format change.
Backward compatible with no data migration. Rows written before this release
hold a raw PEM and are still read unchanged; the discriminator is exact rather
than a heuristic, since a Fernet token is base64url and can never contain the
"-----BEGIN" marker. Legacy rows drain naturally because a CSR's key copy is
NULLed on import.
A key that cannot be decrypted (SECRET_KEY rotated while CSR_ENCRYPTION_KEY was
unset) now fails with an explicit "delete this CSR and create a new one" error.
Previously that situation would have surfaced as the far more confusing
"certificate does not match this CSR's private key".
Also documents all four per-purpose encryption keys in .env.template. Only
VIP_ENCRYPTION_KEY was listed; MFA_ENCRYPTION_KEY and
DNS_PROVIDER_ENCRYPTION_KEY had been missing since v1.6.0 and v1.8.0.
Verified before release, on a corporate pre-production environment and locally:
- Full backend suite 1234 -> 1243 passed (+9 new tests), 0 failed.
- Against a real Postgres: a CSR created through the API stores a Fernet token
with no PEM header in the column, and imports successfully.
- Full 1.10.0 -> 1.10.1 -> 1.10.0 drill on one database volume. The upgrade
logs "Schema already at version 10 (>= 10); skipping migration run", so no
migration executes and the built-in roles are not re-seeded. A CSR created on
1.10.0 with a plaintext key imports successfully after the upgrade, which is
the backward-compatibility guarantee proven against a real row rather than a
mock.
- rsa-2048, rsa-4096 and ecdsa-p384 all round-trip through create, encrypt,
decrypt and import.
- Key derivation is stable across processes: two independent containers sharing
SECRET_KEY decrypt each other's tokens (required for UVICORN_WORKERS > 1 and
multi-replica deployments), while a different SECRET_KEY yields None rather
than a wrong key or an exception.
- Downgrade behaviour was measured, not assumed: 1.10.0 cannot parse the token
and fails with HTTP 500 "key parse failed (encrypted?)" rather than pairing a
wrong key. The rollback note states the measured behaviour.
- No CSR endpoint returns the key in any form: list and detail responses
contain neither a PEM nor a Fernet token.
Not changed here, from the issue's "worth folding in" list: the create rate
limit is not a concurrency guard, create_csr holds a pooled connection across
RSA key generation, detail=str(e) echoes internal error text (a repo-wide
convention), and is_global skips cluster validation in both routers/ssl.py and
routers/csr.py. None are storage concerns and each is a separate change.
HAProxy Management UI - Unit Tests
Overview
Comprehensive unit test suite for the HAProxy Management UI backend, covering critical business logic and ensuring reliability.
Test Structure
Backend Tests (backend/tests/)
test_soft_delete.py- Soft delete functionality and unique constraintstest_apply_process.py- Critical apply process that manages entity statestest_entity_sync.py- Entity-specific agent sync calculationstest_haproxy_config.py- HAProxy configuration generationtest_auth.py- Authentication and authorization
Frontend Tests (frontend/src/components/__tests__/)
EntitySyncStatus.test.js- Agent sync status componentApplyManagement.test.js- Apply management workflowSSLManagement.test.js- SSL certificate management
Running Tests
Backend Tests
# Install test dependencies
pip install -r backend/requirements-test.txt
# Run all tests
pytest
# Run specific test file
pytest backend/tests/test_apply_process.py
# Run with coverage
pytest --cov=backend --cov-report=html
# Run specific test
pytest backend/tests/test_soft_delete.py::TestSoftDeleteUniqueConstraints::test_backend_soft_delete_allows_name_reuse
Frontend Tests
# Run all frontend tests
npm test
# Run with coverage
npm run test:coverage
# Run in CI mode
npm run test:ci
Test Coverage Goals
- Backend: 70% minimum coverage
- Frontend: 70% minimum coverage
- Critical paths: 90%+ coverage (apply process, soft delete, entity sync)
Critical Test Areas
🔴 HIGH PRIORITY
- Apply Process - Prevents entity disappearance bugs
- Soft Delete Logic - Ensures proper unique constraint handling
- Entity Sync Calculations - Agent sync status accuracy
- Authentication/Authorization - Security validation
🟡 MEDIUM PRIORITY
- HAProxy Config Generation - Configuration correctness
- SSL Management - Certificate lifecycle
- Form Validations - Input validation
🟢 LOW PRIORITY
- UI Components - Visual behavior
- Utility Functions - Helper functions
Mock Strategy
Backend Mocking
- Database connections:
AsyncMockfor database operations - External APIs: Mock HTTP calls
- File operations: Mock file system access
Frontend Mocking
- API calls: Mock axios requests
- Ant Design components: Mock component behavior
- Context providers: Mock React contexts
Test Data
All tests use consistent mock data from conftest.py:
- Sample clusters, backends, frontends
- Mock users and authentication
- Config versions and SSL certificates
Debugging Tests
# Run with verbose output
pytest -v -s
# Run specific failing test
pytest backend/tests/test_apply_process.py::TestApplyProcess::test_apply_process_preserves_active_entities -v -s
# Drop into debugger on failure
pytest --pdb
Integration with CI/CD
Tests are designed to run in Azure DevOps pipeline:
# Example pipeline step
- script: |
pip install -r backend/requirements-test.txt
pytest --cov=backend --cov-report=xml
displayName: 'Run Backend Tests'
- script: |
npm ci
npm run test:ci
displayName: 'Run Frontend Tests'
Adding New Tests
- Follow naming convention:
test_*.pyfor backend,*.test.jsfor frontend - Use appropriate fixtures: Leverage existing mock data
- Test edge cases: Include error scenarios and boundary conditions
- Update coverage: Ensure new code maintains coverage thresholds
Common Issues
Backend
- Async tests: Use
@pytest.mark.asynciodecorator - Database mocking: Ensure proper mock setup for database operations
- Import paths: Use relative imports for testable modules
Frontend
- Component rendering: Wait for async operations with
waitFor - Event simulation: Use
fireEventfor user interactions - Mock cleanup: Clear mocks between tests with
jest.clearAllMocks()
Test Philosophy
These tests focus on:
- Business logic correctness over implementation details
- Critical path coverage over 100% coverage
- Regression prevention based on actual bugs encountered
- Maintainability with clear, readable test cases
The test suite is designed to catch the types of bugs we've actually encountered in production, particularly around the apply process and soft delete behavior.
Test deployment trigger - $(date)