|
3 | 3 | # This file is part of Checkmk (https://checkmk.com). It is subject to the terms and |
4 | 4 | # conditions defined in the file COPYING, which is part of this source code package. |
5 | 5 |
|
6 | | -from datetime import datetime, timedelta, UTC |
| 6 | +from datetime import timedelta |
7 | 7 |
|
8 | 8 | from cryptography.hazmat.primitives.asymmetric import rsa |
9 | 9 | from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 |
10 | 10 | from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey |
11 | 11 | from cryptography.hazmat.primitives.hashes import SHA256 |
12 | | -from cryptography.hazmat.primitives.serialization import ( |
13 | | - Encoding, |
14 | | - NoEncryption, |
15 | | - PrivateFormat, |
16 | | -) |
17 | 12 | from cryptography.x509 import ( |
18 | 13 | Certificate, |
19 | | - CertificateBuilder, |
20 | 14 | CertificateSigningRequest, |
21 | 15 | CertificateSigningRequestBuilder, |
22 | 16 | load_pem_x509_certificate, |
|
26 | 20 | from cryptography.x509.oid import NameOID |
27 | 21 |
|
28 | 22 | from cmk.agent_receiver.lib.config import Config |
| 23 | +from cmk.crypto.certificate import CertificateWithPrivateKey |
29 | 24 |
|
30 | 25 | CA_CERT = b"""-----BEGIN PRIVATE KEY----- |
31 | 26 | MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDpDGxoGtI59lZM |
@@ -93,39 +88,20 @@ def generate_site_certificate(config: Config) -> None: |
93 | 88 | Creates a certificate signed by the CA and writes both the private key |
94 | 89 | and certificate to the site certificate path. |
95 | 90 | """ |
96 | | - # Load the CA to sign the site certificate |
97 | | - ca_cert = load_pem_x509_certificate(CA_CERT) |
98 | | - |
99 | | - # Generate a private key and certificate for the site |
100 | | - site_private_key = generate_private_key(2048) |
101 | | - |
102 | | - # Create site certificate signed by CA |
103 | | - now = datetime.now(UTC).replace(tzinfo=None) |
104 | | - site_cert = ( |
105 | | - CertificateBuilder() |
106 | | - .subject_name( |
107 | | - Name( |
108 | | - [ |
109 | | - NameAttribute(NameOID.COMMON_NAME, config.site_name), |
110 | | - ] |
111 | | - ) |
112 | | - ) |
113 | | - .issuer_name(ca_cert.subject) |
114 | | - .public_key(site_private_key.public_key()) |
115 | | - .serial_number(12345) |
116 | | - .not_valid_before(now) |
117 | | - .not_valid_after(now + timedelta(days=365)) |
118 | | - .sign(site_private_key, SHA256()) # Self-signed for tests |
| 91 | + site_ca = CertificateWithPrivateKey.load_combined_file_content( |
| 92 | + config.site_ca_path.read_text(), passphrase=None |
| 93 | + ) |
| 94 | + site_cert = site_ca.issue_new_certificate( |
| 95 | + common_name=config.site_name, |
| 96 | + organization=f"Checkmk Site {config.site_name}", |
| 97 | + expiry=timedelta(days=365), |
| 98 | + key_size=2048, |
119 | 99 | ) |
120 | 100 |
|
121 | 101 | # Write site certificate and private key |
122 | 102 | config.site_cert_path.parent.mkdir(parents=True, exist_ok=True) |
123 | | - site_cert_pem = site_cert.public_bytes(Encoding.PEM) |
124 | | - site_key_pem = site_private_key.private_bytes( |
125 | | - Encoding.PEM, |
126 | | - PrivateFormat.TraditionalOpenSSL, |
127 | | - NoEncryption(), |
128 | | - ) |
| 103 | + site_cert_pem = site_cert.certificate.dump_pem().bytes |
| 104 | + site_key_pem = site_cert.private_key.dump_pem(None).bytes |
129 | 105 | # Site cert file contains both key and cert |
130 | 106 | config.site_cert_path.write_bytes(site_key_pem + site_cert_pem) |
131 | 107 |
|
|
0 commit comments