483b06
From a3801d66f5cf3f08062c3aa67a7b33d13fae56b7 Mon Sep 17 00:00:00 2001
483b06
From: Martin Babinsky <mbabinsk@redhat.com>
483b06
Date: Thu, 11 May 2017 15:55:53 +0200
483b06
Subject: [PATCH] Allow for multivalued server attributes
483b06
483b06
In order to achieve the task, the following changes were required:
483b06
483b06
* vectorize the base class for server attributes
483b06
* add a child class that enforces single-value attributes. It still
483b06
  accepts/returns single-value lists in order to not break Liskov
483b06
  substitution principle
483b06
* Existing attributes inherit from the child class
483b06
483b06
https://pagure.io/freeipa/issue/6937
483b06
483b06
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
483b06
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
483b06
---
483b06
 ipaserver/plugins/serverroles.py            |   4 +-
483b06
 ipaserver/servroles.py                      | 109 +++++++++++++++++++---------
483b06
 ipatests/test_ipaserver/test_serverroles.py |  10 +--
483b06
 3 files changed, 79 insertions(+), 44 deletions(-)
483b06
483b06
diff --git a/ipaserver/plugins/serverroles.py b/ipaserver/plugins/serverroles.py
483b06
index e22eadd7b163469cc9fc4472640aa64d21c9d38f..e81635c3315cc3fca84450f43fb7df883aae57d9 100644
483b06
--- a/ipaserver/plugins/serverroles.py
483b06
+++ b/ipaserver/plugins/serverroles.py
483b06
@@ -136,9 +136,7 @@ class serverroles(Backend):
483b06
 
483b06
         for name, attr in assoc_attributes.items():
483b06
             attr_value = attr.get(self.api)
483b06
-
483b06
-            if attr_value is not None:
483b06
-                result.update({name: attr_value})
483b06
+            result.update({name: attr_value})
483b06
 
483b06
         return result
483b06
 
483b06
diff --git a/ipaserver/servroles.py b/ipaserver/servroles.py
483b06
index cf4599995118c589a5b51236c68e13f14ac1257b..84fed1046b3b46dc9f5f8bbe6e03354725f1136c 100644
483b06
--- a/ipaserver/servroles.py
483b06
+++ b/ipaserver/servroles.py
483b06
@@ -277,29 +277,33 @@ class ServerAttribute(LDAPBasedProperty):
483b06
         try:
483b06
             entries = ldap2.get_entries(search_base, filter=search_filter)
483b06
         except errors.EmptyResult:
483b06
-            return
483b06
+            return []
483b06
 
483b06
-        master_cn = entries[0].dn[1]['cn']
483b06
+        master_cns = {e.dn[1]['cn'] for e in entries}
483b06
 
483b06
         associated_role_providers = set(
483b06
             self._get_assoc_role_providers(api_instance))
483b06
 
483b06
-        if master_cn not in associated_role_providers:
483b06
+        if not master_cns.issubset(associated_role_providers):
483b06
             raise errors.ValidationError(
483b06
                 name=self.name,
483b06
                 error=_("all masters must have %(role)s role enabled" %
483b06
                         {'role': self.associated_role.name})
483b06
             )
483b06
 
483b06
-        return master_cn
483b06
+        return sorted(master_cns)
483b06
 
483b06
-    def _get_master_dn(self, api_instance, server):
483b06
-        return DN(('cn', server), api_instance.env.container_masters,
483b06
-                  api_instance.env.basedn)
483b06
+    def _get_master_dns(self, api_instance, servers):
483b06
+        return [
483b06
+            DN(('cn', server), api_instance.env.container_masters,
483b06
+               api_instance.env.basedn) for server in servers]
483b06
+
483b06
+    def _get_masters_service_entries(self, ldap, master_dns):
483b06
+        service_dns = [
483b06
+            DN(('cn', self.associated_service_name), master_dn) for master_dn
483b06
+            in master_dns]
483b06
 
483b06
-    def _get_masters_service_entry(self, ldap, master_dn):
483b06
-        service_dn = DN(('cn', self.associated_service_name), master_dn)
483b06
-        return ldap.get_entry(service_dn)
483b06
+        return [ldap.get_entry(service_dn) for service_dn in service_dns]
483b06
 
483b06
     def _add_attribute_to_svc_entry(self, ldap, service_entry):
