2014-03-18 10:23:30 -05:00
|
|
|
# Authors: Ade Lee <alee@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2018-04-05 02:21:16 -05:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-05-23 11:35:57 -05:00
|
|
|
import logging
|
2014-03-18 10:23:30 -05:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
2017-07-03 10:10:34 -05:00
|
|
|
import base64
|
2015-09-14 07:03:58 -05:00
|
|
|
|
2014-03-18 10:23:30 -05:00
|
|
|
from ipalib import api
|
2015-08-26 23:44:29 -05:00
|
|
|
from ipalib import x509
|
2014-03-18 10:23:30 -05:00
|
|
|
from ipaplatform.paths import paths
|
2018-05-23 03:37:58 -05:00
|
|
|
from ipapython import directivesetter
|
2014-03-18 10:23:30 -05:00
|
|
|
from ipapython import ipautil
|
|
|
|
from ipapython.dn import DN
|
|
|
|
from ipaserver.install import cainstance
|
2015-08-26 23:44:29 -05:00
|
|
|
from ipaserver.install import installutils
|
2017-01-02 10:00:00 -06:00
|
|
|
from ipaserver.install.dogtaginstance import DogtagInstance
|
2020-10-09 03:11:53 -05:00
|
|
|
|
2017-05-23 11:35:57 -05:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2014-03-18 10:23:30 -05:00
|
|
|
|
|
|
|
# When IPA is installed with DNS support, this CNAME should hold all IPA
|
|
|
|
# replicas with KRA configured
|
|
|
|
IPA_KRA_RECORD = "ipa-kra"
|
|
|
|
|
2015-08-25 14:42:25 -05:00
|
|
|
ADMIN_GROUPS = [
|
|
|
|
'Enterprise CA Administrators',
|
|
|
|
'Enterprise KRA Administrators',
|
|
|
|
'Security Domain Administrators'
|
|
|
|
]
|
2014-03-18 10:23:30 -05:00
|
|
|
|
2019-10-10 23:56:10 -05:00
|
|
|
KRA_BASEDN = DN(('o', 'kra'), ('o', 'ipaca'))
|
|
|
|
KRA_AGENT_DN = DN(('uid', 'ipakra'), ('ou', 'people'), KRA_BASEDN)
|
|
|
|
|
2015-11-19 07:33:49 -06:00
|
|
|
|
2014-03-18 10:23:30 -05:00
|
|
|
class KRAInstance(DogtagInstance):
|
|
|
|
"""
|
|
|
|
We assume that the CA has already been installed, and we use the
|
|
|
|
same tomcat instance to host both the CA and KRA.
|
|
|
|
The mod_nss database will contain the RA agent cert that will be used
|
|
|
|
to do authenticated requests against dogtag. The RA agent cert will
|
|
|
|
be the same for both the CA and KRA.
|
|
|
|
"""
|
|
|
|
|
2019-05-17 01:30:47 -05:00
|
|
|
# Mapping of nicknames for tracking requests, and the profile to
|
|
|
|
# use for that certificate. 'configure_renewal()' reads this
|
|
|
|
# dict. The profile MUST be specified.
|
2019-05-17 01:04:14 -05:00
|
|
|
tracking_reqs = {
|
|
|
|
'auditSigningCert cert-pki-kra': 'caInternalAuthAuditSigningCert',
|
|
|
|
'transportCert cert-pki-kra': 'caInternalAuthTransportCert',
|
|
|
|
'storageCert cert-pki-kra': 'caInternalAuthDRMstorageCert',
|
|
|
|
}
|
2014-10-07 09:46:15 -05:00
|
|
|
|
2015-11-09 11:28:47 -06:00
|
|
|
def __init__(self, realm):
|
2014-03-18 10:23:30 -05:00
|
|
|
super(KRAInstance, self).__init__(
|
|
|
|
realm=realm,
|
|
|
|
subsystem="KRA",
|
|
|
|
service_desc="KRA server",
|
2017-11-08 12:21:22 -06:00
|
|
|
config=paths.KRA_CS_CFG_PATH,
|
2014-03-18 10:23:30 -05:00
|
|
|
)
|
|
|
|
|
2015-08-25 12:38:05 -05:00
|
|
|
def configure_instance(self, realm_name, host_name, dm_password,
|
2015-11-09 11:28:47 -06:00
|
|
|
admin_password, pkcs12_info=None, master_host=None,
|
2019-10-03 22:30:37 -05:00
|
|
|
subject_base=None, ca_subject=None,
|
2018-09-03 05:45:30 -05:00
|
|
|
promote=False, pki_config_override=None):
|
2014-03-18 10:23:30 -05:00
|
|
|
"""Create a KRA instance.
|
|
|
|
|
|
|
|
To create a clone, pass in pkcs12_info.
|
|
|
|
"""
|
|
|
|
self.fqdn = host_name
|
|
|
|
self.dm_password = dm_password
|
2016-10-26 02:44:49 -05:00
|
|
|
self.admin_groups = ADMIN_GROUPS
|
2014-03-18 10:23:30 -05:00
|
|
|
self.admin_password = admin_password
|
|
|
|
self.pkcs12_info = pkcs12_info
|
2016-10-26 02:44:49 -05:00
|
|
|
if self.pkcs12_info is not None or promote:
|
2014-03-18 10:23:30 -05:00
|
|
|
self.clone = True
|
|
|
|
self.master_host = master_host
|
2018-09-03 05:45:30 -05:00
|
|
|
self.pki_config_override = pki_config_override
|
2016-11-16 04:39:23 -06:00
|
|
|
|
|
|
|
self.subject_base = \
|
|
|
|
subject_base or installutils.default_subject_base(realm_name)
|
2019-10-03 22:30:37 -05:00
|
|
|
|
|
|
|
# eagerly convert to DN to ensure validity
|
|
|
|
self.ca_subject = DN(ca_subject)
|
2016-11-16 04:39:23 -06:00
|
|
|
|
2015-06-10 05:35:43 -05:00
|
|
|
self.realm = realm_name
|
|
|
|
self.suffix = ipautil.realm_to_suffix(realm_name)
|
2014-03-18 10:23:30 -05:00
|
|
|
|
2017-01-04 01:41:26 -06:00
|
|
|
# Confirm that a KRA does not already exist
|
|
|
|
if self.is_installed():
|
|
|
|
raise RuntimeError(
|
|
|
|
"KRA already installed.")
|
|
|
|
# Confirm that a Dogtag 10 CA instance already exists
|
|
|
|
ca = cainstance.CAInstance(self.realm)
|
|
|
|
if not ca.is_installed():
|
|
|
|
raise RuntimeError(
|
|
|
|
"KRA configuration failed. "
|
|
|
|
"A Dogtag CA must be installed first")
|
|
|
|
|
|
|
|
if promote:
|
2018-06-22 03:04:38 -05:00
|
|
|
self.step("creating ACIs for admin", self.add_ipaca_aci)
|
2017-01-04 01:41:26 -06:00
|
|
|
self.step("creating installation admin user", self.setup_admin)
|
|
|
|
self.step("configuring KRA instance", self.__spawn_instance)
|
|
|
|
if not self.clone:
|
|
|
|
self.step("create KRA agent",
|
|
|
|
self.__create_kra_agent)
|
|
|
|
if promote:
|
|
|
|
self.step("destroying installation admin user",
|
|
|
|
self.teardown_admin)
|
2017-09-27 16:45:38 -05:00
|
|
|
self.step("enabling ephemeral requests", self.enable_ephemeral)
|
2017-01-04 01:41:26 -06:00
|
|
|
self.step("restarting KRA", self.restart_instance)
|
|
|
|
self.step("configure certmonger for renewals",
|
2020-01-21 22:38:25 -06:00
|
|
|
self.configure_certmonger_renewal_helpers)
|
2017-01-04 01:41:26 -06:00
|
|
|
self.step("configure certificate renewals", self.configure_renewal)
|
|
|
|
if not self.clone:
|
|
|
|
self.step("add vault container", self.__add_vault_container)
|
|
|
|
self.step("apply LDAP updates", self.__apply_updates)
|
|
|
|
|
|
|
|
self.step("enabling KRA instance", self.__enable_instance)
|
2016-10-26 02:44:49 -05:00
|
|
|
|
2017-02-14 09:55:11 -06:00
|
|
|
try:
|
2017-03-01 10:35:56 -06:00
|
|
|
self.start_creation(runtime=120)
|
2017-02-14 09:55:11 -06:00
|
|
|
finally:
|
|
|
|
self.clean_pkispawn_files()
|
2014-03-18 10:23:30 -05:00
|
|
|
|
|
|
|
def __spawn_instance(self):
|
|
|
|
"""
|
|
|
|
Create and configure a new KRA instance using pkispawn.
|
|
|
|
Creates a configuration file with IPA-specific
|
|
|
|
parameters and passes it to the base class to call pkispawn
|
|
|
|
"""
|
|
|
|
|
2017-02-14 09:55:11 -06:00
|
|
|
self.tmp_agent_db = tempfile.mkdtemp(
|
|
|
|
prefix="tmp-", dir=paths.VAR_LIB_IPA)
|
2017-03-27 05:36:09 -05:00
|
|
|
tmp_agent_pwd = ipautil.ipa_generate_password()
|
2014-03-18 10:23:30 -05:00
|
|
|
|
2017-11-14 18:59:32 -06:00
|
|
|
# Create a temporary file for the admin PKCS #12 file
|
|
|
|
(admin_p12_fd, admin_p12_file) = tempfile.mkstemp()
|
|
|
|
os.close(admin_p12_fd)
|
|
|
|
|
2018-08-30 09:42:40 -05:00
|
|
|
cfg = dict(
|
|
|
|
pki_issuing_ca_uri="https://{}".format(
|
|
|
|
ipautil.format_netloc(self.fqdn, 443)),
|
|
|
|
# Client security database
|
|
|
|
pki_client_database_dir=self.tmp_agent_db,
|
|
|
|
pki_client_database_password=tmp_agent_pwd,
|
|
|
|
pki_client_database_purge=True,
|
|
|
|
pki_client_pkcs12_password=self.admin_password,
|
|
|
|
pki_import_admin_cert=False,
|
|
|
|
pki_client_admin_cert_p12=admin_p12_file,
|
|
|
|
)
|
2014-03-18 10:23:30 -05:00
|
|
|
|
2017-04-03 07:08:46 -05:00
|
|
|
if not (os.path.isdir(paths.PKI_TOMCAT_ALIAS_DIR) and
|
|
|
|
os.path.isfile(paths.PKI_TOMCAT_PASSWORD_CONF)):
|
|
|
|
# generate pin which we know can be used for FIPS NSS database
|
|
|
|
pki_pin = ipautil.ipa_generate_password()
|
2018-09-26 02:59:12 -05:00
|
|
|
cfg['pki_server_database_password'] = pki_pin
|
2017-04-03 07:08:46 -05:00
|
|
|
else:
|
|
|
|
pki_pin = None
|
2017-03-28 06:54:16 -05:00
|
|
|
|
2014-03-18 10:23:30 -05:00
|
|
|
_p12_tmpfile_handle, p12_tmpfile_name = tempfile.mkstemp(dir=paths.TMP)
|
2015-11-23 06:43:53 -06:00
|
|
|
|
2014-03-18 10:23:30 -05:00
|
|
|
if self.clone:
|
|
|
|
krafile = self.pkcs12_info[0]
|
|
|
|
shutil.copy(krafile, p12_tmpfile_name)
|
2020-09-11 05:22:02 -05:00
|
|
|
self.service_user.chown(p12_tmpfile_name)
|
2014-03-18 10:23:30 -05:00
|
|
|
|
2018-08-30 09:42:40 -05:00
|
|
|
self._configure_clone(
|
|
|
|
cfg,
|
|
|
|
security_domain_hostname=self.fqdn,
|
|
|
|
clone_pkcs12_path=p12_tmpfile_name,
|
|
|
|
)
|
|
|
|
cfg.update(
|
|
|
|
pki_clone_setup_replication=False,
|
|
|
|
)
|
2015-11-23 06:43:53 -06:00
|
|
|
else:
|
|
|
|
# the admin cert file is needed for the first instance of KRA
|
2017-07-03 10:10:34 -05:00
|
|
|
cert = self.get_admin_cert()
|
2017-01-24 09:55:07 -06:00
|
|
|
# First make sure that the directory exists
|
|
|
|
parentdir = os.path.dirname(paths.ADMIN_CERT_PATH)
|
|
|
|
if not os.path.exists(parentdir):
|
|
|
|
os.makedirs(parentdir)
|
2017-07-27 04:54:44 -05:00
|
|
|
with open(paths.ADMIN_CERT_PATH, "wb") as admin_path:
|
2017-07-03 10:10:34 -05:00
|
|
|
admin_path.write(
|
|
|
|
base64.b64encode(cert.public_bytes(x509.Encoding.DER))
|
|
|
|
)
|
2014-03-18 10:23:30 -05:00
|
|
|
|
|
|
|
# Generate configuration file
|
2018-08-30 09:42:40 -05:00
|
|
|
config = self._create_spawn_config(cfg)
|
2019-04-10 03:12:07 -05:00
|
|
|
with tempfile.NamedTemporaryFile('w', delete=False) as f:
|
2014-03-18 10:23:30 -05:00
|
|
|
config.write(f)
|
2020-09-11 05:22:02 -05:00
|
|
|
self.service_user.chown(f.fileno())
|
2019-04-10 03:12:07 -05:00
|
|
|
cfg_file = f.name
|
2014-03-18 10:23:30 -05:00
|
|
|
|
2018-09-03 05:45:30 -05:00
|
|
|
nolog_list = [
|
|
|
|
self.dm_password, self.admin_password, pki_pin, tmp_agent_pwd
|
|
|
|
]
|
|
|
|
|
2014-03-18 10:23:30 -05:00
|
|
|
try:
|
2016-11-10 07:24:26 -06:00
|
|
|
DogtagInstance.spawn_instance(
|
|
|
|
self, cfg_file,
|
2018-09-03 05:45:30 -05:00
|
|
|
nolog_list=nolog_list
|
2016-11-10 07:24:26 -06:00
|
|
|
)
|
2014-03-18 10:23:30 -05:00
|
|
|
finally:
|
|
|
|
os.remove(p12_tmpfile_name)
|
|
|
|
os.remove(cfg_file)
|
2017-11-14 18:59:32 -06:00
|
|
|
os.remove(admin_p12_file)
|
2014-03-18 10:23:30 -05:00
|
|
|
|
|
|
|
shutil.move(paths.KRA_BACKUP_KEYS_P12, paths.KRACERT_P12)
|
2017-05-23 11:35:57 -05:00
|
|
|
logger.debug("completed creating KRA instance")
|
2014-03-18 10:23:30 -05:00
|
|
|
|
2015-08-26 23:44:29 -05:00
|
|
|
def __create_kra_agent(self):
|
2014-03-18 10:23:30 -05:00
|
|
|
"""
|
2015-08-26 23:44:29 -05:00
|
|
|
Create KRA agent, assign a certificate, and add the user to
|
|
|
|
the appropriate groups for accessing KRA services.
|
2014-03-18 10:23:30 -05:00
|
|
|
"""
|
2020-10-09 03:11:53 -05:00
|
|
|
conn = api.Backend.ldap2
|
2014-03-18 10:23:30 -05:00
|
|
|
|
2017-01-13 02:08:42 -06:00
|
|
|
# get RA agent certificate
|
|
|
|
cert = x509.load_certificate_from_file(paths.RA_AGENT_PEM)
|
2015-08-26 23:44:29 -05:00
|
|
|
|
2017-01-13 02:08:42 -06:00
|
|
|
# create ipakra user with RA agent certificate
|
2015-08-26 23:44:29 -05:00
|
|
|
entry = conn.make_entry(
|
2019-10-10 23:56:10 -05:00
|
|
|
KRA_AGENT_DN,
|
2015-08-26 23:44:29 -05:00
|
|
|
objectClass=['top', 'person', 'organizationalPerson',
|
|
|
|
'inetOrgPerson', 'cmsuser'],
|
|
|
|
uid=["ipakra"],
|
|
|
|
sn=["IPA KRA User"],
|
|
|
|
cn=["IPA KRA User"],
|
|
|
|
usertype=["undefined"],
|
2017-08-23 08:23:43 -05:00
|
|
|
userCertificate=[cert],
|
2015-08-26 23:44:29 -05:00
|
|
|
description=['2;%s;%s;%s' % (
|
2017-02-15 11:10:09 -06:00
|
|
|
cert.serial_number,
|
2019-10-03 22:30:37 -05:00
|
|
|
self.ca_subject,
|
2015-08-26 23:44:29 -05:00
|
|
|
DN(('CN', 'IPA RA'), self.subject_base))])
|
|
|
|
conn.add_entry(entry)
|
|
|
|
|
|
|
|
# add ipakra user to Data Recovery Manager Agents group
|
2019-10-10 23:56:10 -05:00
|
|
|
group_dn = DN(
|
|
|
|
('cn', 'Data Recovery Manager Agents'), ('ou', 'groups'),
|
|
|
|
KRA_BASEDN)
|
|
|
|
conn.add_entry_to_group(KRA_AGENT_DN, group_dn, 'uniqueMember')
|
2015-08-26 23:44:29 -05:00
|
|
|
|
2015-06-10 05:35:43 -05:00
|
|
|
def __add_vault_container(self):
|
2016-11-11 08:45:17 -06:00
|
|
|
self._ldap_mod(
|
|
|
|
'vault.ldif', {'SUFFIX': self.suffix}, raise_on_err=True)
|
2015-09-14 00:56:44 -05:00
|
|
|
|
|
|
|
def __apply_updates(self):
|
2020-09-14 03:23:15 -05:00
|
|
|
self._ldap_update(['40-vault.update'])
|
2015-06-10 05:35:43 -05:00
|
|
|
|
2017-11-08 12:21:22 -06:00
|
|
|
def enable_ephemeral(self):
|
|
|
|
"""
|
|
|
|
Enable ephemeral KRA requests to reduce the number of LDAP
|
|
|
|
write operations.
|
|
|
|
"""
|
|
|
|
with installutils.stopped_service('pki-tomcatd', 'pki-tomcat'):
|
2018-05-23 03:37:58 -05:00
|
|
|
directivesetter.set_directive(
|
2017-11-08 12:21:22 -06:00
|
|
|
self.config,
|
|
|
|
'kra.ephemeralRequests',
|
|
|
|
'true', quotes=False, separator='=')
|
|
|
|
|
|
|
|
# A restart is required
|
|
|
|
|
|
|
|
def update_cert_config(self, nickname, cert):
|
2014-03-18 10:23:30 -05:00
|
|
|
"""
|
|
|
|
When renewing a KRA subsystem certificate the configuration file
|
|
|
|
needs to get the new certificate as well.
|
|
|
|
|
|
|
|
nickname is one of the known nicknames.
|
|
|
|
cert is a DER-encoded certificate.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# The cert directive to update per nickname
|
|
|
|
directives = {
|
|
|
|
'auditSigningCert cert-pki-kra': 'kra.audit_signing.cert',
|
|
|
|
'storageCert cert-pki-kra': 'kra.storage.cert',
|
|
|
|
'transportCert cert-pki-kra': 'kra.transport.cert',
|
|
|
|
'subsystemCert cert-pki-kra': 'kra.subsystem.cert',
|
|
|
|
'Server-Cert cert-pki-ca': 'kra.sslserver.cert'}
|
|
|
|
|
2016-06-16 22:33:26 -05:00
|
|
|
if nickname in directives:
|
2017-11-08 12:21:22 -06:00
|
|
|
super(KRAInstance, self).update_cert_cs_cfg(
|
|
|
|
directives[nickname], cert)
|
2014-03-18 10:23:30 -05:00
|
|
|
|
2015-08-25 14:42:25 -05:00
|
|
|
def __enable_instance(self):
|
2018-07-05 17:04:39 -05:00
|
|
|
self.ldap_configure('KRA', self.fqdn, None, self.suffix)
|