From f41696c31084f363a4c1c42c7b078e6a3fe8b3cd Mon Sep 17 00:00:00 2001 From: root Date: Mon, 18 Apr 2022 09:01:22 +0000 Subject: [PATCH] WIP - Added health check --- powerdnsadmin/lib/errors.py | 8 ++++++++ powerdnsadmin/routes/api.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/powerdnsadmin/lib/errors.py b/powerdnsadmin/lib/errors.py index 687f554..b820017 100644 --- a/powerdnsadmin/lib/errors.py +++ b/powerdnsadmin/lib/errors.py @@ -171,3 +171,11 @@ class UserDeleteFail(StructuredException): StructuredException.__init__(self) self.message = message self.name = name + +class HealthCheckFail(StructuredException): + status_code = 500 + + def __init__(self,name=None, message="Health check failed"): + StructuredException.__init__(self) + self.message = message + self.name = name \ No newline at end of file diff --git a/powerdnsadmin/routes/api.py b/powerdnsadmin/routes/api.py index 4fce368..580e1a1 100644 --- a/powerdnsadmin/routes/api.py +++ b/powerdnsadmin/routes/api.py @@ -23,7 +23,7 @@ from ..lib.errors import ( AccountCreateFail, AccountUpdateFail, AccountDeleteFail, AccountCreateDuplicate, AccountNotExists, UserCreateFail, UserCreateDuplicate, UserUpdateFail, UserDeleteFail, - UserUpdateFailEmail, + UserUpdateFailEmail, HealthCheckFail ) from ..decorators import ( api_basic_auth, api_can_create_domain, is_json, apikey_auth, @@ -1182,3 +1182,36 @@ def sync_domains(): domain = Domain() domain.update() return 'Finished synchronization in background', 200 + +@api_bp.route('/health', methods=['GET']) +def health(): + domain = Domain() + domain_to_query = domain.query.first() + + if not domain_to_query: + current_app.logger.error("No domain found to query a health check") + raise (HealthCheckFail) + + pdns_api_url = Setting().get('pdns_api_url') + pdns_api_key = Setting().get('pdns_api_key') + pdns_version = Setting().get('pdns_version') + api_uri_with_prefix = utils.pdns_api_extended_uri(pdns_version) + api_uri = '/servers/localhost/zones/{}'.format(domain_to_query.name) + headers = {} + headers['X-API-Key'] = pdns_api_key + + try: + resp = utils.fetch_remote(urljoin(pdns_api_url, api_uri_with_prefix + api_uri), + method='GET', + headers=headers, + accept='application/json; q=1', + verify=Setting().get('verify_ssl_connections')) + + except Exception as e: + current_app.logger.error("Health Check - Failed to query authoritative server for domain {}".format(domain_to_query.name)) + return make_response("bad", 503) + + if resp.status_code == 200: + return make_response("good", 200) + else: + return make_response("bad", 503) \ No newline at end of file