def api_login_delete_zone(domain_name): 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_full_uri = api_uri_with_prefix + '/servers/localhost/zones' api_full_uri += '/' + domain_name headers = {} headers['X-API-Key'] = pdns_api_key domain = Domain.query.filter(Domain.name == domain_name) if not domain: abort(404) if g.user.role.name not in ['Administrator', 'Operator']: user_domains_obj_list = g.user.get_domains() user_domains_list = [item.name for item in user_domains_obj_list] if domain_name not in user_domains_list: raise DomainAccessForbidden() msg_str = "Sending request to powerdns API {0}" logging.debug(msg_str.format(domain_name)) try: resp = utils.fetch_remote( urljoin(pdns_api_url, api_full_uri), method='DELETE', headers=headers, accept='application/json; q=1' ) if resp.status_code == 204: logging.debug("Request to powerdns API successful") history = History( msg='Delete domain {0}'.format(domain_name), detail='', created_by=g.user.username ) history.add() domain = Domain() domain.update() except Exception as e: logging.error('Error: {0}'.format(e)) abort(500) return resp.content, resp.status_code, resp.headers.items()
def api_login_create_zone(): 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_full_uri = api_uri_with_prefix + '/servers/localhost/zones' headers = {} headers['X-API-Key'] = pdns_api_key msg_str = "Sending request to powerdns API {0}" msg = msg_str.format(request.get_json(force=True)) logging.debug(msg) resp = utils.fetch_remote( urljoin(pdns_api_url, api_full_uri), method='POST', data=request.get_json(force=True), headers=headers, accept='application/json; q=1' ) if resp.status_code == 201: logging.debug("Request to powerdns API successful") data = request.get_json(force=True) history = History( msg='Add domain {0}'.format(data['name'].rstrip('.')), detail=json.dumps(data), created_by=g.user.username ) history.add() if g.user.role.name not in ['Administrator', 'Operator']: logging.debug("User is ordinary user, assigning created domain") domain = Domain(name=data['name'].rstrip('.')) domain.update() domain.grant_privileges([g.user.username]) domain = Domain() domain.update() return resp.content, resp.status_code, resp.headers.items()
# import dns.name PRETTY_IPV6_PTR = app.config['PRETTY_IPV6_PTR'] else: PRETTY_IPV6_PTR = False PDNS_STATS_URL = app.config['PDNS_STATS_URL'] PDNS_API_KEY = app.config['PDNS_API_KEY'] PDNS_VERSION = app.config['PDNS_VERSION'] NEW_SCHEMA = bool(StrictVersion(PDNS_VERSION) >= StrictVersion('4.0.0')) from app.lib import utils from app.lib.log import logger LOGGING = logger('MODEL', app.config['LOG_LEVEL'], app.config['LOG_FILE']).config() API_EXTENDED_URL = utils.pdns_api_extended_uri(PDNS_VERSION) class PdnsParser(RawConfigParser): """A class to inherit from RawConfigParser. Created to have safe methods to get values So that the config file can not have the value and there will be a default """ def safe_get(self, section, option, default=None): """Safe Get Method.""" retval = None if self.has_option(section, option): retval = self.get(section, option) else: retval = default