Skip to content

Commit

Permalink
Init: Translate environment variables to parameters
Browse files Browse the repository at this point in the history
For following scripts:

* bin/kafka-console-consumer.sh
* bin/kafka-console-producer.sh
* bin/kafka-topics.sh
  • Loading branch information
itadventurer committed Jun 21, 2019
1 parent 23a1fe8 commit 1835248
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bin/kafka-console-consumer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ if [ "x$KAFKA_HEAP_OPTS" = "x" ]; then
export KAFKA_HEAP_OPTS="-Xmx512M"
fi


# Translate environment variables to parameters
PARAMS="$@"
DIR=$( dirname "${BASH_SOURCE[0]}" )
source "$DIR/utils.sh"

PARAMS=$(add_param_from_env "$KAFKA_BOOTSTRAP_SERVERS" "--bootstrap-server" "$PARAMS")
PARAMS=$(add_ssl_to_params "$KAFKA_CA_CERT_LOCATION" "$KAFKA_USER_CERT_LOCATION" "$KAFKA_USER_KEY_LOCATION" "--consumer-property" "$PARAMS")

exec $(dirname $0)/kafka-run-class.sh kafka.tools.ConsoleConsumer $PARAMS
8 changes: 8 additions & 0 deletions bin/kafka-console-producer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ if [ "x$KAFKA_HEAP_OPTS" = "x" ]; then
export KAFKA_HEAP_OPTS="-Xmx512M"
fi

# Translate environment variables to parameters
PARAMS="$@"
DIR=$( dirname "${BASH_SOURCE[0]}" )
source "$DIR/utils.sh"

PARAMS=$(add_param_from_env "$KAFKA_BOOTSTRAP_SERVERS" "--bootstrap-server" "$PARAMS")
PARAMS=$(add_ssl_to_params "$KAFKA_CA_CERT_LOCATION" "$KAFKA_USER_CERT_LOCATION" "$KAFKA_USER_KEY_LOCATION" "--producer-property" "$PARAMS")

exec $(dirname $0)/kafka-run-class.sh kafka.tools.ConsoleProducer "$@"
2 changes: 2 additions & 0 deletions bin/kafka-topics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ PARAMS="$@"
DIR=$( dirname "${BASH_SOURCE[0]}" )
source "$DIR/utils.sh"

PARAMS=$(add_param_from_env "$KAFKA_ZOOKEEPER" "--zookeeper" "$PARAMS")

exec $(dirname $0)/kafka-run-class.sh kafka.admin.TopicCommand $PARAMS
130 changes: 130 additions & 0 deletions bin/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/bin/bash
function add_param_from_env() {
local ENVVAR="$1"
local PARAM="$2"
local PARAMS="$3"
if [ -z "$PARAM" ] ; then
echo "usage: add_param_from_env [ENVVAR] [PARAM] [PARAMS]"
return 1
fi
if [ ! -z "$ENVVAR" ] ; then
if [ "$(echo "$PARAMS" | grep -- "$PARAM" || echo "false")" == "false" ] ; then
PARAMS="$PARAM $ENVVAR $PARAMS"
fi
fi
echo "$PARAMS"
}

function add_config_from_env() {
local ENVVAR="$1"
local ARGNAME="$2"
local PARAM="$3"
local PARAMS="$4"
if [ -z "$PARAM" ] ; then
echo "usage: add_config_from_env [ENVVAR] [ARGNAME] [PARAM] [PARAMS]"
return 1
fi
if [ ! -z "$ENVVAR" ] ; then
PARAMS="$ARGNAME $PARAM=$ENVVAR $PARAMS"
fi
echo "$PARAMS"
}

function pem_to_truststore() {
local KEYSTORE_LOCATION="$1"
local CERT_LOCATION="$2"
local KEYSTORE_PASSWORD="$3"
local KEY_ALIAS="$4"
if [ -z "$KEY_ALIAS" ] ; then
echo "usage: pem_to_truststore [KEYSTORE_LOCATION] [CERT_LOCATION] [KEYSTORE_PASSWORD] [KEY_ALIAS]"
return 1
fi
keytool -import -noprompt \
-keystore "$KEYSTORE_LOCATION" \
-file "$CERT_LOCATION" \
-storepass "$KEYSTORE_PASSWORD" \
-alias "$KEY_ALIAS"
}

