Skip to content

Commit

Permalink
Remove invitation_conduit table from the postgresql datamodel
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel committed Sep 5, 2024
1 parent 1d93bee commit ffd0be4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 163 deletions.
122 changes: 14 additions & 108 deletions server/parsec/components/postgresql/invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,37 +217,23 @@ def from_record(cls, record: Record) -> GreetingAttemptInfo:

_q_insert_invitation = Q(
f"""
WITH new_invitations AS (
INSERT INTO invitation(
organization,
token,
type,
claimer_email,
created_by,
created_on
)
VALUES (
{ q_organization_internal_id("$organization_id") },
$token,
$type,
$claimer_email,
{ q_device_internal_id(organization_id="$organization_id", device_id="$created_by") },
$created_on
)
RETURNING _id, created_by
)
INSERT INTO invitation_conduit(
invitation,
greeter
INSERT INTO invitation(
organization,
token,
type,
claimer_email,
created_by,
created_on
)
VALUES (
(SELECT _id FROM new_invitations),
(
SELECT device.user_
FROM new_invitations
INNER JOIN device ON new_invitations.created_by = device._id
)
{ q_organization_internal_id("$organization_id") },
$token,
$type,
$claimer_email,
{ q_device_internal_id(organization_id="$organization_id", device_id="$created_by") },
$created_on
)
RETURNING _id, created_by
"""
)

