Read and decrypt a Fernet-encrypted JSON file, returning *None* on failure.
(path: Path)
| 185 | |
| 186 | |
| 187 | def _read_encrypted(path: Path) -> dict[str, Any] | None: |
| 188 | """Read and decrypt a Fernet-encrypted JSON file, returning *None* on failure.""" |
| 189 | if not path.exists(): |
| 190 | return None |
| 191 | try: |
| 192 | ciphertext = path.read_bytes() |
| 193 | plaintext = _get_fernet().decrypt(ciphertext) |
| 194 | data = json.loads(plaintext) |
| 195 | if isinstance(data, dict): |
| 196 | return data |
| 197 | except (InvalidToken, json.JSONDecodeError, OSError) as exc: |
| 198 | log.debug("Could not decrypt %s: %s", path, exc) |
| 199 | return None |
| 200 | |
| 201 | |
| 202 | def _write_secure(path: Path, data: dict[str, Any]) -> None: |
no test coverage detected