From d0601a376c355d6859d345073177bcf53fe3481f Mon Sep 17 00:00:00 2001 From: roekatz Date: Wed, 13 Nov 2024 16:33:15 +0200 Subject: [PATCH] Health Check: Report healthy if Offline Mode is active. The health check would instead return a boolean `online` in its response json, to reflect whether data is up to date with server (or loaded from backup) --- packages/opal-client/opal_client/client.py | 18 ++++++++++++++---- .../policy_store/base_policy_store_client.py | 3 +++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/opal-client/opal_client/client.py b/packages/opal-client/opal_client/client.py index 6a9958b98..b56006e55 100644 --- a/packages/opal-client/opal_client/client.py +++ b/packages/opal-client/opal_client/client.py @@ -257,6 +257,10 @@ def _init_fast_api_app(self): self._configure_lifecycle_callbacks(app) return app + async def _is_ready(self): + # Data loaded from file or from server + return self._backup_loaded or await self.policy_store.is_ready() + def _configure_api_routes(self, app: FastAPI): """mounts the api routes on the app object.""" @@ -285,7 +289,15 @@ async def healthy(): if healthy: return JSONResponse( - status_code=status.HTTP_200_OK, content={"status": "ok"} + status_code=status.HTTP_200_OK, + content={"status": "ok", "online": True}, + ) + elif self.offline_mode_enabled and await self._is_ready(): + # When offline mode is enabled, OPAL should be considered healthy when no server updates are available + # TODO: Maybe if server updates were available but storing them to OPA wasn't successful, we should return 503 even with offline mode enabled + return JSONResponse( + status_code=status.HTTP_200_OK, + content={"status": "ok", "online": False}, ) else: return JSONResponse( @@ -296,9 +308,7 @@ async def healthy(): @app.get("/ready", include_in_schema=False) async def ready(): """returns 200 if the policy store is ready to serve requests.""" - ready = self._backup_loaded or await self.policy_store.is_ready() - - if ready: + if await self._is_ready(): return JSONResponse( status_code=status.HTTP_200_OK, content={"status": "ok"} ) diff --git a/packages/opal-client/opal_client/policy_store/base_policy_store_client.py b/packages/opal-client/opal_client/policy_store/base_policy_store_client.py index 94eac87d5..abab6f1fe 100644 --- a/packages/opal-client/opal_client/policy_store/base_policy_store_client.py +++ b/packages/opal-client/opal_client/policy_store/base_policy_store_client.py @@ -67,6 +67,9 @@ async def log_transaction(self, transaction: StoreTransaction): async def is_healthy(self) -> bool: raise NotImplementedError() + async def is_ready(self) -> bool: + raise NotImplementedError() + async def full_export(self, writer: AsyncTextIOWrapper) -> None: raise NotImplementedError()