Created
November 10, 2016 21:11
-
-
Save igr/e0c8ded2d5b624dd9a2b514c451133d4 to your computer and use it in GitHub Desktop.
Installs startssl CA certs into the global Java keystore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Downloads and installs the startssl CA certs into the global Java keystore | |
set -euf -o pipefail | |
# Check if JAVA_HOME is set | |
if [ "$JAVA_HOME" = "" ] | |
then | |
echo "ERROR: JAVA_HOME must be set." | |
exit 1 | |
fi | |
# Check if cacerts file is present | |
if [ ! -f $JAVA_HOME/jre/lib/security/cacerts ] | |
then | |
echo "ERROR: \$JAVA_HOME/jre/lib/security/cacerts not found. JAVA_HOME set correctly?" | |
exit 1 | |
fi | |
### change here the alias and url ### | |
declare -A certificates=( | |
["startcom.ca"]="http://www.startssl.com/certs/ca.crt" | |
["startcom.ca-g2"]="https://www.startssl.com/certs/ca-g2.crt" | |
["startcom.ca-sha2"]="https://www.startssl.com/certs/ca-sha2.crt" | |
) | |
# | |
# install one certificate | |
# usage : installCertificate certificateAlias certificateUrl | |
# | |
function installCertificate() { | |
local certificateAlias=$1 | |
local certificateUrl=$2 | |
echo "Processing $alias - ${certificates["$alias"]} ..."; | |
echo "Downloading certs $certificateAlias : $certificateUrl ..." | |
wget --quiet --continue "$certificateUrl" -O $certificateAlias.crt | |
echo "Deleting cert from cacerts keystore (sudo password required)..." | |
sudo keytool -delete -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit -noprompt -alias $certificateAlias | |
echo "Adding cert to cacerts keystore (sudo password required)..." | |
sudo keytool -import -trustcacerts -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit -noprompt -alias $certificateUrl -file $certificateAlias.crt | |
if [ -f $JAVA_HOME/jre/lib/security/jssecacerts ] | |
then | |
echo "Deleting cert from jssecacerts keystore (sudo password required)..." | |
sudo keytool -delete -keystore $JAVA_HOME/jre/lib/security/jssecacerts -storepass changeit -noprompt -alias $certificateAlias | |
echo "Adding cert to jssecacerts keystore (sudo password required)..." | |
sudo keytool -import -trustcacerts -keystore $JAVA_HOME/jre/lib/security/jssecacerts -storepass changeit -noprompt -alias $certificateUrl -file $certificateAlias.crt | |
fi | |
rm -f $certificateAlias.crt | |
} | |
# loop throw certificates map and call installCertificate | |
for alias in "${!certificates[@]}"; do | |
installCertificate $alias ${certificates["$alias"]}; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
brew
, change shebang line to:#!/usr/local/bin/bash