-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathssl_certifcates_util.py
More file actions
285 lines (247 loc) · 10.5 KB
/
Copy pathssl_certifcates_util.py
File metadata and controls
285 lines (247 loc) · 10.5 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import ipaddress
import logging
import os
import shutil
from datetime import datetime, timedelta
import certifi
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import dh, dsa, ec, rsa
from cryptography.x509 import load_pem_x509_certificate
from cryptography.x509.oid import NameOID
logger = logging.getLogger(__name__)
def generate_mtls_certs(
output_dir: str,
server_san: str = "feature-registry.example.com",
) -> dict[str, str]:
"""
Generate a CA, server, and client certificate chain for mTLS testing.
The server cert's SAN is set to ``server_san`` (no ``localhost``), so a
gRPC client connecting to ``localhost`` must set the ``authority`` channel
option to ``server_san`` — exactly the IAP-tunnel pattern.
Returns a dict with keys: ca_cert, server_key, server_cert,
client_key, client_cert.
"""
os.makedirs(output_dir, exist_ok=True)
paths = {
"ca_cert": os.path.join(output_dir, "ca.crt"),
"server_key": os.path.join(output_dir, "server.key"),
"server_cert": os.path.join(output_dir, "server.crt"),
"client_key": os.path.join(output_dir, "client.key"),
"client_cert": os.path.join(output_dir, "client.crt"),
}
# --- CA (self-signed) ---
ca_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
ca_name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "TestCA")])
ca_cert = (
x509.CertificateBuilder()
.subject_name(ca_name)
.issuer_name(ca_name)
.public_key(ca_key.public_key())
.serial_number(x509.random_serial_number())
.not_valid_before(datetime.utcnow())
.not_valid_after(datetime.utcnow() + timedelta(days=1))
.add_extension(x509.BasicConstraints(ca=True, path_length=None), critical=True)
.sign(ca_key, hashes.SHA256())
)
with open(paths["ca_cert"], "wb") as f:
f.write(ca_cert.public_bytes(serialization.Encoding.PEM))
def _issue_cert(
cn: str,
key_path: str,
cert_path: str,
san: x509.SubjectAlternativeName | None = None,
):
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
subject = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, cn)])
builder = (
x509.CertificateBuilder()
.subject_name(subject)
.issuer_name(ca_cert.subject)
.public_key(key.public_key())
.serial_number(x509.random_serial_number())
.not_valid_before(datetime.utcnow())
.not_valid_after(datetime.utcnow() + timedelta(days=1))
)
if san is not None:
builder = builder.add_extension(san, critical=False)
cert = builder.sign(ca_key, hashes.SHA256())
with open(key_path, "wb") as f:
f.write(
key.private_bytes(
serialization.Encoding.PEM,
serialization.PrivateFormat.TraditionalOpenSSL,
serialization.NoEncryption(),
)
)
with open(cert_path, "wb") as f:
f.write(cert.public_bytes(serialization.Encoding.PEM))
# --- Server cert ---
_issue_cert(
cn=server_san,
key_path=paths["server_key"],
cert_path=paths["server_cert"],
san=x509.SubjectAlternativeName([x509.DNSName(server_san)]),
)
# --- Client cert (no SAN needed) ---
_issue_cert(
cn="TestClient",
key_path=paths["client_key"],
cert_path=paths["client_cert"],
)
logger.info(f"mTLS certificates generated in {output_dir}")
return paths
def generate_self_signed_cert(
cert_path="cert.pem", key_path="key.pem", common_name="localhost"
):
"""
Generate a self-signed certificate and save it to the specified paths.
:param cert_path: Path to save the certificate (PEM format)
:param key_path: Path to save the private key (PEM format)
:param common_name: Common name (CN) for the certificate, defaults to 'localhost'
"""
# Generate private key
key = rsa.generate_private_key(
public_exponent=65537, key_size=2048, backend=default_backend()
)
# Create a self-signed certificate
subject = issuer = x509.Name(
[
x509.NameAttribute(NameOID.COUNTRY_NAME, "US"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "California"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "San Francisco"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Feast"),
x509.NameAttribute(NameOID.COMMON_NAME, common_name),
]
)
# Define the certificate's Subject Alternative Names (SANs)
alt_names = [
x509.DNSName("localhost"), # Hostname
x509.IPAddress(ipaddress.IPv4Address("127.0.0.1")), # Localhost IP
x509.IPAddress(ipaddress.IPv4Address("0.0.0.0")), # Bind-all IP (optional)
]
san = x509.SubjectAlternativeName(alt_names)
certificate = (
x509.CertificateBuilder()
.subject_name(subject)
.issuer_name(issuer)
.public_key(key.public_key())
.serial_number(x509.random_serial_number())
.not_valid_before(datetime.utcnow())
.not_valid_after(
# Certificate valid for 1 year
datetime.utcnow() + timedelta(days=365)
)
.add_extension(san, critical=False)
.sign(key, hashes.SHA256(), default_backend())
)
# Write the private key to a file
with open(key_path, "wb") as f:
f.write(
key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
)
# Write the certificate to a file
with open(cert_path, "wb") as f:
f.write(certificate.public_bytes(serialization.Encoding.PEM))
logger.info(
f"Self-signed certificate and private key have been generated at {cert_path} and {key_path}."
)
def create_ca_trust_store(
public_key_path: str, private_key_path: str, output_trust_store_path: str
):
"""
Create a new CA trust store as a copy of the existing one (if available),
and add the provided public certificate to it.
:param public_key_path: Path to the public certificate (e.g., PEM file).
:param private_key_path: Path to the private key (optional, to verify signing authority).
:param output_trust_store_path: Path to save the new trust store.
"""
try:
# Step 1: Identify the existing trust store (if available via environment variables)
existing_trust_store = os.environ.get("SSL_CERT_FILE") or os.environ.get(
"REQUESTS_CA_BUNDLE"
)
# Step 2: Copy the existing trust store to the new location (if it exists)
if existing_trust_store and os.path.exists(existing_trust_store):
shutil.copy(existing_trust_store, output_trust_store_path)
logger.info(
f"Copied existing trust store from {existing_trust_store} to {output_trust_store_path}"
)
else:
# Log the creation of a new trust store (without opening a file unnecessarily)
logger.info(
f"No existing trust store found. Creating a new trust store at {output_trust_store_path}"
)
# Step 3: Load and validate the public certificate
with open(public_key_path, "rb") as pub_file:
public_cert_data = pub_file.read()
public_cert = load_pem_x509_certificate(
public_cert_data, backend=default_backend()
)
# Verify the private key matches (optional, adds validation)
if private_key_path:
with open(private_key_path, "rb") as priv_file:
private_key_data = priv_file.read()
private_key = serialization.load_pem_private_key(
private_key_data, password=None, backend=default_backend()
)
private_pub = private_key.public_key()
cert_pub = public_cert.public_key()
if isinstance(
private_pub,
(
rsa.RSAPublicKey,
dsa.DSAPublicKey,
ec.EllipticCurvePublicKey,
dh.DHPublicKey,
),
) and isinstance(
cert_pub,
(
rsa.RSAPublicKey,
dsa.DSAPublicKey,
ec.EllipticCurvePublicKey,
dh.DHPublicKey,
),
):
if private_pub.public_numbers() != cert_pub.public_numbers():
raise ValueError(
"Public certificate does not match the private key."
)
else:
logger.warning(
"Key type does not support public_numbers(). Skipping strict public key match."
)
# Step 4: Add the public certificate to the new trust store
with open(output_trust_store_path, "ab") as trust_store_file:
trust_store_file.write(public_cert.public_bytes(serialization.Encoding.PEM))
logger.info(
f"Trust store created/updated successfully at: {output_trust_store_path}"
)
except Exception as e:
logger.error(f"Error creating CA trust store: {e}")
def combine_trust_stores(custom_cert_path: str, output_combined_path: str):
"""
Combine the default certifi CA bundle with a custom certificate file.
:param custom_cert_path: Path to the custom certificate PEM file.
:param output_combined_path: Path where the combined CA bundle will be saved.
"""
try:
# Get the default certifi CA bundle
certifi_ca_bundle = certifi.where()
with open(output_combined_path, "wb") as combined_file:
# Write the default CA bundle
with open(certifi_ca_bundle, "rb") as default_file:
combined_file.write(default_file.read())
# Append the custom certificates
with open(custom_cert_path, "rb") as custom_file:
combined_file.write(custom_file.read())
logger.info(f"Combined trust store created at: {output_combined_path}")
except Exception as e:
logger.error(f"Error combining trust stores: {e}")
raise e