forked from getodk/central
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
envsubst: remove explicit subst of exported vars
For nginx config, a new approach is implemented with mawk. This is similar to envusbst, but more ergonomic: * no need to explicitly list all variables * throw error on missing variables * do not replace nginx vars like $host, $request_uri with empty strings (in contrast to envsubst when executed without an explicit variable list) Risks: There are a couple of changes which may break existing deployments: * changing client-config.json.template * requiring all substituted variable to be defined Closes getodk#473
- Loading branch information
alxndrsn
committed
Dec 9, 2024
1 parent
515bfb9
commit af73093
Showing
30 changed files
with
173 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"oidcEnabled": $OIDC_ENABLED | ||
"oidcEnabled": ${OIDC_ENABLED} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/mawk -f | ||
|
||
# Safer implemention of envsubst. | ||
# | ||
# Significant differences: | ||
# | ||
# * require curly brackets around values | ||
# * require uppercase | ||
# * throw error if value is not defined, instead of just using empty string | ||
# | ||
# mawk is the awk implementation common to the containers we need to run this | ||
# script in, so we use it here explicitly. | ||
|
||
BEGIN { | ||
errorCount = 0; | ||
} | ||
|
||
{ | ||
while(match($0, /\$\{[A-Z_][A-Z_0-9]*\}/) > 0) { | ||
k = substr($0, RSTART+2, RLENGTH-3); | ||
if(k in ENVIRON) { | ||
v = ENVIRON[k]; | ||
} else { | ||
print "ERR: var not defined on line " NR ": ${" k "}" > "/dev/fd/2"; | ||
++errorCount; | ||
v = "!!!VALUE-MISSING: " k "!!!" | ||
} | ||
gsub("\$\{" k "\}", v); | ||
} | ||
print $0; | ||
} | ||
|
||
END { | ||
if(errorCount > 0) { | ||
print "" > "/dev/fd/2"; | ||
print errorCount " error(s) found." > "/dev/fd/2"; | ||
exit 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
${NOT_DEFINED_1} ${NOT_DEFINED_2} | ||
|
||
${NOT_DEFINED_3} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ERR: var not defined on line 1: ${NOT_DEFINED_1} | ||
ERR: var not defined on line 1: ${NOT_DEFINED_2} | ||
ERR: var not defined on line 3: ${NOT_DEFINED_3} | ||
|
||
3 error(s) found. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
!!!VALUE-MISSING: NOT_DEFINED_1!!! !!!VALUE-MISSING: NOT_DEFINED_2!!! | ||
|
||
!!!VALUE-MISSING: NOT_DEFINED_3!!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Alone: | ||
sv_simple | ||
|
||
# end of line | ||
Simple value: sv_simple | ||
|
||
# Missing curlies: | ||
Should not be substituted: $SIMPLE | ||
|
||
# Start of line: | ||
sv_simple and then more content. | ||
|
||
# This is not a sub variable: | ||
$simple | ||
|
||
# Two values on the same line: | ||
first: sub_val_one, second: sub_val_two! | ||
|
||
# Two values on the same line, both repeated: | ||
first: sub_val_one, sub_val_one, sub_val_one, second: sub_val_two sub_val_two! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Alone: | ||
${SIMPLE} | ||
|
||
# end of line | ||
Simple value: ${SIMPLE} | ||
|
||
# Missing curlies: | ||
Should not be substituted: $SIMPLE | ||
|
||
# Start of line: | ||
${SIMPLE} and then more content. | ||
|
||
# This is not a sub variable: | ||
$simple | ||
|
||
# Two values on the same line: | ||
first: ${SUBVAL_1}, second: ${SUBVAL_2}! | ||
|
||
# Two values on the same line, both repeated: | ||
first: ${SUBVAL_1}, ${SUBVAL_1}, ${SUBVAL_1}, second: ${SUBVAL_2} ${SUBVAL_2}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/bin/bash -eu | ||
log() { echo >&2 "[test/envsub] $*"; } | ||
|
||
failCount=0 | ||
|
||
log "should correctly substitute provided values" | ||
if diff <( \ | ||
SIMPLE=sv_simple \ | ||
SUBVAL_1=sub_val_one \ | ||
SUBVAL_2=sub_val_two \ | ||
../../files/shared/envsub.awk \ | ||
< good-example.in | ||
) good-example.expected; then | ||
log " OK" | ||
else | ||
((++failCount)) | ||
log " FAILED" | ||
fi | ||
|
||
log "should fail when asked to substitute undefined value" | ||
if ! ../../files/shared/envsub.awk <<<"\${NOT_DEFINED}" >/dev/null 2>/dev/null; then | ||
log " OK" | ||
else | ||
((++failCount)) | ||
log " FAILED" | ||
fi | ||
|
||
log "should log all issues when asked to substitute multiple undefined values" | ||
out="$(mktemp)" | ||
err="$(mktemp)" | ||
if ../../files/shared/envsub.awk < bad-example.in >"$out" 2>"$err"; then | ||
((++failCount)) | ||
log " FAILED: expected non-zero status code" | ||
elif ! diff "$out" bad-example.stdout.expected; then | ||
((++failCount)) | ||
log " FAILED: generated stdout did not equal expected output" | ||
elif ! diff "$err" bad-example.stderr.expected; then | ||
echo "err: $err" | ||
((++failCount)) | ||
log " FAILED: generated stderr did not equal expected output" | ||
else | ||
log " OK" | ||
fi | ||
|
||
if [[ "$failCount" = 0 ]]; then | ||
log "All tests passed OK." | ||
else | ||
log "$failCount TEST(S) FAILED" | ||
exit 1 | ||
fi |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.