Skip to content

Add health check for database connection#912

Merged
mbuechse merged 3 commits intomainfrom
add-health-monitor
Apr 30, 2025
Merged

Add health check for database connection#912
mbuechse merged 3 commits intomainfrom
add-health-monitor

Conversation

@anjastrunk
Copy link
Contributor

Add health monitor for database connection

Comment on lines +31 to +32
- SCM_HC_USER=healthz
- SCM_HC_PASSWORD=healthzpassword
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to see this removed so that people are forced to invent their own password.

Comment on lines +67 to +68
self.hc_user = os.getenv("SCM_HC_USER", "healthz")
self.hc_password = os.getenv("SCM_HC_PASSWORD", "healthzpassword")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar here: the default should be something that would never match, such as None (then the test credentials.username == settings.hc_user returns False).

Comment on lines +726 to +729
async def get_healthz(
request: Request,
):
"""return monitor's health status"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async def get_healthz(
request: Request,
):
"""return monitor's health status"""
async def get_healthz(request: Request):
"""return compliance monitor's health status"""

# unauthorized user
check_db_connection()

return {"message": "OK"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return {"message": "OK"}
return Response()

for an empty response, which should be sufficient and better than text/json

Comment on lines +732 to +741
# check credentials
if credentials is None:
# no credentials were set
check_db_connection()
elif credentials.username == settings.hc_user and credentials.password == settings.hc_password:
# healthz user
check_db_connection(authorized=True)
else:
# unauthorized user
check_db_connection()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be catching any exception here, otherwise we might disclose some exceptions even if the request is not authorized.

mk_conn(settings=settings)
except Exception as e:
detail = str(e) if authorized else 'internal server error'
return Response(status_code=500, content=detail, media_type='text/plain')

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

Copilot Autofix

AI 10 months ago

To fix the issue, we will modify the code to ensure that detailed exception information is not exposed to external users. Instead, we will log the exception details on the server and return a generic error message to the user. Specifically:

  1. Replace the assignment of detail with a generic error message ("internal server error").
  2. Log the exception details (str(e)) using the logger object for debugging purposes.

This ensures that sensitive information is not leaked to the user while still allowing developers to diagnose issues using server logs.


Suggested changeset 1
compliance-monitor/monitor.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/compliance-monitor/monitor.py b/compliance-monitor/monitor.py
--- a/compliance-monitor/monitor.py
+++ b/compliance-monitor/monitor.py
@@ -735,3 +735,4 @@
     except Exception as e:
-        detail = str(e) if authorized else 'internal server error'
+        logger.error("Health check failed: %s", str(e))
+        detail = 'internal server error'
         return Response(status_code=500, content=detail, media_type='text/plain')
EOF
@@ -735,3 +735,4 @@
except Exception as e:
detail = str(e) if authorized else 'internal server error'
logger.error("Health check failed: %s", str(e))
detail = 'internal server error'
return Response(status_code=500, content=detail, media_type='text/plain')
Copilot is powered by AI and may make mistakes. Always verify output.
Copy link
Contributor

@mbuechse mbuechse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

anjastrunk and others added 3 commits April 30, 2025 21:12
Signed-off-by: Anja Strunk <anja.strunk@cloudandheat.com>
Signed-off-by: Anja Strunk <anja.strunk@cloudandheat.com>
Signed-off-by: Matthias Büchse <matthias.buechse@alasca.cloud>
@mbuechse mbuechse force-pushed the add-health-monitor branch from 7c502a7 to b79b58a Compare April 30, 2025 19:12
@mbuechse mbuechse merged commit fd0d2c5 into main Apr 30, 2025
7 of 8 checks passed
@mbuechse mbuechse deleted the add-health-monitor branch April 30, 2025 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] compliance.sovereignit.cloud should offer a healthz endpoint

2 participants