From 0df6a5793aefd64172d4e62f02e9d592dc715330 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Tue, 16 May 2023 08:35:21 -0700 Subject: [PATCH] Adds maintenance exempt paths (#12592) * adds maintenance exempt paths #11233 * adds maintenance exempt paths #11233 * Rename method & remove login/logout from exempt paths --------- Co-authored-by: jeremystretch --- netbox/netbox/middleware.py | 14 +++++++++----- netbox/netbox/settings.py | 5 +++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/netbox/netbox/middleware.py b/netbox/netbox/middleware.py index 461c018b9..76b3e42a8 100644 --- a/netbox/netbox/middleware.py +++ b/netbox/netbox/middleware.py @@ -181,19 +181,23 @@ class MaintenanceModeMiddleware: def __call__(self, request): if get_config().MAINTENANCE_MODE: - self._prevent_db_write_operations() + self._set_session_type( + allow_write=request.path_info.startswith(settings.MAINTENANCE_EXEMPT_PATHS) + ) return self.get_response(request) @staticmethod - def _prevent_db_write_operations(): + def _set_session_type(allow_write): """ Prevent any write-related database operations. + + Args: + allow_write (bool): If True, write operations will be permitted. """ with connection.cursor() as cursor: - cursor.execute( - 'SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;' - ) + mode = 'READ WRITE' if allow_write else 'READ ONLY' + cursor.execute(f'SET SESSION CHARACTERISTICS AS TRANSACTION {mode};') def process_exception(self, request, exception): """ diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 575755d2b..4d2dd8a81 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -478,6 +478,11 @@ AUTH_EXEMPT_PATHS = ( f'/{BASE_PATH}metrics', ) +# All URLs starting with a string listed here are exempt from maintenance mode enforcement +MAINTENANCE_EXEMPT_PATHS = ( + f'/{BASE_PATH}admin/', +) + SERIALIZATION_MODULES = { 'json': 'utilities.serializers.json', }