Skip to content

Commit

Permalink
WIP overhaul & improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Alveel committed Mar 26, 2024
1 parent cd6c1fb commit b303775
Show file tree
Hide file tree
Showing 6 changed files with 507 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
.idea
.pdm-build/
.pdm-python
.python-version
__pycache_
build/
dist/
out/
src/
venv/
Lib/
Scripts/
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Cert Helper

A script to quickly make a private key and certificate signing request (CSR).
A script to quickly make certificate signing requests (CSR).

Supported key types are RSA and EC (elliptic curve).
Supported key types are RSA and EC (elliptic curve), defaulting to EC.

Optionally you can also immediately create a self-signed certificate.

This script uses the [cryptography], [click], and [PyYAML] libraries.

## Install and use

1. Install `pdm` with `pip install pdm`
1. Run `pdm install`
1. Copy [settings.sample.yaml](settings.sample.yaml) to `settings.yaml` and modify to your needs.
1. Then run `python cert-helper.py` to get started!
1. Install with `pip install cert-helper`
1. Run `cert-helper` once to generate the default `settings.yaml`
1. Modify `settings.yaml` to your needs.
1. Then run `cert-helper` to get started!

[cryptography]: https://cryptography.io/
[click]: https://click.palletsprojects.com/
Expand Down
29 changes: 23 additions & 6 deletions cert-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def private_key_load(key_file_path: Path):
"""
logger.debug("Loading private key file")
with key_file_path.open(mode="rb") as file:
return serialization.load_pem_private_key(file.read(), password=None)
private_key = serialization.load_pem_private_key(file.read(), password=None)
logger.info("Loaded private key from %s", key_file_path.name)
return private_key


def create_ec_key():
Expand Down Expand Up @@ -112,12 +114,12 @@ def sanitise_path(name, suffix):
If our primary domain name is a wildcard, we should replace '*'
with literal "wildcard".
"""
logger.debug("Sanitise path (Path)")
logger.debug("Sanitising path %s", name)
# First sanitise
replace_wildcard = name.replace("*", "wildcard")

# Then build path
path = Path(f"out/{name}/{replace_wildcard}{suffix}")
path = Path(f"out/{replace_wildcard}/{replace_wildcard}{suffix}")
path.parent.mkdir(parents=True, exist_ok=True)
return path

Expand Down Expand Up @@ -170,6 +172,8 @@ def get_key(name, key_type="ec"):
encryption_algorithm=serialization.NoEncryption(),
)
)

logger.info("Saved new private key '%s'.", key_file.name)
except FileExistsError:
logger.error(
"Private key file '%s' already exists, loading contents", key_file.name
Expand Down Expand Up @@ -392,11 +396,24 @@ def interactive():
help="Length of certificate validity, in days",
)
@click.option(
"--force",
"-f",
"--force-key",
is_flag=True,
type=bool,
help="Overwrite existing private key (if present)",
default=False,
)
@click.option(
"--force-csr",
is_flag=True,
type=bool,
help="Overwrite existing certificate signing request (if present)",
default=False,
)
@click.option(
"--force-crt",
is_flag=True,
type=bool,
help="Overwrite existing CSR and certificate, if present",
help="Overwrite existing certificate (if present)",
default=False,
)
def create(domain, key_type, sign, validity, force):
Expand Down
19 changes: 16 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "cert-helper"
version = "0.1.0"
version = "0.1.1"
description = "A CLI utility to easily create SSL keys, CSR's and (self-signed) certificates."
authors = [
{name = "Alwyn Kik", email = "[email protected]"},
Expand All @@ -12,10 +12,23 @@ dependencies = [
]
requires-python = ">=3.8"
readme = "README.md"
license = {text = "MIT"}
classifiers = [
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
"License :: OSI Approved :: MIT License",
]

[project.urls]
Homepage = "https://github.com/Alveel/cert-helper"
Issues = "https://github.com/Alveel/cert-helper/issues"

[project.scripts]
cert-helper = "cert_helper.cli:main"
cert-helper = "cert_helper:cli"

[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"

[tool.pdm.dev-dependencies]
dev = [
"black[d]>=23.10.1",
Expand Down
Empty file added src/__init__.py
Empty file.
Loading

0 comments on commit b303775

Please sign in to comment.