From f0562d6ba7012dd35725c404386d62b8cb12de5b Mon Sep 17 00:00:00 2001 From: nicholasaleks Date: Fri, 20 May 2022 13:43:17 -0400 Subject: [PATCH 1/4] possible fix for subscriptions and audit logs --- core/view_override.py | 32 +++++++++++++++++++++++++++++++- core/views.py | 18 +++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/core/view_override.py b/core/view_override.py index 8324a97..2c50805 100644 --- a/core/view_override.py +++ b/core/view_override.py @@ -3,7 +3,16 @@ from functools import partial from flask import Response, request from rx import AnonymousObservable - +from graphql_ws.gevent import GeventConnectionContext +from graphql_ws.base_sync import BaseSyncSubscriptionServer +from graphql_ws.base import ( + BaseConnectionContext, + ConnectionClosedException, +) +from core.models import ( + Audit +) +import json def format_execution_result(execution_result, format_error,): status_code = 200 @@ -110,3 +119,24 @@ def dispatch_request(self): headers=e.headers, content_type='application/json' ) + +class GeventSubscriptionServerCustom(BaseSyncSubscriptionServer): + def handle(self, ws, request_context=None): + connection_context = GeventConnectionContext(ws, request_context) + self.on_open(connection_context) + while True: + try: + if connection_context.closed: + raise ConnectionClosedException() + message = connection_context.receive() + except ConnectionClosedException: + self.on_close(connection_context) + return + + msg = json.loads(message) + + if msg.get('type', '') == 'start': + Audit.create_audit_entry(msg['payload']['query'], operation_type='subscription') + + + self.on_message(connection_context, message) diff --git a/core/views.py b/core/views.py index ee0e510..7ab99ab 100644 --- a/core/views.py +++ b/core/views.py @@ -31,7 +31,7 @@ middleware ) from sqlalchemy import event -from core.view_override import OverriddenView +from core.view_override import OverriddenView, GeventSubscriptionServerCustom from core.models import ( Owner, @@ -419,18 +419,30 @@ def set_difficulty(): schema = graphene.Schema(query=Query, mutation=Mutations, subscription=Subscription, directives=[ShowNetworkDirective, SkipDirective, DeprecatedDirective]) -subscription_server = GeventSubscriptionServer(schema) + + + + +subscription_server = GeventSubscriptionServerCustom(schema) + sockets = Sockets(app) @sockets.route('/subscriptions') def echo_socket(ws): + + + + subscription_server.handle(ws) + + + msg = json.loads(ws.read_message()) if msg.get('type', '') == 'start': Audit.create_audit_entry(msg['payload']['query'], operation_type='subscription') - subscription_server.handle(ws) + return [] From 0af22be65f7a18f272716ac905328b296627c01b Mon Sep 17 00:00:00 2001 From: nicholasaleks Date: Fri, 20 May 2022 13:47:34 -0400 Subject: [PATCH 2/4] removed duplicate audit entry --- core/views.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/core/views.py b/core/views.py index 7ab99ab..8b3f762 100644 --- a/core/views.py +++ b/core/views.py @@ -419,10 +419,6 @@ def set_difficulty(): schema = graphene.Schema(query=Query, mutation=Mutations, subscription=Subscription, directives=[ShowNetworkDirective, SkipDirective, DeprecatedDirective]) - - - - subscription_server = GeventSubscriptionServerCustom(schema) @@ -431,18 +427,8 @@ def set_difficulty(): @sockets.route('/subscriptions') def echo_socket(ws): - - subscription_server.handle(ws) - - - msg = json.loads(ws.read_message()) - - if msg.get('type', '') == 'start': - Audit.create_audit_entry(msg['payload']['query'], operation_type='subscription') - - return [] From 9cc870c369f38b522fe9f9c2aec04e6a69ba58ae Mon Sep 17 00:00:00 2001 From: nicholasaleks Date: Fri, 20 May 2022 13:48:06 -0400 Subject: [PATCH 3/4] remove json import --- core/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/core/views.py b/core/views.py index 8b3f762..645fca5 100644 --- a/core/views.py +++ b/core/views.py @@ -1,4 +1,3 @@ -import json import graphene from core.directives import * From 37cc71ae04e9e3c4b87f5316a217a21db48ba63e Mon Sep 17 00:00:00 2001 From: nicholasaleks Date: Fri, 20 May 2022 14:11:56 -0400 Subject: [PATCH 4/4] fixed subscription audit logs events while loading every dvga pages --- core/view_override.py | 15 +++++---- core/views.py | 3 +- templates/partials/base.html | 60 +----------------------------------- templates/paste.html | 59 +++++++++++++++++++++++++++++++++++ version.py | 2 +- 5 files changed, 71 insertions(+), 68 deletions(-) diff --git a/core/view_override.py b/core/view_override.py index 2c50805..1a2254f 100644 --- a/core/view_override.py +++ b/core/view_override.py @@ -6,7 +6,6 @@ from graphql_ws.gevent import GeventConnectionContext from graphql_ws.base_sync import BaseSyncSubscriptionServer from graphql_ws.base import ( - BaseConnectionContext, ConnectionClosedException, ) from core.models import ( @@ -120,6 +119,8 @@ def dispatch_request(self): content_type='application/json' ) +import copy + class GeventSubscriptionServerCustom(BaseSyncSubscriptionServer): def handle(self, ws, request_context=None): connection_context = GeventConnectionContext(ws, request_context) @@ -132,11 +133,13 @@ def handle(self, ws, request_context=None): except ConnectionClosedException: self.on_close(connection_context) return - - msg = json.loads(message) - - if msg.get('type', '') == 'start': - Audit.create_audit_entry(msg['payload']['query'], operation_type='subscription') + + if message: + + msg = json.loads(message) + + if msg.get('type', '') == 'start': + Audit.create_audit_entry(msg['payload']['query'], operation_type='subscription') self.on_message(connection_context, message) diff --git a/core/views.py b/core/views.py index 645fca5..cbc8a95 100644 --- a/core/views.py +++ b/core/views.py @@ -4,7 +4,7 @@ from db.solutions import solutions as list_of_solutions from rx.subjects import Subject -from sqlalchemy import create_engine, text +from sqlalchemy import text from flask import ( request, render_template, @@ -15,7 +15,6 @@ from flask_sockets import Sockets from graphql.backend import GraphQLCoreBackend -from graphql_ws.gevent import GeventSubscriptionServer from sqlalchemy.sql import text from graphene_sqlalchemy import ( diff --git a/templates/partials/base.html b/templates/partials/base.html index aec1cf7..9f4a1ae 100644 --- a/templates/partials/base.html +++ b/templates/partials/base.html @@ -21,65 +21,7 @@ - + {% block scripts %} {% endblock %} diff --git a/templates/paste.html b/templates/paste.html index c4522dc..672070c 100644 --- a/templates/paste.html +++ b/templates/paste.html @@ -26,6 +26,65 @@ {% endblock %} {% block scripts %} + {% include 'partials/base_scripts.html' %}