Skip to content

Commit 4529934

Browse files
committed
scripts: renew certs automatically
1 parent da84d43 commit 4529934

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

scripts/cert-check.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import base64
2+
import sys
3+
from datetime import datetime, timedelta
4+
5+
from ndn.app_support.security_v2 import parse_certificate
6+
7+
def main():
8+
if len(sys.argv) != 2:
9+
print(f"Usage: {sys.argv[0]} <cert_path>")
10+
sys.exit(1)
11+
12+
file_path = sys.argv[1]
13+
with open(file_path, 'r') as f:
14+
b64_data = f.read()
15+
text = ''.join(b64_data.split())
16+
17+
try:
18+
cert_data = base64.standard_b64decode(text)
19+
cert = parse_certificate(cert_data)
20+
except (ValueError, IndexError):
21+
print("Malformed certificate", file_path)
22+
exit(1)
23+
24+
date_template = "%Y%m%dT%H%M%S"
25+
not_before = bytes(cert.signature_info.validity_period.not_before).decode()
26+
not_before = datetime.strptime(not_before, date_template)
27+
not_after = bytes(cert.signature_info.validity_period.not_after).decode()
28+
not_after = datetime.strptime(not_after, date_template)
29+
30+
print("Cert Status", file_path, not_before, not_after)
31+
32+
now = datetime.now()
33+
if not_before <= now <= not_after - timedelta(days=91):
34+
exit(0)
35+
else:
36+
print("Certificate is expired or will expire in less than 91 days")
37+
exit(1)
38+
39+
if __name__ == "__main__":
40+
main()

scripts/cert-renew.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
source "$(pwd)/scripts/utils.sh"
4+
5+
NDNCERT_CERTFILE="$(pwd)/dist/ndncert/site.ndncert"
6+
7+
FORCE=""
8+
if [[ $(needs_renewal "${NDNCERT_CERTFILE}") ]]; then
9+
FORCE="--force"
10+
fi
11+
12+
# Check and reissue certificates
13+
bash "$(pwd)/dist/ndncert/renew.sh" "${FORCE}"
14+
bash "$(pwd)/dist/nlsr/renew.sh" "${FORCE}"
15+
bash "$(pwd)/dist/ndn-python-repo/renew.sh" "${FORCE}"

scripts/cron-master.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ if [[ -z "$SKIP_SLEEP" ]]; then
1919
random_sleep 120
2020
fi
2121

22+
# Remove git lock file if it exists and pull the latest changes
23+
rm -f .git/index.lock
2224
git pull
2325

26+
# Renew certificates if needed
27+
bash $(pwd)/scripts/cert-renew.sh
28+
2429
PWD="${ROOT_DIR}" python3 framework/main.py
2530

2631
echo -e "Finished cron-master at $(date)" >&2

scripts/master.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ if [[ -z "$DEBUG" ]]; then
2323
python3 framework/main.py --dry
2424

2525
# Check and reissue certificates
26-
bash dist/ndncert/renew.sh
27-
bash dist/nlsr/renew.sh
28-
bash dist/ndn-python-repo/renew.sh
26+
bash $(pwd)/scripts/cert-renew.sh
2927
else
3028
echo -e "Skipping initial repo pull and bootstrap because DEBUG=1" >&2
3129
fi

scripts/utils.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
4+
35
# Allow call to fail and return exit code
46
allow_fail_status=0
57
function allow_fail {
@@ -26,7 +28,7 @@ function get_csr {
2628
# Check if a certificate needs to be renewed
2729
function needs_renewal {
2830
local cert_path=$1
29-
if [[ ! -f "${cert_path}" || -n "${HAS_FORCE}" ]]; then
31+
if [[ ! -f "${cert_path}" || -n "${HAS_FORCE}" || python3 "${SCRIPT_DIR}/cert-check.py '${cert_path}'" -ne 0 ]]; then
3032
echo -e "1"
3133
else
3234
echo -e "${cert_path} exists, skipping ..." >&2

0 commit comments

Comments
 (0)