99from collections .abc import Iterable
1010from dataclasses import dataclass
1111from pathlib import Path
12- from typing import Final , Literal , Self
12+ from typing import Final , Literal
1313
1414from cryptography import x509
1515from cryptography .hazmat .primitives .serialization import Encoding , load_pem_private_key
2222from cmk .utils .log .security_event import SecurityEvent
2323from cmk .utils .user import UserId
2424
25- from cmk import messaging
2625from cmk .crypto .certificate import (
2726 Certificate ,
2827 CertificatePEM ,
@@ -224,19 +223,14 @@ def __init__(
224223
225224
226225class 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
286273class SiteBrokerCA :
0 commit comments