Blame SOURCES/0005-Add-check-for-IPA-KRA-Agent.patch

8cfcc8
From 3f6ed4393dfa9ddf982e326065a3ea160bef90b6 Mon Sep 17 00:00:00 2001
8cfcc8
From: Antonio Torres <antorres@redhat.com>
8cfcc8
Date: Tue, 23 Feb 2021 16:11:59 +0100
8cfcc8
Subject: [PATCH] Add check for IPA KRA Agent
8cfcc8
8cfcc8
Add check to validate KRA Agent in case KRA is installed, including
8cfcc8
checking for the KRA Agent LDAP entry.
8cfcc8
8cfcc8
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1894781
8cfcc8
Signed-off-by: Antonio Torres <antorres@redhat.com>
8cfcc8
---
8cfcc8
 README.md                       |  16 ++-
8cfcc8
 src/ipahealthcheck/ipa/certs.py | 167 +++++++++++++++++++-------------
8cfcc8
 2 files changed, 112 insertions(+), 71 deletions(-)
8cfcc8
8cfcc8
diff --git a/README.md b/README.md
8cfcc8
index b9c60a2..0f3ed6a 100644
8cfcc8
--- a/README.md
8cfcc8
+++ b/README.md
8cfcc8
@@ -547,7 +547,21 @@ Verify the description and userCertificate values in uid=ipara,ou=People,o=ipaca
8cfcc8
       "kw": {
8cfcc8
         "expected": "2;125;CN=Certificate Authority,O=EXAMPLE.TEST;CN=IPA RA,O=EXAMPLE.TEST",
8cfcc8
         "got": "2;7;CN=Certificate Authority,O=EXAMPLE.TEST;CN=IPA RA,O=EXAMPLE.TEST",
8cfcc8
-        "msg": "RA agent description does not match 2;7;CN=Certificate Authority,O=EXAMPLE.TEST;CN=IPA RA,O=EXAMPLE.TEST in LDAP and expected 2;125;CN=Certificate Authority,O=EXAMPLE.TEST;CN=IPA RA,O=EXAMPLE.TEST"
8cfcc8
+        "msg": "RA agent description does not match. Found 2;7;CN=Certificate Authority,O=EXAMPLE.TEST;CN=IPA RA,O=EXAMPLE.TEST in LDAP and expected 2;125;CN=Certificate Authority,O=EXAMPLE.TEST;CN=IPA RA,O=EXAMPLE.TEST"
8cfcc8
+      }
8cfcc8
+    }
8cfcc8
+
8cfcc8
+### IPAKRAAgent
8cfcc8
+Verify the description and userCertificate values in uid=ipakra,ou=people,o=kra,o=ipaca.
8cfcc8
+
8cfcc8
+    {
8cfcc8
+      "source": "ipahealthcheck.ipa.certs",
8cfcc8
+      "check": "IPAKRAAgent",
8cfcc8
+      "result": "ERROR",
8cfcc8
+      "kw": {
8cfcc8
+        "expected": "2;125;CN=Certificate Authority,O=EXAMPLE.TEST;CN=IPA RA,O=EXAMPLE.TEST",
8cfcc8
+        "got": "2;7;CN=Certificate Authority,O=EXAMPLE.TEST;CN=IPA RA,O=EXAMPLE.TEST",
8cfcc8
+        "msg": "KRA agent description does not match. Found 2;7;CN=Certificate Authority,O=EXAMPLE.TEST;CN=IPA RA,O=EXAMPLE.TEST in LDAP and expected 2;125;CN=Certificate Authority,O=EXAMPLE.TEST;CN=IPA RA,O=EXAMPLE.TEST"
8cfcc8
       }
8cfcc8
     }
8cfcc8
 
8cfcc8
diff --git a/src/ipahealthcheck/ipa/certs.py b/src/ipahealthcheck/ipa/certs.py
8cfcc8
index d3043d0..32c0d76 100644
8cfcc8
--- a/src/ipahealthcheck/ipa/certs.py
8cfcc8
+++ b/src/ipahealthcheck/ipa/certs.py
8cfcc8
@@ -724,6 +724,83 @@ class IPAOpenSSLChainValidation(IPAPlugin):
8cfcc8
                         self, constants.SUCCESS, key=cert)
8cfcc8
 
8cfcc8
 
