483b06
From 2409b5204101cceafb28289db0d99c1474ee2430 Mon Sep 17 00:00:00 2001
483b06
From: Jan Cholasta <jcholast@redhat.com>
483b06
Date: Fri, 7 Apr 2017 07:43:09 +0200
483b06
Subject: [PATCH] dsinstance, httpinstance: consolidate certificate request
483b06
 code
483b06
483b06
A different code path is used for DS and httpd certificate requests in
483b06
replica promotion. This is rather unnecessary and makes the certificate
483b06
request code not easy to follow.
483b06
483b06
Consolidate the non-promotion and promotion code paths into one.
483b06
483b06
https://pagure.io/freeipa/issue/6757
483b06
483b06
Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
483b06
---
483b06
 ipaserver/install/dsinstance.py            | 76 +++++++++---------------------
483b06
 ipaserver/install/httpinstance.py          | 40 ++++++++--------
483b06
 ipaserver/install/server/install.py        |  4 --
483b06
 ipaserver/install/server/replicainstall.py | 22 +--------
483b06
 4 files changed, 43 insertions(+), 99 deletions(-)
483b06
483b06
diff --git a/ipaserver/install/dsinstance.py b/ipaserver/install/dsinstance.py
483b06
index fb5f925de8e658dca9370714413012527f00c39d..31dbd4ec8bcaf4a7545b4f9f316fe609b845cb75 100644
483b06
--- a/ipaserver/install/dsinstance.py
483b06
+++ b/ipaserver/install/dsinstance.py
483b06
@@ -396,10 +396,7 @@ class DsInstance(service.Service):
483b06
 
483b06
         self.step("creating DS keytab", self.request_service_keytab)
483b06
         if self.promote:
483b06
-            if self.pkcs12_info:
483b06
-                self.step("configuring TLS for DS instance", self.__enable_ssl)
483b06
-            else:
483b06
-                self.step("retrieving DS Certificate", self.__get_ds_cert)
483b06
+            self.step("configuring TLS for DS instance", self.__enable_ssl)
483b06
             self.step("restarting directory server", self.__restart_instance)
483b06
 
483b06
         self.step("setting up initial replication", self.__setup_replica)
483b06
@@ -810,18 +807,23 @@ class DsInstance(service.Service):
483b06
                 dsdb.track_server_cert(
483b06
                     self.nickname, self.principal, dsdb.passwd_fname,
483b06
                     'restart_dirsrv %s' % self.serverid)
483b06
+
483b06
+            self.add_cert_to_service()
483b06
         else:
483b06
             dsdb.create_from_cacert()
483b06
-            ca_args = [
483b06
-                paths.CERTMONGER_DOGTAG_SUBMIT,
483b06
-                '--ee-url', 'https://%s:8443/ca/ee/ca' % self.fqdn,
483b06
-                '--certfile', paths.RA_AGENT_PEM,
483b06
-                '--keyfile', paths.RA_AGENT_KEY,
483b06
-                '--cafile', paths.IPA_CA_CRT,
483b06
-                '--agent-submit'
483b06
-            ]
483b06
-            helper = " ".join(ca_args)
483b06
-            prev_helper = certmonger.modify_ca_helper('IPA', helper)
483b06
+            if self.master_fqdn is None:
483b06
+                ca_args = [
483b06
+                    paths.CERTMONGER_DOGTAG_SUBMIT,
483b06
+                    '--ee-url', 'https://%s:8443/ca/ee/ca' % self.fqdn,
483b06
+                    '--certfile', paths.RA_AGENT_PEM,
483b06
+                    '--keyfile', paths.RA_AGENT_KEY,
483b06
+                    '--cafile', paths.IPA_CA_CRT,
483b06
+                    '--agent-submit'
483b06
+                ]
483b06
+                helper = " ".join(ca_args)
483b06
+                prev_helper = certmonger.modify_ca_helper('IPA', helper)
483b06
+            else:
483b06
+                prev_helper = None
483b06
             try:
