The Identity, Policy and Audit system
CentOS Sources
2016-11-03 403b09ab980c02ef36095973349a13e0181c794a
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
From 01fcae3f99ba3368cb88418e14b6bbbe81bc555d Mon Sep 17 00:00:00 2001
From: Fraser Tweedale <ftweedal@redhat.com>
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 <mbabinsk@redhat.com>
---
 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 f340f32bcdca5f5d98177f7aa9af366b67d8dd80..4b97b85b83c8b1a130d6db9cdcc7a76fc569af9e 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 9b351e260f15211330521453b3ffcd41433a04bb..04515dcd25d066d8f1ab79ae8e8b96e909a1d884 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 e94fec5f6fd898b66dc12407be6e3f671ac3f4de..3551887cd8ff8baa5e17f8969c84fb92d7552ef3 100644
--- a/ipaserver/install/cainstance.py
+++ b/ipaserver/install/cainstance.py
@@ -1382,6 +1382,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):
@@ -1447,6 +1450,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:
@@ -2069,6 +2088,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
-- 
2.7.4