Commit c189c6d
[AAP-76852] Fix: catch InvalidToken in from_db() methods to log CRITICAL before crash (#1044)
## Summary
When `SECRET_KEY` changes (e.g., after `podman system reset` destroys
the Podman secret and the installer regenerates a new one), all existing
encrypted database rows become undecryptable. Two `from_db()` methods in
`django-ansible-base` called `decrypt_string()` with no error handling
for `cryptography.fernet.InvalidToken`, causing unhandled exceptions
that crash the service on startup with no actionable diagnostic output.
## Root Cause
- **`AbstractCommonModel.from_db()`**
(`ansible_base/lib/abstract_models/common.py:197-205`) — iterates over
`encrypted_fields` calling `ansible_encryption.decrypt_string()` with no
guard. Affects `ServiceKey.secret`, `AuthenticatorUser.extra_data`, and
any other model with `encrypted_fields`.
- **`Authenticator.from_db()`**
(`ansible_base/authentication/models/authenticator.py:76-90`) — has a
`try/except ImportError` but does not catch `InvalidToken`. Encrypted
authenticator config fields (LDAP `BIND_PASSWORD`, OIDC `SECRET`, SAML
`SP_PRIVATE_KEY`) cause an unhandled crash.
Per the staff engineering decision (June 18): catch `InvalidToken`, log
CRITICAL with model/PK/recovery context, then re-raise. This preserves
the crash behaviour while providing actionable diagnostic output before
the system fails.
## Changes
- `ansible_base/lib/abstract_models/common.py`: Wrap each
`decrypt_string()` call in the `encrypted_fields` loop in `from_db()`
with `try/except InvalidToken`. Log CRITICAL with model class name,
field name, and PK. Re-raise unchanged.
- `ansible_base/authentication/models/authenticator.py`: Add `except
InvalidToken` clause in `from_db()` alongside the existing `except
ImportError`. Log CRITICAL with authenticator name and type. Re-raise
unchanged. Add `from cryptography.fernet import InvalidToken` import and
a module-level logger.
- `test_app/tests/lib/abstract_models/test_common.py`: Add
`test_from_db_invalid_token_logs_and_reraises` — patches
`decrypt_string` to raise `InvalidToken`, asserts re-raise and CRITICAL
log.
- `test_app/tests/authentication/models/test_authenticator.py`: Add
`test_authenticator_from_db_invalid_token_logs_and_reraises` — same
pattern for the LDAP authenticator fixture.
## Risk Assessment
- **Confidence:** high
- **Risk:** low — exception is re-raised so runtime behaviour is
unchanged; the only addition is the CRITICAL log message before the
crash; no data is modified; no migrations; no API changes
- **Scope:** 4 files changed
## Testing
- [ ] Unit tests pass
- [ ] Regression test added (test patches decrypt_string to raise
InvalidToken, asserts re-raise + CRITICAL log)
- [ ] Lint checks pass
## JIRA
- Ticket: [AAP-76852](https://issues.redhat.com/browse/AAP-76852)
---
*Assisted-by: Debuggernaut (claude-opus-4-6) <noreply@redhat.com> |
Review carefully before merging.*
[AAP-76852]:
https://redhat.atlassian.net/browse/AAP-76852?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved error handling when encrypted configuration or model data
cannot be decrypted.
* Added critical logging with relevant details to make decryption
failures easier to diagnose.
* Prevented invalid encrypted data from being loaded silently by
preserving the original error.
* **Tests**
* Added coverage confirming decryption failures are logged and correctly
re-raised.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Aegis-bot <bot@ambient-code.local>
Co-authored-by: jeffh-oss <jheadley@redhat.com>
Co-authored-by: Cursor <cursoragent@cursor.com>1 parent f313c89 commit c189c6d
4 files changed
Lines changed: 75 additions & 1 deletion
File tree
- ansible_base
- authentication/models
- lib/abstract_models
- test_app/tests
- authentication/models
- lib/abstract_models
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
1 | 4 | | |
2 | 5 | | |
3 | 6 | | |
4 | 7 | | |
5 | 8 | | |
6 | 9 | | |
| 10 | + | |
| 11 | + | |
7 | 12 | | |
8 | 13 | | |
9 | 14 | | |
| |||
86 | 91 | | |
87 | 92 | | |
88 | 93 | | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
89 | 104 | | |
90 | 105 | | |
91 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
| 199 | + | |
| 200 | + | |
199 | 201 | | |
200 | 202 | | |
201 | 203 | | |
202 | 204 | | |
203 | | - | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
204 | 217 | | |
205 | 218 | | |
206 | 219 | | |
| |||
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
| |||
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
366 | 366 | | |
367 | 367 | | |
368 | 368 | | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
0 commit comments