From 3df36adbf461e9c5e6b5380c4519ded8d60059c7 Mon Sep 17 00:00:00 2001 From: AdvanticGmbH Date: Mon, 4 Apr 2022 17:22:41 +0200 Subject: [PATCH 1/3] Add more detailed info to the history when a msg and status exists --- powerdnsadmin/routes/admin.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/powerdnsadmin/routes/admin.py b/powerdnsadmin/routes/admin.py index 5573502..f6de2b3 100644 --- a/powerdnsadmin/routes/admin.py +++ b/powerdnsadmin/routes/admin.py @@ -883,6 +883,16 @@ class DetailedHistory(): domain_type=DetailedHistory.get_key_val(detail_dict, "domain_type"), domain_master_ips=DetailedHistory.get_key_val(detail_dict, "domain_master_ips")) + elif DetailedHistory.get_key_val(detail_dict, 'msg') and DetailedHistory.get_key_val(detail_dict, 'status'): + self.detailed_msg = render_template_string(''' + + + +
Status: {{ history_status }}
Message:{{ history_msg }}
+ ''', + history_status=DetailedHistory.get_key_val(detail_dict, 'status'), + history_msg=DetailedHistory.get_key_val(detail_dict, 'msg')) + # check for lower key as well for old databases @staticmethod def get_key_val(_dict, key): From 44c9aff5dba929876dc6a8c6007f90fd1070617f Mon Sep 17 00:00:00 2001 From: AdvanticGmbH Date: Tue, 5 Apr 2022 10:50:15 +0200 Subject: [PATCH 2/3] Use json.dumps for every detail in history This works much better instead of just writing a str to the db and expect it to be loaded just fine from json.loads --- powerdnsadmin/routes/admin.py | 55 +++++++++++++++++----------------- powerdnsadmin/routes/domain.py | 12 ++++---- powerdnsadmin/routes/index.py | 39 +++++++++++++----------- 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/powerdnsadmin/routes/admin.py b/powerdnsadmin/routes/admin.py index f6de2b3..44d2d67 100644 --- a/powerdnsadmin/routes/admin.py +++ b/powerdnsadmin/routes/admin.py @@ -93,7 +93,7 @@ def extract_changelogs_from_a_history_entry(out_changes, history_entry, change_n return if "add_rrests" in history_entry.detail: - detail_dict = json.loads(history_entry.detail.replace("\'", '')) + detail_dict = json.loads(history_entry.detail) else: # not a record entry return @@ -362,13 +362,14 @@ def edit_key(key_id=None): current_app.logger.error('Error: {0}'.format(e)) history = History(msg=history_message, - detail=str({ - 'key': apikey.id, - 'role': apikey.role.name, - 'description': apikey.description, - 'domains': [domain.name for domain in apikey.domains], - 'accounts': [a.name for a in apikey.accounts] - }), + detail=str( + json.dumps({ + 'key': apikey.id, + 'role': apikey.role.name, + 'description': apikey.description, + 'domains': [domain.name for domain in apikey.domains], + 'accounts': [a.name for a in apikey.accounts] + })), created_by=current_user.username) history.add() @@ -411,12 +412,13 @@ def manage_keys(): current_app.logger.info('Delete API key {0}'.format(apikey.id)) history = History(msg='Delete API key {0}'.format(apikey.id), - detail=str({ - 'key': history_apikey_id, - 'role': history_apikey_role, - 'description': history_apikey_description, - 'domains': history_apikey_domains - }), + detail=str( + json.dumps({ + 'key': history_apikey_id, + 'role': history_apikey_role, + 'description': history_apikey_description, + 'domains': history_apikey_domains + })), created_by=current_user.username) history.add() @@ -763,10 +765,7 @@ class DetailedHistory(): self.detailed_msg = "" return - if 'add_rrest' in history.detail: - detail_dict = json.loads(history.detail.replace("\'", '')) - else: - detail_dict = json.loads(history.detail.replace("'", '"')) + detail_dict = json.loads(history.detail) if 'domain_type' in detail_dict and 'account_id' in detail_dict: # this is a domain creation self.detailed_msg = render_template_string(""" @@ -1673,10 +1672,11 @@ def create_template(): result = t.create() if result['status'] == 'ok': history = History(msg='Add domain template {0}'.format(name), - detail=str({ - 'name': name, - 'description': description - }), + detail=str( + json.dumps({ + 'name': name, + 'description': description + })), created_by=current_user.username) history.add() return redirect(url_for('admin.templates')) @@ -1720,10 +1720,11 @@ def create_template_from_zone(): result = t.create() if result['status'] == 'ok': history = History(msg='Add domain template {0}'.format(name), - detail=str({ - 'name': name, - 'description': description - }), + detail=str( + json.dumps({ + 'name': name, + 'description': description + })), created_by=current_user.username) history.add() @@ -1883,7 +1884,7 @@ def delete_template(template): if result['status'] == 'ok': history = History( msg='Deleted domain template {0}'.format(template), - detail=str({'name': template}), + detail=str(json.dumps({'name': template})), created_by=current_user.username) history.add() return redirect(url_for('admin.templates')) diff --git a/powerdnsadmin/routes/domain.py b/powerdnsadmin/routes/domain.py index 7248d36..dfb12e9 100644 --- a/powerdnsadmin/routes/domain.py +++ b/powerdnsadmin/routes/domain.py @@ -409,11 +409,12 @@ def add(): domain_id = Domain().get_id_by_name(domain_name) history = History(msg='Add domain {0}'.format( pretty_domain_name(domain_name)), - detail=str({ + detail=str( + json.dumps({ 'domain_type': domain_type, 'domain_master_ips': domain_master_ips, 'account_id': account_id - }), + })), created_by=current_user.username, domain_id=domain_id) history.add() @@ -464,7 +465,7 @@ def add(): msg= 'Failed to apply template {0} to {1}.' .format(template.name, domain_name), - detail=str(result), + detail=str(json.dumps(result)), created_by=current_user.username) history.add() return redirect(url_for('dashboard.dashboard')) @@ -617,10 +618,11 @@ def change_soa_edit_api(domain_name): history = History( msg='Update soa_edit_api for domain {0}'.format( pretty_domain_name(domain_name)), - detail=str({ + detail=str( + json.dumps({ "domain": domain_name, "soa_edit_api": new_setting - }), + })), created_by=current_user.username, domain_id=d.get_id_by_name(domain_name)) history.add() diff --git a/powerdnsadmin/routes/index.py b/powerdnsadmin/routes/index.py index 8e6d0c1..990854d 100644 --- a/powerdnsadmin/routes/index.py +++ b/powerdnsadmin/routes/index.py @@ -550,12 +550,13 @@ def signin_history(username, authenticator, success): # Write history History(msg='User {} authentication {}'.format(username, str_success), - detail=str({ - "username": username, - "authenticator": authenticator, - "ip_address": request_ip, - "success": 1 if success else 0 - }), + detail=str( + json.dumps({ + "username": username, + "authenticator": authenticator, + "ip_address": request_ip, + "success": 1 if success else 0 + })), created_by='System').add() # Get a list of Azure security groups the user is a member of @@ -864,13 +865,14 @@ def dyndns_update(): if result['status'] == 'ok': history = History( msg='DynDNS update: updated {} successfully'.format(hostname), - detail=str({ - "domain": domain.name, - "record": hostname, - "type": rtype, - "old_value": oldip, - "new_value": str(ip) - }), + detail=str( + json.dumps({ + "domain": domain.name, + "record": hostname, + "type": rtype, + "old_value": oldip, + "new_value": str(ip) + })), created_by=current_user.username, domain_id=domain.id) history.add() @@ -906,11 +908,12 @@ def dyndns_update(): msg= 'DynDNS update: created record {0} in zone {1} successfully' .format(hostname, domain.name, str(ip)), - detail=str({ - "domain": domain.name, - "record": hostname, - "value": str(ip) - }), + detail=str( + json.dumps({ + "domain": domain.name, + "record": hostname, + "value": str(ip) + })), created_by=current_user.username, domain_id=domain.id) history.add() From 26c60f175de74c1213d6765e4bf45e01ca62f49b Mon Sep 17 00:00:00 2001 From: AdvanticGmbH Date: Tue, 26 Apr 2022 09:11:05 +0200 Subject: [PATCH 3/3] Remove unnecessary call to str() * json.dumps() already returns a str --- powerdnsadmin/routes/admin.py | 24 +++++++---------- powerdnsadmin/routes/domain.py | 49 +++++++++++++++------------------- powerdnsadmin/routes/index.py | 39 +++++++++++++-------------- 3 files changed, 50 insertions(+), 62 deletions(-) diff --git a/powerdnsadmin/routes/admin.py b/powerdnsadmin/routes/admin.py index 44d2d67..7919c8c 100644 --- a/powerdnsadmin/routes/admin.py +++ b/powerdnsadmin/routes/admin.py @@ -362,14 +362,13 @@ def edit_key(key_id=None): current_app.logger.error('Error: {0}'.format(e)) history = History(msg=history_message, - detail=str( - json.dumps({ + detail = json.dumps({ 'key': apikey.id, 'role': apikey.role.name, 'description': apikey.description, 'domains': [domain.name for domain in apikey.domains], 'accounts': [a.name for a in apikey.accounts] - })), + }), created_by=current_user.username) history.add() @@ -412,13 +411,12 @@ def manage_keys(): current_app.logger.info('Delete API key {0}'.format(apikey.id)) history = History(msg='Delete API key {0}'.format(apikey.id), - detail=str( - json.dumps({ + detail = json.dumps({ 'key': history_apikey_id, 'role': history_apikey_role, 'description': history_apikey_description, 'domains': history_apikey_domains - })), + }), created_by=current_user.username) history.add() @@ -1672,11 +1670,10 @@ def create_template(): result = t.create() if result['status'] == 'ok': history = History(msg='Add domain template {0}'.format(name), - detail=str( - json.dumps({ + detail = json.dumps({ 'name': name, 'description': description - })), + }), created_by=current_user.username) history.add() return redirect(url_for('admin.templates')) @@ -1720,11 +1717,10 @@ def create_template_from_zone(): result = t.create() if result['status'] == 'ok': history = History(msg='Add domain template {0}'.format(name), - detail=str( - json.dumps({ + detail = json.dumps({ 'name': name, 'description': description - })), + }), created_by=current_user.username) history.add() @@ -1854,7 +1850,7 @@ def apply_records(template): history = History( msg='Apply domain template record changes to domain template {0}' .format(template), - detail=str(json.dumps(jdata)), + detail = json.dumps(jdata), created_by=current_user.username) history.add() return make_response(jsonify(result), 200) @@ -1884,7 +1880,7 @@ def delete_template(template): if result['status'] == 'ok': history = History( msg='Deleted domain template {0}'.format(template), - detail=str(json.dumps({'name': template})), + detail = json.dumps({'name': template}), created_by=current_user.username) history.add() return redirect(url_for('admin.templates')) diff --git a/powerdnsadmin/routes/domain.py b/powerdnsadmin/routes/domain.py index dfb12e9..1d922be 100644 --- a/powerdnsadmin/routes/domain.py +++ b/powerdnsadmin/routes/domain.py @@ -409,12 +409,11 @@ def add(): domain_id = Domain().get_id_by_name(domain_name) history = History(msg='Add domain {0}'.format( pretty_domain_name(domain_name)), - detail=str( - json.dumps({ + detail = json.dumps({ 'domain_type': domain_type, 'domain_master_ips': domain_master_ips, 'account_id': account_id - })), + }), created_by=current_user.username, domain_id=domain_id) history.add() @@ -446,17 +445,16 @@ def add(): history = History( msg='Applying template {0} to {1} successfully.'. format(template.name, domain_name), - detail=str( - json.dumps({ - "domain": + detail = json.dumps({ + 'domain': domain_name, - "template": + 'template': template.name, - "add_rrests": + 'add_rrests': result['data'][0]['rrsets'], - "del_rrests": + 'del_rrests': result['data'][1]['rrsets'] - })), + }), created_by=current_user.username, domain_id=domain_id) history.add() @@ -465,7 +463,7 @@ def add(): msg= 'Failed to apply template {0} to {1}.' .format(template.name, domain_name), - detail=str(json.dumps(result)), + detail = json.dumps(result), created_by=current_user.username) history.add() return redirect(url_for('dashboard.dashboard')) @@ -618,11 +616,10 @@ def change_soa_edit_api(domain_name): history = History( msg='Update soa_edit_api for domain {0}'.format( pretty_domain_name(domain_name)), - detail=str( - json.dumps({ - "domain": domain_name, - "soa_edit_api": new_setting - })), + detail = json.dumps({ + 'domain': domain_name, + 'soa_edit_api': new_setting + }), created_by=current_user.username, domain_id=d.get_id_by_name(domain_name)) history.add() @@ -686,12 +683,11 @@ def record_apply(domain_name): if result['status'] == 'ok': history = History( msg='Apply record changes to domain {0}'.format(pretty_domain_name(domain_name)), - detail=str( - json.dumps({ - "domain": domain_name, - "add_rrests": result['data'][0]['rrsets'], - "del_rrests": result['data'][1]['rrsets'] - })), + detail = json.dumps({ + 'domain': domain_name, + 'add_rrests': result['data'][0]['rrsets'], + 'del_rrests': result['data'][1]['rrsets'] + }), created_by=current_user.username, domain_id=domain.id) history.add() @@ -700,11 +696,10 @@ def record_apply(domain_name): history = History( msg='Failed to apply record changes to domain {0}'.format( pretty_domain_name(domain_name)), - detail=str( - json.dumps({ - "domain": domain_name, - "msg": result['msg'], - })), + detail = json.dumps({ + 'domain': domain_name, + 'msg': result['msg'], + }), created_by=current_user.username) history.add() return make_response(jsonify(result), 400) diff --git a/powerdnsadmin/routes/index.py b/powerdnsadmin/routes/index.py index 990854d..6214617 100644 --- a/powerdnsadmin/routes/index.py +++ b/powerdnsadmin/routes/index.py @@ -550,13 +550,12 @@ def signin_history(username, authenticator, success): # Write history History(msg='User {} authentication {}'.format(username, str_success), - detail=str( - json.dumps({ - "username": username, - "authenticator": authenticator, - "ip_address": request_ip, - "success": 1 if success else 0 - })), + detail = json.dumps({ + 'username': username, + 'authenticator': authenticator, + 'ip_address': request_ip, + 'success': 1 if success else 0 + }), created_by='System').add() # Get a list of Azure security groups the user is a member of @@ -865,14 +864,13 @@ def dyndns_update(): if result['status'] == 'ok': history = History( msg='DynDNS update: updated {} successfully'.format(hostname), - detail=str( - json.dumps({ - "domain": domain.name, - "record": hostname, - "type": rtype, - "old_value": oldip, - "new_value": str(ip) - })), + detail = json.dumps({ + 'domain': domain.name, + 'record': hostname, + 'type': rtype, + 'old_value': oldip, + 'new_value': str(ip) + }), created_by=current_user.username, domain_id=domain.id) history.add() @@ -908,12 +906,11 @@ def dyndns_update(): msg= 'DynDNS update: created record {0} in zone {1} successfully' .format(hostname, domain.name, str(ip)), - detail=str( - json.dumps({ - "domain": domain.name, - "record": hostname, - "value": str(ip) - })), + detail = json.dumps({ + 'domain': domain.name, + 'record': hostname, + 'value': str(ip) + }), created_by=current_user.username, domain_id=domain.id) history.add()