66# ///
77
88import argparse
9- import os
109import sys
10+ from pathlib import Path
1111
1212import trustme
1313
@@ -17,43 +17,41 @@ def main() -> None:
1717 parser .add_argument (
1818 "-d" ,
1919 "--dir" ,
20- default = os . getcwd () ,
20+ default = "." ,
2121 help = "Directory where certificates and keys are written to. Defaults to cwd." ,
2222 )
2323
2424 args = parser .parse_args (sys .argv [1 :])
25- cert_dir = args .dir
25+ cert_dir = Path ( args .dir )
2626
27- if not os . path . isdir ( cert_dir ):
27+ if not cert_dir . is_dir ( ):
2828 raise ValueError (f"--dir={ cert_dir } is not a directory" )
2929
3030 key_type = trustme .KeyType ["ECDSA" ]
3131
3232 # Generate the CA certificate
3333 ca = trustme .CA (key_type = key_type )
3434 # Write the certificate the client should trust
35- ca_cert_path = os . path . join ( cert_dir , "ca.pem" )
35+ ca_cert_path = cert_dir / "ca.pem"
3636 ca .cert_pem .write_to_path (path = ca_cert_path )
3737
3838 # Generate the server certificate
3939 server_cert = ca .issue_cert ("localhost" , "127.0.0.1" , "::1" , key_type = key_type )
4040 # Write the certificate and private key the server should use
41- server_key_path = os . path . join ( cert_dir , "server.key" )
42- server_cert_path = os . path . join ( cert_dir , "server.pem" )
41+ server_key_path = cert_dir / "server.key"
42+ server_cert_path = cert_dir / "server.pem"
4343 server_cert .private_key_pem .write_to_path (path = server_key_path )
44- with open (server_cert_path , mode = "w" ) as f :
45- f .truncate ()
44+ server_cert_path .write_text ("" )
4645 for blob in server_cert .cert_chain_pems :
4746 blob .write_to_path (path = server_cert_path , append = True )
4847
4948 # Generate the client certificate
5049 client_cert = ca .issue_cert ("admin@example.com" , common_name = "admin" , key_type = key_type )
5150 # Write the certificate and private key the client should use
52- client_key_path = os . path . join ( cert_dir , "client.key" )
53- client_cert_path = os . path . join ( cert_dir , "client.pem" )
51+ client_key_path = cert_dir / "client.key"
52+ client_cert_path = cert_dir / "client.pem"
5453 client_cert .private_key_pem .write_to_path (path = client_key_path )
55- with open (client_cert_path , mode = "w" ) as f :
56- f .truncate ()
54+ client_cert_path .write_text ("" )
5755 for blob in client_cert .cert_chain_pems :
5856 blob .write_to_path (path = client_cert_path , append = True )
5957
0 commit comments