Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

extensions script cleanup #316

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 42 additions & 28 deletions keycloak-init-container/extensions.sh
Original file line number Diff line number Diff line change
@@ -1,52 +1,66 @@
#!/bin/bash -e
#!/bin/bash

EXTENSIONS_VOLUME="${EXTENSIONS_VOLUME:-/opt/extensions}"
set -o pipefail -o errexit -o nounset

mkdir -p "$EXTENSIONS_VOLUME"
cd "$EXTENSIONS_VOLUME"
echo "Target directory: $EXTENSIONS_VOLUME"
## External variables used by this script
declare -rx KEYCLOAK_EXTENSIONS
declare -rx EXTENSIONS_VOLUME="${EXTENSIONS_VOLUME:-/opt/extensions}"

# Download a single extension
download_extension() {
local EXTENSION_URL="$1"
local extension_url="$1"

if [[ -z "$EXTENSION_URL" ]]; then
if [[ -z "$extension_url" ]]; then
return
fi
echo
echo "Downloading extension from $EXTENSION_URL"
local CURL_COMMAND="$(curl --verbose --location --remote-name --remote-header-name --write-out "%{http_code} %{filename_effective}" --silent "$EXTENSION_URL" 2> /tmp/headers)"

local STATUS_CODE=${CURL_COMMAND:0:3}

if [ $STATUS_CODE -eq "200" ]; then
local FILENAME=${CURL_COMMAND:4}
echo
echo "Downloading extension from $extension_url"
local curl_command
curl_command="$(curl --verbose --location --remote-name --remote-header-name --write-out "%{http_code} %{filename_effective}" --silent "$extension_url" 2> /tmp/headers)"

local status_code=${curl_command:0:3}

if [[ "$status_code" -eq "200" ]]; then
local filename=${curl_command:4}
echo "Extension downloaded successfully"

# Try to get the filename from the response headers and return
# a random name if that fails
if ! grep -q -i '^< content-disposition:.*filename=' /tmp/headers ; then
local F="$(od -N8 -tx1 -An -v /dev/urandom | tr -d "").jar"
mv "$FILENAME" "$F"
FILENAME="$F"
if ! grep -q -i '^< content-disposition:.*filename=' /tmp/headers; then
local F
F="$(od -N8 -tx1 -An -v /dev/urandom | tr -d "").jar"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This produces a filename with whitespaces included. See #390.

Suggested change
F="$(od -N8 -tx1 -An -v /dev/urandom | tr -d "").jar"
F="$(od -N8 -tx1 -An -v /dev/urandom | tr -d " ").jar"
Suggested change
F="$(od -N8 -tx1 -An -v /dev/urandom | tr -d "").jar"
F="$(head -c8 /dev/urandom | xxd -p).jar"

mv "$filename" "$F"
filename="$F"
fi
echo " --> $FILENAME"
echo " --> $filename"
else
echo -e "Can not download the extension: $EXTENSION_URL\nError code: $STATUS_CODE"
((STATUS+=1))
echo -e "Can not download the extension: ${extension_url}\nError code: ${status_code}"
((FAILED_DOWNLOADS+=1))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since changing variable names to lowercase, should this one be lowercased too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's being used as a global variable, which I typically leave uppercase -- would you prefer it to be lowercase?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to keep it uppercase then.

return 1
fi

}

mkdir -p "$EXTENSIONS_VOLUME"
if ! cd "$EXTENSIONS_VOLUME"; then
echo "Failed to change directory to ${EXTENSIONS_VOLUME}"
exit 1
fi
echo "Target directory: ${EXTENSIONS_VOLUME}"

# Parse the environment variable and download the extensions from the list
IFS=,
STATUS=0
for EXT in ${KEYCLOAK_EXTENSIONS[@]} ; do
download_extension "$EXT"
FAILED_DOWNLOADS=0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If going to lowercase this one, should be lowercased here too.


for EXT in ${KEYCLOAK_EXTENSIONS[*]}; do
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where set -u above would help to catch this kind of variable names typo.

# The true guard allows us to continue attempting to download extensions if
# one fails. This will help surface all failed extension downloads at once,
# rather than needing to iterate through them one at a time.
download_extension "$EXT" || true
done
if [ "$STATUS" -ne 0 ]; then

if [[ "$FAILED_DOWNLOADS" -ne 0 ]]; then
echo
echo -e "Extensions.sh script failed at downloading all required extensions, number of failed downloads: $STATUS \n"
echo -e "Extensions.sh script failed to download all required extensions, number of failed downloads: ${FAILED_DOWNLOADS} \n"
exit 1
else
echo
Expand Down