-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add encryption to db backups/exports.
- Loading branch information
Showing
7 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# SPDX-FileCopyrightText: 2024 Mark Liffiton <[email protected]> | ||
# | ||
# SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import platform | ||
import subprocess | ||
from pathlib import Path | ||
from tempfile import NamedTemporaryFile, TemporaryDirectory | ||
|
||
|
||
def test_db_download_status(app, monkeypatch): | ||
"""Test that db_download_status correctly reflects encryption availability""" | ||
with app.app_context(): | ||
from gened.admin import inject_db_download_status | ||
|
||
# No key configured | ||
app.config['AGE_PUBLIC_KEY'] = None | ||
status = inject_db_download_status()['db_download_status'] | ||
assert not status.encrypted | ||
assert status.reason is not None | ||
assert "No encryption key configured" in status.reason | ||
|
||
# Mock Windows platform | ||
monkeypatch.setattr(platform, "system", lambda: "Windows") | ||
app.config['AGE_PUBLIC_KEY'] = "ssh-ed25519 AAAAC3..." | ||
status = inject_db_download_status()['db_download_status'] | ||
assert not status.encrypted | ||
assert status.reason is not None | ||
assert "Windows" in status.reason | ||
|
||
# Mock non-Windows platform | ||
monkeypatch.setattr(platform, "system", lambda: "Linux") | ||
status = inject_db_download_status()['db_download_status'] | ||
assert status.encrypted | ||
assert status.reason is None | ||
|
||
|
||
def test_backup_db_encryption(app): | ||
"""Test that backup_db handles encryption configuration correctly""" | ||
with app.app_context(): | ||
from gened.db import backup_db | ||
|
||
# Test unencrypted backup (no key configured) | ||
app.config['AGE_PUBLIC_KEY'] = None | ||
with NamedTemporaryFile() as backup_file: | ||
backup_db(Path(backup_file.name)) | ||
header = backup_file.read(16) | ||
assert header.startswith(b'SQLite format 3') # unencrypted SQLite file | ||
|
||
# Test encrypted backup with a real SSH key (test does not run on Windows, where gened does not support this) | ||
if platform.system() == "Windows": | ||
return | ||
|
||
with TemporaryDirectory() as temp_dir: | ||
# Generate SSH keypair | ||
key_path = Path(temp_dir) / "temp_key" | ||
subprocess.run( | ||
["ssh-keygen", "-t", "ed25519", "-N", "", "-f", str(key_path)], | ||
check=True, capture_output=True | ||
) | ||
pubkey = (key_path.with_suffix(".pub")).read_text().strip() | ||
|
||
# Configure and test encryption | ||
app.config['AGE_PUBLIC_KEY'] = pubkey | ||
|
||
backup_path = Path(temp_dir) / "backup.db" | ||
backup_db(backup_path) | ||
with backup_path.open('rb') as f: | ||
header = f.read(6) | ||
assert header == b'age-en' # age encryption header |