Blame SOURCES/0021-Use-the-subject-base-from-the-IPA-configuration-not-.patch

45cc94
From ed5322daad5dc456e4958228835b33a32c7d1608 Mon Sep 17 00:00:00 2001
45cc94
From: Rob Crittenden <rcritten@redhat.com>
45cc94
Date: Tue, 29 Mar 2022 12:58:01 -0400
45cc94
Subject: [PATCH] Use the subject base from the IPA configuration, not REALM
45cc94
45cc94
The expected certificates were hardcoded with O={REALM} which
45cc94
would return false-positives if the customer defined their
45cc94
own certificate subject base.
45cc94
45cc94
Also add a search filter to only retrieve the certificate(s) we
45cc94
want to examine rather than the entire contents.
45cc94
45cc94
Fixes: https://github.com/freeipa/freeipa-healthcheck/issues/253
45cc94
45cc94
Signed-off-by: Rob Crittenden <rcritten@redhat.com>
45cc94
---
45cc94
 src/ipahealthcheck/ipa/certs.py |  24 ++--
45cc94
 tests/test_ipa_cert_match.py    | 202 ++++++++++++++++++++++++--------
45cc94
 2 files changed, 166 insertions(+), 60 deletions(-)
45cc94
45cc94
diff --git a/src/ipahealthcheck/ipa/certs.py b/src/ipahealthcheck/ipa/certs.py
45cc94
index 2ef33f9..fd5e180 100644
45cc94
--- a/src/ipahealthcheck/ipa/certs.py
45cc94
+++ b/src/ipahealthcheck/ipa/certs.py
45cc94
@@ -707,12 +707,14 @@ class IPADogtagCertsMatchCheck(IPAPlugin):
45cc94
 
45cc94
         def match_ldap_nss_certs_by_subject(plugin, ldap, db, dn,
45cc94
                                             expected_nicks_subjects):
45cc94
-            entries = ldap.get_entries(dn)
45cc94
             all_ok = True
45cc94
             for nick, subject in expected_nicks_subjects.items():
45cc94
+                entries = ldap.get_entries(
45cc94
+                    dn,
45cc94
+                    filter=f'subjectname={subject}'
45cc94
+                )
45cc94
                 cert = db.get_cert_from_db(nick)
45cc94
-                ok = any([cert in entry['userCertificate'] and
45cc94
-                          subject == entry['subjectName'][0]
45cc94
+                ok = any([cert in entry['userCertificate']
45cc94
                           for entry in entries
45cc94
                           if 'userCertificate' in entry])
45cc94
                 if not ok:
45cc94
@@ -740,26 +742,28 @@ class IPADogtagCertsMatchCheck(IPAPlugin):
45cc94
                                                       db, dn, 'CACertificate',
45cc94
                                                       casigning_nick)
45cc94
 
45cc94
+        config = api.Command.config_show()
45cc94
+        subject_base = config['result']['ipacertificatesubjectbase'][0]
45cc94
         expected_nicks_subjects = {
45cc94
             'ocspSigningCert cert-pki-ca':
45cc94
-                'CN=OCSP Subsystem,O=%s' % api.env.realm,
45cc94
+                f'CN=OCSP Subsystem,{subject_base}',
45cc94
             'subsystemCert cert-pki-ca':
45cc94
-                'CN=CA Subsystem,O=%s' % api.env.realm,
45cc94
+                f'CN=CA Subsystem,{subject_base}',
45cc94
             'auditSigningCert cert-pki-ca':
45cc94
-                'CN=CA Audit,O=%s' % api.env.realm,
45cc94
+                f'CN=CA Audit,{subject_base}',
45cc94
             'Server-Cert cert-pki-ca':
45cc94
-                'CN=%s,O=%s' % (api.env.host, api.env.realm),
45cc94
+                f'CN={api.env.host},{subject_base}',
45cc94
         }
45cc94
 
45cc94
         kra = krainstance.KRAInstance(api.env.realm)
45cc94
         if kra.is_installed():
45cc94
             kra_expected_nicks_subjects = {
45cc94
                 'transportCert cert-pki-kra':
45cc94
-                    'CN=KRA Transport Certificate,O=%s' % api.env.realm,
45cc94
+                    f'CN=KRA Transport Certificate,{subject_base}',
45cc94
                 'storageCert cert-pki-kra':
45cc94
-                    'CN=KRA Storage Certificate,O=%s' % api.env.realm,
45cc94
+                    f'CN=KRA Storage Certificate,{subject_base}',
45cc94
                 'auditSigningCert cert-pki-kra':
45cc94
-                    'CN=KRA Audit,O=%s' % api.env.realm,
45cc94
+                    f'CN=KRA Audit,{subject_base}',
45cc94
             }
45cc94
             expected_nicks_subjects.update(kra_expected_nicks_subjects)
45cc94
 
45cc94
diff --git a/tests/test_ipa_cert_match.py b/tests/test_ipa_cert_match.py
45cc94
index 460e61a..70ef59e 100644
45cc94
--- a/tests/test_ipa_cert_match.py
45cc94
+++ b/tests/test_ipa_cert_match.py
45cc94
@@ -44,8 +44,15 @@ class mock_ldap:
45cc94
     def get_entries(self, base_dn, scope=SCOPE_SUBTREE, filter=None,
45cc94
                     attrs_list=None, get_effective_rights=False, **kwargs):
45cc94
         if self.results is None:
45cc94
-            raise errors.NotFound(reason='test')
45cc94
-        return self.results.values()
45cc94
+            raise errors.NotFound(reason='None')
45cc94
+        if filter:
45cc94
+            (attr, value) = filter.split('=', maxsplit=1)
45cc94
+            for result in self.results.values():
45cc94
+                if result.get(attr)[0] == value:
45cc94
+                    return [result]
45cc94
+            raise errors.NotFound(reason='Not found %s' % filter)
45cc94
+
45cc94
+        return self.results
45cc94
 
45cc94
 
45cc94
 class mock_ldap_conn:
45cc94
@@ -82,6 +89,10 @@ class TestIPACertMatch(BaseTest):
45cc94
         Mock(return_value=mock_ldap_conn())
45cc94
     }
