2022-05-06 08:17:03 +00:00
|
|
|
import json
|
2019-12-02 03:32:03 +00:00
|
|
|
import re
|
|
|
|
import traceback
|
|
|
|
from flask import current_app
|
2022-06-28 09:16:45 +00:00
|
|
|
from flask_login import current_user
|
2019-12-02 03:32:03 +00:00
|
|
|
from urllib.parse import urljoin
|
|
|
|
from distutils.util import strtobool
|
|
|
|
|
|
|
|
from ..lib import utils
|
|
|
|
from .base import db, domain_apikey
|
|
|
|
from .setting import Setting
|
|
|
|
from .user import User
|
|
|
|
from .account import Account
|
|
|
|
from .account import AccountUser
|
|
|
|
from .domain_user import DomainUser
|
|
|
|
from .domain_setting import DomainSetting
|
|
|
|
from .history import History
|
|
|
|
|
|
|
|
|
|
|
|
class Domain(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(255), index=True, unique=True)
|
|
|
|
master = db.Column(db.String(128))
|
2023-01-10 12:51:04 +00:00
|
|
|
type = db.Column(db.String(8), nullable=False)
|
2019-12-20 04:36:27 +00:00
|
|
|
serial = db.Column(db.BigInteger)
|
|
|
|
notified_serial = db.Column(db.BigInteger)
|
2019-12-02 03:32:03 +00:00
|
|
|
last_check = db.Column(db.Integer)
|
|
|
|
dnssec = db.Column(db.Integer)
|
|
|
|
account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
|
|
|
|
account = db.relationship("Account", back_populates="domains")
|
|
|
|
settings = db.relationship('DomainSetting', back_populates='domain')
|
|
|
|
apikeys = db.relationship("ApiKey",
|
|
|
|
secondary=domain_apikey,
|
|
|
|
back_populates="domains")
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
id=None,
|
|
|
|
name=None,
|
|
|
|
master=None,
|
|
|
|
type='NATIVE',
|
|
|
|
serial=None,
|
|
|
|
notified_serial=None,
|
|
|
|
last_check=None,
|
|
|
|
dnssec=None,
|
|
|
|
account_id=None):
|
|
|
|
self.id = id
|
|
|
|
self.name = name
|
|
|
|
self.master = master
|
|
|
|
self.type = type
|
|
|
|
self.serial = serial
|
|
|
|
self.notified_serial = notified_serial
|
|
|
|
self.last_check = last_check
|
|
|
|
self.dnssec = dnssec
|
|
|
|
self.account_id = account_id
|
|
|
|
# PDNS configs
|
|
|
|
self.PDNS_STATS_URL = Setting().get('pdns_api_url')
|
|
|
|
self.PDNS_API_KEY = Setting().get('pdns_api_key')
|
|
|
|
self.PDNS_VERSION = Setting().get('pdns_version')
|
|
|
|
self.API_EXTENDED_URL = utils.pdns_api_extended_uri(self.PDNS_VERSION)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Domain {0}>'.format(self.name)
|
|
|
|
|
|
|
|
def add_setting(self, setting, value):
|
|
|
|
try:
|
|
|
|
self.settings.append(DomainSetting(setting=setting, value=value))
|
|
|
|
db.session.commit()
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.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
|
|
|
|
"""
|
2020-06-19 01:47:51 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY}
|
2019-12-02 03:32:03 +00:00
|
|
|
jdata = utils.fetch_json(urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}'.format(domain_name)),
|
|
|
|
headers=headers,
|
|
|
|
timeout=int(
|
|
|
|
Setting().get('pdns_api_timeout')),
|
|
|
|
verify=Setting().get('verify_ssl_connections'))
|
2019-12-02 03:32:03 +00:00
|
|
|
return jdata
|
|
|
|
|
|
|
|
def get_domains(self):
|
|
|
|
"""
|
|
|
|
Get all domains which has in PowerDNS
|
|
|
|
"""
|
2020-06-19 01:47:51 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY}
|
2019-12-02 03:32:03 +00:00
|
|
|
jdata = utils.fetch_json(
|
|
|
|
urljoin(self.PDNS_STATS_URL,
|
|
|
|
self.API_EXTENDED_URL + '/servers/localhost/zones'),
|
|
|
|
headers=headers,
|
2020-01-25 18:44:11 +00:00
|
|
|
timeout=int(Setting().get('pdns_api_timeout')),
|
|
|
|
verify=Setting().get('verify_ssl_connections'))
|
2019-12-02 03:32:03 +00:00
|
|
|
return jdata
|
|
|
|
|
|
|
|
def get_id_by_name(self, name):
|
|
|
|
"""
|
|
|
|
Return domain id
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
domain = Domain.query.filter(Domain.name == name).first()
|
|
|
|
return domain.id
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
'Domain does not exist. ERROR: {0}'.format(e))
|
|
|
|
return None
|
|
|
|
|
2022-12-10 02:37:06 +00:00
|
|
|
def search_idn_domains(self, search_string):
|
|
|
|
"""
|
|
|
|
Search for IDN domains using the provided search string.
|
|
|
|
"""
|
|
|
|
# Compile the regular expression pattern for matching IDN domain names
|
|
|
|
idn_pattern = re.compile(r'^xn--')
|
|
|
|
|
|
|
|
# Search for domain names that match the IDN pattern
|
|
|
|
idn_domains = [
|
|
|
|
domain for domain in self.get_domains() if idn_pattern.match(domain)
|
|
|
|
]
|
|
|
|
|
|
|
|
# Filter the search results based on the provided search string
|
|
|
|
return [domain for domain in idn_domains if search_string in domain]
|
|
|
|
|
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
def update(self):
|
|
|
|
"""
|
|
|
|
Fetch zones (domains) from PowerDNS and update into DB
|
|
|
|
"""
|
|
|
|
db_domain = Domain.query.all()
|
|
|
|
list_db_domain = [d.name for d in db_domain]
|
|
|
|
dict_db_domain = dict((x.name, x) for x in db_domain)
|
2020-08-06 13:16:18 +00:00
|
|
|
current_app.logger.info("Found {} domains in PowerDNS-Admin".format(
|
2019-12-02 03:32:03 +00:00
|
|
|
len(list_db_domain)))
|
2020-06-19 01:47:51 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY}
|
2019-12-02 03:32:03 +00:00
|
|
|
try:
|
|
|
|
jdata = utils.fetch_json(
|
|
|
|
urljoin(self.PDNS_STATS_URL,
|
|
|
|
self.API_EXTENDED_URL + '/servers/localhost/zones'),
|
|
|
|
headers=headers,
|
2020-01-25 18:44:11 +00:00
|
|
|
timeout=int(Setting().get('pdns_api_timeout')),
|
|
|
|
verify=Setting().get('verify_ssl_connections'))
|
2019-12-02 03:32:03 +00:00
|
|
|
list_jdomain = [d['name'].rstrip('.') for d in jdata]
|
|
|
|
current_app.logger.info(
|
2020-08-06 13:16:18 +00:00
|
|
|
"Found {} zones in PowerDNS server".format(len(list_jdomain)))
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# domains should remove from db since it doesn't exist in powerdns anymore
|
|
|
|
should_removed_db_domain = list(
|
|
|
|
set(list_db_domain).difference(list_jdomain))
|
|
|
|
for domain_name in should_removed_db_domain:
|
2020-02-02 14:15:20 +00:00
|
|
|
self.delete_domain_from_pdnsadmin(domain_name, do_commit=False)
|
2019-12-02 03:32:03 +00:00
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
'Can not delete domain from DB. DETAIL: {0}'.format(e))
|
|
|
|
current_app.logger.debug(traceback.format_exc())
|
|
|
|
|
|
|
|
# update/add new domain
|
2022-06-18 12:23:05 +00:00
|
|
|
account_cache = {}
|
2019-12-02 03:32:03 +00:00
|
|
|
for data in jdata:
|
|
|
|
if 'account' in data:
|
2022-06-18 12:23:05 +00:00
|
|
|
# if no account is set don't try to query db
|
|
|
|
if data['account'] == '':
|
|
|
|
find_account_id = None
|
|
|
|
else:
|
|
|
|
find_account_id = account_cache.get(data['account'])
|
|
|
|
# if account was not queried in the past and hence not in cache
|
|
|
|
if find_account_id is None:
|
|
|
|
find_account_id = Account().get_id_by_name(data['account'])
|
|
|
|
# add to cache
|
|
|
|
account_cache[data['account']] = find_account_id
|
|
|
|
account_id = find_account_id
|
2019-12-02 03:32:03 +00:00
|
|
|
else:
|
|
|
|
current_app.logger.debug(
|
|
|
|
"No 'account' data found in API result - Unsupported PowerDNS version?"
|
|
|
|
)
|
|
|
|
account_id = None
|
|
|
|
domain = dict_db_domain.get(data['name'].rstrip('.'), None)
|
|
|
|
if domain:
|
2020-02-02 14:15:20 +00:00
|
|
|
self.update_pdns_admin_domain(domain, account_id, data, do_commit=False)
|
2019-12-02 03:32:03 +00:00
|
|
|
else:
|
|
|
|
# add new domain
|
2020-02-02 14:15:20 +00:00
|
|
|
self.add_domain_to_powerdns_admin(domain=data, do_commit=False)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2020-02-02 14:15:20 +00:00
|
|
|
db.session.commit()
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.info('Update domain finished')
|
|
|
|
return {
|
|
|
|
'status': 'ok',
|
|
|
|
'msg': 'Domain table has been updated successfully'
|
|
|
|
}
|
|
|
|
except Exception as e:
|
2020-02-02 14:15:20 +00:00
|
|
|
db.session.rollback()
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error(
|
2020-08-06 13:16:18 +00:00
|
|
|
'Cannot update domain table. Error: {0}'.format(e))
|
|
|
|
return {'status': 'error', 'msg': 'Cannot update domain table'}
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2020-02-02 14:15:20 +00:00
|
|
|
def update_pdns_admin_domain(self, domain, account_id, data, do_commit=True):
|
2019-12-02 03:32:03 +00:00
|
|
|
# existing domain, only update if something actually has changed
|
|
|
|
if (domain.master != str(data['masters'])
|
|
|
|
or domain.type != data['kind']
|
|
|
|
or domain.serial != data['serial']
|
|
|
|
or domain.notified_serial != data['notified_serial']
|
|
|
|
or domain.last_check != (1 if data['last_check'] else 0)
|
|
|
|
or domain.dnssec != data['dnssec']
|
|
|
|
or domain.account_id != account_id):
|
|
|
|
|
|
|
|
domain.master = str(data['masters'])
|
|
|
|
domain.type = data['kind']
|
|
|
|
domain.serial = data['serial']
|
|
|
|
domain.notified_serial = data['notified_serial']
|
|
|
|
domain.last_check = 1 if data['last_check'] else 0
|
|
|
|
domain.dnssec = 1 if data['dnssec'] else 0
|
|
|
|
domain.account_id = account_id
|
|
|
|
try:
|
2020-02-02 14:15:20 +00:00
|
|
|
if do_commit:
|
|
|
|
db.session.commit()
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.info("Updated PDNS-Admin domain {0}".format(
|
|
|
|
domain.name))
|
|
|
|
except Exception as e:
|
|
|
|
db.session.rollback()
|
2020-01-26 23:05:28 +00:00
|
|
|
current_app.logger.info("Rolled back Domain {0} {1}".format(
|
2019-12-02 03:32:03 +00:00
|
|
|
domain.name, e))
|
|
|
|
raise
|
|
|
|
|
|
|
|
def add(self,
|
|
|
|
domain_name,
|
|
|
|
domain_type,
|
|
|
|
soa_edit_api,
|
|
|
|
domain_ns=[],
|
|
|
|
domain_master_ips=[],
|
|
|
|
account_name=None):
|
|
|
|
"""
|
|
|
|
Add a domain to power dns
|
|
|
|
"""
|
|
|
|
|
2022-03-30 21:31:16 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY, 'Content-Type': 'application/json'}
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2019-12-16 07:23:17 +00:00
|
|
|
domain_name = domain_name + '.'
|
|
|
|
domain_ns = [ns + '.' for ns in domain_ns]
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
if soa_edit_api not in ["DEFAULT", "INCREASE", "EPOCH", "OFF"]:
|
|
|
|
soa_edit_api = 'DEFAULT'
|
|
|
|
|
|
|
|
elif soa_edit_api == 'OFF':
|
|
|
|
soa_edit_api = ''
|
|
|
|
|
|
|
|
post_data = {
|
|
|
|
"name": domain_name,
|
|
|
|
"kind": domain_type,
|
|
|
|
"masters": domain_master_ips,
|
|
|
|
"nameservers": domain_ns,
|
|
|
|
"soa_edit_api": soa_edit_api,
|
|
|
|
"account": account_name
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
jdata = utils.fetch_json(
|
|
|
|
urljoin(self.PDNS_STATS_URL,
|
|
|
|
self.API_EXTENDED_URL + '/servers/localhost/zones'),
|
|
|
|
headers=headers,
|
|
|
|
timeout=int(Setting().get('pdns_api_timeout')),
|
|
|
|
method='POST',
|
2020-01-25 18:44:11 +00:00
|
|
|
verify=Setting().get('verify_ssl_connections'),
|
2019-12-02 03:32:03 +00:00
|
|
|
data=post_data)
|
|
|
|
if 'error' in jdata.keys():
|
|
|
|
current_app.logger.error(jdata['error'])
|
2019-12-04 04:50:46 +00:00
|
|
|
if jdata.get('http_code') == 409:
|
|
|
|
return {'status': 'error', 'msg': 'Domain already exists'}
|
2019-12-02 03:32:03 +00:00
|
|
|
return {'status': 'error', 'msg': jdata['error']}
|
|
|
|
else:
|
|
|
|
current_app.logger.info(
|
|
|
|
'Added domain successfully to PowerDNS: {0}'.format(
|
|
|
|
domain_name))
|
|
|
|
self.add_domain_to_powerdns_admin(domain_dict=post_data)
|
|
|
|
return {'status': 'ok', 'msg': 'Added domain successfully'}
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error('Cannot add domain {0} {1}'.format(
|
|
|
|
domain_name, e))
|
|
|
|
current_app.logger.debug(traceback.format_exc())
|
|
|
|
return {'status': 'error', 'msg': 'Cannot add this domain.'}
|
|
|
|
|
2020-02-02 14:15:20 +00:00
|
|
|
def add_domain_to_powerdns_admin(self, domain=None, domain_dict=None, do_commit=True):
|
2019-12-02 03:32:03 +00:00
|
|
|
"""
|
|
|
|
Read Domain from PowerDNS and add into PDNS-Admin
|
|
|
|
"""
|
2020-06-19 01:47:51 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY}
|
2019-12-02 03:32:03 +00:00
|
|
|
if not domain:
|
|
|
|
try:
|
|
|
|
domain = utils.fetch_json(
|
|
|
|
urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}'.format(
|
|
|
|
domain_dict['name'])),
|
2019-12-02 03:32:03 +00:00
|
|
|
headers=headers,
|
2020-01-25 18:44:11 +00:00
|
|
|
timeout=int(Setting().get('pdns_api_timeout')),
|
|
|
|
verify=Setting().get('verify_ssl_connections'))
|
2019-12-02 03:32:03 +00:00
|
|
|
except Exception as e:
|
2020-01-26 23:05:28 +00:00
|
|
|
current_app.logger.error('Can not read domain from PDNS')
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error(e)
|
|
|
|
current_app.logger.debug(traceback.format_exc())
|
|
|
|
|
|
|
|
if 'account' in domain:
|
|
|
|
account_id = Account().get_id_by_name(domain['account'])
|
|
|
|
else:
|
|
|
|
current_app.logger.debug(
|
|
|
|
"No 'account' data found in API result - Unsupported PowerDNS version?"
|
|
|
|
)
|
|
|
|
account_id = None
|
|
|
|
# add new domain
|
|
|
|
d = Domain()
|
2019-12-08 11:23:36 +00:00
|
|
|
d.name = domain['name'].rstrip('.') # lgtm [py/modification-of-default-value]
|
2019-12-02 03:32:03 +00:00
|
|
|
d.master = str(domain['masters'])
|
|
|
|
d.type = domain['kind']
|
|
|
|
d.serial = domain['serial']
|
|
|
|
d.notified_serial = domain['notified_serial']
|
|
|
|
d.last_check = domain['last_check']
|
|
|
|
d.dnssec = 1 if domain['dnssec'] else 0
|
|
|
|
d.account_id = account_id
|
|
|
|
db.session.add(d)
|
|
|
|
try:
|
2020-02-02 14:15:20 +00:00
|
|
|
if do_commit:
|
|
|
|
db.session.commit()
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.info(
|
2020-01-26 23:05:28 +00:00
|
|
|
"Synced PowerDNS Domain to PDNS-Admin: {0}".format(d.name))
|
2019-12-02 03:32:03 +00:00
|
|
|
return {
|
|
|
|
'status': 'ok',
|
|
|
|
'msg': 'Added Domain successfully to PowerDNS-Admin'
|
|
|
|
}
|
|
|
|
except Exception as e:
|
|
|
|
db.session.rollback()
|
2020-01-26 23:05:28 +00:00
|
|
|
current_app.logger.info("Rolled back Domain {0}".format(d.name))
|
2019-12-02 03:32:03 +00:00
|
|
|
raise
|
|
|
|
|
|
|
|
def update_soa_setting(self, domain_name, soa_edit_api):
|
|
|
|
domain = Domain.query.filter(Domain.name == domain_name).first()
|
|
|
|
if not domain:
|
2020-01-26 23:05:28 +00:00
|
|
|
return {'status': 'error', 'msg': 'Domain does not exist.'}
|
2020-06-19 01:47:51 +00:00
|
|
|
|
2022-03-30 21:31:16 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY, 'Content-Type': 'application/json'}
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
if soa_edit_api not in ["DEFAULT", "INCREASE", "EPOCH", "OFF"]:
|
|
|
|
soa_edit_api = 'DEFAULT'
|
|
|
|
|
|
|
|
elif soa_edit_api == 'OFF':
|
|
|
|
soa_edit_api = ''
|
|
|
|
|
|
|
|
post_data = {"soa_edit_api": soa_edit_api, "kind": domain.type}
|
|
|
|
|
|
|
|
try:
|
|
|
|
jdata = utils.fetch_json(urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}'.format(domain.name)),
|
|
|
|
headers=headers,
|
|
|
|
timeout=int(
|
|
|
|
Setting().get('pdns_api_timeout')),
|
|
|
|
method='PUT',
|
|
|
|
verify=Setting().get('verify_ssl_connections'),
|
|
|
|
data=post_data)
|
2019-12-02 03:32:03 +00:00
|
|
|
if 'error' in jdata.keys():
|
|
|
|
current_app.logger.error(jdata['error'])
|
|
|
|
return {'status': 'error', 'msg': jdata['error']}
|
|
|
|
else:
|
|
|
|
current_app.logger.info(
|
|
|
|
'soa-edit-api changed for domain {0} successfully'.format(
|
|
|
|
domain_name))
|
|
|
|
return {
|
|
|
|
'status': 'ok',
|
|
|
|
'msg': 'soa-edit-api changed successfully'
|
|
|
|
}
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.debug(e)
|
|
|
|
current_app.logger.debug(traceback.format_exc())
|
|
|
|
current_app.logger.error(
|
|
|
|
'Cannot change soa-edit-api for domain {0}'.format(
|
|
|
|
domain_name))
|
|
|
|
return {
|
|
|
|
'status': 'error',
|
2020-01-26 23:05:28 +00:00
|
|
|
'msg': 'Cannot change soa-edit-api for this domain.'
|
2019-12-02 03:32:03 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 09:02:36 +00:00
|
|
|
def update_kind(self, domain_name, kind, masters=[]):
|
|
|
|
"""
|
|
|
|
Update zone kind: Native / Master / Slave
|
|
|
|
"""
|
|
|
|
domain = Domain.query.filter(Domain.name == domain_name).first()
|
|
|
|
if not domain:
|
2020-01-26 23:05:28 +00:00
|
|
|
return {'status': 'error', 'msg': 'Domain does not exist.'}
|
2020-06-19 01:47:51 +00:00
|
|
|
|
2022-03-30 21:31:16 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY, 'Content-Type': 'application/json'}
|
2019-12-11 09:02:36 +00:00
|
|
|
|
|
|
|
post_data = {"kind": kind, "masters": masters}
|
|
|
|
|
|
|
|
try:
|
|
|
|
jdata = utils.fetch_json(urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}'.format(domain.name)),
|
|
|
|
headers=headers,
|
|
|
|
timeout=int(
|
|
|
|
Setting().get('pdns_api_timeout')),
|
|
|
|
method='PUT',
|
|
|
|
verify=Setting().get('verify_ssl_connections'),
|
|
|
|
data=post_data)
|
2019-12-11 09:02:36 +00:00
|
|
|
if 'error' in jdata.keys():
|
|
|
|
current_app.logger.error(jdata['error'])
|
|
|
|
return {'status': 'error', 'msg': jdata['error']}
|
|
|
|
else:
|
|
|
|
current_app.logger.info(
|
|
|
|
'Update domain kind for {0} successfully'.format(
|
|
|
|
domain_name))
|
|
|
|
return {
|
|
|
|
'status': 'ok',
|
|
|
|
'msg': 'Domain kind changed successfully'
|
|
|
|
}
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
'Cannot update kind for domain {0}. Error: {1}'.format(
|
|
|
|
domain_name, e))
|
|
|
|
current_app.logger.debug(traceback.format_exc())
|
|
|
|
|
|
|
|
return {
|
|
|
|
'status': 'error',
|
|
|
|
'msg': 'Cannot update kind for this domain.'
|
|
|
|
}
|
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
def create_reverse_domain(self, domain_name, domain_reverse_name):
|
|
|
|
"""
|
|
|
|
Check the existing reverse lookup domain,
|
|
|
|
if not exists create a new one automatically
|
|
|
|
"""
|
|
|
|
domain_obj = Domain.query.filter(Domain.name == domain_name).first()
|
|
|
|
domain_auto_ptr = DomainSetting.query.filter(
|
|
|
|
DomainSetting.domain == domain_obj).filter(
|
2020-06-19 01:47:51 +00:00
|
|
|
DomainSetting.setting == 'auto_ptr').first()
|
2019-12-02 03:32:03 +00:00
|
|
|
domain_auto_ptr = strtobool(
|
|
|
|
domain_auto_ptr.value) if domain_auto_ptr else False
|
|
|
|
system_auto_ptr = Setting().get('auto_ptr')
|
|
|
|
self.name = domain_name
|
|
|
|
domain_id = self.get_id_by_name(domain_reverse_name)
|
2020-06-19 01:47:51 +00:00
|
|
|
if domain_id is None and \
|
|
|
|
(
|
|
|
|
system_auto_ptr or
|
|
|
|
domain_auto_ptr
|
|
|
|
):
|
|
|
|
result = self.add(domain_reverse_name, 'Master', 'DEFAULT', [], [])
|
2019-12-02 03:32:03 +00:00
|
|
|
self.update()
|
|
|
|
if result['status'] == 'ok':
|
|
|
|
history = History(msg='Add reverse lookup domain {0}'.format(
|
|
|
|
domain_reverse_name),
|
2022-05-06 08:17:03 +00:00
|
|
|
detail=json.dumps({
|
2020-06-19 01:47:51 +00:00
|
|
|
'domain_type': 'Master',
|
|
|
|
'domain_master_ips': ''
|
|
|
|
}),
|
|
|
|
created_by='System')
|
2019-12-02 03:32:03 +00:00
|
|
|
history.add()
|
|
|
|
else:
|
|
|
|
return {
|
|
|
|
'status': 'error',
|
|
|
|
'msg': 'Adding reverse lookup domain failed'
|
|
|
|
}
|
|
|
|
domain_user_ids = self.get_user()
|
|
|
|
if len(domain_user_ids) > 0:
|
|
|
|
self.name = domain_reverse_name
|
|
|
|
self.grant_privileges(domain_user_ids)
|
|
|
|
return {
|
|
|
|
'status':
|
2020-06-19 01:47:51 +00:00
|
|
|
'ok',
|
2019-12-02 03:32:03 +00:00
|
|
|
'msg':
|
2020-06-19 01:47:51 +00:00
|
|
|
'New reverse lookup domain created with granted privileges'
|
2019-12-02 03:32:03 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
'status': 'ok',
|
|
|
|
'msg': 'New reverse lookup domain created without users'
|
|
|
|
}
|
|
|
|
return {'status': 'ok', 'msg': 'Reverse lookup domain already exists'}
|
|
|
|
|
|
|
|
def get_reverse_domain_name(self, reverse_host_address):
|
|
|
|
c = 1
|
|
|
|
if re.search('ip6.arpa', reverse_host_address):
|
|
|
|
for i in range(1, 32, 1):
|
|
|
|
address = re.search(
|
|
|
|
'((([a-f0-9]\.){' + str(i) + '})(?P<ipname>.+6.arpa)\.?)',
|
|
|
|
reverse_host_address)
|
|
|
|
if None != self.get_id_by_name(address.group('ipname')):
|
|
|
|
c = i
|
|
|
|
break
|
|
|
|
return re.search(
|
|
|
|
'((([a-f0-9]\.){' + str(c) + '})(?P<ipname>.+6.arpa)\.?)',
|
|
|
|
reverse_host_address).group('ipname')
|
|
|
|
else:
|
|
|
|
for i in range(1, 4, 1):
|
|
|
|
address = re.search(
|
|
|
|
'((([0-9]+\.){' + str(i) + '})(?P<ipname>.+r.arpa)\.?)',
|
|
|
|
reverse_host_address)
|
|
|
|
if None != self.get_id_by_name(address.group('ipname')):
|
|
|
|
c = i
|
|
|
|
break
|
|
|
|
return re.search(
|
|
|
|
'((([0-9]+\.){' + str(c) + '})(?P<ipname>.+r.arpa)\.?)',
|
|
|
|
reverse_host_address).group('ipname')
|
|
|
|
|
|
|
|
def delete(self, domain_name):
|
|
|
|
"""
|
|
|
|
Delete a single domain name from powerdns
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.delete_domain_from_powerdns(domain_name)
|
|
|
|
self.delete_domain_from_pdnsadmin(domain_name)
|
|
|
|
return {'status': 'ok', 'msg': 'Delete domain successfully'}
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
'Cannot delete domain {0}'.format(domain_name))
|
|
|
|
current_app.logger.error(e)
|
|
|
|
current_app.logger.debug(traceback.format_exc())
|
|
|
|
return {'status': 'error', 'msg': 'Cannot delete domain'}
|
|
|
|
|
|
|
|
def delete_domain_from_powerdns(self, domain_name):
|
|
|
|
"""
|
|
|
|
Delete a single domain name from powerdns
|
|
|
|
"""
|
2020-06-19 01:47:51 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY}
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
utils.fetch_json(urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}'.format(domain_name)),
|
|
|
|
headers=headers,
|
|
|
|
timeout=int(Setting().get('pdns_api_timeout')),
|
|
|
|
method='DELETE',
|
|
|
|
verify=Setting().get('verify_ssl_connections'))
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.info(
|
2020-01-26 23:05:28 +00:00
|
|
|
'Deleted domain successfully from PowerDNS: {0}'.format(
|
2019-12-02 03:32:03 +00:00
|
|
|
domain_name))
|
|
|
|
return {'status': 'ok', 'msg': 'Delete domain successfully'}
|
|
|
|
|
2020-02-02 14:15:20 +00:00
|
|
|
def delete_domain_from_pdnsadmin(self, domain_name, do_commit=True):
|
2019-12-02 03:32:03 +00:00
|
|
|
# Revoke permission before deleting domain
|
|
|
|
domain = Domain.query.filter(Domain.name == domain_name).first()
|
|
|
|
domain_user = DomainUser.query.filter(
|
|
|
|
DomainUser.domain_id == domain.id)
|
|
|
|
if domain_user:
|
|
|
|
domain_user.delete()
|
|
|
|
domain_setting = DomainSetting.query.filter(
|
|
|
|
DomainSetting.domain_id == domain.id)
|
|
|
|
if domain_setting:
|
|
|
|
domain_setting.delete()
|
|
|
|
domain.apikeys[:] = []
|
|
|
|
|
2021-05-07 20:55:45 +00:00
|
|
|
# Remove history for domain
|
2023-02-13 10:08:03 +00:00
|
|
|
if not Setting().get('preserve_history'):
|
|
|
|
domain_history = History.query.filter(
|
|
|
|
History.domain_id == domain.id
|
|
|
|
)
|
|
|
|
if domain_history:
|
|
|
|
domain_history.delete()
|
2021-05-07 20:55:45 +00:00
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
# then remove domain
|
|
|
|
Domain.query.filter(Domain.name == domain_name).delete()
|
2020-02-02 14:15:20 +00:00
|
|
|
if do_commit:
|
|
|
|
db.session.commit()
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.info(
|
2020-01-26 23:05:28 +00:00
|
|
|
"Deleted domain successfully from pdnsADMIN: {}".format(
|
2019-12-02 03:32:03 +00:00
|
|
|
domain_name))
|
|
|
|
|
|
|
|
def get_user(self):
|
|
|
|
"""
|
|
|
|
Get users (id) who have access to this domain name
|
|
|
|
"""
|
|
|
|
user_ids = []
|
|
|
|
query = db.session.query(
|
|
|
|
DomainUser, Domain).filter(User.id == DomainUser.user_id).filter(
|
2020-06-19 01:47:51 +00:00
|
|
|
Domain.id == DomainUser.domain_id).filter(
|
|
|
|
Domain.name == self.name).all()
|
2019-12-02 03:32:03 +00:00
|
|
|
for q in query:
|
|
|
|
user_ids.append(q[0].user_id)
|
|
|
|
return user_ids
|
|
|
|
|
|
|
|
def grant_privileges(self, new_user_ids):
|
|
|
|
"""
|
|
|
|
Reconfigure domain_user table
|
|
|
|
"""
|
|
|
|
|
|
|
|
domain_id = self.get_id_by_name(self.name)
|
|
|
|
domain_user_ids = self.get_user()
|
|
|
|
|
|
|
|
removed_ids = list(set(domain_user_ids).difference(new_user_ids))
|
|
|
|
added_ids = list(set(new_user_ids).difference(domain_user_ids))
|
|
|
|
|
|
|
|
try:
|
|
|
|
for uid in removed_ids:
|
|
|
|
DomainUser.query.filter(DomainUser.user_id == uid).filter(
|
|
|
|
DomainUser.domain_id == domain_id).delete()
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.session.rollback()
|
|
|
|
current_app.logger.error(
|
|
|
|
'Cannot revoke user privileges on domain {0}. DETAIL: {1}'.
|
2020-06-19 01:47:51 +00:00
|
|
|
format(self.name, e))
|
2019-12-06 03:32:57 +00:00
|
|
|
current_app.logger.debug(print(traceback.format_exc()))
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
for uid in added_ids:
|
|
|
|
du = DomainUser(domain_id, uid)
|
|
|
|
db.session.add(du)
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.session.rollback()
|
|
|
|
current_app.logger.error(
|
|
|
|
'Cannot grant user privileges to domain {0}. DETAIL: {1}'.
|
2020-06-19 01:47:51 +00:00
|
|
|
format(self.name, e))
|
2019-12-06 03:32:57 +00:00
|
|
|
current_app.logger.debug(print(traceback.format_exc()))
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2021-08-05 17:37:48 +00:00
|
|
|
def revoke_privileges_by_id(self, user_id):
|
|
|
|
"""
|
|
|
|
Remove a single user from privilege list based on user_id
|
|
|
|
"""
|
|
|
|
new_uids = [u for u in self.get_user() if u != user_id]
|
|
|
|
users = []
|
|
|
|
for uid in new_uids:
|
|
|
|
users.append(User(id=uid).get_user_info_by_id().username)
|
|
|
|
|
|
|
|
self.grant_privileges(users)
|
|
|
|
|
|
|
|
def add_user(self, user):
|
|
|
|
"""
|
|
|
|
Add a single user to Domain by User
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
du = DomainUser(self.id, user.id)
|
|
|
|
db.session.add(du)
|
|
|
|
db.session.commit()
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
db.session.rollback()
|
|
|
|
current_app.logger.error(
|
|
|
|
'Cannot add user privileges on domain {0}. DETAIL: {1}'.
|
|
|
|
format(self.name, e))
|
|
|
|
return False
|
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
def update_from_master(self, domain_name):
|
|
|
|
"""
|
|
|
|
Update records from Master DNS server
|
|
|
|
"""
|
|
|
|
domain = Domain.query.filter(Domain.name == domain_name).first()
|
|
|
|
if domain:
|
2020-06-19 01:47:51 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY}
|
2019-12-02 03:32:03 +00:00
|
|
|
try:
|
2019-12-11 06:45:27 +00:00
|
|
|
r = utils.fetch_json(urljoin(
|
2019-12-02 03:32:03 +00:00
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}/axfr-retrieve'.format(
|
|
|
|
domain.name)),
|
|
|
|
headers=headers,
|
|
|
|
timeout=int(
|
|
|
|
Setting().get('pdns_api_timeout')),
|
|
|
|
method='PUT',
|
|
|
|
verify=Setting().get('verify_ssl_connections'))
|
2019-12-11 06:45:27 +00:00
|
|
|
return {'status': 'ok', 'msg': r.get('result')}
|
2019-12-02 03:32:03 +00:00
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
'Cannot update from master. DETAIL: {0}'.format(e))
|
|
|
|
return {
|
|
|
|
'status':
|
2020-06-19 01:47:51 +00:00
|
|
|
'error',
|
2019-12-02 03:32:03 +00:00
|
|
|
'msg':
|
2020-06-19 01:47:51 +00:00
|
|
|
'There was something wrong, please contact administrator'
|
2019-12-02 03:32:03 +00:00
|
|
|
}
|
|
|
|
else:
|
2020-01-26 23:05:28 +00:00
|
|
|
return {'status': 'error', 'msg': 'This domain does not exist'}
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
def get_domain_dnssec(self, domain_name):
|
|
|
|
"""
|
|
|
|
Get domain DNSSEC information
|
|
|
|
"""
|
|
|
|
domain = Domain.query.filter(Domain.name == domain_name).first()
|
|
|
|
if domain:
|
2020-06-19 01:47:51 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY}
|
2019-12-02 03:32:03 +00:00
|
|
|
try:
|
|
|
|
jdata = utils.fetch_json(
|
|
|
|
urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}/cryptokeys'.format(
|
|
|
|
domain.name)),
|
2019-12-02 03:32:03 +00:00
|
|
|
headers=headers,
|
|
|
|
timeout=int(Setting().get('pdns_api_timeout')),
|
2020-01-25 18:44:11 +00:00
|
|
|
method='GET',
|
|
|
|
verify=Setting().get('verify_ssl_connections'))
|
2019-12-02 03:32:03 +00:00
|
|
|
if 'error' in jdata:
|
|
|
|
return {
|
|
|
|
'status': 'error',
|
|
|
|
'msg': 'DNSSEC is not enabled for this domain'
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return {'status': 'ok', 'dnssec': jdata}
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
'Cannot get domain dnssec. DETAIL: {0}'.format(e))
|
|
|
|
return {
|
|
|
|
'status':
|
2020-06-19 01:47:51 +00:00
|
|
|
'error',
|
2019-12-02 03:32:03 +00:00
|
|
|
'msg':
|
2020-06-19 01:47:51 +00:00
|
|
|
'There was something wrong, please contact administrator'
|
2019-12-02 03:32:03 +00:00
|
|
|
}
|
|
|
|
else:
|
2020-01-26 23:05:28 +00:00
|
|
|
return {'status': 'error', 'msg': 'This domain does not exist'}
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
def enable_domain_dnssec(self, domain_name):
|
|
|
|
"""
|
|
|
|
Enable domain DNSSEC
|
|
|
|
"""
|
|
|
|
domain = Domain.query.filter(Domain.name == domain_name).first()
|
|
|
|
if domain:
|
2022-03-30 21:31:16 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY, 'Content-Type': 'application/json'}
|
2019-12-02 03:32:03 +00:00
|
|
|
try:
|
|
|
|
# Enable API-RECTIFY for domain, BEFORE activating DNSSEC
|
|
|
|
post_data = {"api_rectify": True}
|
|
|
|
jdata = utils.fetch_json(
|
|
|
|
urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}'.format(domain.name)),
|
2019-12-02 03:32:03 +00:00
|
|
|
headers=headers,
|
|
|
|
timeout=int(Setting().get('pdns_api_timeout')),
|
|
|
|
method='PUT',
|
2020-01-25 18:44:11 +00:00
|
|
|
verify=Setting().get('verify_ssl_connections'),
|
2019-12-02 03:32:03 +00:00
|
|
|
data=post_data)
|
|
|
|
if 'error' in jdata:
|
|
|
|
return {
|
|
|
|
'status': 'error',
|
|
|
|
'msg':
|
2020-06-19 01:47:51 +00:00
|
|
|
'API-RECTIFY could not be enabled for this domain',
|
2019-12-02 03:32:03 +00:00
|
|
|
'jdata': jdata
|
|
|
|
}
|
|
|
|
|
|
|
|
# Activate DNSSEC
|
|
|
|
post_data = {"keytype": "ksk", "active": True}
|
|
|
|
jdata = utils.fetch_json(
|
|
|
|
urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}/cryptokeys'.format(
|
|
|
|
domain.name)),
|
2019-12-02 03:32:03 +00:00
|
|
|
headers=headers,
|
|
|
|
timeout=int(Setting().get('pdns_api_timeout')),
|
|
|
|
method='POST',
|
2020-01-25 18:44:11 +00:00
|
|
|
verify=Setting().get('verify_ssl_connections'),
|
2019-12-02 03:32:03 +00:00
|
|
|
data=post_data)
|
|
|
|
if 'error' in jdata:
|
|
|
|
return {
|
|
|
|
'status':
|
2020-06-19 01:47:51 +00:00
|
|
|
'error',
|
2019-12-02 03:32:03 +00:00
|
|
|
'msg':
|
2020-06-19 01:47:51 +00:00
|
|
|
'Cannot enable DNSSEC for this domain. Error: {0}'.
|
|
|
|
format(jdata['error']),
|
2019-12-02 03:32:03 +00:00
|
|
|
'jdata':
|
2020-06-19 01:47:51 +00:00
|
|
|
jdata
|
2019-12-02 03:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {'status': 'ok'}
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
'Cannot enable dns sec. DETAIL: {}'.format(e))
|
|
|
|
current_app.logger.debug(traceback.format_exc())
|
|
|
|
return {
|
|
|
|
'status':
|
2020-06-19 01:47:51 +00:00
|
|
|
'error',
|
2019-12-02 03:32:03 +00:00
|
|
|
'msg':
|
2020-06-19 01:47:51 +00:00
|
|
|
'There was something wrong, please contact administrator'
|
2019-12-02 03:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else:
|
|
|
|
return {'status': 'error', 'msg': 'This domain does not exist'}
|
|
|
|
|
|
|
|
def delete_dnssec_key(self, domain_name, key_id):
|
|
|
|
"""
|
|
|
|
Remove keys DNSSEC
|
|
|
|
"""
|
|
|
|
domain = Domain.query.filter(Domain.name == domain_name).first()
|
|
|
|
if domain:
|
2022-03-30 21:31:16 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY, 'Content-Type': 'application/json'}
|
2019-12-02 03:32:03 +00:00
|
|
|
try:
|
|
|
|
# Deactivate DNSSEC
|
|
|
|
jdata = utils.fetch_json(
|
|
|
|
urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}/cryptokeys/{1}'.format(
|
|
|
|
domain.name, key_id)),
|
2019-12-02 03:32:03 +00:00
|
|
|
headers=headers,
|
|
|
|
timeout=int(Setting().get('pdns_api_timeout')),
|
2020-01-25 18:44:11 +00:00
|
|
|
method='DELETE',
|
|
|
|
verify=Setting().get('verify_ssl_connections'))
|
2019-12-02 03:32:03 +00:00
|
|
|
if jdata != True:
|
|
|
|
return {
|
|
|
|
'status':
|
2020-06-19 01:47:51 +00:00
|
|
|
'error',
|
2019-12-02 03:32:03 +00:00
|
|
|
'msg':
|
2020-06-19 01:47:51 +00:00
|
|
|
'Cannot disable DNSSEC for this domain. Error: {0}'.
|
|
|
|
format(jdata['error']),
|
2019-12-02 03:32:03 +00:00
|
|
|
'jdata':
|
2020-06-19 01:47:51 +00:00
|
|
|
jdata
|
2019-12-02 03:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Disable API-RECTIFY for domain, AFTER deactivating DNSSEC
|
|
|
|
post_data = {"api_rectify": False}
|
|
|
|
jdata = utils.fetch_json(
|
|
|
|
urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}'.format(domain.name)),
|
2019-12-02 03:32:03 +00:00
|
|
|
headers=headers,
|
|
|
|
timeout=int(Setting().get('pdns_api_timeout')),
|
|
|
|
method='PUT',
|
2020-01-25 18:44:11 +00:00
|
|
|
verify=Setting().get('verify_ssl_connections'),
|
2019-12-02 03:32:03 +00:00
|
|
|
data=post_data)
|
|
|
|
if 'error' in jdata:
|
|
|
|
return {
|
|
|
|
'status': 'error',
|
|
|
|
'msg':
|
2020-06-19 01:47:51 +00:00
|
|
|
'API-RECTIFY could not be disabled for this domain',
|
2019-12-02 03:32:03 +00:00
|
|
|
'jdata': jdata
|
|
|
|
}
|
|
|
|
|
|
|
|
return {'status': 'ok'}
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
'Cannot delete dnssec key. DETAIL: {0}'.format(e))
|
|
|
|
current_app.logger.debug(traceback.format_exc())
|
|
|
|
return {
|
|
|
|
'status': 'error',
|
|
|
|
'msg':
|
2020-06-19 01:47:51 +00:00
|
|
|
'There was something wrong, please contact administrator',
|
2019-12-02 03:32:03 +00:00
|
|
|
'domain': domain.name,
|
|
|
|
'id': key_id
|
|
|
|
}
|
|
|
|
|
|
|
|
else:
|
2020-01-26 23:05:28 +00:00
|
|
|
return {'status': 'error', 'msg': 'This domain does not exist'}
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2022-02-21 16:31:37 +00:00
|
|
|
def assoc_account(self, account_id, update=True):
|
2019-12-02 03:32:03 +00:00
|
|
|
"""
|
|
|
|
Associate domain with a domain, specified by account id
|
|
|
|
"""
|
|
|
|
domain_name = self.name
|
|
|
|
|
|
|
|
# Sanity check - domain name
|
|
|
|
if domain_name == "":
|
|
|
|
return {'status': False, 'msg': 'No domain name specified'}
|
|
|
|
|
|
|
|
# read domain and check that it exists
|
|
|
|
domain = Domain.query.filter(Domain.name == domain_name).first()
|
|
|
|
if not domain:
|
|
|
|
return {'status': False, 'msg': 'Domain does not exist'}
|
|
|
|
|
2022-03-30 21:31:16 +00:00
|
|
|
headers = {'X-API-Key': self.PDNS_API_KEY, 'Content-Type': 'application/json'}
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2022-06-28 09:16:45 +00:00
|
|
|
account_name_old = Account().get_name_by_id(domain.account_id)
|
2019-12-02 03:32:03 +00:00
|
|
|
account_name = Account().get_name_by_id(account_id)
|
|
|
|
|
|
|
|
post_data = {"account": account_name}
|
|
|
|
|
|
|
|
try:
|
|
|
|
jdata = utils.fetch_json(urljoin(
|
|
|
|
self.PDNS_STATS_URL, self.API_EXTENDED_URL +
|
2020-06-19 01:47:51 +00:00
|
|
|
'/servers/localhost/zones/{0}'.format(domain_name)),
|
|
|
|
headers=headers,
|
|
|
|
timeout=int(
|
|
|
|
Setting().get('pdns_api_timeout')),
|
|
|
|
method='PUT',
|
|
|
|
verify=Setting().get('verify_ssl_connections'),
|
|
|
|
data=post_data)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
if 'error' in jdata.keys():
|
|
|
|
current_app.logger.error(jdata['error'])
|
|
|
|
return {'status': 'error', 'msg': jdata['error']}
|
|
|
|
else:
|
2022-02-21 16:31:37 +00:00
|
|
|
if update:
|
|
|
|
self.update()
|
2019-12-02 03:32:03 +00:00
|
|
|
msg_str = 'Account changed for domain {0} successfully'
|
|
|
|
current_app.logger.info(msg_str.format(domain_name))
|
2022-06-28 09:16:45 +00:00
|
|
|
history = History(msg='Update domain {0} associate account {1}'.format(domain.name, 'none' if account_name == '' else account_name),
|
|
|
|
detail = json.dumps({
|
|
|
|
'assoc_account': 'None' if account_name == '' else account_name,
|
|
|
|
'dissoc_account': 'None' if account_name_old == '' else account_name_old
|
|
|
|
}),
|
|
|
|
created_by=current_user.username)
|
|
|
|
history.add()
|
2019-12-02 03:32:03 +00:00
|
|
|
return {'status': 'ok', 'msg': 'account changed successfully'}
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.debug(e)
|
|
|
|
current_app.logger.debug(traceback.format_exc())
|
|
|
|
msg_str = 'Cannot change account for domain {0}'
|
|
|
|
current_app.logger.error(msg_str.format(domain_name))
|
|
|
|
return {
|
|
|
|
'status': 'error',
|
|
|
|
'msg': 'Cannot change account for this domain.'
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_account(self):
|
|
|
|
"""
|
|
|
|
Get current account associated with this domain
|
|
|
|
"""
|
|
|
|
domain = Domain.query.filter(Domain.name == self.name).first()
|
|
|
|
|
|
|
|
return domain.account
|
|
|
|
|
|
|
|
def is_valid_access(self, user_id):
|
|
|
|
"""
|
|
|
|
Check if the user is allowed to access this
|
|
|
|
domain name
|
|
|
|
"""
|
|
|
|
return db.session.query(Domain) \
|
|
|
|
.outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \
|
|
|
|
.outerjoin(Account, Domain.account_id == Account.id) \
|
|
|
|
.outerjoin(AccountUser, Account.id == AccountUser.account_id) \
|
|
|
|
.filter(
|
2020-06-19 01:47:51 +00:00
|
|
|
db.or_(
|
|
|
|
DomainUser.user_id == user_id,
|
|
|
|
AccountUser.user_id == user_id
|
|
|
|
)).filter(Domain.id == self.id).first()
|
2022-06-17 15:50:51 +00:00
|
|
|
|
|
|
|
# Return None if this domain does not exist as record,
|
|
|
|
# Return the parent domain that hold the record if exist
|
|
|
|
def is_overriding(self, domain_name):
|
|
|
|
upper_domain_name = '.'.join(domain_name.split('.')[1:])
|
|
|
|
while upper_domain_name != '':
|
|
|
|
if self.get_id_by_name(upper_domain_name.rstrip('.')) != None:
|
|
|
|
upper_domain = self.get_domain_info(upper_domain_name)
|
|
|
|
if 'rrsets' in upper_domain:
|
|
|
|
for r in upper_domain['rrsets']:
|
|
|
|
if domain_name.rstrip('.') in r['name'].rstrip('.'):
|
|
|
|
current_app.logger.error('Domain already exists as a record: {} under domain: {}'.format(r['name'].rstrip('.'), upper_domain_name))
|
|
|
|
return upper_domain_name
|
|
|
|
upper_domain_name = '.'.join(upper_domain_name.split('.')[1:])
|
2022-12-10 02:37:06 +00:00
|
|
|
return None
|