This is an automated email from the ASF dual-hosted git repository.

AlinsRan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 53e5bb503f fix(cli): remove stale key after resolving env var in 
config keys (#12885)
53e5bb503f is described below

commit 53e5bb503ff75660b194650682c0f8d00d417a3b
Author: Artem Pronchakov <[email protected]>
AuthorDate: Wed Jul 15 02:04:00 2026 +0300

    fix(cli): remove stale key after resolving env var in config keys (#12885)
---
 apisix/cli/file.lua                   | 41 +++++++++++++++++++----------------
 t/cli/test_http_config.sh             | 25 +++++++++++++++++++++
 t/config-center-json/route-upstream.t | 28 ++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 19 deletions(-)

diff --git a/apisix/cli/file.lua b/apisix/cli/file.lua
index 3944efbe36..ec5bb534ae 100644
--- a/apisix/cli/file.lua
+++ b/apisix/cli/file.lua
@@ -102,25 +102,8 @@ end
 
 
 local function resolve_conf_var(conf)
-    local new_keys = {}
+    local renamed_keys
     for key, val in pairs(conf) do
-        -- avoid re-iterating the table for already iterated key
-        if new_keys[key] then
-            goto continue
-        end
-        -- substitute environment variables from conf keys
-        if type(key) == "string" then
-            local new_key, _, err = var_sub(key)
-            if err then
-                return nil, err
-            end
-            if new_key ~= key then
-                new_keys[new_key] = "dummy" -- we only care about checking the 
key
-                conf.key = nil
-                conf[new_key] = val
-                key = new_key
-            end
-        end
         if type(val) == "table" then
             local ok, err = resolve_conf_var(val)
             if not ok then
@@ -146,7 +129,27 @@ local function resolve_conf_var(conf)
 
             conf[key] = new_val
         end
-        ::continue::
+
+        -- substitute environment variables from conf keys. The rename is
+        -- deferred because inserting a new key while iterating with pairs()
+        -- is undefined behavior.
+        if type(key) == "string" then
+            local new_key, _, err = var_sub(key)
+            if err then
+                return nil, err
+            end
+            if new_key ~= key then
+                renamed_keys = renamed_keys or {}
+                renamed_keys[key] = new_key
+            end
+        end
+    end
+
+    if renamed_keys then
+        for key, new_key in pairs(renamed_keys) do
+            conf[new_key] = conf[key]
+            conf[key] = nil
+        end
     end
 
     return true
diff --git a/t/cli/test_http_config.sh b/t/cli/test_http_config.sh
index 4059ca69a7..7b8bc7e96c 100755
--- a/t/cli/test_http_config.sh
+++ b/t/cli/test_http_config.sh
@@ -39,6 +39,31 @@ echo "passed: define custom shdict"
 
 git checkout conf/config.yaml
 
+# resolve environment variable used as a config key: the resolved key must be
+# rendered and the stale unresolved key must be removed from the config table
+echo '
+nginx_config:
+  http:
+    custom_lua_shared_dict:
+      "${{DICT_NAME}}": 1m
+' > conf/config.yaml
+
+DICT_NAME=env_dict make init
+
+if ! grep "lua_shared_dict env_dict 1m;" conf/nginx.conf > /dev/null; then
+    echo "failed: resolve env var used as config key"
+    exit 1
+fi
+
+if grep -F '${{DICT_NAME}}' conf/nginx.conf > /dev/null; then
+    echo "failed: stale unresolved env var key should be removed"
+    exit 1
+fi
+
+echo "passed: resolve env var used as config key"
+
+git checkout conf/config.yaml
+
 echo "
 plugins:
     - ip-restriction
diff --git a/t/config-center-json/route-upstream.t 
b/t/config-center-json/route-upstream.t
index 7082de27cb..52e0ced7b4 100644
--- a/t/config-center-json/route-upstream.t
+++ b/t/config-center-json/route-upstream.t
@@ -21,6 +21,8 @@ log_level('info');
 no_root_location();
 no_shuffle();
 
+$ENV{TEST_UPSTREAM_NODE} = '127.0.0.1:1980';
+
 run_tests();
 
 __DATA__
@@ -242,3 +244,29 @@ GET /hello
 test: one
 --- error_log
 proxy request to 127.0.0.1:1980
+
+
+
+=== TEST 8: env var used as a node key is resolved, stale key is removed
+--- yaml_config eval: $::yaml_config
+--- apisix_json
+{
+  "routes": [
+    {
+      "id": 1,
+      "uri": "/hello",
+      "upstream": {
+        "nodes": {
+          "${{TEST_UPSTREAM_NODE}}": 1
+        },
+        "type": "roundrobin"
+      }
+    }
+  ]
+}
+--- request
+GET /hello
+--- response_body
+hello world
+--- no_error_log
+failed to parse domain

Reply via email to