Skip to content

Commit f5aa703

Browse files
committed
Read env_file line by line so values may contain spaces
The whole file was passed through an unquoted command substitution, so the shell word-split it before export ran. Any value with a space in it was truncated to its first token and the remainder was handed to export as a separate argument, which then failed with a message naming the second token: export: `-Xmx1536m': not a valid identifier No quoting style in the env file could work around it, because the split happened after substitution. JVM options were the obvious casualty, but connection strings and user agents hit it too. Read one line at a time instead and pass export a single quoted argument. Values are taken verbatim, matching `docker --env-file`, so quotes in the file are part of the value rather than delimiters around it. The SC2046 suppression that was hiding this is gone. A line that is not NAME=VALUE is now rejected with a message naming the offending line, rather than reaching export and producing one about identifiers. This is also the error someone gets after passing a path where the option wants the variables themselves. Note that ENV_FILE carries no trailing newline, so the loop needs the -n test to avoid dropping the last line. Closes #21
1 parent 9cee47e commit f5aa703

2 files changed

Lines changed: 65 additions & 8 deletions

File tree

‎scripts/docker-entrypoint.sh‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,31 @@ configure_ssh_key() {
3434

3535
configure_env_file() {
3636
printf '%s' "$ENV_FILE" > "${ENV_FILE_PATH}"
37-
env_file_len=$(grep -v '^#' ${ENV_FILE_PATH}|grep -v '^$' -c)
38-
if [[ $env_file_len -gt 0 ]]; then
37+
env_file_len=$(grep -cv -e '^#' -e '^$' "${ENV_FILE_PATH}" || true)
38+
if [[ ${env_file_len} -gt 0 ]]; then
3939
echo "Environment Variables: Additional values"
4040
if [ "${DEBUG}" != "0" ]; then
4141
echo "Environment vars before: $(env|wc -l)"
4242
fi
43-
# shellcheck disable=SC2046
44-
export $(grep -v '^#' ${ENV_FILE_PATH} | grep -v '^$' | xargs -d '\n')
43+
# Read one line at a time and hand export a single quoted argument, so a
44+
# value containing spaces survives. Passing the whole file through an
45+
# unquoted $(...) word-split it before export ever ran.
46+
#
47+
# Values are taken verbatim, matching `docker --env-file`: quotes in the
48+
# file are part of the value, not delimiters around it.
49+
#
50+
# ENV_FILE has no trailing newline, so the `-n` test is what keeps the
51+
# final line from being dropped by read's non-zero exit.
52+
while IFS= read -r line || [ -n "${line}" ]; do
53+
case "${line}" in
54+
''|\#*) continue ;;
55+
esac
56+
if [[ ! "${line}" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
57+
echo "Environment Variables: '${line}' is not in NAME=VALUE format"
58+
exit 1
59+
fi
60+
export "${line?}"
61+
done < "${ENV_FILE_PATH}"
4562
if [ "${DEBUG}" != "0" ]; then
4663
echo "Environment vars after: $(env|wc -l)"
4764
fi

‎tests/entrypoint.bats‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,60 @@ export_from_env_file() {
151151
[ "$output" = "key=value" ]
152152
}
153153

154-
@test "issue #21: env_file exports a value containing spaces" {
155-
skip "https://github.com/kitconcept/docker-stack-deploy/issues/21 -- the unquoted \$(...) word-splits the value before export runs"
154+
@test "env_file exports a value containing spaces (issue #21)" {
156155
export_from_env_file 'SOLR_JAVA_MEM=-Xms1536m -Xmx1536m' SOLR_JAVA_MEM
157156
[ "$status" -eq 0 ]
158157
[ "$output" = "-Xms1536m -Xmx1536m" ]
159158
}
160159

161-
@test "issue #21: quotes in an env_file value are kept verbatim" {
162-
skip "https://github.com/kitconcept/docker-stack-deploy/issues/21 -- pending the parser rewrite; we match 'docker --env-file' and do not strip quotes"
160+
@test "env_file keeps quotes in a value verbatim (issue #21)" {
163161
export_from_env_file 'GREETING="hello world"' GREETING
164162
[ "$status" -eq 0 ]
165163
[ "$output" = '"hello world"' ]
166164
}
167165

166+
@test "env_file preserves several space-bearing values in one file (issue #21)" {
167+
export_from_env_file $'JAVA_OPTS=-Xms1g -Xmx2g\nDB_USER=plone' JAVA_OPTS
168+
[ "$status" -eq 0 ]
169+
[ "$output" = "-Xms1g -Xmx2g" ]
170+
}
171+
172+
@test "env_file still exports later keys after a space-bearing value" {
173+
export_from_env_file $'JAVA_OPTS=-Xms1g -Xmx2g\nDB_USER=plone' DB_USER
174+
[ "$status" -eq 0 ]
175+
[ "$output" = "plone" ]
176+
}
177+
178+
@test "env_file preserves a trailing space in a value" {
179+
export_from_env_file 'GREETING=hello ' GREETING
180+
[ "$status" -eq 0 ]
181+
[ "$output" = "hello " ]
182+
}
183+
184+
@test "env_file rejects a line that is not NAME=VALUE" {
185+
# Issue #3 reported passing a path (".env") and getting an opaque
186+
# "not a valid identifier" error from export. Fail with a clear message.
187+
run env -i PATH="${PATH}" HOME="${BATS_TEST_TMPDIR}" ENV_FILE=".env" \
188+
bash -c "
189+
source '${ENTRYPOINT}'
190+
ENV_FILE_PATH=\"\${HOME}/.env\"
191+
configure_env_file
192+
"
193+
[ "$status" -eq 1 ]
194+
[[ "$output" == *"'.env' is not in NAME=VALUE format"* ]]
195+
}
196+
197+
@test "env_file rejects a name with a hyphen" {
198+
run env -i PATH="${PATH}" HOME="${BATS_TEST_TMPDIR}" ENV_FILE="MY-VAR=1" \
199+
bash -c "
200+
source '${ENTRYPOINT}'
201+
ENV_FILE_PATH=\"\${HOME}/.env\"
202+
configure_env_file
203+
"
204+
[ "$status" -eq 1 ]
205+
[[ "$output" == *"is not in NAME=VALUE format"* ]]
206+
}
207+
168208
# --- scale_after --------------------------------------------------------------
169209

170210
@test "scale_after does nothing when unset" {

0 commit comments

Comments
 (0)