45cc94
 
45cc94
+    trust = {
45cc94
+        ('%s IPA CA' % m_api.env.realm): 'u,u,u'
45cc94
+    }
45cc94
+
45cc94
     @patch('ipalib.x509.load_certificate_list_from_file')
45cc94
     @patch('ipaserver.install.certs.CertDB')
45cc94
     def test_certs_match_ok(self, mock_certdb, mock_load_cert):
45cc94
@@ -92,11 +103,8 @@ class TestIPACertMatch(BaseTest):
45cc94
                                    'cn=certificates,cn=ipa,cn=etc',
45cc94
                                     m_api.env.basedn),
45cc94
                                 CACertificate=[IPACertificate()])
45cc94
-        trust = {
45cc94
-            ('%s IPA CA' % m_api.env.realm): 'u,u,u'
45cc94
-        }
45cc94
 
45cc94
-        mock_certdb.return_value = mock_CertDB(trust)
45cc94
+        mock_certdb.return_value = mock_CertDB(self.trust)
45cc94
         mock_load_cert.return_value = [IPACertificate()]
45cc94
 
45cc94
         framework = object()
45cc94
@@ -121,11 +129,8 @@ class TestIPACertMatch(BaseTest):
45cc94
                                    'cn=certificates,cn=ipa,cn=etc',
45cc94
                                     m_api.env.basedn),
45cc94
                                 CACertificate=[IPACertificate()])
45cc94
-        trust = {
45cc94
-            ('%s IPA CA' % m_api.env.realm): 'u,u,u'
45cc94
-        }
45cc94
 
45cc94
-        mock_certdb.return_value = mock_CertDB(trust)
45cc94
+        mock_certdb.return_value = mock_CertDB(self.trust)
45cc94
         mock_load_cert.return_value = [IPACertificate(serial_number=2)]
45cc94
 
45cc94
         framework = object()
45cc94
@@ -155,15 +160,54 @@ class TestIPACertMatch(BaseTest):
45cc94
         assert len(self.results) == 0
45cc94
 
45cc94
 
45cc94
+default_subject_base = [{
45cc94
+    'result':
45cc94
+        {
45cc94
+            'ipacertificatesubjectbase': [f'O={m_api.env.realm}'],
45cc94
+        },
45cc94
+}]
45cc94
+
45cc94
+custom_subject_base = [{
45cc94
+    'result':
45cc94
+        {
45cc94
+            'ipacertificatesubjectbase': ['OU=Eng,O=ACME'],
45cc94
+        },
45cc94
+}]
45cc94
+
45cc94
+
45cc94
 class TestIPADogtagCertMatch(BaseTest):