8cfcc8
+def check_agent(plugin, base_dn, agent_type):
8cfcc8
+    """Check RA/KRA Agent"""
8cfcc8
+
8cfcc8
+    try:
8cfcc8
+        cert = x509.load_certificate_from_file(paths.RA_AGENT_PEM)
8cfcc8
+    except Exception as e:
8cfcc8
+        yield Result(plugin, constants.ERROR,
8cfcc8
+                     error=str(e),
8cfcc8
+                     msg='Unable to load RA cert: {error}')
8cfcc8
+        return
8cfcc8
+    serial_number = cert.serial_number
8cfcc8
+    subject = DN(cert.subject)
8cfcc8
+    issuer = DN(cert.issuer)
8cfcc8
+    description = '2;%d;%s;%s' % (serial_number, issuer, subject)
8cfcc8
+    logger.debug('%s agent description should be %s', agent_type, description)
8cfcc8
+    db_filter = ldap2.ldap2.combine_filters(
8cfcc8
+        [
8cfcc8
+            ldap2.ldap2.make_filter({'objectClass': 'inetOrgPerson'}),
8cfcc8
+            ldap2.ldap2.make_filter(
8cfcc8
+                {'description': ';%s;%s' % (issuer, subject)},
8cfcc8
+                exact=False, trailing_wildcard=False),
8cfcc8
+        ],
8cfcc8
+        ldap2.ldap2.MATCH_ALL)
8cfcc8
+    try:
8cfcc8
+        entries = plugin.conn.get_entries(base_dn,
8cfcc8
+                                          plugin.conn.SCOPE_SUBTREE,
8cfcc8
+                                          db_filter)
8cfcc8
+    except errors.NotFound:
8cfcc8
+        yield Result(plugin, constants.ERROR,
8cfcc8
+                     description=description,
8cfcc8
+                     msg='%s agent not found in LDAP' % agent_type)
8cfcc8
+        return
8cfcc8
+    except Exception as e:
8cfcc8
+        yield Result(plugin, constants.ERROR,
8cfcc8
+                     error=str(e),
8cfcc8
+                     msg='Retrieving %s agent from LDAP failed {error}'
8cfcc8
+                         % agent_type)
8cfcc8
+        return
8cfcc8
+    else:
8cfcc8
+        logger.debug('%s agent description is %s', agent_type, description)
8cfcc8
+        if len(entries) != 1:
8cfcc8
+            yield Result(plugin, constants.ERROR,
8cfcc8
+                         found=len(entries),
8cfcc8
+                         msg='Too many %s agent entries found, {found}'
8cfcc8
+                             % agent_type)
8cfcc8
+            return
8cfcc8
+        entry = entries[0]
8cfcc8
+        raw_desc = entry.get('description')
8cfcc8
+        if raw_desc is None:
8cfcc8
+            yield Result(plugin, constants.ERROR,
8cfcc8
+                         msg='%s agent is missing the description '
8cfcc8
+                             'attribute or it is not readable' % agent_type)
8cfcc8
+            return
8cfcc8
+        ra_desc = raw_desc[0]
8cfcc8
+        ra_certs = entry.get('usercertificate')
8cfcc8
+        if ra_desc != description:
8cfcc8
+            yield Result(plugin, constants.ERROR,
8cfcc8
+                         expected=description,
8cfcc8
+                         got=ra_desc,
8cfcc8
+                         msg='%s agent description does not match. Found '
8cfcc8
+                         '{got} in LDAP and expected {expected}' % agent_type)
8cfcc8
+            return
8cfcc8
+        found = False
8cfcc8
+        for candidate in ra_certs:
8cfcc8
+            if candidate == cert:
8cfcc8
+                found = True
8cfcc8
+                break
8cfcc8
+        if not found:
8cfcc8
+            yield Result(plugin, constants.ERROR,
8cfcc8
+                         certfile=paths.RA_AGENT_PEM,
8cfcc8
+                         dn=str(entry.dn),
8cfcc8
+                         msg='%s agent certificate in {certfile} not '
8cfcc8
+                             'found in LDAP userCertificate attribute '
8cfcc8
+                             'for the entry {dn}' % agent_type)
8cfcc8
+        yield Result(plugin, constants.SUCCESS)
8cfcc8
+
8cfcc8
+
8cfcc8
 @registry
8cfcc8
 class IPARAAgent(IPAPlugin):
8cfcc8
     """Validate the RA Agent used to talk to the CA
8cfcc8
@@ -739,82 +816,32 @@ class IPARAAgent(IPAPlugin):
8cfcc8
             logger.debug('CA is not configured, skipping RA Agent check')
8cfcc8
             return
8cfcc8
 
8cfcc8
-        try:
8cfcc8
-            cert = x509.load_certificate_from_file(paths.RA_AGENT_PEM)
8cfcc8
-        except Exception as e:
8cfcc8
-            yield Result(self, constants.ERROR,
8cfcc8
-                         error=str(e),
8cfcc8
-                         msg='Unable to load RA cert: {error}')
8cfcc8
-            return
8cfcc8
+        base_dn = DN('uid=ipara,ou=people,o=ipaca')
8cfcc8
+        yield from check_agent(self, base_dn, 'RA')
8cfcc8
 
8cfcc8
-        serial_number = cert.serial_number
8cfcc8
-        subject = DN(cert.subject)
8cfcc8
-        issuer = DN(cert.issuer)
8cfcc8
-        description = '2;%d;%s;%s' % (serial_number, issuer, subject)
8cfcc8
 
8cfcc8
-        logger.debug('RA agent description should be %s', description)
8cfcc8
+@registry
8cfcc8
+class IPAKRAAgent(IPAPlugin):
8cfcc8
+    """Validate the KRA Agent
8cfcc8
 