483b06
                 cmd = 'restart_dirsrv %s' % self.serverid
483b06
                 certmonger.request_and_wait_for_cert(
483b06
@@ -835,7 +837,8 @@ class DsInstance(service.Service):
483b06
                     dns=[self.fqdn],
483b06
                     post_command=cmd)
483b06
             finally:
483b06
-                certmonger.modify_ca_helper('IPA', prev_helper)
483b06
+                if prev_helper is not None:
483b06
+                    certmonger.modify_ca_helper('IPA', prev_helper)
483b06
 
483b06
             # restart_dirsrv in the request above restarts DS, reconnect ldap2
483b06
             api.Backend.ldap2.disconnect()
483b06
@@ -843,6 +846,9 @@ class DsInstance(service.Service):
483b06
 
483b06
             self.dercert = dsdb.get_cert_from_db(self.nickname, pem=False)
483b06
 
483b06
+            if prev_helper is not None:
483b06
+                self.add_cert_to_service()
483b06
+
483b06
         dsdb.create_pin_file()
483b06
 
483b06
         self.cacert_name = dsdb.cacert_name
483b06
@@ -1236,46 +1242,6 @@ class DsInstance(service.Service):
483b06
         ipautil.config_replace_variables(paths.SYSCONFIG_DIRSRV,
483b06
                                          replacevars=vardict)
483b06
 
483b06
-    def __get_ds_cert(self):
483b06
-        nssdb_dir = config_dirname(self.serverid)
483b06
-        db = certs.CertDB(
483b06
-            self.realm,
483b06
-            nssdir=nssdb_dir,
483b06
-            subject_base=self.subject_base,
483b06
-            ca_subject=self.ca_subject,
483b06
-        )
483b06
-        db.create_from_cacert()
483b06
-        db.request_service_cert(self.nickname, self.principal, self.fqdn)
483b06
-        db.create_pin_file()
483b06
-
483b06
-        # Connect to self over ldapi as Directory Manager and configure SSL
483b06
-        ldap_uri = ipaldap.get_ldap_uri(protocol='ldapi', realm=self.realm)
483b06
-        conn = ipaldap.LDAPClient(ldap_uri)
483b06
-        conn.external_bind()
483b06
-
483b06
-        mod = [(ldap.MOD_REPLACE, "nsSSLClientAuth", "allowed"),
483b06
-               (ldap.MOD_REPLACE, "nsSSL3Ciphers", "default"),
483b06
-               (ldap.MOD_REPLACE, "allowWeakCipher", "off")]
483b06
-        conn.modify_s(DN(('cn', 'encryption'), ('cn', 'config')), mod)
483b06
-
483b06
-        mod = [(ldap.MOD_ADD, "nsslapd-security", "on")]
483b06
-        conn.modify_s(DN(('cn', 'config')), mod)
483b06
-
483b06
-        entry = conn.make_entry(
483b06
-            DN(('cn', 'RSA'), ('cn', 'encryption'), ('cn', 'config')),
483b06
-            objectclass=["top", "nsEncryptionModule"],
483b06
-            cn=["RSA"],
483b06
-            nsSSLPersonalitySSL=[self.nickname],
483b06
-            nsSSLToken=["internal (software)"],
483b06
-            nsSSLActivation=["on"],
483b06
-        )
483b06
-        conn.add_entry(entry)
483b06
-
483b06
-        conn.unbind()
483b06
-
483b06
-        # check for open secure port 636 from now on
483b06
-        self.open_ports.append(636)
483b06
-
483b06
 
483b06
 def write_certmap_conf(realm, ca_subject):
483b06
     """(Re)write certmap.conf with given CA subject DN."""
