Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rastandy/384cad598b4e9464c17d4192a641eed9 to your computer and use it in GitHub Desktop.
Save rastandy/384cad598b4e9464c17d4192a641eed9 to your computer and use it in GitHub Desktop.
Extract AWS RDS Global Bundle certificates into a PKCS#12 keystore compatible with Java
#!/bin/bash
# Based on https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL-certificate-rotation.html
#
# Improvements
# - Removed Perl dependency
mydir=tmp/certs
if [ ! -e "${mydir}" ]
then
mkdir -p "${mydir}"
fi
truststore=${mydir}/rds-truststore.p12
storepassword=changeit
curl -sS "https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem" > ${mydir}/global-bundle.pem
awk 'split_after == 1 {n++;split_after=0} /-----END CERTIFICATE-----/ {split_after=1}{print > "rds-ca-" n ".pem"}' < ${mydir}/global-bundle.pem
for CERT in rds-ca-*; do
alias=$(openssl x509 -noout -subject -in $CERT | awk '{ split($0, fields, "/"); for (f in fields) { if (fields[f] ~ "CN=") { print substr(fields[f], 4) } } }')
echo "File: ${CERT}"
openssl x509 -in ${CERT} -out "$(basename ${CERT} .pem)".crt
rm -f ${CERT}
# echo "Importing $alias"
# keytool -import -file ${CERT} -alias "${alias}" -storepass ${storepassword} -keystore ${truststore} -noprompt
done
# rm ${mydir}/global-bundle.pem
# echo "Trust store content is: "
# keytool -list -v -keystore "$truststore" -storepass ${storepassword} | grep Alias | cut -d " " -f3- | while read alias
# do
# expiry=`keytool -list -v -keystore "$truststore" -storepass ${storepassword} -alias "${alias}" | sed -n 's/^Valid.*until: \(.*$\)/\1/p'`
# echo " Certificate ${alias} expires in '$expiry'"
# done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment