Federation data and actor keys (phase 1, step 1)
- federation_settings (per user; off, followers-only, not discoverable by default) and federation_keys (RSA-2048; NULL user = instance actor), on the federation migration branch - Private keys encrypted at rest with Fernet, keyed by HKDF-SHA256 from FEDERATION_KEY_SECRET (now required, 32+ chars, never printed) - Keys are created when a user first enables sharing and never regenerated; sharing.shared_user() treats unknown and non-sharing users identically - One flask-smorest Api per app (get_api(app)), so apps with and without federation never share an OpenAPI spec - Test fixtures for a second, federation-enabled app on the same DB Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
1 parent
00ad8e9e6d
commit
98c65547f3
12 files changed
+513
-5
No files matched your search
@@ -142,3 +142,43 @@ def ui_token(user):
|
||||
def auth(ui_token):
|
||||
"""Authorization headers for the default user."""
|
||||
return {"Authorization": f"Bearer {ui_token}"}
|
||||
|
||||
|
||||
# --- Federation ---------------------------------------------------------------------
|
||||
|
||||
from scrobbler.config import TestConfig as _TestConfig # noqa: E402
|
||||
|
||||
|
||||
class FederatedTestConfig(_TestConfig):
|
||||
FEDERATION_ENABLED = "1"
|
||||
FEDERATION_DOMAIN = "scrobble.test"
|
||||
FEDERATION_BASE_URL = "https://scrobble.test"
|
||||
FEDERATION_KEY_SECRET = "test-only-federation-key-secret-0123456789"
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def fed_app(app):
|
||||
"""A second app, with federation on, sharing the test database."""
|
||||
return create_app(FederatedTestConfig)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fed_client(fed_app):
|
||||
return fed_app.test_client()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fed_ctx(fed_app):
|
||||
"""Run the test inside the federation app's context (for services using current_app)."""
|
||||
with fed_app.app_context():
|
||||
yield
|
||||
db.session.remove()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sharing_user(fed_ctx, user):
|
||||
"""The default user, with sharing switched on."""
|
||||
from scrobbler.federation import sharing
|
||||
|
||||
sharing.update(user.id, {"enabled": True, "display_name": "Alice", "bio": "Listening."})
|
||||
return user
|
||||
@@ -168,7 +168,8 @@ def test_nothing_is_registered_when_federation_is_off(app):
|
||||
class _Enabled(TestConfig):
|
||||
FEDERATION_ENABLED = "1"
|
||||
FEDERATION_DOMAIN = "scrobble.example"
|
||||
FEDERATION_BASE_URL = "https://api.scrobble.example"
|
||||
FEDERATION_BASE_URL = "https://scrobble.example"
|
||||
FEDERATION_KEY_SECRET = "x" * 32
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -178,6 +179,7 @@ class _Enabled(TestConfig):
|
||||
({"FEDERATION_DOMAIN": "https://scrobble.example"}, "FEDERATION_DOMAIN"),
|
||||
({"FEDERATION_BASE_URL": "http://api.scrobble.example"}, "FEDERATION_BASE_URL"),
|
||||
({"FEDERATION_BASE_URL": ""}, "FEDERATION_BASE_URL"),
|
||||
({"FEDERATION_KEY_SECRET": "too-short"}, "FEDERATION_KEY_SECRET"),
|
||||
],
|
||||
)
|
||||
def test_enabling_federation_requires_valid_settings(overrides, message):
|
||||
@@ -192,5 +194,6 @@ def test_federation_can_be_enabled():
|
||||
assert (config.enabled, config.domain, config.base_url) == (
|
||||
True,
|
||||
"scrobble.example",
|
||||
"https://api.scrobble.example",
|
||||
"https://scrobble.example",
|
||||
)
|
||||
assert "x" * 32 not in repr(config)
|
||||
@@ -0,0 +1,72 @@
|
||||
import pytest
|
||||
from cryptography.fernet import InvalidToken
|
||||
from cryptography.hazmat.primitives import hashes, serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import padding
|
||||
|
||||
from scrobbler.extensions import db
|
||||
from scrobbler.federation import keys, sharing
|
||||
from scrobbler.federation.models import FederationKey
|
||||
|
||||
|
||||
def test_defaults_are_private(fed_ctx, user):
|
||||
settings = sharing.settings_for(user.id)
|
||||
assert (settings.enabled, settings.visibility) == (False, "followers")
|
||||
assert not (settings.discoverable or settings.indexable or settings.manually_approves_followers)
|
||||
assert sharing.shared_user("alice") is None
|
||||
|
||||
|
||||
def test_enabling_creates_a_key_pair_that_signs(fed_ctx, user):
|
||||
_, toggled = sharing.update(user.id, {"enabled": True})
|
||||
assert toggled is True
|
||||
key = db.session.scalar(db.select(FederationKey).filter_by(user_id=user.id))
|
||||
assert key.public_key_pem.startswith("-----BEGIN PUBLIC KEY-----")
|
||||
assert b"PRIVATE KEY" not in key.private_key_encrypted # encrypted at rest
|
||||
|
||||
message = b"hello fediverse"
|
||||
signature = keys.private_key(key).sign(message, padding.PKCS1v15(), hashes.SHA256())
|
||||
public = serialization.load_pem_public_key(key.public_key_pem.encode())
|
||||
public.verify(signature, message, padding.PKCS1v15(), hashes.SHA256()) # no exception
|
||||
|
||||
|
||||
def test_keys_survive_disable_and_enable(fed_ctx, user):
|
||||
sharing.update(user.id, {"enabled": True})
|
||||
first = keys.key_for(user.id).public_key_pem
|
||||
assert sharing.update(user.id, {"enabled": False})[1] is False
|
||||
assert sharing.update(user.id, {"enabled": True})[1] is True
|
||||
assert keys.key_for(user.id).public_key_pem == first
|
||||
|
||||
|
||||
def test_the_wrong_secret_cannot_decrypt(fed_ctx, fed_app, user):
|
||||
sharing.update(user.id, {"enabled": True})
|
||||
key = keys.key_for(user.id)
|
||||
real = fed_app.extensions["federation"]
|
||||
fed_app.extensions["federation"] = type(real)(
|
||||
enabled=True, domain=real.domain, base_url=real.base_url, key_secret="y" * 40
|
||||
)
|
||||
try:
|
||||
with pytest.raises(InvalidToken):
|
||||
keys.private_key(key)
|
||||
finally:
|
||||
fed_app.extensions["federation"] = real
|
||||
|
||||
|
||||
def test_instance_actor_key(fed_ctx):
|
||||
assert keys.key_for(None).user_id is None
|
||||
assert keys.key_for(None).id == keys.key_for(None).id # created once
|
||||
|
||||
|
||||
def test_shared_user_is_case_insensitive_and_requires_sharing(fed_ctx, user, make_user):
|
||||
sharing.update(user.id, {"enabled": True})
|
||||
make_user(username="bob") # exists, not sharing
|
||||
assert sharing.shared_user("ALICE")[0].id == user.id
|
||||
assert sharing.shared_user("bob") is None
|
||||
assert sharing.shared_user("nobody") is None
|
||||
assert sharing.sharing_count() == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"changes", [{"visibility": "everyone"}, {"user_id": 99}, {"enabled": True, "handle": "x"}]
|
||||
)
|
||||
def test_invalid_changes_are_rejected(fed_ctx, user, changes):
|
||||
with pytest.raises(ValueError):
|
||||
sharing.update(user.id, changes)
|
||||
Reference in new issue
Block a user