diff options
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/db')
| -rw-r--r-- | packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/f8a9b0c1d2e3_node_keys.py | 63 | ||||
| -rw-r--r-- | packages/meshbay-hub/src/meshbay_hub/db/models.py | 23 |
2 files changed, 85 insertions, 1 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/f8a9b0c1d2e3_node_keys.py b/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/f8a9b0c1d2e3_node_keys.py new file mode 100644 index 0000000..b02167b --- /dev/null +++ b/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/f8a9b0c1d2e3_node_keys.py @@ -0,0 +1,63 @@ +"""several node keys per account: one per machine the person runs a node on + +Revision ID: f8a9b0c1d2e3 +Revises: e7f8a9b0c1d2 +""" + +import uuid +from collections.abc import Sequence +from datetime import UTC, datetime + +import sqlalchemy as sa +from alembic import op + +revision: str = "f8a9b0c1d2e3" +down_revision: str | Sequence[str] | None = "e7f8a9b0c1d2" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + node_keys = op.create_table( + "node_keys", + sa.Column("id", sa.String(36), primary_key=True), + sa.Column("user_id", sa.String(36), sa.ForeignKey("users.id"), nullable=False), + sa.Column("pk_node_ed25519", sa.String(64), nullable=False), + sa.Column("linked_at", sa.DateTime(timezone=True)), + ) + op.create_index("uq_node_keys_user_key", "node_keys", + ["user_id", "pk_node_ed25519"], unique=True) + + # The key each account had linked keeps working: it becomes that account's + # first row, so no node has to be linked again after the upgrade. + conn = op.get_bind() + linked = conn.execute(sa.text( + "SELECT id, pk_node_ed25519 FROM users WHERE pk_node_ed25519 IS NOT NULL " + "AND pk_node_ed25519 <> ''")).fetchall() + now = datetime.now(UTC) + if linked: + op.bulk_insert(node_keys, [ + {"id": str(uuid.uuid4()), "user_id": uid, "pk_node_ed25519": pk, + "linked_at": now} + for uid, pk in linked]) + + with op.batch_alter_table("users") as batch: + batch.drop_column("pk_node_ed25519") + + +def downgrade() -> None: + with op.batch_alter_table("users") as batch: + batch.add_column(sa.Column("pk_node_ed25519", sa.String(64), nullable=True)) + + # One column holds one key: the most recently linked one is kept, which is + # the node the account would have had linked last before this revision. + conn = op.get_bind() + rows = conn.execute(sa.text( + "SELECT user_id, pk_node_ed25519 FROM node_keys ORDER BY linked_at")).fetchall() + latest = {uid: pk for uid, pk in rows} + for uid, pk in latest.items(): + conn.execute(sa.text("UPDATE users SET pk_node_ed25519 = :pk WHERE id = :uid"), + {"pk": pk, "uid": uid}) + + op.drop_index("uq_node_keys_user_key", table_name="node_keys") + op.drop_table("node_keys") diff --git a/packages/meshbay-hub/src/meshbay_hub/db/models.py b/packages/meshbay-hub/src/meshbay_hub/db/models.py index 7291485..e37a479 100644 --- a/packages/meshbay-hub/src/meshbay_hub/db/models.py +++ b/packages/meshbay-hub/src/meshbay_hub/db/models.py @@ -53,7 +53,6 @@ class User(Base): # the node does the wrapping, nothing reads a key from this directory. Keys # are generated per node and pinned there (meshbay_node/roster.py). email_hash: Mapped[str | None] = mapped_column(String(64), nullable=True) # HMAC blind index - pk_node_ed25519: Mapped[str | None] = mapped_column(String(64), nullable=True) hub_id: Mapped[str] = mapped_column(String(128), nullable=False) # user|moderator|admin role: Mapped[str] = mapped_column(String(16), default="user") @@ -104,6 +103,28 @@ class Node(Base): __table_args__ = (Index("ix_nodes_user_id", "user_id"),) +class NodeKey(Base): + """A node key this account has linked: the key `/v1/nodes/auth` accepts for it. + + One row per machine the person runs a node on. Linking is done from a + browser session and nowhere else; a node's own token can neither add nor + remove one (`require_user_scope`). Removing the row cuts that node off at + once — its socket is closed and the tokens it holds name this key (`nk`), + so they stop working — and leaves the account's other nodes alone. + """ + + __tablename__ = "node_keys" + + id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid) + user_id: Mapped[str] = mapped_column(ForeignKey("users.id"), nullable=False) + pk_node_ed25519: Mapped[str] = mapped_column(String(64), nullable=False) + linked_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now) + + __table_args__ = ( + Index("uq_node_keys_user_key", "user_id", "pk_node_ed25519", unique=True), + ) + + # ── Groups ──────────────────────────────────────────────────────────────────── class Group(Base): |