I'm trying to set up a deploy script to import a renewed certificate into a Java keystore and seem to be running into permissions issues. The code being run is:
#!/bin/bash
# Convert the private key and certificate to a PKCS12 file
openssl pkcs12 -export -in /etc/letsencrypt/live/ **DOMAIN_SUBDOMAIN** /fullchain.pem -inkey /etc/letsencrypt/live/ **DOMAIN_SUBDOMAIN** /privkey.pem -out /etc/letsencrypt/live/ **DOMAIN_SUBDOMAIN** /pkcs.p12 -name **KEYCLOAK_SSL_ALIAS** -passout pass: **KEYCLOAK_SSL_PASSWORD**
# Remove the old certificate from our keystore
keytool -delete -noprompt -alias **KEYCLOAK_SSL_ALIAS** -keystore /opt/keycloak/current/standalone/configuration/keycloak.jks -storepass **KEYSTORE_PASSWORD**
# Import the new certificate to our keystore
keytool -importkeystore -deststorepass **KEYSTORE_PASSWORD** -destkeypass **KEYCLOAK_SSL_PASSWORD** -destkeystore /opt/keycloak/current/standalone/configuration/keycloak.jks -srckeystore /etc/letsencrypt/live/ **DOMAIN_SUBDOMAIN** /pkcs.p12 -srcstoretype PKCS12 -srcstorepass **KEYCLOAK_SSL_PASSWORD** -alias **KEYCLOAK_SSL_ALIAS**
# Restart Keycloak
systemctl restart keycloak
The file is executable. For testing I have put this in the post folder. If I run
sudo certbot renew --dry-run
I get:
Error output from post-hook command new-cert-to-keystore.sh:
Importing keystore /etc/letsencrypt/live/my.domain.com/pkcs.p12 to /opt/keycloak/current/standalone/configuration/keycloak.jks...
But no actual detail.
If I run the script directly with:
sudo ./new-cert-to-keystore.sh
it works fine.
This is on Ubuntu 20.04. How should Certbot and this script be set up/installed/permissions applied for the script to run successfully? Many thanks for any pointers.