function pem_to_keystore() {
local KEYSTORE_LOCATION="$1"
local CERT_LOCATION="$2"
local KEYSTORE_PASSWORD="$3"
local KEY_ALIAS="$4"
local KEY_LOCATION="$5"
if [ -z "$KEY_LOCATION" ] ; then
echo "usage: pem_to_keystore [KEYSTORE_LOCATION] [CERT_LOCATION] [KEYSTORE_PASSWORD] [KEY_ALIAS] [KEY_LOCATION]"
return 1
fi

# If a key and a cert is given, create a keystore
PEMFILE=$(mktemp)
PKCS12FILE=$(mktemp)
cat "$KEY_LOCATION" "$CERT_LOCATION" > $PEMFILE

# Create pkcs12 file
openssl pkcs12 -export \
-out $PKCS12FILE \
-in $PEMFILE \
-passout pass:"$KEYSTORE_PASSWORD"

# Create Java Keystore
keytool -v -importkeystore \
-srckeystore $PKCS12FILE \
-srcstoretype PKCS12 \
-destkeystore "$KEYSTORE_LOCATION" \
-storepass "$KEYSTORE_PASSWORD" \
-srcstorepass "$KEYSTORE_PASSWORD" \
-alias 1 \
-destalias "$KEY_ALIAS"

rm $PEMFILE $PKCS12FILE
}

function rand_str() {
LENGTH=$1
if [ -z "$LENGTH" ] ; then
LENGTH=10
fi
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $LENGTH | head -n 1
}

function add_ssl_to_params() {
local CA_CERT_LOCATION="$1"
local USER_CERT_LOCATION="$2"
local USER_KEY_LOCATION="$3"
local CONFIG_ARG="$4"
local PARAMS="$5"

if [ ! -z "$CA_CERT_LOCATION" ] || [ ! -z "$USER_KEY_LOCATION" ] || [ ! -z "$USER_CERT_LOCATION" ] ; then
if [ -z "$CA_CERT_LOCATION" ] ; then
echo "Missing \$CA_CERT_LOCATION!"
exit 1
fi
if [ -z "$USER_CERT_LOCATION" ] ; then
echo "Missing \$USER_CERT_LOCATION!"
exit 1
fi
if [ -z "$USER_KEY_LOCATION" ] ; then
echo "Missing \$USER_KEY_LOCATION!"
exit 1
fi
KEYSTORE_PASSWORD=$(rand_str 20)
KEY_ALIAS="mykey"

PARAMS=$(add_config_from_env "ssl" "$CONFIG_ARG" "security.protocol" "$PARAMS")

# Keystore
KEYSTORE_LOCATION=/tmp/kafka-keystore-$(rand_str 5).jks
pem_to_keystore "$KEYSTORE_LOCATION" "$USER_CERT_LOCATION" "$KEYSTORE_PASSWORD" "$KEY_ALIAS" "$USER_KEY_LOCATION" 2&>1 > /dev/null
PARAMS=$(add_config_from_env "$KEYSTORE_LOCATION" "$CONFIG_ARG" "ssl.keystore.location" "$PARAMS")
PARAMS=$(add_config_from_env "$KEYSTORE_PASSWORD" "$CONFIG_ARG" "ssl.keystore.password" "$PARAMS")

# Truststore
TRUSTSTORE_LOCATION=/tmp/kafka-truststore-$(rand_str 5).jks
pem_to_truststore "$TRUSTSTORE_LOCATION" "$CA_CERT_LOCATION" "$KEYSTORE_PASSWORD" "$KEY_ALIAS" 2&>1 > /dev/null
PARAMS=$(add_config_from_env "$TRUSTSTORE_LOCATION" "$CONFIG_ARG" "ssl.truststore.location" "$PARAMS")
PARAMS=$(add_config_from_env "$KEYSTORE_PASSWORD" "$CONFIG_ARG" "ssl.truststore.password" "$PARAMS")
fi
echo "$PARAMS"
}

0 comments on commit 1835248

Please sign in to comment.