45cc94
     patches = {
45cc94
         'ipaserver.install.krainstance.KRAInstance':
45cc94
         Mock(return_value=KRAInstance()),
45cc94
     }
45cc94
+    trust = {
45cc94
+        'ocspSigningCert cert-pki-ca': 'u,u,u',
45cc94
+        'caSigningCert cert-pki-ca': 'u,u,u',
45cc94
+        'subsystemCert cert-pki-ca': 'u,u,u',
45cc94
+        'auditSigningCert cert-pki-ca': 'u,u,Pu',
45cc94
+        'Server-Cert cert-pki-ca': 'u,u,u',
45cc94
+        'transportCert cert-pki-kra': 'u,u,u',
45cc94
+        'storageCert cert-pki-kra': 'u,u,u',
45cc94
+        'auditSigningCert cert-pki-kra': 'u,u,Pu',
45cc94
+    }
45cc94
+
45cc94
+    def get_dogtag_subjects(self, hostname, base):
45cc94
+        subject_base = base[0]['result']['ipacertificatesubjectbase'][0]
45cc94
+        return (
45cc94
+            f'CN=OCSP Subsystem,{subject_base}',
45cc94
+            f'CN=CA Subsystem,{subject_base}',
45cc94
+            f'CN=CA Audit,{subject_base}',
45cc94
+            f'CN=%s,{subject_base}',
45cc94
+            f'CN=KRA Transport Certificate,{subject_base}',
45cc94
+            f'CN=KRA Storage Certificate,{subject_base}',
45cc94
+            f'CN=KRA Audit,{subject_base}',
45cc94
+            f'CN={hostname},{subject_base}',
45cc94
+        )
45cc94
 
45cc94
     @patch('ipaserver.install.certs.CertDB')
45cc94
     def test_certs_match_ok(self, mock_certdb):
45cc94
         """ Ensure match check is ok"""
45cc94
+        m_api.Command.config_show.side_effect = default_subject_base
45cc94
         fake_conn = LDAPClient('ldap://localhost', no_schema=True)
45cc94
         pkidbentry = LDAPEntry(fake_conn,
45cc94
                                DN('uid=pkidbuser,ou=people,o=ipaca'),
45cc94
@@ -177,25 +221,9 @@ class TestIPADogtagCertMatch(BaseTest):
45cc94
                                 userCertificate=[IPACertificate()],
45cc94
                                 subjectName=['test'])
45cc94
         ldap_entries = [pkidbentry, casignentry]
45cc94
-        trust = {
45cc94
-            'ocspSigningCert cert-pki-ca': 'u,u,u',
45cc94
-            'caSigningCert cert-pki-ca': 'u,u,u',
45cc94
-            'subsystemCert cert-pki-ca': 'u,u,u',
45cc94
-            'auditSigningCert cert-pki-ca': 'u,u,Pu',
45cc94
-            'Server-Cert cert-pki-ca': 'u,u,u',
45cc94
-            'transportCert cert-pki-kra': 'u,u,u',
45cc94
-            'storageCert cert-pki-kra': 'u,u,u',
45cc94
-            'auditSigningCert cert-pki-kra': 'u,u,Pu',
45cc94
-        }
45cc94
-
45cc94
-        dogtag_entries_subjects = (
45cc94
-            'CN=OCSP Subsystem,O=%s' % m_api.env.realm,
45cc94
-            'CN=CA Subsystem,O=%s' % m_api.env.realm,
45cc94
-            'CN=CA Audit,O=%s' % m_api.env.realm,
45cc94
-            'CN=%s,O=%s' % (m_api.env.host, m_api.env.realm),
45cc94
-            'CN=KRA Transport Certificate,O=%s' % m_api.env.realm,
45cc94
-            'CN=KRA Storage Certificate,O=%s' % m_api.env.realm,
45cc94
-            'CN=KRA Audit,O=%s' % m_api.env.realm,
45cc94
+
45cc94
+        dogtag_entries_subjects = self.get_dogtag_subjects(
45cc94
+            m_api.env.host, default_subject_base
45cc94
         )
45cc94
 
45cc94
         for i, subject in enumerate(dogtag_entries_subjects):
45cc94
@@ -206,7 +234,7 @@ class TestIPADogtagCertMatch(BaseTest):
45cc94
                               subjectName=[subject])
45cc94
             ldap_entries.append(entry)
