Using domain model and added authentication

This commit is contained in:
Cloud User 2022-05-15 12:19:04 +00:00
parent 4958423cc7
commit b3271e84d6
2 changed files with 6 additions and 30 deletions

View File

@ -171,11 +171,3 @@ class UserDeleteFail(StructuredException):
StructuredException.__init__(self) StructuredException.__init__(self)
self.message = message self.message = message
self.name = name 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

View File

@ -23,7 +23,7 @@ from ..lib.errors import (
AccountCreateFail, AccountUpdateFail, AccountDeleteFail, AccountCreateFail, AccountUpdateFail, AccountDeleteFail,
AccountCreateDuplicate, AccountNotExists, AccountCreateDuplicate, AccountNotExists,
UserCreateFail, UserCreateDuplicate, UserUpdateFail, UserDeleteFail, UserCreateFail, UserCreateDuplicate, UserUpdateFail, UserDeleteFail,
UserUpdateFailEmail, HealthCheckFail UserUpdateFailEmail
) )
from ..decorators import ( from ..decorators import (
api_basic_auth, api_can_create_domain, is_json, apikey_auth, api_basic_auth, api_can_create_domain, is_json, apikey_auth,
@ -1184,35 +1184,19 @@ def sync_domains():
return 'Finished synchronization in background', 200 return 'Finished synchronization in background', 200
@api_bp.route('/health', methods=['GET']) @api_bp.route('/health', methods=['GET'])
@apikey_auth
def health(): def health():
domain = Domain() domain = Domain()
domain_to_query = domain.query.first() domain_to_query = domain.query.first()
if not domain_to_query: if not domain_to_query:
current_app.logger.error("No domain found to query a health check") current_app.logger.error("No domain found to query a health check")
raise (HealthCheckFail) return make_response("Down", 503)
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: try:
resp = utils.fetch_remote(urljoin(pdns_api_url, api_uri_with_prefix + api_uri), resp = domain.get_domain_info(domain_to_query.name)
method='GET',
headers=headers,
accept='application/json; q=1',
verify=Setting().get('verify_ssl_connections'))
except Exception as e: except Exception as e:
current_app.logger.error("Health Check - Failed to query authoritative server for domain {}".format(domain_to_query.name)) current_app.logger.error("Health Check - Failed to query authoritative server for domain {}".format(domain_to_query.name))
return make_response("bad", 503) return make_response("Down", 503)
if resp.status_code == 200: return make_response("Up", 200)
return make_response("good", 200)
else:
return make_response("bad", 503)