From 08267025372f532e6c6a8ca455c68c3620590941 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Fri, 6 Apr 2018 13:22:09 +0700 Subject: [PATCH 01/11] Update config template --- app/views.py | 2 +- config_template.py | 2 +- configs/development.py | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/views.py b/app/views.py index 0cb9bf9..2a02863 100644 --- a/app/views.py +++ b/app/views.py @@ -551,7 +551,7 @@ def domain(domain_name): if not re.search('ip6\.arpa|in-addr\.arpa$', domain_name): editable_records = app.config['RECORDS_ALLOW_EDIT'] else: - editable_records = app.config['REVERSE_ALLOW_EDIT'] + editable_records = app.config['REVERSE_RECORDS_ALLOW_EDIT'] return render_template('domain.html', domain=domain, records=records, editable_records=editable_records) else: for jr in jrecords: diff --git a/config_template.py b/config_template.py index 2a0766a..cbc4d2a 100644 --- a/config_template.py +++ b/config_template.py @@ -123,7 +123,7 @@ PDNS_VERSION = '4.1.1' # RECORDS ALLOWED TO EDIT RECORDS_ALLOW_EDIT = ['SOA', 'A', 'AAAA', 'CAA', 'CNAME', 'MX', 'PTR', 'SPF', 'SRV', 'TXT', 'LOC', 'NS', 'PTR'] FORWARD_RECORDS_ALLOW_EDIT = ['A', 'AAAA', 'CAA', 'CNAME', 'MX', 'PTR', 'SPF', 'SRV', 'TXT', 'LOC' 'NS'] -REVERSE_RECORDS_ALLOW_EDIT = ['TXT', 'LOC', 'NS', 'PTR'] +REVERSE_RECORDS_ALLOW_EDIT = ['SOA', 'TXT', 'LOC', 'NS', 'PTR'] # EXPERIMENTAL FEATURES PRETTY_IPV6_PTR = False diff --git a/configs/development.py b/configs/development.py index 45ad679..f46b97a 100644 --- a/configs/development.py +++ b/configs/development.py @@ -111,10 +111,9 @@ PDNS_API_KEY = 'changeme' PDNS_VERSION = '4.1.1' # RECORDS ALLOWED TO EDIT -RECORDS_ALLOW_EDIT = ['A', 'AAAA', 'CAA', 'CNAME', 'MX', 'PTR', 'SPF', 'SRV', 'TXT', 'NS'] - -# RECORDS ALLOWED TO EDIT FOR REVERSE DOMAINS -REVERSE_ALLOW_EDIT = ['PTR', 'NS'] +RECORDS_ALLOW_EDIT = ['SOA', 'A', 'AAAA', 'CAA', 'CNAME', 'MX', 'PTR', 'SPF', 'SRV', 'TXT', 'LOC', 'NS', 'PTR'] +FORWARD_RECORDS_ALLOW_EDIT = ['A', 'AAAA', 'CAA', 'CNAME', 'MX', 'PTR', 'SPF', 'SRV', 'TXT', 'LOC' 'NS'] +REVERSE_RECORDS_ALLOW_EDIT = ['SOA', 'TXT', 'LOC', 'NS', 'PTR'] # EXPERIMENTAL FEATURES PRETTY_IPV6_PTR = False From fc4e9dc9a057f826a99d1a620fa95c62b6db3c4a Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Fri, 6 Apr 2018 18:05:38 +0700 Subject: [PATCH 02/11] #233. Make sure password hash is string before inserting to the db --- app/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models.py b/app/models.py index 25024b7..3d74cb7 100644 --- a/app/models.py +++ b/app/models.py @@ -315,6 +315,7 @@ class User(db.Model): self.role_id = Role.query.filter_by(name='Administrator').first().id self.password = self.get_hashed_password(self.plain_text_password) + self.password = self.password.decode("utf-8") db.session.add(self) db.session.commit() From 5354d27f885301df9121a21d513c84ce4c59f3b1 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Sun, 8 Apr 2018 09:09:08 +0700 Subject: [PATCH 03/11] Fix #234 --- app/templates/user_profile.html | 3 ++- app/views.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/templates/user_profile.html b/app/templates/user_profile.html index a94efe8..dbda925 100644 --- a/app/templates/user_profile.html +++ b/app/templates/user_profile.html @@ -32,7 +32,8 @@ {% if not external_account %}
  • Change Password
  • Authentication -
  • {% endif %}> + + {% endif %}
    diff --git a/app/views.py b/app/views.py index 2a02863..4df2cee 100644 --- a/app/views.py +++ b/app/views.py @@ -1150,7 +1150,7 @@ def admin_settings_edit(setting): @login_required def user_profile(): external_account = False - if session.has_key('external_auth'): + if 'external_auth' in session: external_account = session['external_auth'] if request.method == 'GET' or external_account: return render_template('user_profile.html', external_account=external_account) From 060e0917bc2b38bf5b4b42119510cecee7a932f1 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Mon, 9 Apr 2018 18:50:55 +0700 Subject: [PATCH 04/11] Fix #236 --- app/lib/utils.py | 3 +++ docker-compose.dev.yml | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/lib/utils.py b/app/lib/utils.py index 84c4a75..5ec8644 100644 --- a/app/lib/utils.py +++ b/app/lib/utils.py @@ -196,6 +196,9 @@ def email_to_gravatar_url(email="", size=100): """ AD doesn't necessarily have email """ + if email is None: + email = "" + hash_string = hashlib.md5(email.encode('utf-8')).hexdigest() return "https://s.gravatar.com/avatar/{0}?s={1}".format(hash_string, size) diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 57b1e16..5f283c2 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -9,7 +9,6 @@ services: container_name: powerdns-admin mem_limit: 256M memswap_limit: 256M - tty: true command: /usr/bin/supervisord -c /etc/supervisord.conf ports: - "9191:9191" From fdf849744b4bc64d58bfdfb89b715bf355a63f08 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Tue, 10 Apr 2018 07:08:22 +0700 Subject: [PATCH 05/11] Fix #238 --- app/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views.py b/app/views.py index 4df2cee..fca2623 100644 --- a/app/views.py +++ b/app/views.py @@ -118,7 +118,7 @@ def login_via_authorization_header(request): if auth_header: auth_header = auth_header.replace('Basic ', '', 1) try: - auth_header = base64.b64decode(auth_header) + auth_header = str(base64.b64decode(auth_header), 'utf-8') username,password = auth_header.split(":") except TypeError as e: return None From 84d4bfaed09ba7041ddf7de88754cfe4b5f476b7 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Tue, 10 Apr 2018 08:59:28 +0700 Subject: [PATCH 06/11] Mark LDAP authentication as external_auth. Fix OTP secret update. #237 --- app/models.py | 11 +++++-- app/views.py | 82 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 59 insertions(+), 34 deletions(-) diff --git a/app/models.py b/app/models.py index 3d74cb7..0758aa8 100644 --- a/app/models.py +++ b/app/models.py @@ -135,6 +135,9 @@ class User(db.Model): def get_hashed_password(self, plain_text_password=None): # Hash a password for the first time # (Using bcrypt, the salt is saved into the hash itself) + if plain_text_password == None: + return plain_text_password + pw = plain_text_password if plain_text_password else self.plain_text_password return bcrypt.hashpw(pw.encode('utf-8'), bcrypt.gensalt()) @@ -315,7 +318,9 @@ class User(db.Model): self.role_id = Role.query.filter_by(name='Administrator').first().id self.password = self.get_hashed_password(self.plain_text_password) - self.password = self.password.decode("utf-8") + + if self.password: + self.password = self.password.decode("utf-8") db.session.add(self) db.session.commit() @@ -336,7 +341,9 @@ class User(db.Model): user.password = self.get_hashed_password(self.plain_text_password) if self.plain_text_password else user.password user.avatar = self.avatar if self.avatar else user.avatar - user.otp_secret = "" + if enable_otp is not None: + user.otp_secret = "" + if enable_otp == True: # generate the opt secret key user.otp_secret = base64.b32encode(os.urandom(10)).decode('utf-8') diff --git a/app/views.py b/app/views.py index fca2623..ffa01aa 100644 --- a/app/views.py +++ b/app/views.py @@ -312,12 +312,13 @@ def login(): return redirect(url_for('index')) if request.method == 'GET': - return render_template('login.html', - github_enabled=GITHUB_ENABLE, - google_enabled=GOOGLE_ENABLE, - saml_enabled=SAML_ENABLED, - ldap_enabled=LDAP_ENABLED, login_title=LOGIN_TITLE, - basic_enabled=BASIC_ENABLED, signup_enabled=SIGNUP_ENABLED) + return render_template('login.html', github_enabled=GITHUB_ENABLE, + google_enabled=GOOGLE_ENABLE, + saml_enabled=SAML_ENABLED, + ldap_enabled=LDAP_ENABLED, + login_title=LOGIN_TITLE, + basic_enabled=BASIC_ENABLED, + signup_enabled=SIGNUP_ENABLED) # process login username = request.form['username'] @@ -331,6 +332,9 @@ def login(): email = request.form.get('email') rpassword = request.form.get('rpassword') + if auth_method != 'LOCAL': + session['external_auth'] = True + if None in [firstname, lastname, email]: #login case remember_me = False @@ -342,37 +346,46 @@ def login(): try: auth = user.is_validate(method=auth_method) if auth == False: - return render_template('login.html', error='Invalid credentials', ldap_enabled=LDAP_ENABLED, - login_title=LOGIN_TITLE, - basic_enabled=BASIC_ENABLED, - signup_enabled=SIGNUP_ENABLED, - github_enabled=GITHUB_ENABLE, - saml_enabled=SAML_ENABLED) + return render_template('login.html', error='Invalid credentials', + github_enabled=GITHUB_ENABLE, + google_enabled=GOOGLE_ENABLE, + saml_enabled=SAML_ENABLED, + ldap_enabled=LDAP_ENABLED, + login_title=LOGIN_TITLE, + basic_enabled=BASIC_ENABLED, + signup_enabled=SIGNUP_ENABLED) except Exception as e: - return render_template('login.html', error=e, ldap_enabled=LDAP_ENABLED, login_title=LOGIN_TITLE, - basic_enabled=BASIC_ENABLED, - signup_enabled=SIGNUP_ENABLED, - github_enabled=GITHUB_ENABLE, - saml_enabled=SAML_ENABLED) + return render_template('login.html', error=e, + github_enabled=GITHUB_ENABLE, + google_enabled=GOOGLE_ENABLE, + saml_enabled=SAML_ENABLED, + ldap_enabled=LDAP_ENABLED, + login_title=LOGIN_TITLE, + basic_enabled=BASIC_ENABLED, + signup_enabled=SIGNUP_ENABLED) # check if user enabled OPT authentication if user.otp_secret: if otp_token: good_token = user.verify_totp(otp_token) if not good_token: - return render_template('login.html', error='Invalid credentials', ldap_enabled=LDAP_ENABLED, - login_title=LOGIN_TITLE, - basic_enabled=BASIC_ENABLED, - signup_enabled=SIGNUP_ENABLED, - github_enabled=GITHUB_ENABLE, - saml_enabled=SAML_ENABLED) + return render_template('login.html', error='Invalid credentials', + github_enabled=GITHUB_ENABLE, + google_enabled=GOOGLE_ENABLE, + saml_enabled=SAML_ENABLED, + ldap_enabled=LDAP_ENABLED, + login_title=LOGIN_TITLE, + basic_enabled=BASIC_ENABLED, + signup_enabled=SIGNUP_ENABLED) else: - return render_template('login.html', error='Token required', ldap_enabled=LDAP_ENABLED, - login_title=LOGIN_TITLE, - basic_enabled=BASIC_ENABLED, - signup_enabled=SIGNUP_ENABLED, - github_enabled = GITHUB_ENABLE, - saml_enabled = SAML_ENABLED) + return render_template('login.html', error='Token required', + github_enabled=GITHUB_ENABLE, + google_enabled=GOOGLE_ENABLE, + saml_enabled=SAML_ENABLED, + ldap_enabled=LDAP_ENABLED, + login_title=LOGIN_TITLE, + basic_enabled=BASIC_ENABLED, + signup_enabled=SIGNUP_ENABLED) login_user(user, remember = remember_me) return redirect(request.args.get('next') or url_for('index')) @@ -389,9 +402,14 @@ def login(): try: result = user.create_local_user() if result == True: - return render_template('login.html', username=username, password=password, ldap_enabled=LDAP_ENABLED, - login_title=LOGIN_TITLE, basic_enabled=BASIC_ENABLED, signup_enabled=SIGNUP_ENABLED, - github_enabled=GITHUB_ENABLE,saml_enabled=SAML_ENABLED) + return render_template('login.html', username=username, password=password, + github_enabled=GITHUB_ENABLE, + google_enabled=GOOGLE_ENABLE, + saml_enabled=SAML_ENABLED, + ldap_enabled=LDAP_ENABLED, + login_title=LOGIN_TITLE, + basic_enabled=BASIC_ENABLED, + signup_enabled=SIGNUP_ENABLED) else: return render_template('register.html', error=result['msg']) except Exception as e: From fc737cf61f037e6e2a6678c459f2b0205aa30f16 Mon Sep 17 00:00:00 2001 From: Kees Monshouwer Date: Wed, 11 Apr 2018 18:09:38 +0200 Subject: [PATCH 07/11] strip domain part from names while cloning a zone to a template --- app/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views.py b/app/views.py index ffa01aa..d40bd2e 100644 --- a/app/views.py +++ b/app/views.py @@ -916,17 +916,18 @@ def create_template_from_zone(): if NEW_SCHEMA: for jr in jrecords: - name = '@' if jr['name'] == domain_name else jr['name'] if jr['type'] in app.config['RECORDS_ALLOW_EDIT']: + name = '@' if jr['name'] == domain_name else jr['name'].replace('.'+domain_name, '') for subrecord in jr['records']: - record = DomainTemplateRecord(name=name, type=jr['type'], status=True if subrecord['disabled'] else False, ttl=jr['ttl'], data=subrecord['content']) records.append(record) else: for jr in jrecords: if jr['type'] in app.config['RECORDS_ALLOW_EDIT']: + name = '@' if jr['name'] == domain_name else jr['name'].replace('.'+domain_name, '') record = DomainTemplateRecord(name=name, type=jr['type'], status=True if jr['disabled'] else False, ttl=jr['ttl'], data=jr['content']) records.append(record) + result_records = t.replace_records(records) if result_records['status'] == 'ok': From a12af5345d5cc704d03276f7a9b9ec5740b59fa0 Mon Sep 17 00:00:00 2001 From: Kees Monshouwer Date: Thu, 12 Apr 2018 02:11:34 +0200 Subject: [PATCH 08/11] fix clear history --- app/templates/admin_history.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/templates/admin_history.html b/app/templates/admin_history.html index 59773ac..6ce9469 100644 --- a/app/templates/admin_history.html +++ b/app/templates/admin_history.html @@ -96,7 +96,7 @@
    From 52b6966c83a1c0d53557af2362c6d2cd8c1549df Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Thu, 12 Apr 2018 11:18:44 +0700 Subject: [PATCH 09/11] Check zone serial before allowing user to submit their change. #183 --- app/__init__.py | 4 +++ app/models.py | 45 ++++++++++++++++++++-------------- app/static/custom/js/custom.js | 31 +++++++++++++++++++++++ app/templates/domain.html | 11 ++++++--- app/views.py | 35 ++++++++++++++++++++------ 5 files changed, 97 insertions(+), 29 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 897db56..c309b78 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -7,6 +7,10 @@ app = Flask(__name__) app.config.from_object('config') app.wsgi_app = ProxyFix(app.wsgi_app) +#### CONFIGURE LOGGER #### +from app.lib.log import logger +logging = logger('powerdns-admin', app.config['LOG_LEVEL'], app.config['LOG_FILE']).config() + login_manager = LoginManager() login_manager.init_app(app) db = SQLAlchemy(app) diff --git a/app/models.py b/app/models.py index 0758aa8..0901c5a 100644 --- a/app/models.py +++ b/app/models.py @@ -10,6 +10,7 @@ import pyotp import re import dns.reversename import sys +import logging as logger from datetime import datetime from urllib.parse import urljoin @@ -19,10 +20,8 @@ from flask_login import AnonymousUserMixin from app import app, db from app.lib import utils -from app.lib.log import logger -# LOG CONFIGS -logging = logger('MODEL', app.config['LOG_LEVEL'], app.config['LOG_FILE']).config() +logging = logger.getLogger(__name__) if 'LDAP_TYPE' in app.config.keys(): LDAP_URI = app.config['LDAP_URI'] @@ -510,24 +509,18 @@ class Domain(db.Model): logging.error('Can not create setting {0} for domain {1}. {2}'.format(setting, self.name, e)) return False + def get_domain_info(self, domain_name): + """ + Get all domains which has in PowerDNS + """ + headers = {} + headers['X-API-Key'] = PDNS_API_KEY + jdata = utils.fetch_json(urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/{0}'.format(domain_name)), headers=headers) + return jdata + def get_domains(self): """ Get all domains which has in PowerDNS - jdata example: - [ - { - "id": "example.org.", - "url": "/servers/localhost/zones/example.org.", - "name": "example.org", - "kind": "Native", - "dnssec": false, - "account": "", - "masters": [], - "serial": 2015101501, - "notified_serial": 0, - "last_check": 0 - } - ] """ headers = {} headers['X-API-Key'] = PDNS_API_KEY @@ -886,6 +879,7 @@ class Domain(db.Model): else: return {'status': 'error', 'msg': 'This domain doesnot exist'} + class DomainUser(db.Model): __tablename__ = 'domain_user' id = db.Column(db.Integer, primary_key = True) @@ -1183,6 +1177,7 @@ class Record(object): return {'status': 'error', 'msg': jdata2['error']} else: self.auto_ptr(domain, new_records, deleted_records) + self.update_db_serial(domain) logging.info('Record was applied successfully.') return {'status': 'ok', 'msg': 'Record was applied successfully'} except Exception as e: @@ -1335,6 +1330,20 @@ class Record(object): logging.error("Cannot add record {0}/{1}/{2} to domain {3}. DETAIL: {4}".format(self.name, self.type, self.data, domain, e)) return {'status': 'error', 'msg': 'There was something wrong, please contact administrator'} + def update_db_serial(self, domain): + headers = {} + headers['X-API-Key'] = PDNS_API_KEY + jdata = utils.fetch_json(urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/localhost/zones/{0}'.format(domain)), headers=headers, method='GET') + serial = jdata['serial'] + + domain = Domain.query.filter(Domain.name==domain).first() + if domain: + domain.serial = serial + db.session.commit() + return {'status': True, 'msg': 'Synced local serial for domain name {0}'.format(domain)} + else: + return {'status': False, 'msg': 'Could not find domain name {0} in local db'.format(domain)} + class Server(object): """ diff --git a/app/static/custom/js/custom.js b/app/static/custom/js/custom.js index 936b8ee..279463d 100644 --- a/app/static/custom/js/custom.js +++ b/app/static/custom/js/custom.js @@ -30,6 +30,37 @@ function applyChanges(data, url, showResult, refreshPage) { }); } +function applyRecordChanges(data, domain) { + var success = false; + $.ajax({ + type : "POST", + url : $SCRIPT_ROOT + '/domain/' + domain + '/apply', + data : JSON.stringify(data),// now data come in this function + contentType : "application/json; charset=utf-8", + crossDomain : true, + dataType : "json", + success : function(data, status, jqXHR) { + // update Apply button value + $.getJSON($SCRIPT_ROOT + '/domain/' + domain + '/info', function(data) { + $(".button_apply_changes").val(data['serial']); + }); + + console.log("Applied changes successfully.") + var modal = $("#modal_success"); + modal.find('.modal-body p').text("Applied changes successfully"); + modal.modal('show'); + }, + + error : function(jqXHR, status) { + console.log(jqXHR); + var modal = $("#modal_error"); + var responseJson = jQuery.parseJSON(jqXHR.responseText); + modal.find('.modal-body p').text(responseJson['msg']); + modal.modal('show'); + } + }); +} + function getTableData(table) { var rData = [] diff --git a/app/templates/domain.html b/app/templates/domain.html index f91d374..203a8f1 100644 --- a/app/templates/domain.html +++ b/app/templates/domain.html @@ -25,7 +25,7 @@ - {% else %} @@ -196,11 +196,14 @@ var modal = $("#modal_apply_changes"); var table = $("#tbl_records").DataTable(); var domain = $(this).prop('id'); + var serial = $(".button_apply_changes").val(); var info = "Are you sure you want to apply your changes?"; modal.find('.modal-body p').text(info); - modal.find('#button_apply_confirm').click(function() { - var data = getTableData(table); - applyChanges(data, $SCRIPT_ROOT + '/domain/' + domain + '/apply', true); + + // following unbind("click") is to avoid multiple times execution + modal.find('#button_apply_confirm').unbind("click").click(function() { + var data = {'serial': serial, 'record': getTableData(table)}; + applyRecordChanges(data, domain); modal.modal('hide'); }) modal.modal('show'); diff --git a/app/views.py b/app/views.py index ffa01aa..70b2f78 100644 --- a/app/views.py +++ b/app/views.py @@ -1,5 +1,6 @@ import base64 import json +import logging as logger import os import traceback import re @@ -19,15 +20,13 @@ from werkzeug.security import gen_salt from .models import User, Domain, Record, Server, History, Anonymous, Setting, DomainSetting, DomainTemplate, DomainTemplateRecord from app import app, login_manager, github, google from app.lib import utils -from app.lib.log import logger from app.decorators import admin_role_required, can_access_domain if app.config['SAML_ENABLED']: from onelogin.saml2.auth import OneLogin_Saml2_Auth from onelogin.saml2.utils import OneLogin_Saml2_Utils -# LOG CONFIG -logging = logger('MODEL', app.config['LOG_LEVEL'], app.config['LOG_FILE']).config() +logging = logger.getLogger(__name__) # FILTERS jinja2.filters.FILTERS['display_record_name'] = utils.display_record_name @@ -541,7 +540,6 @@ def dashboard_domains(): @app.route('/domain/', methods=['GET', 'POST']) -@app.route('/domain', methods=['GET', 'POST']) @login_required @can_access_domain def domain(domain_name): @@ -580,7 +578,7 @@ def domain(domain_name): editable_records = app.config['FORWARD_RECORDS_ALLOW_EDIT'] else: editable_records = app.config['REVERSE_RECORDS_ALLOW_EDIT'] - return render_template('domain.html', domain=domain, records=records, editable_records=editable_records,pdns_version=app.config['PDNS_VERSION']) + return render_template('domain.html', domain=domain, records=records, editable_records=editable_records, pdns_version=app.config['PDNS_VERSION']) @app.route('/admin/domain/add', methods=['GET', 'POST']) @@ -593,7 +591,6 @@ def domain_add(): domain_name = request.form.getlist('domain_name')[0] domain_type = request.form.getlist('radio_type')[0] domain_template = request.form.getlist('domain_template')[0] - logging.info("Selected template ==== {0}".format(domain_template)) soa_edit_api = request.form.getlist('radio_type_soa_edit_api')[0] if ' ' in domain_name or not domain_name or not domain_type: @@ -716,11 +713,26 @@ def record_apply(domain_name): example jdata: {u'record_ttl': u'1800', u'record_type': u'CNAME', u'record_name': u'test4', u'record_status': u'Active', u'record_data': u'duykhanh.me'} """ #TODO: filter removed records / name modified records. + try: jdata = request.json + submitted_serial = jdata['serial'] + submitted_record = jdata['record'] + + domain = Domain.query.filter(Domain.name==domain_name).first() + + logging.debug('Your submitted serial: {0}'.format(submitted_serial)) + logging.debug('Current domain serial: {0}'.format(domain.serial)) + + if domain: + if int(submitted_serial) != domain.serial: + return make_response(jsonify( {'status': 'error', 'msg': 'The zone has been changed by another session or user. Please refresh this web page to load updated records.'} ), 500) + else: + return make_response(jsonify( {'status': 'error', 'msg': 'Domain name {0} does not exist'.format(domain_name)} ), 404) + r = Record() - result = r.apply(domain_name, jdata) + result = r.apply(domain_name, submitted_record) if result['status'] == 'ok': history = History(msg='Apply record changes to domain {0}'.format(domain_name), detail=str(jdata), created_by=current_user.username) history.add() @@ -770,6 +782,15 @@ def record_delete(domain_name, record_name, record_type): return redirect(url_for('domain', domain_name=domain_name)) +@app.route('/domain//info', methods=['GET']) +@login_required +@can_access_domain +def domain_info(domain_name): + domain = Domain() + domain_info = domain.get_domain_info(domain_name) + return make_response(jsonify(domain_info), 200) + + @app.route('/domain//dnssec', methods=['GET']) @login_required @can_access_domain From 8b7653ad4a95f20487886916328bb0745e07e3da Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Thu, 12 Apr 2018 11:44:56 +0700 Subject: [PATCH 10/11] Change data column data type of DomainTemplateRecord to TEXT --- app/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models.py b/app/models.py index 0901c5a..b0e0fcf 100644 --- a/app/models.py +++ b/app/models.py @@ -1561,7 +1561,7 @@ class DomainTemplateRecord(db.Model): name = db.Column(db.String(255)) type = db.Column(db.String(64)) ttl = db.Column(db.Integer) - data = db.Column(db.String(255)) + data = db.Column(db.Text) status = db.Column(db.Boolean) template_id = db.Column(db.Integer, db.ForeignKey('domain_template.id')) template = db.relationship('DomainTemplate', back_populates='records') From df9e392e2699490375773d2a5e1fad0e4202c742 Mon Sep 17 00:00:00 2001 From: Kees Monshouwer Date: Thu, 12 Apr 2018 02:20:49 +0200 Subject: [PATCH 11/11] domain stripping was not limited to the end of a name --- app/lib/utils.py | 2 +- app/views.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/lib/utils.py b/app/lib/utils.py index 5ec8644..934ee4d 100644 --- a/app/lib/utils.py +++ b/app/lib/utils.py @@ -130,7 +130,7 @@ def display_record_name(data): if record_name == domain_name: return '@' else: - return record_name.replace('.'+domain_name, '') + return re.sub('\.{}$'.format(domain_name), '', record_name) def display_master_name(data): diff --git a/app/views.py b/app/views.py index d40bd2e..d775189 100644 --- a/app/views.py +++ b/app/views.py @@ -917,14 +917,14 @@ def create_template_from_zone(): if NEW_SCHEMA: for jr in jrecords: if jr['type'] in app.config['RECORDS_ALLOW_EDIT']: - name = '@' if jr['name'] == domain_name else jr['name'].replace('.'+domain_name, '') + name = '@' if jr['name'] == domain_name else re.sub('\.{}$'.format(domain_name), '', jr['name']) for subrecord in jr['records']: record = DomainTemplateRecord(name=name, type=jr['type'], status=True if subrecord['disabled'] else False, ttl=jr['ttl'], data=subrecord['content']) records.append(record) else: for jr in jrecords: if jr['type'] in app.config['RECORDS_ALLOW_EDIT']: - name = '@' if jr['name'] == domain_name else jr['name'].replace('.'+domain_name, '') + name = '@' if jr['name'] == domain_name else re.sub('\.{}$'.format(domain_name), '', jr['name']) record = DomainTemplateRecord(name=name, type=jr['type'], status=True if jr['disabled'] else False, ttl=jr['ttl'], data=jr['content']) records.append(record)