Blame SOURCES/0007-Don-t-always-override-the-port-in-import_included_profiles_rhbz#2022483.patch

07426e
From edb216849e4f47d6cae95981edf0c3fe2653fd7a Mon Sep 17 00:00:00 2001
07426e
From: Rob Crittenden <rcritten@redhat.com>
07426e
Date: Fri, 28 Jan 2022 16:46:35 -0500
07426e
Subject: [PATCH] Don't always override the port in import_included_profiles
07426e
07426e
I can only guess to the original purpose of this override. I
07426e
believe it was because this is called in the installer prior
07426e
to Apache being set up. The expectation was that this would
07426e
only be called locally. It predates the RestClient class.
07426e
07426e
RestClient will attempt to find an available service. In this
07426e
case, during a CA installation, the local server is not
07426e
considered available because it lacks an entry in
07426e
cn=masters. So it will never be returned as an option.
07426e
07426e
So by overriding the port to 8443 the remote connection will
07426e
likely fail because we don't require that the port be open.
07426e
07426e
So instead, instantiate a RestClient and see what happens.
07426e
07426e
There are several use-cases:
07426e
07426e
1. Installing an initial server. The RestClient connection
07426e
   should fail, so we will fall back to the override port and
07426e
   use the local server. If Apache happens to be running with
07426e
   a globally-issued certificate then the RestClient will
07426e
   succeed. In this case if the connected host and the local
07426e
   hostname are the same, override in that case as well.
07426e
07426e
2. Installing as a replica. In this case the local server should
07426e
   be ignored in all cases and a remote CA will be picked with
07426e
   no override done.
07426e
07426e
3. Switching from CA-less to CA-ful. The web server will be
07426e
   trusted but the RestClient login will fail with a 404. Fall
07426e
   back to the override port in this case.
07426e
07426e
The motivation for this is trying to install an EL 8.x replica
07426e
against an EL 7.9 server. 8.5+ includes the ACME service and
07426e
a new profile is needed which doesn't exist in 7. This was
07426e
failing because the RestClient determined that the local server
07426e
wasn't running a CA so tried the remote one (7.9) on the override
07426e
port 8443. Since this port isn't open: failure.
07426e
07426e
Chances are that adding the profile is still going to fail
07426e
because again, 7.9 lacks ACME capabilities, but it will fail in
07426e
a way that allows the installation to continue.
07426e
07426e
I suspect that all of the overrides can similarly handled, or
07426e
handled directly within the RestClient class, but for the sake
07426e
of "do no harm" I'm only changing this instance for now.
07426e
07426e
https://pagure.io/freeipa/issue/9100
07426e
07426e
Signed-off-by: Rob Crittenden <rcritten@redhat.com>
07426e
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
07426e
---
07426e
 ipaserver/install/cainstance.py | 30 +++++++++++++++++++++++++++++-
07426e
 1 file changed, 29 insertions(+), 1 deletion(-)
07426e
07426e
diff --git a/ipaserver/install/cainstance.py b/ipaserver/install/cainstance.py
07426e
index 8c8bf1b3a..ad206aad4 100644
07426e
--- a/ipaserver/install/cainstance.py
07426e
+++ b/ipaserver/install/cainstance.py
07426e
@@ -1953,7 +1953,35 @@ def import_included_profiles():
07426e
         cn=['certprofiles'],
07426e
     )
07426e
 
07426e
-    api.Backend.ra_certprofile.override_port = 8443
07426e
+    # At this point Apache may or may not be running with a valid
07426e
+    # certificate. The local server is not yet recognized as a full
07426e
+    # CA yet so it isn't discoverable. So try to do some detection
07426e
+    # on what port to use, 443 (remote) or 8443 (local) for importing
07426e
+    # the profiles.
07426e
+    #
07426e
+    # api.Backend.ra_certprofile invokes the RestClient class
07426e
+    # which will discover and login to the CA REST API. We can
07426e
+    # use this information to detect where to import the profiles.
07426e
+    #
07426e
+    # If the login is successful (e.g. doesn't raise an exception)
07426e
+    # and it returns our hostname (it prefers the local host) then
07426e
+    # we override and talk locally.
07426e
+    #
07426e
+    # Otherwise a NetworkError means we can't connect on 443 (perhaps
07426e
+    # a firewall) or we get an HTTP error (valid TLS certificate on
07426e
+    # Apache but no CA, login fails with 404) so we override to the
07426e
+    # local server.
07426e
+    #
07426e
+    # When override port was always set to 8443 the RestClient could
07426e
+    # pick a remote server and since 8443 isn't in our firewall profile
07426e
+    # setting up a new server would fail.
07426e
+    try:
07426e
+        with api.Backend.ra_certprofile as profile_api:
07426e
+            if profile_api.ca_host == api.env.host:
07426e
+                api.Backend.ra_certprofile.override_port = 8443
07426e
+    except (errors.NetworkError, errors.RemoteRetrieveError) as e:
07426e
+        logger.debug('Overriding CA port: %s', e)
07426e
+        api.Backend.ra_certprofile.override_port = 8443
07426e
 
07426e
     for (profile_id, desc, store_issued) in dogtag.INCLUDED_PROFILES:
07426e
         dn = DN(('cn', profile_id),
07426e
-- 
07426e
2.34.1
07426e