483b06
         """
483b06
@@ -341,65 +345,98 @@ class ServerAttribute(LDAPBasedProperty):
483b06
             r[u'server_server'] for r in self.associated_role.status(
483b06
                 api_instance) if r[u'status'] == ENABLED]
483b06
 
483b06
-    def _remove(self, api_instance, master):
483b06
+    def _remove(self, api_instance, masters):
483b06
         """
483b06
-        remove attribute from the master
483b06
+        remove attribute from one or more masters
483b06
 
483b06
         :param api_instance: API instance
483b06
-        :param master: master FQDN
483b06
+        :param master: list or iterable containing master FQDNs
483b06
         """
483b06
 
483b06
         ldap = api_instance.Backend.ldap2
483b06
 
483b06
-        master_dn = self._get_master_dn(api_instance, master)
483b06
-        service_entry = self._get_masters_service_entry(ldap, master_dn)
483b06
-        self._remove_attribute_from_svc_entry(ldap, service_entry)
483b06
+        master_dns = self._get_master_dns(api_instance, masters)
483b06
+        service_entries = self._get_masters_service_entries(ldap, master_dns)
483b06
+
483b06
+        for service_entry in service_entries:
483b06
+            self._remove_attribute_from_svc_entry(ldap, service_entry)
483b06
 
483b06
-    def _add(self, api_instance, master):
483b06
+    def _add(self, api_instance, masters):
483b06
         """
483b06
         add attribute to the master
483b06
         :param api_instance: API instance
483b06
-        :param master: master FQDN
483b06
+        :param master: iterable containing master FQDNs
483b06
 
483b06
         :raises: * errors.ValidationError if the associated role is not enabled
483b06
                    on the master
483b06
         """
483b06
 
483b06
-        assoc_role_providers = self._get_assoc_role_providers(api_instance)
483b06
+        assoc_role_providers = set(
483b06
+            self._get_assoc_role_providers(api_instance))
483b06
+        masters_set = set(masters)
483b06
         ldap = api_instance.Backend.ldap2
483b06
 
483b06
-        if master not in assoc_role_providers:
483b06
+        masters_without_role = masters_set - assoc_role_providers
483b06
+
483b06
+        if masters_without_role:
483b06
             raise errors.ValidationError(
483b06
-                name=master,
483b06
+                name=', '.join(sorted(masters_without_role)),
483b06
                 error=_("must have %(role)s role enabled" %
483b06
                         {'role': self.associated_role.name})
483b06
             )
483b06
 
483b06
-        master_dn = self._get_master_dn(api_instance, master)
483b06
-        service_entry = self._get_masters_service_entry(ldap, master_dn)
483b06
-        self._add_attribute_to_svc_entry(ldap, service_entry)
483b06
+        master_dns = self._get_master_dns(api_instance, masters)
483b06
+        service_entries = self._get_masters_service_entries(ldap, master_dns)
483b06
+        for service_entry in service_entries:
483b06
+            self._add_attribute_to_svc_entry(ldap, service_entry)
483b06
 
483b06
-    def set(self, api_instance, master):
483b06
+    def set(self, api_instance, masters):
483b06
         """
483b06
-        set the attribute on master
483b06
+        set the attribute on masters
483b06
 
483b06
         :param api_instance: API instance
483b06
-        :param master: FQDN of the new master
483b06
+        :param masters: an interable with FQDNs of the new masters
483b06
 
483b06
-        the attribute is automatically unset from previous master if present
483b06
+        the attribute is automatically unset from previous masters if present
483b06
 
483b06
         :raises: errors.EmptyModlist if the new masters is the same as
