4
4
import ipaddress
5
5
import datetime
6
6
import os
7
+ import sys
8
+ import ipaddress
7
9
8
10
from typing import List
9
11
@@ -53,7 +55,7 @@ def generate_cert(hostnames: list[str], ip_addresses: list[str] = None,
53
55
if ip_addresses :
54
56
for addr in ip_addresses :
55
57
# openssl wants DNSnames for ips...
56
- alt_names .append (x509 .DNSName (addr ))
58
+ # we add above: alt_names.append(x509.DNSName(addr))
57
59
# ... whereas golang's crypto/tls is stricter, and needs IPAddresses
58
60
# note: older versions of cryptography do not understand ip_address objects
59
61
alt_names .append (x509 .IPAddress (ipaddress .ip_address (addr )))
@@ -196,10 +198,11 @@ def save_key(fh, key: rsa.RSAPrivateKey):
196
198
format = serialization .PrivateFormat .TraditionalOpenSSL ,
197
199
encryption_algorithm = serialization .NoEncryption ()))
198
200
199
- def main ():
201
+ def main () -> int :
200
202
args = get_args ()
201
203
ca_privkey = None
202
204
ca_cert = None
205
+ ipaddresses = list ()
203
206
204
207
certfile = args .cert
205
208
keyfile = args .key
@@ -228,10 +231,19 @@ def main():
228
231
# ca_privkey = serialization.load_pem_private_key(fh.read(), password=None)
229
232
230
233
234
+ for h in args .hostnames :
235
+ try :
236
+ ipaddress .ip_address (h )
237
+ ipaddresses .append (h )
238
+ except ValueError :
239
+ pass
240
+
241
+
231
242
cert , key = generate_cert (hostnames = args .hostnames ,
232
- days = args .days , bits = args .bits ,
233
- cakey = ca_privkey , cacert = ca_cert ,
234
- ca = args .ca )
243
+ ip_addresses = ipaddresses ,
244
+ days = args .days , bits = args .bits ,
245
+ cakey = ca_privkey , cacert = ca_cert ,
246
+ ca = args .ca )
235
247
236
248
237
249
with open (certfile , "wb" ) as fh :
@@ -244,5 +256,7 @@ def main():
244
256
with open (keyfile , "wb" ) as fh :
245
257
save_key (fh , key )
246
258
259
+ return 0
260
+
247
261
if __name__ == '__main__' :
248
- main ()
262
+ sys . exit ( main () )
0 commit comments