From d6e714d1a1187c2ba56f319e6d6929ec5dd5922a Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Sat, 3 Aug 2019 14:18:01 +0200 Subject: [PATCH 1/2] stop accepting events from retired account --- accounts/accounts/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/accounts/accounts/views.py b/accounts/accounts/views.py index dfd930a..bc008c6 100644 --- a/accounts/accounts/views.py +++ b/accounts/accounts/views.py @@ -20,7 +20,7 @@ class RemoteServerException(Exception): ) -def create_remote_account(name, account_id): +def create_remote_account(account_id): # expires in 30 seconds as this will mean the HTTP request would have # timed out anyways expiry = datetime.utcnow() + timedelta(seconds=30) @@ -32,7 +32,7 @@ def create_remote_account(name, account_id): r = requests.post( "{}/accounts".format(app.config["SERVER_HOST"]), - json={"name": name, "accountId": account_id}, + json={"accountId": account_id}, headers={"X-RPC-Authentication": encoded}, ) @@ -59,7 +59,7 @@ class AccountView(ModelView): def after_model_change(self, form, model, is_created): if is_created: try: - create_remote_account(model.name, model.account_id) + create_remote_account(model.account_id) except RemoteServerException as server_error: db.session.delete(model) db.session.commit() From 3a59998a76aff7720fd0c6bff7d61573954f8ab6 Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Sat, 3 Aug 2019 15:27:06 +0200 Subject: [PATCH 2/2] retire accounts via rpc on deletion --- accounts/accounts/models.py | 2 +- accounts/accounts/views.py | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/accounts/accounts/models.py b/accounts/accounts/models.py index 3fe5909..6068da1 100644 --- a/accounts/accounts/models.py +++ b/accounts/accounts/models.py @@ -11,7 +11,7 @@ class Account(db.Model): __tablename__ = "accounts" account_id = db.Column(db.String(36), primary_key=True, default=generate_key) name = db.Column(db.Text, nullable=False) - users = db.relationship("AccountUserAssociation", back_populates="account") + users = db.relationship("AccountUserAssociation", back_populates="account", cascade="delete") def __repr__(self): return self.name diff --git a/accounts/accounts/views.py b/accounts/accounts/views.py index bc008c6..e5921a3 100644 --- a/accounts/accounts/views.py +++ b/accounts/accounts/views.py @@ -20,7 +20,7 @@ class RemoteServerException(Exception): ) -def create_remote_account(account_id): +def _call_remote_server(account_id, method): # expires in 30 seconds as this will mean the HTTP request would have # timed out anyways expiry = datetime.utcnow() + timedelta(seconds=30) @@ -30,7 +30,16 @@ def create_remote_account(account_id): algorithm="RS256", ).decode("utf-8") - r = requests.post( + do_request = None + if method == "POST": + do_request = requests.post + elif method == "DELETE": + do_request = requests.delete + + if not do_request: + raise Exception("Received unsupported method {}, cannot continue.".format(method)) + + r = do_request( "{}/accounts".format(app.config["SERVER_HOST"]), json={"accountId": account_id}, headers={"X-RPC-Authentication": encoded}, @@ -43,6 +52,14 @@ def create_remote_account(account_id): raise remote_err +def create_remote_account(account_id): + return _call_remote_server(account_id, "POST") + + +def retire_remote_account(account_id): + return _call_remote_server(account_id, "DELETE") + + class AccountForm(Form): name = StringField( "Account Name", @@ -65,6 +82,9 @@ class AccountView(ModelView): db.session.commit() raise server_error + def after_model_delete(self, model): + retire_remote_account(model.account_id) + class UserView(ModelView): inline_models = [(AccountUserAssociation, dict(form_columns=["id", "account"]))]