Skip to content

Commit

Permalink
Add SLACK_CHANNEL env var to allow to customise the Slack channel ins…
Browse files Browse the repository at this point in the history
…tead of the hardcoded #certificates
  • Loading branch information
uschti committed Aug 27, 2018
1 parent ceab979 commit f229e6a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ ARG GD_KEY=undefined
ARG GD_SECRET=undefined
ARG SLACK_URL=undefined
ARG SLACK_DOMAIN_TITLE=undefined
ARG SLACK_CHANNEL=undefined

ENV GD_KEY $GD_KEY
ENV GD_SECRET $GD_SECRET
ENV SLACK_URL $SLACK_URL
ENV SLACK_DOMAIN_TITLE $SLACK_DOMAIN_TITLE
ENV SLACK_CHANNEL $SLACK_CHANNEL


# Copy config and script files
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ docker container run --rm -e GD_KEY=someMagicKey \
-e GD_SECRET=theCorrespondingSecret \
-e SLACK_URL=https://hooks.slack.com/services/XXXXXXXXXXX \
-e SLACK_DOMAIN_TITLE=yourdomain.com \
-e SLACK_CHANNEL=#certificates \
-v /data/certs:/data/certs \
-v /data/keys:/data/keys \
-v renew_certificates.conf:/data/letsencrypt/renew_certificates.conf \
Expand All @@ -44,6 +45,7 @@ docker container run --rm -e GD_KEY=someMagicKey \
- **SLACK_URL**: Slack incoming webhook URL
- **SLACK_DOMAIN_TITLE**: Slack title (to easily identify the domain)
- **SLACK_CHANNEL**: Slack channel

### Volumes
- **/data/certs**: Folder where the created certificate will be copied (example of created file: yourdomain.com.crt)
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
GD_SECRET: theCorrespondingSecret
SLACK_URL: https://hooks.slack.com/services/XXXXXXXXXXX
SLACK_DOMAIN_TITLE: yourdomain.com
SLACK_CHANNEL: "#certificates"
volumes:
- /data/certs:/data/certs
- /data/keys:/data/keys
Expand Down
11 changes: 6 additions & 5 deletions renew_certificates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export LC_CTYPE="en_US.UTF-8"
## Slack URL
SLACK_URL=$SLACK_URL
SLACK_WEBHOOK_USERNAME=$SLACK_DOMAIN_TITLE
SLACK_CHANNEL=$SLACK_CHANNEL

## Preconditions
if [ "$EUID" -eq 0 ]; then
Expand All @@ -21,7 +22,7 @@ if [ "$EUID" -eq 0 ]; then
LETSENCRYPT_BASEPATH="/data/letsencrypt"
CONFIG_FILE="/data/letsencrypt/renew_certificates.conf"

## Copy the domains to dehydrated config
## Copy the domains to dehydrated config
cat $CONFIG_FILE > dehydrated/domains.txt

## Generate the certificates
Expand Down Expand Up @@ -49,11 +50,11 @@ while read d; do
expirationDate=$(openssl x509 -enddate -noout -in $CERT_FILE)
expirationDate=${expirationDate#*=}

curl -X POST --data-urlencode "payload={\"channel\": \"#certificates\", \"username\": \"$SLACK_WEBHOOK_USERNAME\", \"attachments\": [ { \"title\": \"*$DOMAIN*\", \"color\": \"good\", \"text\": \"Certificate successfully renewed! Next expiration date: *$expirationDate*\" } ], \"icon_url\": \"https://cdn.uschti.com/images/sdeiuhfeigf82788238bdhjkdsfb820923eguf4.png\"}" $SLACK_URL
curl -X POST --data-urlencode "payload={\"channel\": \"$SLACK_CHANNEL\", \"username\": \"$SLACK_WEBHOOK_USERNAME\", \"attachments\": [ { \"title\": \"*$DOMAIN*\", \"color\": \"good\", \"text\": \"Certificate successfully renewed! Next expiration date: *$expirationDate*\" } ], \"icon_url\": \"https://cdn.uschti.com/images/sdeiuhfeigf82788238bdhjkdsfb820923eguf4.png\"}" $SLACK_URL

else
echo "WARN: Cannot find Certificate: "$CERT_FILE" or privatekey: "$KEY_FILE"!!!"
curl -X POST --data-urlencode "payload={\"channel\": \"#certificates\", \"username\": \"$SLACK_WEBHOOK_USERNAME\", \"attachments\": [ { \"title\": \"*$DOMAIN*\", \"color\": \"warning\", \"text\": \"Cannot find Certificate: "$CERT_FILE" or privatekey: "$KEY_FILE"!!! \" } ], \"icon_url\": \"https://cdn.uschti.com/images/sdeiuhfeigf82788238bdhjkdsfb820923eguf4.png\"}" $SLACK_URL
curl -X POST --data-urlencode "payload={\"channel\": \"$SLACK_CHANNEL\", \"username\": \"$SLACK_WEBHOOK_USERNAME\", \"attachments\": [ { \"title\": \"*$DOMAIN*\", \"color\": \"warning\", \"text\": \"Cannot find Certificate: "$CERT_FILE" or privatekey: "$KEY_FILE"!!! \" } ], \"icon_url\": \"https://cdn.uschti.com/images/sdeiuhfeigf82788238bdhjkdsfb820923eguf4.png\"}" $SLACK_URL
fi
done <$CONFIG_FILE
echo "INFO: DONE to move the certificates and keys to /data/certs and /data/keys !"
Expand All @@ -62,12 +63,12 @@ echo "INFO: DONE with certificate renew!"

else
echo "ERROR: Please run script as root!"
curl -X POST --data-urlencode "payload={\"channel\": \"#certificates\", \"username\": \"$SLACK_WEBHOOK_USERNAME\", \"attachments\": [ { \"title\": \"*$DOMAIN*\", \"color\": \"danger\", \"text\": \"Please run script as root! \" } ], \"icon_url\": \"https://cdn.uschti.ch/images/sdeiuhfeigf82788238bdhjkdsfb820923eguf4.png\"}" $SLACK_URL
curl -X POST --data-urlencode "payload={\"channel\": \"$SLACK_CHANNEL\", \"username\": \"$SLACK_WEBHOOK_USERNAME\", \"attachments\": [ { \"title\": \"*$DOMAIN*\", \"color\": \"danger\", \"text\": \"Please run script as root! \" } ], \"icon_url\": \"https://cdn.uschti.ch/images/sdeiuhfeigf82788238bdhjkdsfb820923eguf4.png\"}" $SLACK_URL
fi

## Log End date
echo ""
echo "----------------------------------------"
echo "End date: "`date`
echo "----------------------------------------------------------------------------------------------------"
echo ""
echo ""

0 comments on commit f229e6a

Please sign in to comment.