#!/bin/bash
if [ "$USER" != "root" ]
then
echo "Certificates can only be retrieved by root."
exit 2
fi
KEYSTORE_PASSWORD=$1
CERT_PASSWORD=$2
# Change into the keystore folder where keystore.jks resides
cd /keystore/folder
echo `date` > cert.log
# Using certbot request a new certificate (note --force-renew is needed for subsequent runs)
certbot certonly --force-renew --webroot -w /http/web/resources/root/ -d domain.name.com 2>&1 >> cert.log
# Use openssl to convert and merge the privkey.pen and fullchain.pem into a cert.p12
openssl pkcs12 -export -out cert.p12 -inkey /etc/letsencrypt/live/domain.name.com/privkey.pem -in /etc/letsencrypt/live/domain.name.com/fullchain.pem -name "domain.name.com" -password pass:$CERT_PASSWORD 2>&1 >> cert.log
# Using keytool delete the older certificate so it won't ask if you want to overwrite
keytool -delete -keystore keystore.jks -alias domain.name.com -storepass $KEYSTORE_PASSWORD 2>&1 >> cert.log
# Using keytool add the new certificate
keytool -importkeystore -srckeystore cert.p12 -srcstoretype pkcs12 -srcstorepass $CERT_PASSWORD -destkeystore keystore.jks -deststorepass $KEYSTORE_PASSWORD 2>&1 >> cert.log
# Restart the web service
service webd restart
echo `date` >> cert.log