Changed jira to handle branch names that are just numbers
[clearscm.git] / bin / certbot_authentication.sh
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         certbot_authentication.sh
5 # Revision:     1.0
6 # Description:  Perform domain validation by creating a TXT record on the domain
7 #               from certbot. This script is designed to work with
8 #               Dreamhost.com's API and certbot running on Ubuntu 20.04. Note
9 #               that it has not been extended to handle multiple domains.
10 #
11 #               Domain validation is the process of validating you have control
12 #               over a domain. Services like Let's Encrypt can then issue you
13 #               domain validated TLS certificates for use to secure websites.
14 #
15 # See also:     https://help.dreamhost.com/hc/en-us/articles/217555707-DNS-API-commands
16 #
17 # Crontab:      0 0 20 Jan,Apr,Jul,Oct * certbot renew
18 #
19 # Note:         If you symlink /etc/letsencrypt/renewal-hooks/{pre|post|deploy}
20 #               to the proper scripts then all you need is certbox renew. Also
21 #               if certbot doesn't think it's time to renew certs you can force it
22 #               with --force-renewal
23 #
24 # Author:       Andrew@DeFaria.com
25 # Created:      Fri 04 Jun 2021 11:20:16 PDT
26 # Modified:     Mon Oct 24 11:53:38 AM PDT 2022
27 # Language:     Bash
28 #
29 # (c) Copyright 2021, ClearSCM, Inc., all rights reserved
30 #
31 ################################################################################
32 certdir=/System/Certificates
33
34 mkdir -p $certdir
35
36 logfile="$certdir/$(basename $0).log"
37
38 rm -f $logfile
39
40 function log {
41     echo $1 >> $logfile
42 } # log
43
44 log "Starting $0"
45
46 # The following are environment variables that certbot passes to us
47 #
48 # CERTBOT_DOMAIN:     Domain being authenticated.
49 # CERTBOT_VALIDATION: Validation string for domain.
50 #
51 # Check that CERTBOT_DOMAIN and CERTBOT_VALIDATION have been passed in properly:
52 if [ -z "$CERTBOT_DOMAIN"]; then
53     log "CERTBOT_DOMAIN not passed in!"
54     exit 1
55 else
56     log "CERTBOT_DOMAIN = $CERTBOT_DOMAIN"
57 fi
58
59 if [ -z "$CERTBOT_VALIDATION"]; then
60     log "CERTBOT_VALIDATION not passed in!"
61     exit 1
62 else
63     log "CERTBOT_VALIDATION = $CERTBOT_VALIDATION"
64 fi
65
66 # My DNS registar is Dreamhost. These variables are specific to their DNS API.
67 # Yours will probably be different.
68 #
69 # Dreamhost key - generate at https://panel.dreamhost.com/?tree=home.api
70 key=KHY6UJQXD9MEJZHR
71
72 # URL where the REST endpoint is
73 url="https://api.dreamhost.com/?key=$key"
74
75 # Add a TXT record to domain
76 function addTXT {
77     log "Adding TXT record $CERTBOT_DOMAIN = $CERTBOT_VALIDATION"
78     cmd="$url&unique_id=$(uuidgen)&cmd=dns-add_record&record=_acme-challenge.$CERTBOT_DOMAIN&type=TXT&value=$CERTBOT_VALIDATION"
79
80     log "cmd = $cmd"
81
82     response=$(wget -O- -q "$cmd")
83
84     log "Response = $response"
85 } # addTXT
86
87 # Verifies that the TXT record has propogated.
88 function verifyPropagation {
89     log "Enter verifyPropagation"
90
91     # We will try 20 times waiting 1 minutes in between
92     max_attempts=20
93     time_between_attempts=60
94
95     # Obviously it's not propagated immediately so first wait
96     attempt=0
97     while [ $attempt -lt $max_attempts ]; do
98         log "Waiting $time_between_attempts seconds for TXT record $CERTBOT_DOMAIN to propagate..."
99         sleep $time_between_attempts
100
101         ((attempt++))
102         log "Attempt #$attempt: Validating of propagation of TXT record $CERTBOT_DOMAIN"
103         TXT=$(nslookup -type=TXT _acme-challenge.$CERTBOT_DOMAIN | grep -vi "can't find" | grep $CERTBOT_DOMAIN)
104
105         if [ -n "$TXT" ]; then
106             log "TXT record _acme-challenge.$CERTBOT_DOMAIN propagated"
107             return
108         else
109             log "TXT record _acme-challenge.$CERTBOT_DOMAIN not propagated yet"
110         fi
111     done
112
113     log "ERROR: Unable to validate propagation"
114     exit 1
115 } # verifyPropagation
116
117 addTXT
118 verifyPropagation
119
120 # If we get here then new certs are produced but need to be made available
121 # for importation to the Synology. $certdir is a directory that is on the
122 # Synology mounted via NFS.
123 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/privkey.pem     $certdir && chmod 444 $certdir/privkey.pem
124 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/cert.pem        $certdir && chmod 444 $certdir/cert.pem
125 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/chain.pem       $certdir && chmod 444 $certdir/chain.pem
126 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/fullchain.pem   $certdir && chmod 444 $certdir/fullchain.pem