86baa9
From 406a70cb9e481fb194bbbc67461f3cfd3c97a108 Mon Sep 17 00:00:00 2001
86baa9
From: Christian Heimes <cheimes@redhat.com>
86baa9
Date: Wed, 27 Mar 2019 11:30:40 +0100
86baa9
Subject: [PATCH] Consolidate container_masters queries
86baa9
86baa9
Replace manual queries of container_masters with new APIs get_masters()
86baa9
and is_service_enabled().
86baa9
86baa9
Signed-off-by: Christian Heimes <cheimes@redhat.com>
86baa9
Reviewed-By: Thomas Woerner <twoerner@redhat.com>
86baa9
---
86baa9
 ipaserver/install/bindinstance.py |  8 ++---
86baa9
 ipaserver/install/ipa_restore.py  | 12 ++------
86baa9
 ipaserver/masters.py              | 51 +++++++++++++++++++++++++++++++
86baa9
 ipaserver/plugins/cert.py         | 15 +++------
86baa9
 ipaserver/plugins/dns.py          | 49 ++++-------------------------
86baa9
 ipaserver/plugins/user.py         | 15 ++-------
86baa9
 ipaserver/plugins/vault.py        | 12 ++------
86baa9
 7 files changed, 70 insertions(+), 92 deletions(-)
