Skip to content

Commit 54d3f71

Browse files
committed
fix(setup): Ensure built-in users exist before proceeding
Fixes #786
1 parent 384e50b commit 54d3f71

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

setup/entrypoint.sh

+16-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -eu
44
set -o pipefail
55

6-
source "$(dirname "${BASH_SOURCE[0]}")/helpers.sh"
6+
source "${BASH_SOURCE[0]%/*}"/helpers.sh
77

88

99
# --------------------------------------------------------
@@ -33,7 +33,7 @@ roles_files=(
3333

3434
echo "-------- $(date) --------"
3535

36-
state_file="$(dirname "${BASH_SOURCE[0]}")/state/.done"
36+
state_file="${BASH_SOURCE[0]%/*}"/state/.done
3737
if [[ -e "$state_file" ]]; then
3838
log "State file exists at '${state_file}', skipping setup"
3939
exit 0
@@ -65,11 +65,22 @@ fi
6565

6666
sublog 'Elasticsearch is running'
6767

68+
log 'Waiting for initialization of built-in users'
69+
70+
wait_for_builtin_users || exit_code=$?
71+
72+
if ((exit_code)); then
73+
suberr 'Timed out waiting for condition'
74+
exit $exit_code
75+
fi
76+
77+
sublog 'Built-in users were initialized'
78+
6879
for role in "${!roles_files[@]}"; do
6980
log "Role '$role'"
7081

7182
declare body_file
72-
body_file="$(dirname "${BASH_SOURCE[0]}")/roles/${roles_files[$role]:-}"
83+
body_file="${BASH_SOURCE[0]%/*}/roles/${roles_files[$role]:-}"
7384
if [[ ! -f "${body_file:-}" ]]; then
7485
sublog "No role body found at '${body_file}', skipping"
7586
continue
@@ -94,7 +105,7 @@ for user in "${!users_passwords[@]}"; do
94105
set_user_password "$user" "${users_passwords[$user]}"
95106
else
96107
if [[ -z "${users_roles[$user]:-}" ]]; then
97-
err ' No role defined, skipping creation'
108+
suberr ' No role defined, skipping creation'
98109
continue
99110
fi
100111

@@ -103,5 +114,5 @@ for user in "${!users_passwords[@]}"; do
103114
fi
104115
done
105116

106-
mkdir -p "$(dirname "${state_file}")"
117+
mkdir -p "${state_file%/*}"
107118
touch "$state_file"

setup/helpers.sh

+47
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,53 @@ function wait_for_elasticsearch {
5757
return $result
5858
}
5959

60+
# Poll the Elasticsearch users API until it returns users.
61+
function wait_for_builtin_users {
62+
local elasticsearch_host="${ELASTICSEARCH_HOST:-elasticsearch}"
63+
64+
local -a args=( '-s' '-D-' '-m15' "http://${elasticsearch_host}:9200/_security/user?pretty" )
65+
66+
if [[ -n "${ELASTIC_PASSWORD:-}" ]]; then
67+
args+=( '-u' "elastic:${ELASTIC_PASSWORD}" )
68+
fi
69+
70+
local -i result=1
71+
72+
local line
73+
local -i exit_code
74+
local -i num_users
75+
76+
# retry for max 30s (30*1s)
77+
for _ in $(seq 1 30); do
78+
num_users=0
79+
80+
# read exits with a non-zero code if the last read input doesn't end
81+
# with a newline character. The printf without newline that follows the
82+
# curl command ensures that the final input not only contains curl's
83+
# exit code, but causes read to fail so we can capture the return value.
84+
# Ref. https://unix.stackexchange.com/a/176703/152409
85+
while IFS= read -r line || ! exit_code="$line"; do
86+
if [[ "$line" =~ _reserved.+true ]]; then
87+
(( num_users++ ))
88+
fi
89+
done < <(curl "${args[@]}"; printf '%s' "$?")
90+
91+
if ((exit_code)); then
92+
result=$exit_code
93+
fi
94+
95+
# we expect more than just the 'elastic' user in the result
96+
if (( num_users > 1 )); then
97+
result=0
98+
break
99+
fi
100+
101+
sleep 1
102+
done
103+
104+
return $result
105+
}
106+
60107
# Verify that the given Elasticsearch user exists.
61108
function check_user_exists {
62109
local username=$1

0 commit comments

Comments
 (0)