Skip to content

Commit

Permalink
tool: Add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Spindel committed Apr 18, 2024
1 parent b81922d commit eafabe4
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions caramel/scripts/tool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/env python3
# vim: expandtab shiftwidth=4 softtabstop=4 tabstop=17 filetype=python :
"""Admin tool to sign/refresh certificates."""

import argparse
import concurrent.futures
Expand All @@ -18,6 +19,7 @@


def cmdline():
"""Parse commandline."""
parser = argparse.ArgumentParser()

config.add_inifile_argument(parser)
Expand Down Expand Up @@ -82,13 +84,15 @@ def cmdline():


def error_out(message, exc=None):
"""Print error message and exit with failure code."""
LOG.error(message)
if exc is not None:
LOG.error(str(exc))
sys.exit(1)


def print_list():
"""Print a list of certificates."""
valid_requests = models.CSR.list_csr_printable()

def unsigned_last(csr):
Expand All @@ -104,12 +108,14 @@ def unsigned_last(csr):


def calc_lifetime(lifetime=relativedelta(hours=24)):
"""Calculate lifetime of certificate."""
now = datetime.datetime.utcnow()
future = now + lifetime
return future - now


def csr_wipe(csr_id):
"""Wipe a certain csr."""
with transaction.manager:
CSR = models.CSR.query().get(csr_id)
if not CSR:
Expand All @@ -119,6 +125,7 @@ def csr_wipe(csr_id):


def csr_clean(csr_id):
"""Clean out old certs."""
with transaction.manager:
CSR = models.CSR.query().get(csr_id)
if not CSR:
Expand All @@ -129,12 +136,14 @@ def csr_clean(csr_id):


def clean_all():
"""Clean out all old requests."""
csrlist = models.CSR.refreshable()
for csr in csrlist:
csr_clean(csr.id)


def csr_reject(csr_id):
"""Reject a request."""
with transaction.manager:
CSR = models.CSR.query().get(csr_id)
if not CSR:
Expand All @@ -145,6 +154,7 @@ def csr_reject(csr_id):


def csr_sign(csr_id, ca, timedelta, backdate):
"""Sign a request with ca, valid for timedelta, or backdate as well."""
with transaction.manager:
CSR = models.CSR.query().get(csr_id)
if not CSR:
Expand Down Expand Up @@ -172,6 +182,7 @@ def csr_sign(csr_id, ca, timedelta, backdate):


def refresh(csr, ca, lifetime_short, lifetime_long, backdate):
"""Refresh a single csr."""
last = csr.certificates.first()
old_lifetime = last.not_after - last.not_before
# XXX: In a backdated cert, this is almost always true.
Expand All @@ -185,6 +196,7 @@ def refresh(csr, ca, lifetime_short, lifetime_long, backdate):


def csr_resign(ca, lifetime_short, lifetime_long, backdate):
"""Re-sign all requests for lifetime."""
with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor:
try:
csrlist = models.CSR.refreshable()
Expand Down

0 comments on commit eafabe4

Please sign in to comment.