45cc94
 
45cc94
-        mock_certdb.return_value = mock_CertDB(trust)
45cc94
+        mock_certdb.return_value = mock_CertDB(self.trust)
45cc94
 
45cc94
         framework = object()
45cc94
         registry.initialize(framework, config.Config())
45cc94
@@ -223,6 +251,7 @@ class TestIPADogtagCertMatch(BaseTest):
45cc94
     @patch('ipaserver.install.certs.CertDB')
45cc94
     def test_certs_mismatch(self, mock_certdb):
45cc94
         """ Ensure mismatches are detected"""
45cc94
+        m_api.Command.config_show.side_effect = default_subject_base
45cc94
         fake_conn = LDAPClient('ldap://localhost', no_schema=True)
45cc94
         pkidbentry = LDAPEntry(fake_conn,
45cc94
                                DN('uid=pkidbuser,ou=people,o=ipaca'),
45cc94
@@ -238,25 +267,9 @@ class TestIPADogtagCertMatch(BaseTest):
45cc94
                                 userCertificate=[IPACertificate()],
45cc94
                                 subjectName=['test'])
45cc94
         ldap_entries = [pkidbentry, casignentry]
45cc94
-        trust = {
45cc94
-            'ocspSigningCert cert-pki-ca': 'u,u,u',
45cc94
-            'caSigningCert cert-pki-ca': 'u,u,u',
45cc94
-            'subsystemCert cert-pki-ca': 'u,u,u',
45cc94
-            'auditSigningCert cert-pki-ca': 'u,u,Pu',
45cc94
-            'Server-Cert cert-pki-ca': 'u,u,u',
45cc94
-            'transportCert cert-pki-kra': 'u,u,u',
45cc94
-            'storageCert cert-pki-kra': 'u,u,u',
45cc94
-            'auditSigningCert cert-pki-kra': 'u,u,Pu',
45cc94
-        }
45cc94
-
45cc94
-        dogtag_entries_subjects = (
45cc94
-            'CN=OCSP Subsystem,O=%s' % m_api.env.realm,
45cc94
-            'CN=CA Subsystem,O=%s' % m_api.env.realm,
45cc94
-            'CN=CA Audit,O=%s' % m_api.env.realm,
45cc94
-            'CN=%s,O=%s' % (m_api.env.host, m_api.env.realm),
45cc94
-            'CN=KRA Transport Certificate,O=%s' % m_api.env.realm,
45cc94
-            'CN=KRA Storage Certificate,O=%s' % m_api.env.realm,
45cc94
-            'CN=KRA Audit,O=%s' % m_api.env.realm,
45cc94
+
45cc94
+        dogtag_entries_subjects = self.get_dogtag_subjects(
45cc94
+            m_api.env.host, default_subject_base
45cc94
         )
45cc94
 
45cc94
         for i, subject in enumerate(dogtag_entries_subjects):
45cc94
@@ -267,7 +280,7 @@ class TestIPADogtagCertMatch(BaseTest):
45cc94
                               subjectName=[subject])
45cc94
             ldap_entries.append(entry)
45cc94
 
45cc94
-        mock_certdb.return_value = mock_CertDB(trust)
45cc94
+        mock_certdb.return_value = mock_CertDB(self.trust)
45cc94
 
45cc94
         framework = object()
45cc94
         registry.initialize(framework, config.Config())
45cc94
@@ -280,3 +293,92 @@ class TestIPADogtagCertMatch(BaseTest):
45cc94
         assert result.result == constants.ERROR
45cc94
         assert result.source == 'ipahealthcheck.ipa.certs'
45cc94
         assert result.check == 'IPADogtagCertsMatchCheck'