Expand Down Expand Up @@ -385,86 +371,6 @@ def make_q_info_invitation(
"""
)

_q_get_invitation_created_by = Q(
f"""
SELECT
{ q_user(_id=q_device(_id="invitation.created_by", select="user_"), select="user_id") } as created_by
FROM invitation
WHERE
organization = { q_organization_internal_id("$organization_id") }
AND token = $token
"""
)

_q_conduit_greeter_info = Q(
f"""
SELECT
invitation._id AS invitation_internal_id,
invitation_conduit._id AS conduit_internal_id,
conduit_state,
conduit_greeter_payload,
conduit_claimer_payload,
deleted_on,
last_exchange
FROM invitation
INNER JOIN invitation_conduit ON
invitation._id = invitation_conduit.invitation
WHERE
invitation.organization = { q_organization_internal_id("$organization_id") }
AND invitation.token = $token
AND invitation_conduit.greeter = { q_user_internal_id(organization_id="$organization_id", user_id="$greeter_user_id") }
FOR UPDATE
"""
)


_q_conduit_claimer_info = Q(
f"""
SELECT
invitation._id AS invitation_internal_id,
invitation_conduit._id AS conduit_internal_id,
conduit_state,
conduit_greeter_payload,
conduit_claimer_payload,
deleted_on,
last_exchange
FROM invitation
INNER JOIN invitation_conduit ON
invitation._id = invitation_conduit.invitation
WHERE
invitation.organization = { q_organization_internal_id("$organization_id") }
AND token = $token
AND invitation_conduit.greeter = { q_user_internal_id(organization_id="$organization_id", user_id="$greeter_user_id") }
FOR UPDATE
"""
)


_q_conduit_update = Q(
"""
UPDATE invitation_conduit
SET
conduit_state = $conduit_state,
conduit_greeter_payload = $conduit_greeter_payload,
conduit_claimer_payload = $conduit_claimer_payload
WHERE
_id = $conduit_internal_id
"""
)

_q_conduit_update_with_last_exchange = Q(
"""
UPDATE invitation_conduit
SET
conduit_state = $conduit_state,
conduit_greeter_payload = $conduit_greeter_payload,
conduit_claimer_payload = $conduit_claimer_payload,
last_exchange = $last
WHERE
_id = $conduit_internal_id
"""
)

_q_retrieve_active_human_by_email = Q(
f"""
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS

-------------------------------------------------------
-- Migration
-------------------------------------------------------


DROP TABLE invitation_conduit;
DROP TYPE INVITATION_CONDUIT_STATE;
24 changes: 0 additions & 24 deletions server/parsec/components/postgresql/migrations/datamodel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,6 @@ CREATE TYPE invitation_type AS ENUM ('USER', 'DEVICE', 'SHAMIR_RECOVERY');
CREATE TYPE invitation_deleted_reason AS ENUM (
'FINISHED', 'CANCELLED', 'ROTTEN'
);
CREATE TYPE invitation_conduit_state AS ENUM (
'1_WAIT_PEERS',
'2_1_CLAIMER_HASHED_NONCE',
'2_2_GREETER_NONCE',
'2_3_CLAIMER_NONCE',
'3_1_CLAIMER_TRUST',
'3_2_GREETER_TRUST',
'4_COMMUNICATE'
);

CREATE TYPE cancelled_greeting_attempt_reason AS ENUM (
'MANUALLY_CANCELLED',
Expand Down Expand Up @@ -279,21 +270,6 @@ CREATE TABLE greeting_step (
UNIQUE (greeting_attempt, step)
);


CREATE TABLE invitation_conduit (
_id SERIAL PRIMARY KEY,
invitation INTEGER REFERENCES invitation (_id) NOT NULL,
greeter INTEGER REFERENCES user_ (_id) NOT NULL,

conduit_state INVITATION_CONDUIT_STATE NOT NULL DEFAULT '1_WAIT_PEERS',
conduit_greeter_payload BYTEA,
conduit_claimer_payload BYTEA,
last_exchange BOOLEAN,

UNIQUE (invitation, greeter)
);


-------------------------------------------------------
-- PKI enrollment
-------------------------------------------------------
Expand Down
30 changes: 0 additions & 30 deletions server/parsec/components/postgresql/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
WHERE organization in (select * from deleted_organizations)
RETURNING _id
),
deleted_invitation_conduits AS (
DELETE FROM invitation_conduit
WHERE invitation in (select * from deleted_invitations)
RETURNING _id
),
deleted_realms AS (
DELETE FROM realm
WHERE organization in (select * from deleted_organizations)
Expand Down Expand Up @@ -324,31 +319,6 @@
WHERE organization = { q_organization_internal_id("$source_id") }
RETURNING _id, token
),
new_invitation_conduits AS (
INSERT INTO invitation_conduit (
invitation,
greeter,
conduit_state,
conduit_greeter_payload,
conduit_claimer_payload
)
SELECT
(
SELECT _id FROM new_invitations
WHERE token = { q_invitation(_id="invitation_conduit.invitation", select="token") }
),
(
SELECT _id FROM new_users
WHERE user_id = { q_user(_id="invitation_conduit.greeter", select="user_id") }
),
conduit_state,
conduit_greeter_payload,
conduit_claimer_payload
FROM invitation_conduit
INNER JOIN invitation ON invitation._id = invitation_conduit.invitation
WHERE invitation.organization = { q_organization_internal_id("$source_id") }
RETURNING _id
),
new_realms AS (
INSERT INTO realm (
organization,
Expand Down
1 change: 0 additions & 1 deletion server/tests/common/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ async def init_db():
SETVAL(PG_GET_SERIAL_SEQUENCE('shamir_recovery_setup', '_id'), 7000),
SETVAL(PG_GET_SERIAL_SEQUENCE('shamir_recovery_share', '_id'), 8000),
SETVAL(PG_GET_SERIAL_SEQUENCE('invitation', '_id'), 9000),
SETVAL(PG_GET_SERIAL_SEQUENCE('invitation_conduit', '_id'), 10000),
SETVAL(PG_GET_SERIAL_SEQUENCE('pki_enrollment', '_id'), 11000),
SETVAL(PG_GET_SERIAL_SEQUENCE('realm', '_id'), 12000),
SETVAL(PG_GET_SERIAL_SEQUENCE('realm_user_role', '_id'), 13000),
Expand Down

0 comments on commit ffd0be4

Please sign in to comment.