Merge branch 'master' of git+ssh://github.com/adefaria/clearscm
[clearscm.git] / bin / certbot_cleanup.sh
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         certbot_cleanup.sh
5 # Revision:     1.0
6 # Description:  Perform cleanup after domain validation by removing the TXT
7 #               record on the domain created by certbot_authentication.sh
8 #
9 #               Domain validation is the process of validating you have control
10 #               over a domain. Services like Let's Encrypt can then issue you
11 #               domain validated TLS certificates for use to secure websites.
12 #
13 # See also:     https://help.dreamhost.com/hc/en-us/articles/217555707-DNS-API-commands
14 #
15 # Crontab:      0 0 20 Jan,Apr,Jul,Oct * certbot renew
16 #
17 # Author:       Andrew@DeFaria.com
18 # Created:      Fri 04 Jun 2021 11:20:16 PDT
19 # Modified:     Mon Oct 24 11:53:38 AM PDT 2022
20 # Language:     Bash
21 #
22 # (c) Copyright 2021, ClearSCM, Inc., all rights reserved
23 #
24 ################################################################################
25 certdir="/System/Certificates"
26
27 mkdir -p $certdir
28
29 logfile="$certdir/$(basename $0).log"
30
31 rm -f $logfile
32
33 function log {
34     echo $1 >> $logfile
35 } # log
36
37 log "Starting $0"
38
39 # The following are environment variables that certbot passes to us
40 #
41 # CERTBOT_DOMAIN:     Domain being authenticated.
42 # CERTBOT_VALIDATION: Validation string for domain
43 #
44 # Check that CERTBOT_DOMAIN and CERTBOT_VALIDATION have been passed in properly
45 if [ -z "$CERTBOT_DOMAIN"]; then
46     log "CERTBOT_DOMAIN not passed in!"
47     exit 1
48 else
49     log "CERTBOT_DOMAIN = $CERTBOT_DOMAIN"
50 fi
51
52 if [ -z "$CERTBOT_VALIDATION"]; then
53     log "CERTBOT_VALIDATION not passed in!"
54     exit 1
55 else
56     log "CERTBOT_VALIDATION = $CERTBOT_VALIDATION"
57 fi
58
59 # My DNS registar is Dreamhost. These variables are specific to their DNS API.
60 # Yours will probably be different.
61 #
62 # Dreamhost key - generate at https://panel.dreamhost.com/?tree=home.api
63 key=KHY6UJQXD9MEJZHR
64
65 # URL where the REST endpoint is
66 url="https://api.dreamhost.com/?key=$key"
67
68 # Remove a TXT record. Oddly you must also specify the value.
69 function removeTXT {
70     log "Removing TXT record $CERTBOT_DOMAIN = $CERTBOT_VALIDATION"
71     cmd="$url&unique_id=$(uuidgen)&cmd=dns-remove_record&record=_acme-challenge.$CERTBOT_DOMAIN&type=TXT&value=$CERTBOT_VALIDATION"
72     log "cmd: $cmd"
73
74     response=$(wget -O- -q "$cmd")
75
76     log "Response = $response"
77 } # removeTXT
78
79 removeTXT
80
81 # Removal is instanteous but propagation will take some time. No need to wait
82 # around though...
83
84 # Now deploy new certs
85 /opt/clearscm/bin/certbot_deploy.sh