86baa9
86baa9
diff --git a/ipaserver/install/bindinstance.py b/ipaserver/install/bindinstance.py
86baa9
index 6156ecdfbd1a62d5b1e0a26db47ef2b9a9448bc1..940667db9c40559d5369188395bb18f2d1eac025 100644
86baa9
--- a/ipaserver/install/bindinstance.py
86baa9
+++ b/ipaserver/install/bindinstance.py
86baa9
@@ -40,6 +40,7 @@ from ipaserver.dns_data_management import (
86baa9
 from ipaserver.install import installutils
86baa9
 from ipaserver.install import service
86baa9
 from ipaserver.install import sysupgrade
86baa9
+from ipaserver.masters import get_masters
86baa9
 from ipapython import ipautil
86baa9
 from ipapython import dnsutil
86baa9
 from ipapython.dnsutil import DNSName
86baa9
@@ -1037,13 +1038,8 @@ class BindInstance(service.Service):
86baa9
             cname_fqdn[cname] = fqdn
86baa9
 
86baa9
         # get FQDNs of all IPA masters
86baa9
-        ldap = self.api.Backend.ldap2
86baa9
         try:
86baa9
-            entries = ldap.get_entries(
86baa9
-                DN(('cn', 'masters'), ('cn', 'ipa'), ('cn', 'etc'),
86baa9
-                   self.api.env.basedn),
86baa9
-                ldap.SCOPE_ONELEVEL, None, ['cn'])
86baa9
-            masters = set(e['cn'][0] for e in entries)
86baa9
+            masters = set(get_masters(self.api.Backend.ldap2))
86baa9
         except errors.NotFound:
86baa9
             masters = set()
86baa9
 
86baa9
diff --git a/ipaserver/install/ipa_restore.py b/ipaserver/install/ipa_restore.py
86baa9
index bd065a038db4d523048f0566f65458402d801e18..c7e996bbe284a7eb2d03fbedb4798d3b15f3dcc0 100644
86baa9
--- a/ipaserver/install/ipa_restore.py
86baa9
+++ b/ipaserver/install/ipa_restore.py
86baa9
@@ -41,6 +41,7 @@ from ipaserver.install.replication import (wait_for_task, ReplicationManager,
86baa9
                                            get_cs_replication_manager)
86baa9
 from ipaserver.install import installutils
86baa9
 from ipaserver.install import dsinstance, httpinstance, cainstance, krbinstance
86baa9
+from ipaserver.masters import get_masters
86baa9
 from ipapython import ipaldap
86baa9
 import ipapython.errors
86baa9
 from ipaplatform.constants import constants
86baa9
@@ -485,16 +486,7 @@ class Restore(admintool.AdminTool):
86baa9
             logger.error('Unable to get connection, skipping disabling '
86baa9
                          'agreements: %s', e)
86baa9
             return
86baa9
-        masters = []
86baa9
-        dn = DN(('cn', 'masters'), ('cn', 'ipa'), ('cn', 'etc'), api.env.basedn)
86baa9
-        try:
86baa9
-            entries = conn.get_entries(dn, conn.SCOPE_ONELEVEL)
86baa9
-        except Exception as e:
86baa9
-            raise admintool.ScriptError(
86baa9
-                "Failed to read master data: %s" % e)
86baa9
-        else:
86baa9
-            masters = [ent.single_value['cn'] for ent in entries]
86baa9
-
86baa9
+        masters = get_masters(conn)
86baa9
         for master in masters:
86baa9
             if master == api.env.host:
86baa9
                 continue
86baa9
diff --git a/ipaserver/masters.py b/ipaserver/masters.py
86baa9
index 171c3abe0d6eea5aa6bcc642815eceae3ae885e7..6fa8f02332ceaa10ec30aa5142912f351fb58936 100644
86baa9
--- a/ipaserver/masters.py
86baa9
+++ b/ipaserver/masters.py
86baa9
@@ -121,3 +121,54 @@ def find_providing_server(svcname, conn=None, preferred_hosts=(), api=api):
86baa9
         return None
86baa9
     else:
86baa9
         return servers[0]
86baa9
+
86baa9
+
86baa9
+def get_masters(conn=None, api=api):
86baa9
+    """Get all master hostnames
86baa9
+
86baa9
+    :param conn: a connection to the LDAP server
86baa9
+    :param api: ipalib.API instance
86baa9
+    :return: list of hostnames
86baa9
+    """
86baa9
+    if conn is None:
86baa9
+        conn = api.Backend.ldap2
86baa9
+
86baa9
+    dn = DN(api.env.container_masters, api.env.basedn)
86baa9
+    entries = conn.get_entries(dn, conn.SCOPE_ONELEVEL, None, ['cn'])
86baa9
+    return list(e['cn'][0] for e in entries)
86baa9
+
86baa9
+
86baa9
+def is_service_enabled(svcname, conn=None, api=api):
86baa9
+    """Check if service is enabled on any master
86baa9
+
86baa9
+    The check function only looks for presence of service entries. It
86baa9
+    ignores enabled/hidden flags.
86baa9
+
86baa9
+    :param svcname: The service to find
86baa9
+    :param conn: a connection to the LDAP server
86baa9
+    :param api: ipalib.API instance
86baa9
+    :return: True/False
86baa9
+    """
86baa9
+    if svcname not in SERVICE_LIST:
86baa9
+        raise ValueError("Unknown service '{}'.".format(svcname))
86baa9
+    if conn is None:
86baa9
+        conn = api.Backend.ldap2
86baa9
+
86baa9
+    dn = DN(api.env.container_masters, api.env.basedn)
86baa9
+    query_filter = conn.make_filter(
86baa9
+        {
86baa9
+            'objectClass': 'ipaConfigObject',
86baa9
+            'cn': svcname
86baa9
+        },
86baa9
+        rules='&'
86baa9
+    )
86baa9
+    try:
86baa9
+        conn.find_entries(
86baa9
+            filter=query_filter,
86baa9
+            attrs_list=[],
86baa9
+            base_dn=dn
86baa9
+        )
86baa9
+    except errors.NotFound:
86baa9
+        return False
86baa9
+    else:
86baa9
+        return True
86baa9
diff --git a/ipaserver/plugins/cert.py b/ipaserver/plugins/cert.py
86baa9
index ff750e9d38ff98e0e1fa1c2eee5a3d0719da94bf..36a7a859292b2ddb9dd721e2bf786c6be130f323 100644
86baa9
--- a/ipaserver/plugins/cert.py
86baa9
+++ b/ipaserver/plugins/cert.py
86baa9
@@ -55,7 +55,9 @@ from ipalib import output
86baa9
 from ipapython import dnsutil, kerberos
86baa9
 from ipapython.dn import DN
86baa9
 from ipaserver.plugins.service import normalize_principal, validate_realm
86baa9
-from ipaserver.masters import ENABLED_SERVICE, CONFIGURED_SERVICE
86baa9
+from ipaserver.masters import (
86baa9
+    ENABLED_SERVICE, CONFIGURED_SERVICE, is_service_enabled
86baa9
+)
86baa9
 
86baa9
 try:
86baa9
     import pyhbac
86baa9
@@ -1908,14 +1910,5 @@ class ca_is_enabled(Command):
86baa9
     has_output = output.standard_value
86baa9
 
86baa9
     def execute(self, *args, **options):
86baa9
-        base_dn = DN(('cn', 'masters'), ('cn', 'ipa'), ('cn', 'etc'),
86baa9
-                     self.api.env.basedn)
86baa9
-        filter = '(&(objectClass=ipaConfigObject)(cn=CA))'
86baa9
-        try:
86baa9
-            self.api.Backend.ldap2.find_entries(
86baa9
-                base_dn=base_dn, filter=filter, attrs_list=[])
86baa9
-        except errors.NotFound:
86baa9
-            result = False
86baa9
-        else:
86baa9
-            result = True
86baa9
+        result = is_service_enabled('CA', conn=self.api.Backend.ldap2)
86baa9
         return dict(result=result, value=pkey_to_value(None, options))
86baa9
diff --git a/ipaserver/plugins/dns.py b/ipaserver/plugins/dns.py
86baa9
index 3c006c5701a426cc03971c9e3361dfd091770241..d6baa2fc1769701ac62b8c6d686fbed74ea3f002 100644
86baa9
--- a/ipaserver/plugins/dns.py
86baa9
+++ b/ipaserver/plugins/dns.py
86baa9
@@ -86,6 +86,7 @@ from ipaserver.dns_data_management import (
86baa9
     IPASystemRecords,
86baa9
     IPADomainIsNotManagedByIPAError,
86baa9
 )
86baa9
+from ipaserver.masters import find_providing_servers, is_service_enabled
86baa9
 
86baa9
 if six.PY3:
86baa9
     unicode = str
86baa9
@@ -1593,19 +1594,7 @@ def dnssec_installed(ldap):
86baa9
     :param ldap: ldap connection
86baa9
     :return: True if DNSSEC was installed, otherwise False
86baa9
     """
86baa9
-    dn = DN(api.env.container_masters, api.env.basedn)
86baa9
-
86baa9
-    filter_attrs = {
86baa9
-        u'cn': u'DNSSEC',
86baa9
-        u'objectclass': u'ipaConfigObject',
86baa9
-    }
86baa9
-    only_masters_f = ldap.make_filter(filter_attrs, rules=ldap.MATCH_ALL)
86baa9
-
86baa9
-    try:
86baa9
-        ldap.find_entries(filter=only_masters_f, base_dn=dn)
86baa9
-    except errors.NotFound:
86baa9
-        return False
86baa9
-    return True
86baa9
+    return is_service_enabled('DNSSEC', conn=ldap)
86baa9
 
86baa9
 
86baa9
 def default_zone_update_policy(zone):
86baa9
@@ -3191,24 +3180,9 @@ class dnsrecord(LDAPObject):
86baa9
         return cliname
86baa9
 
86baa9
     def get_dns_masters(self):
86baa9
-        ldap = self.api.Backend.ldap2
86baa9
-        base_dn = DN(('cn', 'masters'), ('cn', 'ipa'), ('cn', 'etc'), self.api.env.basedn)
86baa9
-        ldap_filter = '(&(objectClass=ipaConfigObject)(cn=DNS))'
86baa9
-        dns_masters = []
86baa9
-
86baa9
-        try:
86baa9
-            entries = ldap.find_entries(filter=ldap_filter, base_dn=base_dn)[0]
86baa9
-
86baa9
-            for entry in entries:
86baa9
-                try:
86baa9
-                    master = entry.dn[1]['cn']
86baa9
-                    dns_masters.append(master)
86baa9
-                except (IndexError, KeyError):
86baa9
-                    pass
86baa9
-        except errors.NotFound:
86baa9
-            return []
86baa9
-
86baa9
-        return dns_masters
86baa9
+        return find_providing_servers(
86baa9
+            'DNS', self.api.Backend.ldap2, preferred_hosts=[api.env.host]
86baa9
+        )
86baa9
 
86baa9
     def get_record_entry_attrs(self, entry_attrs):
86baa9
         entry_attrs = entry_attrs.copy()
86baa9
@@ -4077,19 +4051,8 @@ class dns_is_enabled(Command):
86baa9
     NO_CLI = True
86baa9
     has_output = output.standard_value
86baa9
 
86baa9
-    base_dn = DN(('cn', 'masters'), ('cn', 'ipa'), ('cn', 'etc'), api.env.basedn)
86baa9
-    filter = '(&(objectClass=ipaConfigObject)(cn=DNS))'
86baa9
-
86baa9
     def execute(self, *args, **options):
86baa9
-        ldap = self.api.Backend.ldap2
86baa9
-        dns_enabled = False
86baa9
-
86baa9
-        try:
86baa9
-            ldap.find_entries(filter=self.filter, base_dn=self.base_dn)
86baa9
-            dns_enabled = True
86baa9
-        except errors.EmptyResult:
86baa9
-            dns_enabled = False
86baa9
-
86baa9
+        dns_enabled = is_service_enabled('DNS', conn=self.api.Backend.ldap2)
86baa9
         return dict(result=dns_enabled, value=pkey_to_value(None, options))
86baa9
 
86baa9
 
86baa9
diff --git a/ipaserver/plugins/user.py b/ipaserver/plugins/user.py
86baa9
index 1aa19ab3c2763983b2216e332013b45df1a4a496..980385dc83e93ec4a65726077b34917e21115efa 100644
86baa9
--- a/ipaserver/plugins/user.py
86baa9
+++ b/ipaserver/plugins/user.py
86baa9
@@ -69,6 +69,7 @@ from ipapython.dn import DN
86baa9
 from ipapython.ipaldap import LDAPClient
86baa9
 from ipapython.ipautil import ipa_generate_password, TMP_PWD_ENTROPY_BITS
86baa9
 from ipalib.capabilities import client_has_capability
86baa9
+from ipaserver.masters import get_masters
86baa9
 
86baa9
 if six.PY3:
86baa9
     unicode = str
86baa9
@@ -1105,21 +1106,11 @@ class user_status(LDAPQuery):
86baa9
         attr_list = ['krbloginfailedcount', 'krblastsuccessfulauth', 'krblastfailedauth', 'nsaccountlock']
86baa9
 
86baa9
         disabled = False
86baa9
-        masters = []
86baa9
-        # Get list of masters
86baa9
-        try:
86baa9
-            masters, _truncated = ldap.find_entries(
86baa9
-                None, ['*'], DN(('cn', 'masters'), ('cn', 'ipa'), ('cn', 'etc'), api.env.basedn),
86baa9
-                ldap.SCOPE_ONELEVEL
86baa9
-            )
86baa9
-        except errors.NotFound:
86baa9
-            # If this happens we have some pretty serious problems
86baa9
-            logger.error('No IPA masters found!')
86baa9
+        masters = get_masters(ldap)
86baa9
 
86baa9
         entries = []
86baa9
         count = 0
86baa9
-        for master in masters:
86baa9
-            host = master['cn'][0]
86baa9
+        for host in masters:
86baa9
             if host == api.env.host:
86baa9
                 other_ldap = self.obj.backend
86baa9
             else:
86baa9
diff --git a/ipaserver/plugins/vault.py b/ipaserver/plugins/vault.py
86baa9
index 682f6bea74df024bd28e15157c9b809888d08c38..a2603217cc3b79c6ec891b4deadeea91364eeeb9 100644
86baa9
--- a/ipaserver/plugins/vault.py
86baa9
+++ b/ipaserver/plugins/vault.py
86baa9
@@ -34,6 +34,7 @@ from .service import normalize_principal, validate_realm
86baa9
 from ipalib import _, ngettext
86baa9
 from ipapython import kerberos
86baa9
 from ipapython.dn import DN
86baa9
+from ipaserver.masters import is_service_enabled
86baa9
 
86baa9
 if api.env.in_server:
86baa9
     import pki.account
86baa9
@@ -1220,14 +1221,5 @@ class kra_is_enabled(Command):
86baa9
     has_output = output.standard_value
86baa9
 
86baa9
     def execute(self, *args, **options):
86baa9
-        base_dn = DN(('cn', 'masters'), ('cn', 'ipa'), ('cn', 'etc'),
86baa9
-                     self.api.env.basedn)
86baa9
-        filter = '(&(objectClass=ipaConfigObject)(cn=KRA))'
86baa9
-        try:
86baa9
-            self.api.Backend.ldap2.find_entries(
86baa9
-                base_dn=base_dn, filter=filter, attrs_list=[])
86baa9
-        except errors.NotFound:
86baa9
-            result = False
86baa9
-        else:
86baa9
-            result = True
86baa9
+        result = is_service_enabled('KRA', conn=self.api.Backend.ldap2)
86baa9
         return dict(result=result, value=pkey_to_value(None, options))
86baa9
-- 
86baa9
2.20.1
86baa9