From 55276a8d87dc4dc6dcaf9bc7fa0aadfc19fb518d Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Sat, 13 May 2023 01:41:29 +0530 Subject: [PATCH 1/3] adds maintenance exempt paths #11233 --- netbox/netbox/middleware.py | 14 +++++++++----- netbox/netbox/settings.py | 6 ++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/netbox/netbox/middleware.py b/netbox/netbox/middleware.py index 461c018b93d..7efc0708ab7 100644 --- a/netbox/netbox/middleware.py +++ b/netbox/netbox/middleware.py @@ -181,19 +181,23 @@ def __init__(self, get_response): def __call__(self, request): if get_config().MAINTENANCE_MODE: - self._prevent_db_write_operations() + self._prevent_db_write_operations( + allow_write=request.path_info.startswith(settings.MAINTENANCE_EXEMPT_PATHS) + ) return self.get_response(request) @staticmethod - def _prevent_db_write_operations(): + def _prevent_db_write_operations(allow_write=False): """ Prevent any write-related database operations. + + Args: + allow_write (bool): Whether to allow write operations. """ 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 575755d2b5e..4264566726f 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -478,6 +478,12 @@ def _setting(name, default=None): 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/extras/configrevision/', + f'/{BASE_PATH}login/', +) + SERIALIZATION_MODULES = { 'json': 'utilities.serializers.json', } From de4ebaf36b4a9e0ea718fdfc1fa09614cacd829a Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Sat, 13 May 2023 01:43:29 +0530 Subject: [PATCH 2/3] adds maintenance exempt paths #11233 --- netbox/netbox/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 4264566726f..d9ca216ec21 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -482,6 +482,7 @@ def _setting(name, default=None): MAINTENANCE_EXEMPT_PATHS = ( f'/{BASE_PATH}admin/extras/configrevision/', f'/{BASE_PATH}login/', + f'/{BASE_PATH}logout/', ) SERIALIZATION_MODULES = { From 563d189729853604a9c832c7e3d594c8f1bf6187 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Tue, 16 May 2023 11:20:16 -0400 Subject: [PATCH 3/3] Rename method & remove login/logout from exempt paths --- netbox/netbox/middleware.py | 6 +++--- netbox/netbox/settings.py | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/netbox/netbox/middleware.py b/netbox/netbox/middleware.py index 7efc0708ab7..76b3e42a845 100644 --- a/netbox/netbox/middleware.py +++ b/netbox/netbox/middleware.py @@ -181,19 +181,19 @@ def __init__(self, get_response): 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(allow_write=False): + def _set_session_type(allow_write): """ Prevent any write-related database operations. Args: - allow_write (bool): Whether to allow write operations. + allow_write (bool): If True, write operations will be permitted. """ with connection.cursor() as cursor: mode = 'READ WRITE' if allow_write else 'READ ONLY' diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index d9ca216ec21..4d2dd8a8160 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -480,9 +480,7 @@ def _setting(name, default=None): # All URLs starting with a string listed here are exempt from maintenance mode enforcement MAINTENANCE_EXEMPT_PATHS = ( - f'/{BASE_PATH}admin/extras/configrevision/', - f'/{BASE_PATH}login/', - f'/{BASE_PATH}logout/', + f'/{BASE_PATH}admin/', ) SERIALIZATION_MODULES = {