Skip to content
Open
Show file tree
Hide file tree
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
29 changes: 25 additions & 4 deletions apisix/cli/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ local getenv = os.getenv
local str_gmatch = string.gmatch
local str_find = string.find
local str_sub = string.sub
local table_concat = table.concat
local print = print

local _M = {}
Expand Down Expand Up @@ -92,12 +93,32 @@ end

-- Substitute env vars in raw text before parsing. YAML parser then infers
-- types naturally: `"${{VAR}}"` stays string, `${{VAR}}` infers from value.
-- Substitution runs line by line so that full-line comments stay untouched:
-- a commented-out `# ${{VAR}}` must not fail startup when VAR is unset.
-- Known limits of this text-level heuristic: an inline trailing comment is
-- still substituted (`#` may legally appear inside YAML strings), and a
-- block-scalar content line starting with `#` is skipped like a comment.
local function resolve_conf_var_in_text(text)
local new_text, _, err = var_sub(text)
if err then
return nil, err
local parts = {}
-- a UTF-8 BOM would hide the `#` of a first-line comment, so detach it
if str_sub(text, 1, 3) == "\239\187\191" then
parts[1] = str_sub(text, 1, 3)
text = str_sub(text, 4)
end
for line, eol in str_gmatch(text, "([^\n]*)(\n?)") do
if is_empty_yaml_line(line) or not str_find(line, "${{", 1, true) then
-- comment / blank / no reference: keep the line as-is
parts[#parts + 1] = line
else
local new_line, _, err = var_sub(line)
if err then
return nil, err
end
parts[#parts + 1] = new_line
end
parts[#parts + 1] = eol
end
return new_text
return table_concat(parts)
end


Expand Down
50 changes: 50 additions & 0 deletions t/cli/test_standalone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -660,5 +660,55 @@ fi

echo "passed: missing env var produces clear startup error"

# test: unset env vars referenced only in commented-out lines must be ignored
echo '
apisix:
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
' > conf/config.yaml

echo '
# ${{COMMENTED_UNSET_VAR}}
routes:
-
uri: /test-commented-var
# indented comment: ${{ANOTHER_COMMENTED_UNSET_VAR}}
plugins:
response-rewrite:
body: "commented-ok"
status_code: 200
upstream:
nodes:
"127.0.0.1:9091": 1
type: roundrobin
#END
' > conf/apisix.yaml

unset COMMENTED_UNSET_VAR
unset ANOTHER_COMMENTED_UNSET_VAR
make init

if ! make run > output.log 2>&1; then
cat output.log
echo "failed: make run should ignore env vars in commented-out lines"
exit 1
fi
sleep 0.1

code=$(curl -o /tmp/response_body -s -m 5 -w %{http_code} http://127.0.0.1:9080/test-commented-var)
body=$(cat /tmp/response_body)
if [ "$code" -ne 200 ] || [ "$body" != "commented-ok" ]; then
echo "failed: expected 200/commented-ok for /test-commented-var, got code: $code body: $body"
exit 1
fi

make stop
sleep 0.5

echo "passed: commented-out env var references are not resolved"

git checkout conf/config.yaml
git checkout conf/apisix.yaml
Loading