483b06
diff --git a/ipaserver/install/httpinstance.py b/ipaserver/install/httpinstance.py
483b06
index d7cd776ab9831b5408797ae41b7c7fbb10707b18..45bf479d1088c3b3396d955bf2592c4bce1e886f 100644
483b06
--- a/ipaserver/install/httpinstance.py
483b06
+++ b/ipaserver/install/httpinstance.py
483b06
@@ -376,12 +376,12 @@ class HTTPInstance(service.Service):
483b06
         return False
483b06
 
483b06
     def __setup_ssl(self):
483b06
-        truncate = not self.promote or not self.ca_is_configured
483b06
         db = certs.CertDB(self.realm, nssdir=paths.HTTPD_ALIAS_DIR,
483b06
                           subject_base=self.subject_base, user="root",
483b06
                           group=constants.HTTPD_GROUP,
483b06
-                          truncate=truncate)
483b06
+                          truncate=True)
483b06
         self.disable_system_trust()
483b06
+        self.create_password_conf()
483b06
         if self.pkcs12_info:
483b06
             if self.ca_is_configured:
483b06
                 trust_flags = 'CT,C,C'
483b06
@@ -394,8 +394,6 @@ class HTTPInstance(service.Service):
483b06
             if len(server_certs) == 0:
483b06
                 raise RuntimeError("Could not find a suitable server cert in import in %s" % self.pkcs12_info[0])
483b06
 
483b06
-            self.create_password_conf()
483b06
-
483b06
             # We only handle one server cert
483b06
             nickname = server_certs[0][0]
483b06
             if nickname == 'ipaCert':
483b06
@@ -410,7 +408,6 @@ class HTTPInstance(service.Service):
483b06
 
483b06
         else:
483b06
             if not self.promote:
483b06
-                self.create_password_conf()
483b06
                 ca_args = [
483b06
                     paths.CERTMONGER_DOGTAG_SUBMIT,
483b06
                     '--ee-url', 'https://%s:8443/ca/ee/ca' % self.fqdn,
483b06
@@ -421,23 +418,26 @@ class HTTPInstance(service.Service):
483b06
                 ]
483b06
                 helper = " ".join(ca_args)
483b06
                 prev_helper = certmonger.modify_ca_helper('IPA', helper)
483b06
-
483b06
-                try:
483b06
-                    certmonger.request_and_wait_for_cert(
483b06
-                        certpath=db.secdir,
483b06
-                        nickname=self.cert_nickname,
483b06
-                        principal=self.principal,
483b06
-                        passwd_fname=db.passwd_fname,
483b06
-                        subject=str(DN(('CN', self.fqdn), self.subject_base)),
483b06
-                        ca='IPA',
483b06
-                        profile=dogtag.DEFAULT_PROFILE,
483b06
-                        dns=[self.fqdn],
483b06
-                        post_command='restart_httpd')
483b06
-                    self.dercert = db.get_cert_from_db(
483b06
-                        self.cert_nickname, pem=False)
483b06
-                finally:
483b06
+            else:
483b06
+                prev_helper = None
483b06
+            try:
483b06
+                certmonger.request_and_wait_for_cert(
483b06
+                    certpath=db.secdir,
483b06
+                    nickname=self.cert_nickname,
483b06
+                    principal=self.principal,
483b06
+                    passwd_fname=db.passwd_fname,
483b06
+                    subject=str(DN(('CN', self.fqdn), self.subject_base)),
483b06
+                    ca='IPA',
483b06
+                    profile=dogtag.DEFAULT_PROFILE,
483b06
+                    dns=[self.fqdn],
483b06
+                    post_command='restart_httpd')
483b06
+            finally:
483b06
+                if prev_helper is not None:
483b06
                     certmonger.modify_ca_helper('IPA', prev_helper)
483b06
 
483b06
+            self.dercert = db.get_cert_from_db(self.cert_nickname, pem=False)
483b06
+
483b06
+            if prev_helper is not None:
483b06
                 self.add_cert_to_service()
483b06
 
483b06
             # Verify we have a valid server cert
