Skip to content

Commit

Permalink
add to entrypoint support for trusting a CA cert for Elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredjennings committed May 11, 2021
1 parent 330bc25 commit d8ecae8
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions package/docker/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ test "${no_config:-0}" == 1
CONFIG=$?
test "${no_config_secret:-0}" == 1
CONFIG_SECRET=$?
ES_TRUSTED_CA_CERT_FILES=()
IFS=',' read -r -a ES_TRUSTED_CA_CERT_FILES <<< "${es_trusted_ca_cert_files:-$es_trusted_ca_cert_file}"
test "${no_config_es:-0}" == 1
CONFIG_ES=$?
ES_URI=${es_uri:-}
Expand All @@ -28,6 +30,7 @@ function usage {
--no-config-secret | do not add random secret to configuration
--no-config-es | do not add elasticsearch hosts to configuration
--es-uri <uri> | use this string to configure elasticsearch hosts (format: http(s)://host:port,host:port(/prefix)?querystring)
--es-trust-ca-cert <file.pem>| trust a CA for outbound Elasticsearch TLS connections (can use multiple times)
--es-hostname <host> | resolve this hostname to find elasticsearch instances
--secret <secret> | secret to secure sessions
--show-secret | show the generated secret
Expand All @@ -51,6 +54,7 @@ do
"--es-hosts") echo "--es-hosts is deprecated, please use --es-uri"
usage;;
"--es-uri") shift; ES_URI=$1;;
"--es-trust-ca-cert") shift; ES_TRUSTED_CA_CERT_FILES+=($1);;
"--es-hostname") shift; ES_HOSTNAME=$1;;
"--secret") shift; SECRET=$1;;
"--show-secret") SHOW_SECRET=1;;
Expand Down Expand Up @@ -108,6 +112,37 @@ then
else
echo elasticsearch host not configured
fi

if test ${#ES_TRUSTED_CA_CERT_FILES} -gt 0
then
# elastic4play only lets us specify one truststore, so let's
# make it a JKS with whatever is needed inside it
echo "Creating trust store"
ES_TRUST_STORE_PASSWORD=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 64 | head -n 1)
ES_TRUST_STORE=$(mktemp --tmpdir cortex-XXXXXX.ts)
# yes, removing this temp file could cause a race condition, but
# keytool won't work if the file exists
rm -f $ES_TRUST_STORE
for cacert_pem_file in "${ES_TRUSTED_CA_CERT_FILES[@]}"
do
keytool -importcert -keystore $ES_TRUST_STORE \
-file $cacert_pem_file \
-alias $(basename $cacert_pem_file) \
-storepass $ES_TRUST_STORE_PASSWORD -noprompt
done
# ssl context is only set up if keyStore given: see
# sslContextMaybe,
# .../app/org/elastic4play/database/DBConfiguration.scala. we
# won't have any certs with private keys in this trust store,
# but that's ok. change this if ES client cert support is added
# to this script.
echo "search.keyStore.path=\"$ES_TRUST_STORE\"" >> "$CONFIG_FILE"
echo "search.keyStore.type=\"JKS\"" >> "$CONFIG_FILE"
echo "search.keyStore.password=\"$ES_TRUST_STORE_PASSWORD\"" >> "$CONFIG_FILE"
echo "search.trustStore.path=\"$ES_TRUST_STORE\"" >> "$CONFIG_FILE"
echo "search.trustStore.type=\"JKS\"" >> "$CONFIG_FILE"
echo "search.trustStore.password=\"$ES_TRUST_STORE_PASSWORD\"" >> "$CONFIG_FILE"
fi
fi

test -n "$JOB_DIRECTORY" && echo "job.directory=\"$JOB_DIRECTORY\"" >> "$CONFIG_FILE"
Expand Down Expand Up @@ -141,6 +176,7 @@ touch /var/log/cortex/application.log
chown -R "$DAEMON_USER" /var/log/cortex
chown -R "$DAEMON_USER" /etc/cortex
chown -R "$DAEMON_USER" "$CONFIG_FILE"
test -n "$ES_TRUST_STORE" && chown "$DAEMON_USER" "$ES_TRUST_STORE"
test -e /var/run/docker.sock && chown "$DAEMON_USER" /var/run/docker.sock
if test -n "$JOB_DIRECTORY"
then
Expand Down

0 comments on commit d8ecae8

Please sign in to comment.