From 08b768313020c45bfa82d67cd214afabf605f4b3 Mon Sep 17 00:00:00 2001 From: Fraser Tweedale Date: Tue, 23 Aug 2016 16:14:30 +1000 Subject: [PATCH] Track lightweight CAs on replica installation Add Certmonger tracking requests for lightweight CAs on replica installation. As part of this change, extract most of the lightweight CA tracking code out of ipa-certupdate and into cainstance. Fixes: https://fedorahosted.org/freeipa/ticket/6019 Reviewed-By: Martin Babinsky --- ipaclient/ipa_certupdate.py | 53 +++++--------------------- ipalib/constants.py | 2 + ipaserver/install/cainstance.py | 66 +++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 44 deletions(-) diff --git a/ipaclient/ipa_certupdate.py b/ipaclient/ipa_certupdate.py index f340f32bc..4b97b85b8 100644 --- a/ipaclient/ipa_certupdate.py +++ b/ipaclient/ipa_certupdate.py @@ -29,10 +29,8 @@ from ipaplatform import services from ipaplatform.paths import paths from ipaplatform.tasks import tasks from ipalib import api, errors, x509, certstore -from ipalib.constants import IPA_CA_CN +from ipalib.constants import IPA_CA_NICKNAME, RENEWAL_CA_NAME -IPA_CA_NICKNAME = 'caSigningCert cert-pki-ca' -RENEWAL_CA_NAME = 'dogtag-ipa-ca-renew-agent' class CertUpdate(admintool.AdminTool): command_name = 'ipa-certupdate' @@ -85,12 +83,7 @@ class CertUpdate(admintool.AdminTool): certs = certstore.get_ca_certs(ldap, api.env.basedn, api.env.realm, ca_enabled) - # find lightweight CAs (on renewal master only) - lwcas = [] - if ca_enabled: - for ca_obj in api.Command.ca_find()['result']: - if IPA_CA_CN not in ca_obj['cn']: - lwcas.append(ca_obj) + lwcas = api.Command.ca_find()['result'] api.Backend.rpcclient.disconnect() finally: @@ -99,8 +92,13 @@ class CertUpdate(admintool.AdminTool): server_fstore = sysrestore.FileStore(paths.SYSRESTORE) if server_fstore.has_files(): self.update_server(certs) - for entry in lwcas: - self.server_track_lightweight_ca(entry) + try: + from ipaserver.install import cainstance + cainstance.add_lightweight_ca_tracking_requests( + self.log, lwcas) + except Exception as e: + self.log.exception( + "Failed to add lightweight CA tracking requests") self.update_client(certs) @@ -164,39 +162,6 @@ class CertUpdate(admintool.AdminTool): self.update_file(paths.CA_CRT, certs) - def server_track_lightweight_ca(self, entry): - nickname = "{} {}".format(IPA_CA_NICKNAME, entry['ipacaid'][0]) - criteria = { - 'cert-database': paths.PKI_TOMCAT_ALIAS_DIR, - 'cert-nickname': nickname, - 'ca-name': RENEWAL_CA_NAME, - } - request_id = certmonger.get_request_id(criteria) - if request_id is None: - try: - certmonger.dogtag_start_tracking( - secdir=paths.PKI_TOMCAT_ALIAS_DIR, - pin=certmonger.get_pin('internal'), - pinfile=None, - nickname=nickname, - ca=RENEWAL_CA_NAME, - pre_command='stop_pkicad', - post_command='renew_ca_cert "%s"' % nickname, - ) - request_id = certmonger.get_request_id(criteria) - certmonger.modify(request_id, profile='ipaCACertRenewal') - self.log.debug( - 'Lightweight CA renewal: ' - 'added tracking request for "%s"', nickname) - except RuntimeError as e: - self.log.error( - 'Lightweight CA renewal: Certmonger failed to ' - 'start tracking certificate: %s', e) - else: - self.log.debug( - 'Lightweight CA renewal: ' - 'already tracking certificate "%s"', nickname) - def update_file(self, filename, certs, mode=0o444): certs = (c[0] for c in certs if c[2] is not False) try: diff --git a/ipalib/constants.py b/ipalib/constants.py index 9b351e260..04515dcd2 100644 --- a/ipalib/constants.py +++ b/ipalib/constants.py @@ -274,3 +274,5 @@ CA_SUFFIX_NAME = 'ca' PKI_GSSAPI_SERVICE_NAME = 'dogtag' IPA_CA_CN = u'ipa' IPA_CA_RECORD = "ipa-ca" +IPA_CA_NICKNAME = 'caSigningCert cert-pki-ca' +RENEWAL_CA_NAME = 'dogtag-ipa-ca-renew-agent' diff --git a/ipaserver/install/cainstance.py b/ipaserver/install/cainstance.py index c4b8e9ae3..ab006be8f 100644 --- a/ipaserver/install/cainstance.py +++ b/ipaserver/install/cainstance.py @@ -1383,6 +1383,9 @@ class CAInstance(DogtagInstance): self.step("enabling CA instance", self.__enable_instance) + self.step("configuring certmonger renewal for lightweight CAs", + self.__add_lightweight_ca_tracking_requests) + self.start_creation(runtime=210) def setup_lightweight_ca_key_retrieval(self): @@ -1448,6 +1451,22 @@ class CAInstance(DogtagInstance): os.chmod(keyfile, 0o600) os.chown(keyfile, pent.pw_uid, pent.pw_gid) + def __add_lightweight_ca_tracking_requests(self): + if not self.admin_conn: + self.ldap_connect() + + try: + lwcas = self.admin_conn.get_entries( + base_dn=api.env.basedn, + filter='(objectclass=ipaca)', + attrs_list=['cn', 'ipacaid'], + ) + add_lightweight_ca_tracking_requests(self.log, lwcas) + except errors.NotFound: + # shouldn't happen, but don't fail if it does + root_logger.warning( + "Did not find any lightweight CAs; nothing to track") + def replica_ca_install_check(config): if not config.setup_ca: @@ -2070,6 +2089,53 @@ def ensure_default_caacl(): api.Backend.ldap2.disconnect() +def add_lightweight_ca_tracking_requests(logger, lwcas): + """Add tracking requests for the given lightweight CAs. + + The entries must have the 'cn' and 'ipacaid' attributes. + + The IPA CA, if present, is skipped. + + """ + for entry in lwcas: + if ipalib.constants.IPA_CA_CN in entry['cn']: + continue + + nickname = "{} {}".format( + ipalib.constants.IPA_CA_NICKNAME, + entry['ipacaid'][0]) + criteria = { + 'cert-database': paths.PKI_TOMCAT_ALIAS_DIR, + 'cert-nickname': nickname, + 'ca-name': ipalib.constants.RENEWAL_CA_NAME, + } + request_id = certmonger.get_request_id(criteria) + if request_id is None: + try: + certmonger.dogtag_start_tracking( + secdir=paths.PKI_TOMCAT_ALIAS_DIR, + pin=certmonger.get_pin('internal'), + pinfile=None, + nickname=nickname, + ca=ipalib.constants.RENEWAL_CA_NAME, + pre_command='stop_pkicad', + post_command='renew_ca_cert "%s"' % nickname, + ) + request_id = certmonger.get_request_id(criteria) + certmonger.modify(request_id, profile='ipaCACertRenewal') + logger.debug( + 'Lightweight CA renewal: ' + 'added tracking request for "%s"', nickname) + except RuntimeError as e: + logger.error( + 'Lightweight CA renewal: Certmonger failed to ' + 'start tracking certificate: %s', e) + else: + logger.debug( + 'Lightweight CA renewal: ' + 'already tracking certificate "%s"', nickname) + + def update_ipa_conf(): """ Update IPA configuration file to ensure that RA plugins are enabled and