Skip to content

Commit cb2059c

Browse files
committed
fetchers/messaging: untangle V
CMK-21657 Change-Id: I1a786c0f327c389afb68527beafad1b166a1d269
1 parent 9bcedbb commit cb2059c

File tree

4 files changed

+48
-48
lines changed

4 files changed

+48
-48
lines changed

bin/message-broker-certs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ def initialize_message_broker_certs(omd_root: Path, site_name: str) -> None:
3131
ca_cert_bundle.certificate_path.read_bytes()
3232
)
3333

34-
site_cert = SiteBrokerCertificate.create(site_name, omd_root, issuer=ca_cert_bundle)
35-
site_cert.persist(omd_root)
34+
site_broker_ca = SiteBrokerCertificate(
35+
messaging.site_cert_file(omd_root), messaging.site_key_file(omd_root)
36+
)
37+
site_broker_ca.persist(site_broker_ca.create_bundle(site_name, ca_cert_bundle))
3638

3739

3840
def _parse_arguments(argv: list[str]) -> Arguments:

cmk/gui/watolib/broker_certificates.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from cmk import messaging
3636
from cmk.crypto.certificate import (
3737
CertificateSigningRequest,
38+
CertificateWithPrivateKey,
3839
PersistedCertificateWithPrivateKey,
3940
X509Name,
4041
)
@@ -168,10 +169,13 @@ def create_remote_broker_certs(
168169
Create a new certificate with private key for the broker of a remote site.
169170
"""
170171

171-
site_cert = SiteBrokerCertificate.create(site_id, paths.omd_root, signing_ca_bundle)
172+
site_broker_ca = SiteBrokerCertificate(
173+
messaging.site_cert_file(paths.omd_root), messaging.site_key_file(paths.omd_root)
174+
)
175+
site_ca_bundle = site_broker_ca.create_bundle(site_id, signing_ca_bundle)
172176

173177
return messaging.BrokerCertificates(
174-
cert=site_cert.cert_bundle.certificate.dump_pem().bytes,
178+
cert=site_ca_bundle.certificate.dump_pem().bytes,
175179
signing_ca=signing_ca_bundle.certificate.dump_pem().bytes,
176180
)
177181

@@ -223,21 +227,23 @@ def plugin_name(self, instance: BrokerCertificateSync) -> str:
223227
broker_certificate_sync_registry = BrokerCertificateSyncRegistry()
224228

225229

226-
def _create_message_broker_certs() -> SiteBrokerCertificate:
230+
def _create_message_broker_certs() -> CertificateWithPrivateKey:
227231
"""Initialize the CA and create the certificate for use with the message broker.
228232
These might be replaced by the "store-broker-certs" automation.
229233
"""
230234

231235
ca = SiteBrokerCA(messaging.cacert_file(paths.omd_root), messaging.ca_key_file(paths.omd_root))
232-
bundle = ca.create_and_persist(omd_site())
236+
ca_bundle = ca.create_and_persist(omd_site())
233237
MessagingTrustedCAs(messaging.trusted_cas_file(paths.omd_root)).write(
234-
bundle.certificate_path.read_bytes()
238+
ca_bundle.certificate_path.read_bytes()
235239
)
236240

237-
site_cert = SiteBrokerCertificate.create(omd_site(), paths.omd_root, issuer=bundle)
238-
site_cert.persist(paths.omd_root)
241+
site_broker_ca = SiteBrokerCertificate(
242+
messaging.site_cert_file(paths.omd_root), messaging.site_key_file(paths.omd_root)
243+
)
244+
site_broker_ca.persist(bundle := site_broker_ca.create_bundle(omd_site(), issuer=ca_bundle))
239245

240-
return site_cert
246+
return bundle
241247

242248

243249
def _create_csr(private_key: PrivateKey) -> CertificateSigningRequest:
@@ -300,8 +306,13 @@ def get_request(self) -> StoreBrokerCertificatesData:
300306

301307
def execute(self, api_request: StoreBrokerCertificatesData) -> bool:
302308
trusted_cas_store = MessagingTrustedCAs(messaging.trusted_cas_file(paths.omd_root))
303-
SiteBrokerCertificate.persist_broker_certificates(
304-
paths.omd_root, api_request.certificates, trusted_cas_store
309+
SiteBrokerCertificate(
310+
messaging.site_cert_file(paths.omd_root), messaging.site_key_file(paths.omd_root)
311+
).persist_broker_certificates(
312+
signing_ca=api_request.certificates.signing_ca,
313+
cert=api_request.certificates.cert,
314+
additionally_trusted_ca=api_request.certificates.additionally_trusted_ca,
315+
trusted_cas_store=trusted_cas_store,
305316
)
306317

307318
# Remove local CA files to avoid confusion. They have no use anymore.
@@ -327,5 +338,5 @@ def get_request(self) -> None:
327338
pass
328339

329340
def execute(self, api_request: None) -> BrokerCertsCSR:
330-
private_key = _create_message_broker_certs().cert_bundle.private_key
341+
private_key = _create_message_broker_certs().private_key
331342
return {"csr": _create_csr(private_key).csr.public_bytes(Encoding.PEM)}

cmk/post_rename_site/plugins/actions/messaging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from cmk.ccc.i18n import _
1111

1212
from cmk.utils import paths
13-
from cmk.utils.certs import SiteBrokerCertificate
1413

1514
from cmk.gui.watolib.activate_changes import get_all_replicated_sites
1615
from cmk.gui.watolib.broker_certificates import (
@@ -19,6 +18,7 @@
1918
from cmk.gui.watolib.changes import add_change
2019
from cmk.gui.watolib.config_domains import ConfigDomainGUI
2120

21+
from cmk import messaging
2222
from cmk.post_rename_site.registry import rename_action_registry, RenameAction
2323

2424

@@ -31,7 +31,7 @@ def update_broker_config(old_site_id: SiteId, new_site_id: SiteId, logger: Logge
3131
created with the new names.
3232
"""
3333
logger.debug("Deleting broker certificates of site %s", old_site_id)
34-
SiteBrokerCertificate.cert_path(paths.omd_root).unlink(missing_ok=True)
34+
messaging.site_cert_file(paths.omd_root).unlink(missing_ok=True)
3535
logger.debug("Deleting broker certificates of replicated sites")
3636
clean_remote_sites_certs(kept_sites=[])
3737

cmk/utils/certs.py

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections.abc import Iterable
1010
from dataclasses import dataclass
1111
from pathlib import Path
12-
from typing import Final, Literal, Self
12+
from typing import Final, Literal
1313

1414
from cryptography import x509
1515
from cryptography.hazmat.primitives.serialization import Encoding, load_pem_private_key
@@ -22,7 +22,6 @@
2222
from cmk.utils.log.security_event import SecurityEvent
2323
from cmk.utils.user import UserId
2424

25-
from cmk import messaging
2625
from cmk.crypto.certificate import (
2726
Certificate,
2827
CertificatePEM,
@@ -224,19 +223,14 @@ def __init__(
224223

225224

226225
class SiteBrokerCertificate:
227-
def __init__(self, bundle: CertificateWithPrivateKey) -> None:
228-
self.cert_bundle = bundle
229-
230-
@classmethod
231-
def key_path(cls, omd_root: Path) -> Path:
232-
return messaging.site_key_file(omd_root)
233-
234-
@classmethod
235-
def cert_path(cls, omd_root: Path) -> Path:
236-
return messaging.site_cert_file(omd_root)
226+
def __init__(self, cert_path: Path, key_path: Path) -> None:
227+
self.cert_path: Final = cert_path
228+
self.key_path: Final = key_path
237229

238230
@classmethod
239-
def create(cls, site_name: str, omd_root: Path, issuer: CertificateWithPrivateKey) -> Self:
231+
def create_bundle(
232+
cls, site_name: str, issuer: CertificateWithPrivateKey
233+
) -> CertificateWithPrivateKey:
240234
"""Have the site's certificate issued by the given CA.
241235
242236
The certificate and key are not persisted to disk directly because this method is also used
@@ -247,40 +241,33 @@ def create(cls, site_name: str, omd_root: Path, issuer: CertificateWithPrivateKe
247241
is_ca = False
248242
key_size = 4096
249243

250-
cert_bundle = issuer.issue_new_certificate(
244+
return issuer.issue_new_certificate(
251245
common_name=site_name,
252246
organization=organization,
253247
expiry=expires,
254248
key_size=key_size,
255249
is_ca=is_ca,
256250
)
257251

258-
return cls(cert_bundle)
259-
260-
def persist(self, omd_root: Path) -> None:
261-
cert_path = self.cert_path(omd_root)
262-
key_path = self.key_path(omd_root)
263-
264-
cert_path.parent.mkdir(parents=True, exist_ok=True)
265-
PersistedCertificateWithPrivateKey.persist(self.cert_bundle, cert_path, key_path)
252+
def persist(self, cert_bundle: CertificateWithPrivateKey) -> None:
253+
self.cert_path.parent.mkdir(parents=True, exist_ok=True)
254+
PersistedCertificateWithPrivateKey.persist(cert_bundle, self.cert_path, self.key_path)
266255

267-
@classmethod
268256
def persist_broker_certificates(
269-
cls,
270-
omd_root: Path,
271-
received: messaging.BrokerCertificates,
257+
self,
258+
signing_ca: bytes,
259+
cert: bytes,
260+
additionally_trusted_ca: bytes,
272261
trusted_cas_store: MessagingTrustedCAs,
273262
) -> None:
274263
"""Persist the received certificates to disk."""
275-
cert_path = cls.cert_path(omd_root)
276-
277-
ca = Certificate.load_pem(CertificatePEM(received.signing_ca))
278-
Certificate.load_pem(CertificatePEM(received.cert)).verify_is_signed_by(ca)
264+
ca = Certificate.load_pem(CertificatePEM(signing_ca))
265+
Certificate.load_pem(CertificatePEM(cert)).verify_is_signed_by(ca)
279266

280-
cert_path.parent.mkdir(parents=True, exist_ok=True)
267+
self.cert_path.parent.mkdir(parents=True, exist_ok=True)
281268

282-
trusted_cas_store.write(received.signing_ca + received.additionally_trusted_ca)
283-
cert_path.write_bytes(received.cert)
269+
trusted_cas_store.write(signing_ca + additionally_trusted_ca)
270+
self.cert_path.write_bytes(cert)
284271

285272

286273
class SiteBrokerCA:

0 commit comments

Comments
 (0)