483b06
-                 the original on
483b06
+                 the original ones
483b06
         """
483b06
-        old_master = self.get(api_instance)
483b06
+        old_masters = self.get(api_instance)
483b06
 
483b06
-        if old_master == master:
483b06
+        if sorted(old_masters) == sorted(masters):
483b06
             raise errors.EmptyModlist
483b06
 
483b06
-        self._add(api_instance, master)
483b06
+        if old_masters:
483b06
+            self._remove(api_instance, old_masters)
483b06
+
483b06
+        self._add(api_instance, masters)
483b06
+
483b06
+
483b06
+class SingleValuedServerAttribute(ServerAttribute):
483b06
+    """
483b06
+    Base class for server attributes that are forced to be single valued
483b06
+
483b06
+    this means that `get` method will return a one-element list, and `set`
483b06
+    method will accept only one-element list
483b06
+    """
483b06
+
483b06
+    def set(self, api_instance, masters):
483b06
+        if len(masters) > 1:
483b06
+            raise errors.ValidationError(
483b06
+                name=self.attr_name,
483b06
+                error=_("must be enabled only on a single master"))
483b06
+
483b06
+        super(SingleValuedServerAttribute, self).set(api_instance, masters)
483b06
+
483b06
+    def get(self, api_instance):
483b06
+        masters = super(SingleValuedServerAttribute, self).get(api_instance)
483b06
+        num_masters = len(masters)
483b06
+
483b06
+        if num_masters > 1:
483b06
+            raise errors.SingleMatchExpected(found=num_masters)
483b06
 
483b06
-        if old_master is not None:
483b06
-            self._remove(api_instance, old_master)
483b06
+        return masters
483b06
 
483b06
 
483b06
 _Service = namedtuple('Service', ['name', 'enabled'])
483b06
@@ -574,14 +611,14 @@ role_instances = (
483b06
 )
483b06
 
483b06
 attribute_instances = (
483b06
-    ServerAttribute(
483b06
+    SingleValuedServerAttribute(
483b06
         u"ca_renewal_master_server",
483b06
         u"CA renewal master",
483b06
         u"ca_server_server",
483b06
         u"CA",
483b06
         u"caRenewalMaster",
483b06
     ),
483b06
-    ServerAttribute(
483b06
+    SingleValuedServerAttribute(
483b06
         u"dnssec_key_master_server",
483b06
         u"DNSSec key master",
483b06
         u"dns_server_server",
483b06
diff --git a/ipatests/test_ipaserver/test_serverroles.py b/ipatests/test_ipaserver/test_serverroles.py
483b06
index d8844df3007f076532a34d625379ab552ef45363..e671272783d8d71c2ee56074459433b98b79dd0a 100644
483b06
--- a/ipatests/test_ipaserver/test_serverroles.py
483b06
+++ b/ipatests/test_ipaserver/test_serverroles.py
483b06
@@ -706,7 +706,7 @@ class TestServerAttributes(object):
483b06
         actual_attr_masters = self.config_retrieve(
483b06
             assoc_role, mock_api)[attr_name]
483b06
 
483b06
-        assert actual_attr_masters == fqdn
483b06
+        assert actual_attr_masters == [fqdn]
483b06
 
483b06
     def test_set_attribute_on_the_same_provider_raises_emptymodlist(
483b06
             self, mock_api, mock_masters):
483b06
@@ -727,7 +727,7 @@ class TestServerAttributes(object):
483b06
         non_ca_fqdn = mock_masters.get_fqdn('trust-controller-dns')
483b06
 
483b06
         with pytest.raises(errors.ValidationError):
483b06
-            self.config_update(mock_api, **{attr_name: non_ca_fqdn})
483b06
+            self.config_update(mock_api, **{attr_name: [non_ca_fqdn]})
483b06
 
483b06
     def test_set_unknown_attribute_on_master_raises_notfound(
483b06
             self, mock_api, mock_masters):
483b06
@@ -735,7 +735,7 @@ class TestServerAttributes(object):
483b06
         fqdn = mock_masters.get_fqdn('trust-controller-ca')
483b06
 
483b06
         with pytest.raises(errors.NotFound):
483b06
-            self.config_update(mock_api, **{attr_name: fqdn})
483b06
+            self.config_update(mock_api, **{attr_name: [fqdn]})
483b06
 
483b06
     def test_set_ca_renewal_master_on_other_ca_and_back(self, mock_api,
483b06
                                                         mock_masters):
483b06
@@ -747,7 +747,7 @@ class TestServerAttributes(object):
483b06
         other_ca_server = mock_masters.get_fqdn('trust-controller-ca')
483b06
 
483b06
         for host in (other_ca_server, original_renewal_master):
483b06
-            self.config_update(mock_api, **{attr_name: host})
483b06
+            self.config_update(mock_api, **{attr_name: [host]})
483b06
 
483b06
             assert (
483b06
-                self.config_retrieve(role_name, mock_api)[attr_name] == host)
483b06
+                self.config_retrieve(role_name, mock_api)[attr_name] == [host])
483b06
-- 
483b06
2.9.4
483b06