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

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