483b06
diff --git a/ipaserver/install/server/install.py b/ipaserver/install/server/install.py
483b06
index d7eb0bfacd0815026c82f59d76962f527e2b7dad..f8e64ec26e85bbc6218018eec8f403a0567b45a2 100644
483b06
--- a/ipaserver/install/server/install.py
483b06
+++ b/ipaserver/install/server/install.py
483b06
@@ -807,10 +807,6 @@ def install(installer):
483b06
     if setup_ca:
483b06
         ca.install_step_1(False, None, options)
483b06
 
483b06
-    # The DS instance is created before the keytab, add the SSL cert we
483b06
-    # generated
483b06
-    ds.add_cert_to_service()
483b06
-
483b06
     otpd = otpdinstance.OtpdInstance()
483b06
     otpd.create_instance('OTPD', host_name,
483b06
                          ipautil.realm_to_suffix(realm_name))
483b06
diff --git a/ipaserver/install/server/replicainstall.py b/ipaserver/install/server/replicainstall.py
483b06
index f489e691999fd9d6e82879341922510e56eac47d..cd6a62f9540f4a46da70e0cc5686eff5f54e7dfe 100644
483b06
--- a/ipaserver/install/server/replicainstall.py
483b06
+++ b/ipaserver/install/server/replicainstall.py
483b06
@@ -27,7 +27,6 @@ from ipapython.dn import DN
483b06
 from ipapython.ipa_log_manager import root_logger
483b06
 from ipapython.admintool import ScriptError
483b06
 from ipaplatform import services
483b06
-from ipaplatform.constants import constants as pconstants
483b06
 from ipaplatform.tasks import tasks
483b06
 from ipaplatform.paths import paths
483b06
 from ipalib import api, constants, create_api, errors, rpc, x509
483b06
@@ -77,18 +76,6 @@ def make_pkcs12_info(directory, cert_name, password_name):
483b06
         return None
483b06
 
483b06
 
483b06
-def install_http_certs(host_name, realm_name, subject_base):
483b06
-    principal = 'HTTP/%s@%s' % (host_name, realm_name)
483b06
-    subject = subject_base or DN(('O', realm_name))
483b06
-    db = certs.CertDB(realm_name, nssdir=paths.HTTPD_ALIAS_DIR,
483b06
-                      subject_base=subject, user="root",
483b06
-                      group=pconstants.HTTPD_GROUP, truncate=True)
483b06
-    db.request_service_cert('Server-Cert', principal, host_name)
483b06
-    # Obtain certificate for the HTTP service
483b06
-    http = httpinstance.HTTPInstance()
483b06
-    http.create_password_conf()
483b06
-
483b06
-
483b06
 def install_replica_ds(config, options, ca_is_configured, remote_api,
483b06
                        ca_file, promote=False, pkcs12_info=None):
483b06
     dsinstance.check_ports()
483b06
@@ -175,7 +162,8 @@ def install_http(config, auto_redirect, ca_is_configured, ca_file,
483b06
     http.create_instance(
483b06
         config.realm_name, config.host_name, config.domain_name,
483b06
         pkcs12_info, auto_redirect=auto_redirect, ca_file=ca_file,
483b06
-        ca_is_configured=ca_is_configured, promote=promote)
483b06
+        ca_is_configured=ca_is_configured, promote=promote,
483b06
+        subject_base=config.subject_base)
483b06
 
483b06
     return http
483b06
 
483b06
@@ -1414,12 +1402,6 @@ def install(installer):
483b06
         # Always try to install DNS records
483b06
         install_dns_records(config, options, remote_api)
483b06
 
483b06
-        if promote and ca_enabled:
483b06
-            # we need to install http certs to setup ssl for httpd
483b06
-            install_http_certs(config.host_name,
483b06
-                               config.realm_name,
483b06
-                               config.subject_base)
483b06
-
483b06
         ntpinstance.ntp_ldap_enable(config.host_name, ds.suffix,
483b06
                                     remote_api.env.realm)
483b06
     finally:
483b06
-- 
483b06
2.9.3
483b06