Fix some bugs in the 'cert' UI

- os.path.exists() allows directories so use os.path.isfile() instead
- os.path.isfile() does not interpret '~' as /home/<user> so add note to users
- Use right certificate directory in `mycert` example
- Display a message and abort if no previously generated certs
This commit is contained in:
govynnus 2020-08-14 21:29:21 +01:00 committed by Callum Brown
parent ce834dd231
commit 99e5ceec65
1 changed files with 10 additions and 5 deletions

15
av98.py
View File

@ -7,6 +7,7 @@
# - <jprjr@tilde.club> # - <jprjr@tilde.club>
# - <vee@vnsf.xyz> # - <vee@vnsf.xyz>
# - Klaus Alexander Seistrup <klaus@seistrup.dk> # - Klaus Alexander Seistrup <klaus@seistrup.dk>
# - govynnus <govynnus@sdf.org>
import argparse import argparse
import cmd import cmd
@ -915,11 +916,12 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
format. format.
""" """
print("Loading client certificate file, in PEM format (blank line to cancel)") print("Loading client certificate file, in PEM format (blank line to cancel)")
print("Do not use `~` to represent your home directory.")
certfile = input("Certfile path: ").strip() certfile = input("Certfile path: ").strip()
if not certfile: if not certfile:
print("Aborting.") print("Aborting.")
return return
elif not os.path.exists(certfile): elif not os.path.isfile(certfile):
print("Certificate file {} does not exist.".format(certfile)) print("Certificate file {} does not exist.".format(certfile))
return return
print("Loading private key file, in PEM format (blank line to cancel)") print("Loading private key file, in PEM format (blank line to cancel)")
@ -927,7 +929,7 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
if not keyfile: if not keyfile:
print("Aborting.") print("Aborting.")
return return
elif not os.path.exists(keyfile): elif not os.path.isfile(keyfile):
print("Private key file {} does not exist.".format(keyfile)) print("Private key file {} does not exist.".format(keyfile))
return return
self._activate_client_cert(certfile, keyfile) self._activate_client_cert(certfile, keyfile)
@ -948,13 +950,13 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
Interactively use `openssl` command to generate a new persistent client Interactively use `openssl` command to generate a new persistent client
certificate with one year of validity. certificate with one year of validity.
""" """
certdir = os.path.join(self.config_dir, "client_certs")
print("What do you want to name this new certificate?") print("What do you want to name this new certificate?")
print("Answering `mycert` will create `~/.av98/certs/mycert.crt` and `~/.av98/certs/mycert.key`") print("Answering `mycert` will create `{0}/mycert.crt` and `{0}/mycert.key`".format(certdir))
name = input() name = input("> ")
if not name.strip(): if not name.strip():
print("Aborting.") print("Aborting.")
return return
certdir = os.path.join(self.config_dir, "client_certs")
self._generate_client_cert(certdir, name) self._generate_client_cert(certdir, name)
def _generate_client_cert(self, certdir, basename, transient=False): def _generate_client_cert(self, certdir, basename, transient=False):
@ -980,6 +982,9 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
""" """
certdir = os.path.join(self.config_dir, "client_certs") certdir = os.path.join(self.config_dir, "client_certs")
certs = glob.glob(os.path.join(certdir, "*.crt")) certs = glob.glob(os.path.join(certdir, "*.crt"))
if len(certs) == 0:
print("There are no previously generated certificates.")
return
certdir = {} certdir = {}
for n, cert in enumerate(certs): for n, cert in enumerate(certs):
certdir[str(n+1)] = (cert, os.path.splitext(cert)[0] + ".key") certdir[str(n+1)] = (cert, os.path.splitext(cert)[0] + ".key")