45cc94
+
45cc94
+    @patch('ipaserver.install.certs.CertDB')
45cc94
+    def test_certs_match_ok_subject(self, mock_certdb):
45cc94
+        """ Ensure match check is ok"""
45cc94
+        m_api.Command.config_show.side_effect = custom_subject_base
45cc94
+        fake_conn = LDAPClient('ldap://localhost', no_schema=True)
45cc94
+        pkidbentry = LDAPEntry(fake_conn,
45cc94
+                               DN('uid=pkidbuser,ou=people,o=ipaca'),
45cc94
+                               userCertificate=[IPACertificate()],
45cc94
+                               subjectName=['test'])
45cc94
+        casignentry = LDAPEntry(fake_conn,
45cc94
+                                DN('cn=%s IPA CA' % m_api.env.realm,
45cc94
+                                   'cn=certificates,cn=ipa,cn=etc',
45cc94
+                                    m_api.env.basedn),
45cc94
+                                CACertificate=[IPACertificate()],
45cc94
+                                userCertificate=[IPACertificate()],
45cc94
+                                subjectName=['test'])
45cc94
+        ldap_entries = [pkidbentry, casignentry]
45cc94
+
45cc94
+        dogtag_entries_subjects = self.get_dogtag_subjects(
45cc94
+            m_api.env.host, custom_subject_base
45cc94
+        )
45cc94
+
45cc94
+        for i, subject in enumerate(dogtag_entries_subjects):
45cc94
+            entry = LDAPEntry(fake_conn,
45cc94
+                              DN('cn=%i,ou=certificateRepository' % i,
45cc94
+                                 'ou=ca,o=ipaca'),
45cc94
+                              userCertificate=[IPACertificate()],
45cc94
+                              subjectName=[subject])
45cc94
+            ldap_entries.append(entry)
45cc94
+
45cc94
+        mock_certdb.return_value = mock_CertDB(self.trust)
45cc94
+
45cc94
+        framework = object()
45cc94
+        registry.initialize(framework, config.Config())
45cc94
+        f = IPADogtagCertsMatchCheck(registry)
45cc94
+        f.conn = mock_ldap(ldap_entries)
45cc94
+        self.results = capture_results(f)
45cc94
+
45cc94
+        assert len(self.results) == 3
45cc94
+        for result in self.results.results:
45cc94
+            assert result.result == constants.SUCCESS
45cc94
+            assert result.source == 'ipahealthcheck.ipa.certs'
45cc94
+            assert result.check == 'IPADogtagCertsMatchCheck'
45cc94
+
45cc94
+    @patch('ipaserver.install.certs.CertDB')
45cc94
+    def test_certs_mismatch_subject(self, mock_certdb):
45cc94
+        """ Ensure mismatches are detected"""
45cc94
+        m_api.Command.config_show.side_effect = custom_subject_base
45cc94
+        fake_conn = LDAPClient('ldap://localhost', no_schema=True)
45cc94
+        pkidbentry = LDAPEntry(fake_conn,
45cc94
+                               DN('uid=pkidbuser,ou=people,o=ipaca'),
45cc94
+                               userCertificate=[IPACertificate(
45cc94
+                                   serial_number=2
45cc94
+                               )],
45cc94
+                               subjectName=['test'])
45cc94
+        casignentry = LDAPEntry(fake_conn,
45cc94
+                                DN('cn=%s IPA CA' % m_api.env.realm,
45cc94
+                                   'cn=certificates,cn=ipa,cn=etc',
45cc94
+                                    m_api.env.basedn),
45cc94
+                                CACertificate=[IPACertificate()],
45cc94
+                                userCertificate=[IPACertificate()],
45cc94
+                                subjectName=['test'])
45cc94
+        ldap_entries = [pkidbentry, casignentry]
45cc94
+
45cc94
+        dogtag_entries_subjects = self.get_dogtag_subjects(
45cc94
+            m_api.env.host, custom_subject_base
45cc94
+        )
45cc94
+
45cc94
+        for i, subject in enumerate(dogtag_entries_subjects):
45cc94
+            entry = LDAPEntry(fake_conn,
45cc94
+                              DN('cn=%i,ou=certificateRepository' % i,
45cc94
+                                 'ou=ca,o=ipaca'),
45cc94
+                              userCertificate=[IPACertificate()],
45cc94
+                              subjectName=[subject])
45cc94
+            ldap_entries.append(entry)
45cc94
+
45cc94
+        mock_certdb.return_value = mock_CertDB(self.trust)
45cc94
+
45cc94
+        framework = object()
45cc94
+        registry.initialize(framework, config.Config())
45cc94
+        f = IPADogtagCertsMatchCheck(registry)
45cc94
+        f.conn = mock_ldap(ldap_entries)
45cc94
+        self.results = capture_results(f)
45cc94
+
45cc94
+        assert len(self.results) == 3
45cc94
+        result = self.results.results[0]
45cc94
+        assert result.result == constants.ERROR
45cc94
+        assert result.source == 'ipahealthcheck.ipa.certs'
45cc94
-- 
45cc94
2.31.1
45cc94