/
configuration.py
243 lines (176 loc) · 8.76 KB
/
configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Authors:
# Matthew Harmsen <mharmsen@redhat.com>
#
# 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; version 2 of the License.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2012 Red Hat, Inc.
# All rights reserved.
#
from __future__ import absolute_import
import logging
# PKI Deployment Imports
from .. import pkiconfig as config
from .. import pkiscriptlet
import pki.nssdb
logger = logging.getLogger(__name__)
# PKI Deployment Configuration Scriptlet
class PkiScriptlet(pkiscriptlet.AbstractBasePkiScriptlet):
def spawn(self, deployer):
external = deployer.configuration_file.external
standalone = deployer.configuration_file.standalone
subordinate = deployer.configuration_file.subordinate
clone = deployer.configuration_file.clone
step_one = deployer.configuration_file.external_step_one
skip_configuration = deployer.configuration_file.skip_configuration
if (external or standalone) and step_one or skip_configuration:
logger.info('Skipping configuration')
return
logger.info('Configuring subsystem')
instance = self.instance
instance.load()
subsystem = instance.get_subsystem(deployer.subsystem_type.lower())
deployer.configure_subsystem(subsystem)
subsystem.save()
deployer.import_system_cert_requests(subsystem)
nssdb = instance.open_nssdb()
try:
deployer.import_system_certs(nssdb, subsystem)
finally:
nssdb.close()
deployer.update_system_certs(subsystem)
subsystem.save()
deployer.update_sslserver_cert_nickname(subsystem)
if config.str2bool(deployer.mdict['pki_security_domain_setup']):
deployer.setup_security_domain(subsystem)
system_certs_imported = \
deployer.mdict['pki_server_pkcs12_path'] != '' or \
deployer.mdict['pki_clone_pkcs12_path'] != ''
if subsystem.type == 'CA' and external or \
subsystem.type in ['KRA', 'OCSP'] and standalone:
# For external sub-CA and standalone KRA/OCSP, no need to retrieve
# the cert chain since it's already imported from a local file by
# PKIDeployer.import_cert_chain().
subsystem.set_config('preop.ca.pkcs7', '')
elif (subsystem.type == 'CA' and subordinate or subsystem.type != 'CA') and \
not clone and not system_certs_imported:
# For primary (not clone) sub-CA and KRA, OCSP, TKS, and TPS,
# retrieve the cert chain from the issuing CA unless it's already
# imported from PKCS #12 file by PKIDeployer.import_server_pkcs12().
issuing_ca = deployer.mdict['pki_issuing_ca']
pem_chain = deployer.retrieve_cert_chain(issuing_ca)
base64_chain = pki.nssdb.convert_pkcs7(pem_chain, 'pem', 'base64')
subsystem.set_config('preop.ca.pkcs7', base64_chain)
elif subsystem.type == 'CA' and clone and not system_certs_imported:
# For root CA and sub-CA clone, retrieve the cert chain from the
# primary server unless it's already imported from a PKCS #12 file
# by PKIDeployer.import_clone_pkcs12().
master_url = deployer.mdict['pki_clone_uri']
pem_chain = deployer.retrieve_cert_chain(master_url)
subsystem.save()
if clone:
master_config = deployer.import_master_config(subsystem)
else:
master_config = None
if config.str2bool(deployer.mdict['pki_ds_setup']):
if clone:
deployer.request_ranges(subsystem)
deployer.setup_database(subsystem, master_config)
if not clone and subsystem.type == 'CA':
subsystem.import_profiles(
input_folder='/usr/share/pki/ca/profiles/ca')
subsystem.load()
# Check whether the subsystem uses a legacy ID generator.
using_legacy_id_generator = deployer.is_using_legacy_id_generator(subsystem)
# Optionally prepare to enable a java debugger
# (e. g. - 'eclipse'):
if config.str2bool(deployer.mdict['pki_enable_java_debugger']):
config.prepare_for_an_external_java_debugger(instance.service_conf)
# Start/Restart this Tomcat PKI Process
if len(instance.get_subsystems()) == 1:
logger.info('Enabling %s subsystem', subsystem.type)
subsystem.enable()
if using_legacy_id_generator:
logger.info('Creating temporary SSL server cert')
deployer.create_temp_sslserver_cert()
logger.info('Starting PKI server')
instance.start(
wait=True,
max_wait=deployer.startup_timeout,
timeout=deployer.request_timeout)
logger.info('Waiting for %s subsystem', subsystem.type)
subsystem.wait_for_startup(deployer.startup_timeout, deployer.request_timeout)
# Optionally wait for debugger to attach (e. g. - 'eclipse'):
if config.str2bool(deployer.mdict['pki_enable_java_debugger']):
config.wait_to_attach_an_external_java_debugger()
deployer.pki_connect()
# If pki_one_time_pin is not already defined, load from CS.cfg
if 'pki_one_time_pin' not in deployer.mdict:
deployer.mdict['pki_one_time_pin'] = subsystem.config['preop.pin']
nssdb = subsystem.instance.open_nssdb()
try:
system_certs = deployer.setup_system_certs(nssdb, subsystem)
subsystem.save()
deployer.validate_system_certs(subsystem)
finally:
nssdb.close()
if config.str2bool(deployer.mdict['pki_security_domain_setup']) and \
subsystem.type == 'CA':
logger.info('Setting up subsystem user')
deployer.setup_subsystem_user(subsystem, system_certs['subsystem'])
if config.str2bool(deployer.mdict['pki_security_domain_setup']):
deployer.setup_security_domain_manager(subsystem)
if config.str2bool(deployer.mdict['pki_admin_setup']) and not clone:
logger.info('Setting up admin cert')
admin_cert = deployer.setup_admin_cert(subsystem)
logger.info('Setting up admin user')
deployer.setup_admin_user(subsystem, admin_cert)
if not config.str2bool(deployer.mdict['pki_share_db']) and not clone:
logger.info('Setting up database user')
deployer.setup_database_user(subsystem)
deployer.finalize_subsystem(subsystem)
logger.info('%s configuration complete', subsystem.type)
if len(instance.get_subsystems()) == 1:
if using_legacy_id_generator:
logger.info('Stopping PKI server')
instance.stop(
wait=True,
max_wait=deployer.startup_timeout,
timeout=deployer.request_timeout)
# Remove temp SSL server cert.
deployer.remove_temp_sslserver_cert()
# Store perm SSL server cert nickname and token
nickname = system_certs['sslserver']['nickname']
token = pki.nssdb.normalize_token(system_certs['sslserver']['token'])
if not token:
token = deployer.mdict.get('pki_sslserver_token')
if not token:
token = deployer.mdict['pki_token_name']
instance.set_sslserver_cert_nickname(nickname, token)
else:
if config.str2bool(deployer.mdict['pki_hsm_enable']):
logger.info('Stopping PKI server')
instance.stop(
wait=True,
max_wait=deployer.startup_timeout,
timeout=deployer.request_timeout)
else:
logger.info('Starting %s subsystem', subsystem.type)
subsystem.enable(
wait=True,
max_wait=deployer.startup_timeout,
timeout=deployer.request_timeout)
logger.info('Waiting for %s subsystem', subsystem.type)
subsystem.wait_for_startup(deployer.startup_timeout, deployer.request_timeout)
def destroy(self, deployer):
pass