3f51ca
From 6049b791567dad33be050b05bb08ef2040f473ee Mon Sep 17 00:00:00 2001
3f51ca
From: Rob Crittenden <rcritten@redhat.com>
3f51ca
Date: Tue, 24 Oct 2017 15:43:08 -0400
3f51ca
Subject: [PATCH] Fix cert-find for CA-less installations
3f51ca
3f51ca
Change 49f9d799c171c7ae2ac546a33a353c2c40b4719c deferred the
3f51ca
detailed lookup until all certs were collected but introduced
3f51ca
a bug where the ra backend was always retrieved. This generated a
3f51ca
backtrace in a CA-less install because there is no ra backend in
3f51ca
the CA-less case.
3f51ca
3f51ca
The deferral also removes the certificate value from the LDAP
3f51ca
search output resulting in only the serial number being displayed
3f51ca
unless --all is provided. Add a new class variable,
3f51ca
self.ca_enabled, to add an exception for the CA-less case.
3f51ca
3f51ca
Fixes https://pagure.io/freeipa/issue/7202
3f51ca
3f51ca
Signed-off-by: Rob Crittenden <rcritten@redhat.com>
3f51ca
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
3f51ca
---
3f51ca
 ipaserver/plugins/cert.py | 22 ++++++++++++++++++++--
3f51ca
 1 file changed, 20 insertions(+), 2 deletions(-)
3f51ca
3f51ca
diff --git a/ipaserver/plugins/cert.py b/ipaserver/plugins/cert.py
3f51ca
index bb11713317abad55577b1c280253ab5d6d68c508..c1d389217265f44e646ac27d9adc8d5524c74ce7 100644
3f51ca
--- a/ipaserver/plugins/cert.py
3f51ca
+++ b/ipaserver/plugins/cert.py
3f51ca
@@ -1453,6 +1453,7 @@ class cert_find(Search, CertMethod):
3f51ca
 
3f51ca
             truncated = bool(truncated)
3f51ca
 
3f51ca
+        ca_enabled = getattr(context, 'ca_enabled')
3f51ca
         for entry in entries:
3f51ca
             for attr in ('usercertificate', 'usercertificate;binary'):
3f51ca
                 for cert in entry.get(attr, []):
3f51ca
@@ -1466,7 +1467,12 @@ class cert_find(Search, CertMethod):
3f51ca
                         obj = result[issuer, serial_number]
3f51ca
                     except KeyError:
3f51ca
                         obj = {'serial_number': serial_number}
3f51ca
-                        if not pkey_only and all:
3f51ca
+                        if not pkey_only and (all or not ca_enabled):
3f51ca
+                            # Retrieving certificate details is now deferred
3f51ca
+                            # until after all certificates are collected.
3f51ca
+                            # For the case of CA-less we need to keep
3f51ca
+                            # the certificate because getting it again later
3f51ca
+                            # would require unnecessary LDAP searches.
3f51ca
                             obj['certificate'] = (
3f51ca
                                 base64.b64encode(cert).decode('ascii'))
3f51ca
                         result[issuer, serial_number] = obj
3f51ca
@@ -1480,6 +1486,11 @@ class cert_find(Search, CertMethod):
3f51ca
 
3f51ca
     def execute(self, criteria=None, all=False, raw=False, pkey_only=False,
3f51ca
                 no_members=True, timelimit=None, sizelimit=None, **options):
3f51ca
+        # Store ca_enabled status in the context to save making the API
3f51ca
+        # call multiple times.
3f51ca
+        ca_enabled = self.api.Command.ca_is_enabled()['result']
3f51ca
+        setattr(context, 'ca_enabled', ca_enabled)
3f51ca
+
3f51ca
         if 'cacn' in options:
3f51ca
             ca_obj = api.Command.ca_show(options['cacn'])['result']
3f51ca
             ca_sdn = unicode(ca_obj['ipacasubjectdn'][0])
3f51ca
@@ -1534,7 +1545,8 @@ class cert_find(Search, CertMethod):
3f51ca
 
3f51ca
         if not pkey_only:
3f51ca
             ca_objs = {}
3f51ca
-            ra = self.api.Backend.ra
3f51ca
+            if ca_enabled:
3f51ca
+                ra = self.api.Backend.ra
3f51ca
 
3f51ca
             for key, obj in six.iteritems(result):
3f51ca
                 if all and 'cacn' in obj:
3f51ca
@@ -1561,6 +1573,12 @@ class cert_find(Search, CertMethod):
3f51ca
 
3f51ca
                 if not raw:
3f51ca
                     self.obj._parse(obj, all)
3f51ca
+                    if not ca_enabled and not all:
3f51ca
+                        # For the case of CA-less don't display the full
3f51ca
+                        # certificate unless requested. It is kept in the
3f51ca
+                        # entry from _ldap_search() so its attributes can
3f51ca
+                        # be retrieved.
3f51ca
+                        obj.pop('certificate', None)
3f51ca
                     self.obj._fill_owners(obj)
3f51ca
 
3f51ca
         result = list(six.itervalues(result))
3f51ca
-- 
3f51ca
2.13.6
3f51ca