Skip to content

Commit b79b58a

Browse files
committed
apply review suggestions
Signed-off-by: Matthias Büchse <[email protected]>
1 parent 9525a9e commit b79b58a

File tree

2 files changed

+15
-31
lines changed

2 files changed

+15
-31
lines changed

compliance-monitor/docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ services:
2828
- SCM_DB_HOST=postgres
2929
- SCM_DB_PORT=5432
3030
- SCM_DB_PASSWORD_FILE=/run/secrets/db_password
31-
- SCM_HC_USER=healthz
32-
- SCM_HC_PASSWORD=healthzpassword
31+
# pass the following two from the shell
32+
- SCM_HC_USER
33+
- SCM_HC_PASSWORD
3334
- SCM_BASE_URL=https://compliance.sovereignit.cloud/
3435
volumes:
3536
- ../Tests:/Tests

compliance-monitor/monitor.py

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ def __init__(self):
6464
self.db_host = os.getenv("SCM_DB_HOST", "localhost")
6565
self.db_port = os.getenv("SCM_DB_PORT", 5432)
6666
self.db_user = os.getenv("SCM_DB_USER", "postgres")
67-
self.hc_user = os.getenv("SCM_HC_USER", "healthz")
68-
self.hc_password = os.getenv("SCM_HC_PASSWORD", "healthzpassword")
67+
# use default value of None for security reasons (won't be matched)
68+
self.hc_user = os.getenv("SCM_HC_USER", None)
69+
self.hc_password = os.getenv("SCM_HC_PASSWORD", None)
6970
password_file_path = os.getenv("SCM_DB_PASSWORD_FILE", None)
7071
if password_file_path:
7172
with open(os.path.abspath(password_file_path), "r") as fileobj:
@@ -723,37 +724,19 @@ async def post_results(
723724

724725

725726
@app.get("/healthz")
726-
async def get_healthz(
727-
request: Request,
728-
):
729-
"""return monitor's health status"""
727+
async def get_healthz(request: Request):
728+
"""return compliance monitor's health status"""
730729
credentials = await optional_security(request)
730+
authorized = credentials and \
731+
credentials.username == settings.hc_user and credentials.password == settings.hc_password
731732

732-
# check credentials
733-
if credentials is None:
734-
# no credentials were set
735-
check_db_connection()
736-
elif credentials.username == settings.hc_user and credentials.password == settings.hc_password:
737-
# healthz user
738-
check_db_connection(authorized=True)
739-
else:
740-
# unauthorized user
741-
check_db_connection()
742-
743-
return {"message": "OK"}
744-
745-
746-
def check_db_connection(authorized: bool = False):
747-
# check database connection
748733
try:
749734
mk_conn(settings=settings)
750-
except psycopg2.OperationalError as e:
751-
if authorized:
752-
# authorized user
753-
raise HTTPException(status_code=500,
754-
detail="Database Connection Error. " + e.args[0].capitalize())
755-
else:
756-
raise HTTPException(status_code=500, detail="Internal Server Error")
735+
except Exception as e:
736+
detail = str(e) if authorized else 'internal server error'
737+
return Response(status_code=500, content=detail, media_type='text/plain')
738+
739+
return Response() # empty response with status 200
757740

758741

759742
def pick_filter(results, subject, scope):

0 commit comments

Comments
 (0)