8cfcc8
-        db_filter = ldap2.ldap2.combine_filters(
8cfcc8
-            [
8cfcc8
-                ldap2.ldap2.make_filter({'objectClass': 'inetOrgPerson'}),
8cfcc8
-                ldap2.ldap2.make_filter({'sn': 'ipara'}),
8cfcc8
-                ldap2.ldap2.make_filter(
8cfcc8
-                    {'description': ';%s;%s' % (issuer, subject)},
8cfcc8
-                    exact=False, trailing_wildcard=False),
8cfcc8
-            ],
8cfcc8
-            ldap2.ldap2.MATCH_ALL)
8cfcc8
+       Compare the description and usercertificate values.
8cfcc8
+    """
8cfcc8
 
8cfcc8
-        base_dn = DN(('o', 'ipaca'))
8cfcc8
-        try:
8cfcc8
-            entries = self.conn.get_entries(base_dn,
8cfcc8
-                                            self.conn.SCOPE_SUBTREE,
8cfcc8
-                                            db_filter)
8cfcc8
-        except errors.NotFound:
8cfcc8
-            yield Result(self, constants.ERROR,
8cfcc8
-                         description=description,
8cfcc8
-                         msg='RA agent not found in LDAP')
8cfcc8
+    requires = ('dirsrv',)
8cfcc8
+
8cfcc8
+    @duration
8cfcc8
+    def check(self):
8cfcc8
+        if not self.ca.is_configured():
8cfcc8
+            logger.debug('CA is not configured, skipping KRA Agent check')
8cfcc8
             return
8cfcc8
-        except Exception as e:
8cfcc8
-            yield Result(self, constants.ERROR,
8cfcc8
-                         error=str(e),
8cfcc8
-                         msg='Retrieving RA agent from LDAP failed {error}')
8cfcc8
+
8cfcc8
+        kra = krainstance.KRAInstance(api.env.realm)
8cfcc8
+        if not kra.is_installed():
8cfcc8
+            logger.debug('KRA is not installed, skipping KRA Agent check')
8cfcc8
             return
8cfcc8
-        else:
8cfcc8
-            logger.debug('RA agent description is %s', description)
8cfcc8
-            if len(entries) != 1:
8cfcc8
-                yield Result(self, constants.ERROR,
8cfcc8
-                             found=len(entries),
8cfcc8
-                             msg='Too many RA agent entries found, {found}')
8cfcc8
-                return
8cfcc8
-            entry = entries[0]
8cfcc8
-            raw_desc = entry.get('description')
8cfcc8
-            if raw_desc is None:
8cfcc8
-                yield Result(self, constants.ERROR,
8cfcc8
-                             msg='RA agent is missing the description '
8cfcc8
-                                 'attribute or it is not readable')
8cfcc8
-                return
8cfcc8
-            ra_desc = raw_desc[0]
8cfcc8
-            ra_certs = entry.get('usercertificate')
8cfcc8
-            if ra_desc != description:
8cfcc8
-                yield Result(self, constants.ERROR,
8cfcc8
-                             expected=description,
8cfcc8
-                             got=ra_desc,
8cfcc8
-                             msg='RA agent description does not match. Found '
8cfcc8
-                             '{got} in LDAP and expected {expected}')
8cfcc8
-                return
8cfcc8
-            found = False
8cfcc8
-            for candidate in ra_certs:
8cfcc8
-                if candidate == cert:
8cfcc8
-                    found = True
8cfcc8
-                    break
8cfcc8
-            if not found:
8cfcc8
-                yield Result(self, constants.ERROR,
8cfcc8
-                             certfile=paths.RA_AGENT_PEM,
8cfcc8
-                             dn=str(entry.dn),
8cfcc8
-                             msg='RA agent certificate in {certfile} not '
8cfcc8
-                                 'found in LDAP userCertificate attribute '
8cfcc8
-                                 'for the entry {dn}')
8cfcc8
-            yield Result(self, constants.SUCCESS)
8cfcc8
+
8cfcc8
+        base_dn = DN('uid=ipakra,ou=people,o=kra,o=ipaca')
8cfcc8
+        yield from check_agent(self, base_dn, 'KRA')
8cfcc8
 
8cfcc8
 
8cfcc8
 @registry
8cfcc8
-- 
8cfcc8
2.26.2
8cfcc8