2016-06-08 10:53:58 -05:00
|
|
|
#
|
|
|
|
# Copyright (C) 2016 FreeIPA Contributors see COPYING for license
|
|
|
|
#
|
|
|
|
|
2019-09-24 07:34:52 -05:00
|
|
|
from __future__ import absolute_import
|
2016-06-08 10:53:58 -05:00
|
|
|
|
2017-05-24 09:35:07 -05:00
|
|
|
import logging
|
|
|
|
|
2016-06-10 10:03:25 -05:00
|
|
|
import six
|
|
|
|
|
2018-07-06 14:47:32 -05:00
|
|
|
from collections import defaultdict, OrderedDict
|
2016-06-08 10:53:58 -05:00
|
|
|
from dns import (
|
2016-10-11 05:09:17 -05:00
|
|
|
rdata,
|
2016-06-08 10:53:58 -05:00
|
|
|
rdataclass,
|
|
|
|
rdatatype,
|
|
|
|
zone,
|
|
|
|
)
|
|
|
|
|
2016-06-20 11:23:51 -05:00
|
|
|
from time import sleep, time
|
|
|
|
|
2016-06-08 10:53:58 -05:00
|
|
|
from ipalib import errors
|
2022-07-11 13:20:32 -05:00
|
|
|
from ipalib.constants import IPA_CA_RECORD
|
2016-06-08 10:53:58 -05:00
|
|
|
from ipalib.dns import record_name_format
|
2020-10-06 06:12:41 -05:00
|
|
|
from ipapython.dnsutil import DNSName
|
|
|
|
from ipaserver.install import installutils
|
2016-06-08 10:53:58 -05:00
|
|
|
|
2016-06-10 10:03:25 -05:00
|
|
|
if six.PY3:
|
|
|
|
unicode=str
|
|
|
|
|
2017-05-24 09:35:07 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-06-10 10:03:25 -05:00
|
|
|
|
2016-06-08 10:53:58 -05:00
|
|
|
IPA_DEFAULT_MASTER_SRV_REC = (
|
|
|
|
# srv record name, port
|
2019-09-23 16:30:22 -05:00
|
|
|
(DNSName('_ldap._tcp'), 389),
|
2021-08-25 10:13:55 -05:00
|
|
|
# Kerberos records are provided for MIT KRB5 < 1.15 and AD
|
2019-09-23 16:30:22 -05:00
|
|
|
(DNSName('_kerberos._tcp'), 88),
|
|
|
|
(DNSName('_kerberos._udp'), 88),
|
|
|
|
(DNSName('_kerberos-master._tcp'), 88),
|
|
|
|
(DNSName('_kerberos-master._udp'), 88),
|
|
|
|
(DNSName('_kpasswd._tcp'), 464),
|
|
|
|
(DNSName('_kpasswd._udp'), 464),
|
2016-06-08 10:53:58 -05:00
|
|
|
)
|
|
|
|
|
2021-08-25 10:13:55 -05:00
|
|
|
IPA_DEFAULT_MASTER_URI_REC = (
|
|
|
|
# URI record name, URI template
|
|
|
|
|
|
|
|
# MIT KRB5 1.15+ prefers URI records for service discovery
|
|
|
|
# scheme: always krb5srv
|
|
|
|
# flags: empty or 'm' for primary server
|
|
|
|
# transport: 'tcp', 'udp', or 'kkdcp')
|
|
|
|
# residual: 'hostname', 'hostname:port', or 'https://' URL
|
|
|
|
(DNSName('_kerberos'), "krb5srv:m:tcp:{hostname}"),
|
|
|
|
(DNSName('_kerberos'), "krb5srv:m:udp:{hostname}"),
|
|
|
|
(DNSName('_kpasswd'), "krb5srv:m:tcp:{hostname}"),
|
|
|
|
(DNSName('_kpasswd'), "krb5srv:m:udp:{hostname}"),
|
|
|
|
)
|
|
|
|
|
2016-06-08 10:53:58 -05:00
|
|
|
IPA_DEFAULT_ADTRUST_SRV_REC = (
|
|
|
|
# srv record name, port
|
2019-09-23 16:30:22 -05:00
|
|
|
(DNSName('_ldap._tcp.Default-First-Site-Name._sites.dc._msdcs'), 389),
|
|
|
|
(DNSName('_ldap._tcp.dc._msdcs'), 389),
|
|
|
|
(DNSName('_kerberos._tcp.Default-First-Site-Name._sites.dc._msdcs'), 88),
|
|
|
|
(DNSName('_kerberos._udp.Default-First-Site-Name._sites.dc._msdcs'), 88),
|
|
|
|
(DNSName('_kerberos._tcp.dc._msdcs'), 88),
|
|
|
|
(DNSName('_kerberos._udp.dc._msdcs'), 88),
|
2016-06-08 10:53:58 -05:00
|
|
|
)
|
|
|
|
|
2016-06-16 14:16:39 -05:00
|
|
|
IPA_DEFAULT_NTP_SRV_REC = (
|
|
|
|
# srv record name, port
|
|
|
|
(DNSName("_ntp._udp"), 123),
|
|
|
|
)
|
|
|
|
|
2022-10-04 08:03:28 -05:00
|
|
|
IPA_DEFAULT_KRB_TXT_REC = (
|
|
|
|
(DNSName('_kerberos'), "\"{realm}\""),
|
|
|
|
)
|
|
|
|
|
2020-10-06 06:12:41 -05:00
|
|
|
CA_RECORDS_DNS_TIMEOUT = 15 # timeout in seconds
|
2017-07-28 08:43:16 -05:00
|
|
|
|
2016-06-08 10:53:58 -05:00
|
|
|
|
|
|
|
class IPADomainIsNotManagedByIPAError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-09-26 04:59:50 -05:00
|
|
|
class IPASystemRecords:
|
2016-06-08 10:53:58 -05:00
|
|
|
|
|
|
|
# fixme do it configurable
|
|
|
|
PRIORITY_HIGH = 0
|
|
|
|
PRIORITY_LOW = 50
|
2021-08-25 10:13:55 -05:00
|
|
|
# FIXME: use TTL from config
|
|
|
|
TTL = 3600
|
2016-06-08 10:53:58 -05:00
|
|
|
|
2018-07-05 17:04:39 -05:00
|
|
|
def __init__(self, api_instance, all_servers=False):
|
2016-06-08 10:53:58 -05:00
|
|
|
self.api_instance = api_instance
|
|
|
|
self.domain_abs = DNSName(self.api_instance.env.domain).make_absolute()
|
2018-07-06 14:47:32 -05:00
|
|
|
self.servers_data = OrderedDict()
|
2018-07-05 17:04:39 -05:00
|
|
|
self.__init_data(all_servers=all_servers)
|
2016-06-08 10:53:58 -05:00
|
|
|
|
|
|
|
def reload_data(self):
|
|
|
|
"""
|
|
|
|
After any change made to IPA servers, this method must be called to
|
|
|
|
update data in the object, otherwise invalid records may be
|
|
|
|
created/updated
|
|
|
|
"""
|
|
|
|
self.__init_data()
|
|
|
|
|
2016-06-22 06:12:52 -05:00
|
|
|
def __get_server_attrs(self, server_result):
|
2019-09-23 16:30:22 -05:00
|
|
|
weight = int(server_result.get('ipaserviceweight', ['100'])[0])
|
2016-06-08 10:53:58 -05:00
|
|
|
location = server_result.get('ipalocation_location', [None])[0]
|
|
|
|
roles = set(server_result.get('enabled_role_servrole', ()))
|
|
|
|
|
|
|
|
return weight, location, roles
|
|
|
|
|
2016-06-16 09:46:29 -05:00
|
|
|
def __get_location_suffix(self, location):
|
|
|
|
return location + DNSName('_locations') + self.domain_abs
|
|
|
|
|
2018-07-05 17:04:39 -05:00
|
|
|
def __init_data(self, all_servers=False):
|
2018-07-06 14:47:32 -05:00
|
|
|
self.servers_data.clear()
|
2016-06-08 10:53:58 -05:00
|
|
|
|
2018-07-05 17:04:39 -05:00
|
|
|
kwargs = dict(no_members=False)
|
|
|
|
if not all_servers:
|
|
|
|
# only active, fully installed masters]
|
2019-09-23 16:30:22 -05:00
|
|
|
kwargs["servrole"] = "IPA master"
|
2018-07-05 17:04:39 -05:00
|
|
|
servers = self.api_instance.Command.server_find(**kwargs)
|
|
|
|
|
|
|
|
for s in servers['result']:
|
2016-06-08 10:53:58 -05:00
|
|
|
weight, location, roles = self.__get_server_attrs(s)
|
2016-06-22 06:12:52 -05:00
|
|
|
self.servers_data[s['cn'][0]] = {
|
2016-06-08 10:53:58 -05:00
|
|
|
'weight': weight,
|
|
|
|
'location': location,
|
|
|
|
'roles': roles,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __add_srv_records(
|
|
|
|
self, zone_obj, hostname, rname_port_map,
|
|
|
|
weight=100, priority=0, location=None
|
|
|
|
):
|
|
|
|
assert isinstance(hostname, DNSName)
|
|
|
|
assert isinstance(priority, int)
|
|
|
|
assert isinstance(weight, int)
|
|
|
|
|
|
|
|
if location:
|
2016-06-16 09:46:29 -05:00
|
|
|
suffix = self.__get_location_suffix(location)
|
2016-06-08 10:53:58 -05:00
|
|
|
else:
|
|
|
|
suffix = self.domain_abs
|
|
|
|
|
|
|
|
for name, port in rname_port_map:
|
2016-10-11 05:09:17 -05:00
|
|
|
rd = rdata.from_text(
|
2016-06-08 10:53:58 -05:00
|
|
|
rdataclass.IN, rdatatype.SRV,
|
2016-10-11 05:09:17 -05:00
|
|
|
'{0} {1} {2} {3}'.format(
|
|
|
|
priority, weight, port, hostname.make_absolute()
|
|
|
|
)
|
2016-06-08 10:53:58 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
r_name = name.derelativize(suffix)
|
|
|
|
|
|
|
|
rdataset = zone_obj.get_rdataset(
|
|
|
|
r_name, rdatatype.SRV, create=True)
|
2021-08-25 10:13:55 -05:00
|
|
|
rdataset.add(rd, ttl=self.TTL)
|
|
|
|
|
|
|
|
def __add_uri_records(
|
|
|
|
self, zone_obj, hostname, rname_uri_map,
|
|
|
|
weight=100, priority=0, location=None
|
|
|
|
):
|
|
|
|
assert isinstance(hostname, DNSName)
|
|
|
|
assert isinstance(priority, int)
|
|
|
|
assert isinstance(weight, int)
|
|
|
|
|
|
|
|
if location:
|
|
|
|
suffix = self.__get_location_suffix(location)
|
|
|
|
else:
|
|
|
|
suffix = self.domain_abs
|
|
|
|
|
|
|
|
for name, uri_template in rname_uri_map:
|
|
|
|
uri = uri_template.format(hostname=hostname.make_absolute())
|
|
|
|
rd = rdata.from_text(
|
|
|
|
rdataclass.IN, rdatatype.URI,
|
|
|
|
'{0} {1} {2}'.format(
|
|
|
|
priority, weight, uri
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
r_name = name.derelativize(suffix)
|
|
|
|
|
|
|
|
rdataset = zone_obj.get_rdataset(
|
|
|
|
r_name, rdatatype.URI, create=True)
|
|
|
|
rdataset.add(rd, ttl=self.TTL)
|
2016-06-08 10:53:58 -05:00
|
|
|
|
|
|
|
def __add_ca_records_from_hostname(self, zone_obj, hostname):
|
|
|
|
assert isinstance(hostname, DNSName) and hostname.is_absolute()
|
2022-07-11 13:20:32 -05:00
|
|
|
r_name = DNSName(IPA_CA_RECORD) + self.domain_abs
|
2020-10-06 06:12:41 -05:00
|
|
|
rrsets = None
|
2017-07-28 08:43:16 -05:00
|
|
|
end_time = time() + CA_RECORDS_DNS_TIMEOUT
|
2020-10-06 06:12:41 -05:00
|
|
|
while True:
|
2016-06-20 11:23:51 -05:00
|
|
|
try:
|
2020-10-06 06:12:41 -05:00
|
|
|
# function logs errors
|
|
|
|
rrsets = installutils.resolve_rrsets_nss(hostname)
|
|
|
|
except OSError:
|
|
|
|
# also retry on EAI_AGAIN, EAI_FAIL
|
2016-06-20 11:23:51 -05:00
|
|
|
pass
|
|
|
|
if rrsets:
|
|
|
|
break
|
2020-10-06 06:12:41 -05:00
|
|
|
if time() >= end_time:
|
|
|
|
break
|
|
|
|
sleep(3)
|
2016-06-20 11:23:51 -05:00
|
|
|
|
|
|
|
if not rrsets:
|
2017-05-24 09:35:07 -05:00
|
|
|
logger.error('unable to resolve host name %s to IP address, '
|
|
|
|
'ipa-ca DNS record will be incomplete', hostname)
|
2016-06-20 11:23:51 -05:00
|
|
|
return
|
|
|
|
|
2016-06-08 10:53:58 -05:00
|
|
|
for rrset in rrsets:
|
|
|
|
for rd in rrset:
|
2022-07-11 13:20:32 -05:00
|
|
|
logger.debug("Adding CA IP %s for %s", rd.to_text(), hostname)
|
2016-06-08 10:53:58 -05:00
|
|
|
rdataset = zone_obj.get_rdataset(
|
|
|
|
r_name, rd.rdtype, create=True)
|
2021-08-25 10:13:55 -05:00
|
|
|
rdataset.add(rd, ttl=self.TTL)
|
2016-06-08 10:53:58 -05:00
|
|
|
|
2022-10-04 08:03:28 -05:00
|
|
|
def __add_kerberos_txt_rec(self, zone_obj, location=None):
|
2016-06-08 10:53:58 -05:00
|
|
|
# FIXME: with external DNS, this should generate records for all
|
|
|
|
# realmdomains
|
2022-10-04 08:03:28 -05:00
|
|
|
if location:
|
|
|
|
suffix = self.__get_location_suffix(location)
|
|
|
|
else:
|
|
|
|
suffix = self.domain_abs
|
|
|
|
|
|
|
|
r_name = DNSName('_kerberos') + suffix
|
2016-10-11 05:09:17 -05:00
|
|
|
rd = rdata.from_text(rdataclass.IN, rdatatype.TXT,
|
|
|
|
self.api_instance.env.realm)
|
2016-06-08 10:53:58 -05:00
|
|
|
rdataset = zone_obj.get_rdataset(
|
|
|
|
r_name, rdatatype.TXT, create=True
|
|
|
|
)
|
2021-08-25 10:13:55 -05:00
|
|
|
rdataset.add(rd, ttl=self.TTL)
|
2016-06-08 10:53:58 -05:00
|
|
|
|
|
|
|
def _add_base_dns_records_for_server(
|
|
|
|
self, zone_obj, hostname, roles=None, include_master_role=True,
|
|
|
|
include_kerberos_realm=True,
|
|
|
|
):
|
|
|
|
server = self.servers_data[hostname]
|
|
|
|
if roles:
|
|
|
|
eff_roles = server['roles'] & set(roles)
|
|
|
|
else:
|
|
|
|
eff_roles = server['roles']
|
|
|
|
hostname_abs = DNSName(hostname).make_absolute()
|
|
|
|
|
|
|
|
if include_kerberos_realm:
|
2022-10-04 08:03:28 -05:00
|
|
|
self.__add_kerberos_txt_rec(zone_obj, location=None)
|
2016-06-08 10:53:58 -05:00
|
|
|
|
|
|
|
# get master records
|
|
|
|
if include_master_role:
|
|
|
|
self.__add_srv_records(
|
|
|
|
zone_obj,
|
|
|
|
hostname_abs,
|
|
|
|
IPA_DEFAULT_MASTER_SRV_REC,
|
|
|
|
weight=server['weight']
|
|
|
|
)
|
2021-08-25 10:13:55 -05:00
|
|
|
self.__add_uri_records(
|
|
|
|
zone_obj,
|
|
|
|
hostname_abs,
|
|
|
|
IPA_DEFAULT_MASTER_URI_REC,
|
|
|
|
weight=server['weight']
|
|
|
|
)
|
2016-06-08 10:53:58 -05:00
|
|
|
|
|
|
|
if 'CA server' in eff_roles:
|
|
|
|
self.__add_ca_records_from_hostname(zone_obj, hostname_abs)
|
|
|
|
|
|
|
|
if 'AD trust controller' in eff_roles:
|
|
|
|
self.__add_srv_records(
|
|
|
|
zone_obj,
|
|
|
|
hostname_abs,
|
|
|
|
IPA_DEFAULT_ADTRUST_SRV_REC,
|
|
|
|
weight=server['weight']
|
|
|
|
)
|
|
|
|
|
2016-06-16 14:16:39 -05:00
|
|
|
if 'NTP server' in eff_roles:
|
|
|
|
self.__add_srv_records(
|
|
|
|
zone_obj,
|
|
|
|
hostname_abs,
|
|
|
|
IPA_DEFAULT_NTP_SRV_REC,
|
|
|
|
weight=server['weight']
|
|
|
|
)
|
|
|
|
|
2016-06-08 10:53:58 -05:00
|
|
|
def _get_location_dns_records_for_server(
|
|
|
|
self, zone_obj, hostname, locations,
|
2022-10-04 08:03:28 -05:00
|
|
|
roles=None, include_master_role=True,
|
|
|
|
include_kerberos_realm=True):
|
2016-06-08 10:53:58 -05:00
|
|
|
server = self.servers_data[hostname]
|
|
|
|
if roles:
|
|
|
|
eff_roles = server['roles'] & roles
|
|
|
|
else:
|
|
|
|
eff_roles = server['roles']
|
|
|
|
hostname_abs = DNSName(hostname).make_absolute()
|
|
|
|
|
|
|
|
# generate locations specific records
|
|
|
|
for location in locations:
|
|
|
|
if location == self.servers_data[hostname]['location']:
|
|
|
|
priority = self.PRIORITY_HIGH
|
|
|
|
else:
|
|
|
|
priority = self.PRIORITY_LOW
|
|
|
|
|
2022-10-04 08:03:28 -05:00
|
|
|
if include_kerberos_realm:
|
|
|
|
self.__add_kerberos_txt_rec(zone_obj, location)
|
|
|
|
|
2016-06-08 10:53:58 -05:00
|
|
|
if include_master_role:
|
|
|
|
self.__add_srv_records(
|
|
|
|
zone_obj,
|
|
|
|
hostname_abs,
|
|
|
|
IPA_DEFAULT_MASTER_SRV_REC,
|
|
|
|
weight=server['weight'],
|
|
|
|
priority=priority,
|
|
|
|
location=location
|
|
|
|
)
|
2021-08-25 10:13:55 -05:00
|
|
|
self.__add_uri_records(
|
|
|
|
zone_obj,
|
|
|
|
hostname_abs,
|
|
|
|
IPA_DEFAULT_MASTER_URI_REC,
|
|
|
|
weight=server['weight'],
|
|
|
|
priority=priority,
|
|
|
|
location=location
|
|
|
|
)
|
2016-06-08 10:53:58 -05:00
|
|
|
|
|
|
|
if 'AD trust controller' in eff_roles:
|
|
|
|
self.__add_srv_records(
|
|
|
|
zone_obj,
|
|
|
|
hostname_abs,
|
|
|
|
IPA_DEFAULT_ADTRUST_SRV_REC,
|
|
|
|
weight=server['weight'],
|
|
|
|
priority=priority,
|
|
|
|
location=location
|
|
|
|
)
|
|
|
|
|
2016-06-16 14:16:39 -05:00
|
|
|
if 'NTP server' in eff_roles:
|
|
|
|
self.__add_srv_records(
|
|
|
|
zone_obj,
|
|
|
|
hostname_abs,
|
|
|
|
IPA_DEFAULT_NTP_SRV_REC,
|
|
|
|
weight=server['weight'],
|
|
|
|
priority=priority,
|
|
|
|
location=location
|
|
|
|
)
|
|
|
|
|
2016-06-08 10:53:58 -05:00
|
|
|
return zone_obj
|
|
|
|
|
|
|
|
def __prepare_records_update_dict(self, node):
|
|
|
|
update_dict = defaultdict(list)
|
|
|
|
for rdataset in node:
|
|
|
|
for rdata in rdataset:
|
|
|
|
option_name = (record_name_format % rdatatype.to_text(
|
|
|
|
rdata.rdtype).lower())
|
2016-06-10 10:03:25 -05:00
|
|
|
update_dict[option_name].append(unicode(rdata.to_text()))
|
2016-06-08 10:53:58 -05:00
|
|
|
return update_dict
|
|
|
|
|
|
|
|
def __update_dns_records(
|
|
|
|
self, record_name, nodes, set_cname_template=True
|
|
|
|
):
|
|
|
|
update_dict = self.__prepare_records_update_dict(nodes)
|
|
|
|
cname_template = {
|
2019-09-23 16:30:22 -05:00
|
|
|
'addattr': ['objectclass=idnsTemplateObject'],
|
2016-06-08 10:53:58 -05:00
|
|
|
'setattr': [
|
2019-09-23 16:30:22 -05:00
|
|
|
r'idnsTemplateAttribute;cnamerecord=%s'
|
|
|
|
r'.\{substitutionvariable_ipalocation\}._locations' %
|
2016-06-08 10:53:58 -05:00
|
|
|
record_name.relativize(self.domain_abs)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
if set_cname_template:
|
|
|
|
# only srv records should have configured cname templates
|
|
|
|
update_dict.update(cname_template)
|
|
|
|
self.api_instance.Command.dnsrecord_mod(
|
|
|
|
self.domain_abs, record_name,
|
|
|
|
**update_dict
|
|
|
|
)
|
|
|
|
except errors.NotFound:
|
|
|
|
# because internal API magic, addattr and setattr doesn't work with
|
|
|
|
# dnsrecord-add well, use dnsrecord-mod instead later
|
|
|
|
update_dict.pop('addattr', None)
|
|
|
|
update_dict.pop('setattr', None)
|
|
|
|
|
|
|
|
self.api_instance.Command.dnsrecord_add(
|
|
|
|
self.domain_abs, record_name, **update_dict)
|
|
|
|
|
|
|
|
if set_cname_template:
|
|
|
|
try:
|
|
|
|
self.api_instance.Command.dnsrecord_mod(
|
|
|
|
self.domain_abs,
|
|
|
|
record_name, **cname_template)
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_base_records(
|
|
|
|
self, servers=None, roles=None, include_master_role=True,
|
|
|
|
include_kerberos_realm=True
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Generate IPA service records for specific servers and roles
|
|
|
|
:param servers: list of server which will be used in records,
|
|
|
|
if None all IPA servers will be used
|
|
|
|
:param roles: roles for which DNS records will be generated,
|
|
|
|
if None all roles will be used
|
|
|
|
:param include_master_role: generate records required by IPA master
|
|
|
|
role
|
|
|
|
:return: dns.zone.Zone object that contains base DNS records
|
|
|
|
"""
|
|
|
|
|
|
|
|
zone_obj = zone.Zone(self.domain_abs, relativize=False)
|
|
|
|
if servers is None:
|
2018-07-06 14:47:32 -05:00
|
|
|
servers = list(self.servers_data)
|
2016-06-08 10:53:58 -05:00
|
|
|
|
|
|
|
for server in servers:
|
|
|
|
self._add_base_dns_records_for_server(zone_obj, server,
|
|
|
|
roles=roles, include_master_role=include_master_role,
|
|
|
|
include_kerberos_realm=include_kerberos_realm
|
|
|
|
)
|
|
|
|
return zone_obj
|
|
|
|
|
|
|
|
def get_locations_records(
|
2022-10-04 08:03:28 -05:00
|
|
|
self, servers=None, roles=None, include_master_role=True,
|
|
|
|
include_kerberos_realm=True
|
|
|
|
):
|
2016-06-08 10:53:58 -05:00
|
|
|
"""
|
|
|
|
Generate IPA location records for specific servers and roles.
|
|
|
|
:param servers: list of server which will be used in records,
|
|
|
|
if None all IPA servers will be used
|
|
|
|
:param roles: roles for which DNS records will be generated,
|
|
|
|
if None all roles will be used
|
|
|
|
:param include_master_role: generate records required by IPA master
|
|
|
|
role
|
|
|
|
:return: dns.zone.Zone object that contains location DNS records
|
|
|
|
"""
|
|
|
|
zone_obj = zone.Zone(self.domain_abs, relativize=False)
|
|
|
|
if servers is None:
|
2018-07-06 14:47:32 -05:00
|
|
|
servers = list(self.servers_data)
|
2016-06-08 10:53:58 -05:00
|
|
|
|
2016-06-21 11:04:13 -05:00
|
|
|
locations_result = self.api_instance.Command.location_find()['result']
|
|
|
|
locations = [l['idnsname'][0] for l in locations_result]
|
|
|
|
|
2016-06-08 10:53:58 -05:00
|
|
|
for server in servers:
|
|
|
|
self._get_location_dns_records_for_server(
|
|
|
|
zone_obj, server,
|
2016-06-21 11:04:13 -05:00
|
|
|
locations, roles=roles,
|
2022-10-04 08:03:28 -05:00
|
|
|
include_master_role=include_master_role,
|
|
|
|
include_kerberos_realm=include_kerberos_realm)
|
2016-06-08 10:53:58 -05:00
|
|
|
return zone_obj
|
|
|
|
|
|
|
|
def update_base_records(self):
|
|
|
|
"""
|
|
|
|
Update base DNS records for IPA services
|
|
|
|
:return: [(record_name, node), ...], [(record_name, node, error), ...]
|
|
|
|
where the first list contains successfully updated records, and the
|
|
|
|
second list contains failed updates with particular exceptions
|
|
|
|
"""
|
|
|
|
fail = []
|
|
|
|
success = []
|
|
|
|
names_requiring_cname_templates = set(
|
|
|
|
rec[0].derelativize(self.domain_abs) for rec in (
|
2022-10-04 08:03:28 -05:00
|
|
|
IPA_DEFAULT_MASTER_SRV_REC
|
|
|
|
+ IPA_DEFAULT_MASTER_URI_REC
|
|
|
|
+ IPA_DEFAULT_KRB_TXT_REC
|
|
|
|
+ IPA_DEFAULT_ADTRUST_SRV_REC
|
|
|
|
+ IPA_DEFAULT_NTP_SRV_REC
|
2016-06-08 10:53:58 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-07-11 13:20:32 -05:00
|
|
|
# Remove the ipa-ca record(s). They will be reconstructed in
|
|
|
|
# get_base_records().
|
|
|
|
r_name = DNSName(IPA_CA_RECORD) + self.domain_abs
|
|
|
|
try:
|
|
|
|
self.api_instance.Command.dnsrecord_del(
|
|
|
|
self.domain_abs, r_name, del_all=True)
|
|
|
|
except errors.NotFound:
|
|
|
|
pass
|
2016-06-08 10:53:58 -05:00
|
|
|
base_zone = self.get_base_records()
|
|
|
|
for record_name, node in base_zone.items():
|
|
|
|
set_cname_template = record_name in names_requiring_cname_templates
|
|
|
|
try:
|
|
|
|
self.__update_dns_records(
|
|
|
|
record_name, node, set_cname_template)
|
|
|
|
except errors.PublicError as e:
|
|
|
|
fail.append((record_name, node, e))
|
|
|
|
else:
|
|
|
|
success.append((record_name, node))
|
|
|
|
return success, fail
|
|
|
|
|
|
|
|
def update_locations_records(self):
|
|
|
|
"""
|
|
|
|
Update locations DNS records for IPA services
|
|
|
|
:return: [(record_name, node), ...], [(record_name, node, error), ...]
|
|
|
|
where the first list contains successfully updated records, and the
|
|
|
|
second list contains failed updates with particular exceptions
|
|
|
|
"""
|
|
|
|
fail = []
|
|
|
|
success = []
|
|
|
|
|
|
|
|
location_zone = self.get_locations_records()
|
|
|
|
for record_name, nodes in location_zone.items():
|
|
|
|
try:
|
|
|
|
self.__update_dns_records(
|
|
|
|
record_name, nodes,
|
|
|
|
set_cname_template=False)
|
|
|
|
except errors.PublicError as e:
|
|
|
|
fail.append((record_name, nodes, e))
|
|
|
|
else:
|
|
|
|
success.append((record_name, nodes))
|
|
|
|
return success, fail
|
|
|
|
|
|
|
|
def update_dns_records(self):
|
|
|
|
"""
|
|
|
|
Update all IPA DNS records
|
|
|
|
:return: (sucessfully_updated_base_records, failed_base_records,
|
|
|
|
sucessfully_updated_locations_records, failed_locations_records)
|
|
|
|
For format see update_base_records or update_locations_method
|
|
|
|
:raise IPADomainIsNotManagedByIPAError: if IPA domain is not managed by
|
|
|
|
IPA DNS
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.api_instance.Command.dnszone_show(self.domain_abs)
|
|
|
|
except errors.NotFound:
|
|
|
|
raise IPADomainIsNotManagedByIPAError()
|
|
|
|
|
|
|
|
return (
|
|
|
|
self.update_base_records(),
|
|
|
|
self.update_locations_records()
|
|
|
|
)
|
2016-06-10 10:03:25 -05:00
|
|
|
|
2016-06-16 09:46:29 -05:00
|
|
|
def remove_location_records(self, location):
|
|
|
|
"""
|
|
|
|
Remove all location records
|
|
|
|
:param location: DNSName object
|
|
|
|
:return: list of successfuly removed record names, list of record
|
|
|
|
names that cannot be removed and returned exception in tuples
|
|
|
|
[rname1, ...], [(rname2, exc), ...]
|
|
|
|
"""
|
|
|
|
success = []
|
|
|
|
failed = []
|
|
|
|
|
|
|
|
location = DNSName(location)
|
|
|
|
loc_records = []
|
|
|
|
for records in (
|
|
|
|
IPA_DEFAULT_MASTER_SRV_REC,
|
|
|
|
IPA_DEFAULT_ADTRUST_SRV_REC,
|
2016-06-16 14:16:39 -05:00
|
|
|
IPA_DEFAULT_NTP_SRV_REC
|
2016-06-16 09:46:29 -05:00
|
|
|
):
|
|
|
|
for name, _port in records:
|
|
|
|
loc_records.append(
|
|
|
|
name + self.__get_location_suffix(location))
|
|
|
|
|
|
|
|
for rname in loc_records:
|
|
|
|
try:
|
|
|
|
self.api_instance.Command.dnsrecord_del(
|
|
|
|
self.domain_abs, rname, del_all=True)
|
|
|
|
except errors.NotFound:
|
|
|
|
pass
|
|
|
|
except errors.PublicError as e:
|
|
|
|
failed.append((rname, e))
|
|
|
|
else:
|
|
|
|
success.append(rname)
|
|
|
|
return success, failed
|
|
|
|
|
|
|
|
|
2016-06-10 10:03:25 -05:00
|
|
|
@classmethod
|
|
|
|
def records_list_from_node(cls, name, node):
|
|
|
|
records = []
|
|
|
|
for rdataset in node:
|
|
|
|
for rd in rdataset:
|
|
|
|
records.append(
|
2019-09-23 16:30:22 -05:00
|
|
|
'{name} {ttl} {rdclass} {rdtype} {rdata}'.format(
|
2016-06-10 10:03:25 -05:00
|
|
|
name=name.ToASCII(),
|
|
|
|
ttl=rdataset.ttl,
|
|
|
|
rdclass=rdataclass.to_text(rd.rdclass),
|
|
|
|
rdtype=rdatatype.to_text(rd.rdtype),
|
|
|
|
rdata=rd.to_text()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return records
|
2016-06-23 07:50:11 -05:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def records_list_from_zone(cls, zone_obj, sort=True):
|
|
|
|
records = []
|
|
|
|
for name, node in zone_obj.items():
|
|
|
|
records.extend(IPASystemRecords.records_list_from_node(name, node))
|
|
|
|
if sort:
|
|
|
|
records.sort()
|
|
|
|
return records
|