You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "nic-6443 (via GitHub)" <gi...@apache.org> on 2026/06/11 04:40:32 UTC

[PR] fix: redis connections with different database/credentials must not share a keepalive pool [apisix]

nic-6443 opened a new pull request, #13516:
URL: https://github.com/apache/apisix/pull/13516

   ### Description
   
   When multiple plugin configurations point at the same redis `host:port` but use a different `redis_database`, `redis_username` or `redis_password`, their counters end up mixed together.
   
   For example, two routes using `limit-count` with `policy: redis` against the same redis instance but `redis_database: 1` and `redis_database: 2` cross-contaminate: route A's `plugin-limit-count*` keys land in route B's database and vice versa.
   
   Root cause: `apisix/utils/redis.lua` calls `red:connect()` without an explicit `pool` option, so the cosocket keepalive pool defaults to `host:port` and is shared by all configurations pointing at that address, while `AUTH` and `SELECT` are only executed on fresh connections (`get_reused_times() == 0`). A connection created (and `SELECT`ed / `AUTH`ed) for one configuration is later reused as-is by another configuration with a different database or credentials.
   
   Fix: pass an explicit `pool` name in the connect options so each keepalive pool only holds connections with the same scheme (redis/rediss), host, port, database and credentials. Credentials are included as a crc32 digest rather than plaintext. `AUTH`/`SELECT` on fresh connections only remains correct once pools are homogeneous.
   
   This affects `limit-count`, `limit-req` and `limit-conn` with `policy: redis`, since they all share `apisix/utils/redis.lua` and return connections to the pool via `set_keepalive`.
   
   #### Which issue(s) this PR fixes:
   
   Fixes #13454
   
   ### Checklist
   
   - [x] I have explained the need for this PR and the problem it solves
   - [x] I have explained the changes or the new features added to this PR
   - [x] I have added tests corresponding to this change
   - [ ] I have updated the documentation to reflect this change
   - [x] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: redis connections with different database/credentials must not share a keepalive pool [apisix]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13516:
URL: https://github.com/apache/apisix/pull/13516#discussion_r3394726780


##########
t/plugin/limit-count-redis5.t:
##########
@@ -0,0 +1,298 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+    if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+        $SkipReason = "unavailable for the hup tests";
+
+    } else {
+        $ENV{TEST_NGINX_USE_HUP} = 1;
+        undef $ENV{TEST_NGINX_USE_STAP};
+    }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->error_log && !$block->no_error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+
+    my $extra_init_worker_by_lua = $block->extra_init_worker_by_lua // "";
+    $extra_init_worker_by_lua .= <<_EOC_;
+        require("lib.test_redis").flush_all({password = "foobared"})
+_EOC_
+
+    $block->set_value("extra_init_worker_by_lua", $extra_init_worker_by_lua);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: routes pointing to the same redis but different databases must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 1,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 2,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.sleep(0.5)
+
+            -- alternate requests between the two routes so that the second
+            -- route is served by a connection put into the keepalive pool
+            -- by the first one if the pool is wrongly shared
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"])
+                end
+            end
+
+            -- each database must contain only its own route's counter,
+            -- decremented by exactly the 2 requests sent to that route
+            local redis = require "resty.redis"
+            for db = 1, 2 do
+                local red = redis:new()
+                red:set_timeout(1000)
+                local ok, err = red:connect("127.0.0.1", 6379)
+                if not ok then
+                    ngx.say("failed to connect: ", err)
+                    return
+                end
+                red:select(db)
+                local keys, err = red:keys("plugin-limit-count*")
+                if not keys then
+                    ngx.say("failed to get keys: ", err)
+                    return
+                end
+                ngx.say("db ", db, " keys: ", #keys)
+                for _, key in ipairs(keys) do
+                    local counter = red:get(key)
+                    ngx.say("db ", db, " counter: ", counter)
+                end
+                red:close()
+            end
+        }
+    }
+--- timeout: 10
+--- response_body
+200 remaining: 4
+200 remaining: 4
+200 remaining: 3
+200 remaining: 3
+db 1 keys: 1
+db 1 counter: 3
+db 2 keys: 1
+db 2 counter: 3
+
+
+
+=== TEST 2: routes with different credentials must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- the CI redis has a user alice with the same permissions
+            -- as the default user
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_username": "alice",
+                            "redis_password": "somepassword",
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.sleep(0.5)
+
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"])

Review Comment:
   When a request fails (e.g. 500 due to Redis AUTH), the rate-limit header may be absent; printing it without a fallback makes the output less explicit. Use an `or "nil"` fallback so the expected response body can assert failure cases reliably.



##########
t/plugin/limit-count-redis5.t:
##########
@@ -0,0 +1,298 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+    if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+        $SkipReason = "unavailable for the hup tests";
+
+    } else {
+        $ENV{TEST_NGINX_USE_HUP} = 1;
+        undef $ENV{TEST_NGINX_USE_STAP};
+    }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->error_log && !$block->no_error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+
+    my $extra_init_worker_by_lua = $block->extra_init_worker_by_lua // "";
+    $extra_init_worker_by_lua .= <<_EOC_;
+        require("lib.test_redis").flush_all({password = "foobared"})
+_EOC_
+
+    $block->set_value("extra_init_worker_by_lua", $extra_init_worker_by_lua);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: routes pointing to the same redis but different databases must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 1,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 2,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.sleep(0.5)
+
+            -- alternate requests between the two routes so that the second
+            -- route is served by a connection put into the keepalive pool
+            -- by the first one if the pool is wrongly shared
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"])
+                end
+            end
+
+            -- each database must contain only its own route's counter,
+            -- decremented by exactly the 2 requests sent to that route
+            local redis = require "resty.redis"
+            for db = 1, 2 do
+                local red = redis:new()
+                red:set_timeout(1000)
+                local ok, err = red:connect("127.0.0.1", 6379)
+                if not ok then
+                    ngx.say("failed to connect: ", err)
+                    return
+                end
+                red:select(db)
+                local keys, err = red:keys("plugin-limit-count*")
+                if not keys then
+                    ngx.say("failed to get keys: ", err)
+                    return
+                end
+                ngx.say("db ", db, " keys: ", #keys)
+                for _, key in ipairs(keys) do
+                    local counter = red:get(key)
+                    ngx.say("db ", db, " counter: ", counter)
+                end
+                red:close()
+            end
+        }
+    }
+--- timeout: 10
+--- response_body
+200 remaining: 4
+200 remaining: 4
+200 remaining: 3
+200 remaining: 3
+db 1 keys: 1
+db 1 counter: 3
+db 2 keys: 1
+db 2 counter: 3
+
+
+
+=== TEST 2: routes with different credentials must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- the CI redis has a user alice with the same permissions
+            -- as the default user
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_username": "alice",
+                            "redis_password": "somepassword",
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_keepalive_pool": 1
+                        }

Review Comment:
   TEST 2 currently configures route 2 with no credentials, so it can still pass even if connections are incorrectly shared (route 2 can reuse the already-AUTHed connection from route 1). To actually assert pool separation by credentials, configure route 2 with intentionally wrong credentials and expect it to keep failing even after route 1 has warmed the keepalive pool.



##########
t/plugin/limit-count-redis5.t:
##########
@@ -0,0 +1,298 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+    if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+        $SkipReason = "unavailable for the hup tests";
+
+    } else {
+        $ENV{TEST_NGINX_USE_HUP} = 1;
+        undef $ENV{TEST_NGINX_USE_STAP};
+    }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->error_log && !$block->no_error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+
+    my $extra_init_worker_by_lua = $block->extra_init_worker_by_lua // "";
+    $extra_init_worker_by_lua .= <<_EOC_;
+        require("lib.test_redis").flush_all({password = "foobared"})
+_EOC_
+
+    $block->set_value("extra_init_worker_by_lua", $extra_init_worker_by_lua);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: routes pointing to the same redis but different databases must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 1,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 2,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.sleep(0.5)
+
+            -- alternate requests between the two routes so that the second
+            -- route is served by a connection put into the keepalive pool
+            -- by the first one if the pool is wrongly shared
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"])
+                end
+            end
+
+            -- each database must contain only its own route's counter,
+            -- decremented by exactly the 2 requests sent to that route
+            local redis = require "resty.redis"
+            for db = 1, 2 do
+                local red = redis:new()
+                red:set_timeout(1000)
+                local ok, err = red:connect("127.0.0.1", 6379)
+                if not ok then
+                    ngx.say("failed to connect: ", err)
+                    return
+                end
+                red:select(db)
+                local keys, err = red:keys("plugin-limit-count*")
+                if not keys then
+                    ngx.say("failed to get keys: ", err)
+                    return
+                end
+                ngx.say("db ", db, " keys: ", #keys)
+                for _, key in ipairs(keys) do
+                    local counter = red:get(key)
+                    ngx.say("db ", db, " counter: ", counter)
+                end
+                red:close()
+            end
+        }
+    }
+--- timeout: 10
+--- response_body
+200 remaining: 4
+200 remaining: 4
+200 remaining: 3
+200 remaining: 3
+db 1 keys: 1
+db 1 counter: 3
+db 2 keys: 1
+db 2 counter: 3
+
+
+
+=== TEST 2: routes with different credentials must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- the CI redis has a user alice with the same permissions
+            -- as the default user
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_username": "alice",
+                            "redis_password": "somepassword",
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.sleep(0.5)
+
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"])
+                end
+            end
+        }
+    }
+--- timeout: 10
+--- response_body
+200 remaining: 4
+200 remaining: 4
+200 remaining: 3
+200 remaining: 3

Review Comment:
   If route 2 is configured with wrong credentials to validate pool separation, the expected output in TEST 2 should assert that `/hello1` keeps failing instead of returning 200s.



##########
apisix/utils/redis.lua:
##########
@@ -26,9 +27,22 @@ local function redis_cli(conf)
 
     red:set_timeouts(timeout, timeout, timeout)
 
+    -- AUTH and SELECT are run only on fresh connections, so connections
+    -- with different databases or credentials must not share the default
+    -- host:port keepalive pool, otherwise a reused connection may be bound
+    -- to an unexpected database or user
+    local pool = (conf.redis_ssl and "rediss#" or "redis#")
+                 .. conf.redis_host .. "#" .. (conf.redis_port or 6379)
+                 .. "#" .. (conf.redis_database or 0)
+    if conf.redis_password and conf.redis_password ~= '' then

Review Comment:
   The keepalive pool name does not include the `redis_ssl_verify` flag. If two configs differ only by `redis_ssl_verify`, a strict config can reuse a connection established with verification disabled, effectively bypassing its TLS verification expectation. Include `redis_ssl_verify` (and any other TLS-affecting options you support) in the pool name to keep pools homogeneous.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: redis connections with different database/credentials must not share a keepalive pool [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 merged PR #13516:
URL: https://github.com/apache/apisix/pull/13516


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: redis connections with different database/credentials must not share a keepalive pool [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #13516:
URL: https://github.com/apache/apisix/pull/13516#discussion_r3397095564


##########
apisix/utils/redis.lua:
##########
@@ -26,9 +27,22 @@ local function redis_cli(conf)
 
     red:set_timeouts(timeout, timeout, timeout)
 
+    -- AUTH and SELECT are run only on fresh connections, so connections
+    -- with different databases or credentials must not share the default
+    -- host:port keepalive pool, otherwise a reused connection may be bound
+    -- to an unexpected database or user
+    local pool = (conf.redis_ssl and "rediss#" or "redis#")
+                 .. conf.redis_host .. "#" .. (conf.redis_port or 6379)
+                 .. "#" .. (conf.redis_database or 0)
+    if conf.redis_password and conf.redis_password ~= '' then

Review Comment:
   Agreed — since the TLS handshake is also skipped on reused connections, a verify=true config could reuse a connection established without verification. Fixed in f0ef2dac7: the pool name now distinguishes redis / rediss / rediss-verify.



##########
t/plugin/limit-count-redis5.t:
##########
@@ -0,0 +1,298 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+    if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+        $SkipReason = "unavailable for the hup tests";
+
+    } else {
+        $ENV{TEST_NGINX_USE_HUP} = 1;
+        undef $ENV{TEST_NGINX_USE_STAP};
+    }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->error_log && !$block->no_error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+
+    my $extra_init_worker_by_lua = $block->extra_init_worker_by_lua // "";
+    $extra_init_worker_by_lua .= <<_EOC_;
+        require("lib.test_redis").flush_all({password = "foobared"})
+_EOC_
+
+    $block->set_value("extra_init_worker_by_lua", $extra_init_worker_by_lua);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: routes pointing to the same redis but different databases must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 1,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 2,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.sleep(0.5)
+
+            -- alternate requests between the two routes so that the second
+            -- route is served by a connection put into the keepalive pool
+            -- by the first one if the pool is wrongly shared
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"])
+                end
+            end
+
+            -- each database must contain only its own route's counter,
+            -- decremented by exactly the 2 requests sent to that route
+            local redis = require "resty.redis"
+            for db = 1, 2 do
+                local red = redis:new()
+                red:set_timeout(1000)
+                local ok, err = red:connect("127.0.0.1", 6379)
+                if not ok then
+                    ngx.say("failed to connect: ", err)
+                    return
+                end
+                red:select(db)
+                local keys, err = red:keys("plugin-limit-count*")
+                if not keys then
+                    ngx.say("failed to get keys: ", err)
+                    return
+                end
+                ngx.say("db ", db, " keys: ", #keys)
+                for _, key in ipairs(keys) do
+                    local counter = red:get(key)
+                    ngx.say("db ", db, " counter: ", counter)
+                end
+                red:close()
+            end
+        }
+    }
+--- timeout: 10
+--- response_body
+200 remaining: 4
+200 remaining: 4
+200 remaining: 3
+200 remaining: 3
+db 1 keys: 1
+db 1 counter: 3
+db 2 keys: 1
+db 2 counter: 3
+
+
+
+=== TEST 2: routes with different credentials must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- the CI redis has a user alice with the same permissions
+            -- as the default user
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_username": "alice",
+                            "redis_password": "somepassword",
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_keepalive_pool": 1
+                        }

Review Comment:
   You're right, with the CI redis's no-password default user this test couldn't fail pre-fix. Redesigned in f0ef2dac7: route 2 now uses wrong credentials and must consistently fail with 500, which only holds when pools are isolated — verified it fails on the unpatched code by wrongly reusing route 1's authenticated connection.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: redis connections with different database/credentials must not share a keepalive pool [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #13516:
URL: https://github.com/apache/apisix/pull/13516#discussion_r3397097002


##########
t/plugin/limit-count-redis5.t:
##########
@@ -0,0 +1,298 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+    if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+        $SkipReason = "unavailable for the hup tests";
+
+    } else {
+        $ENV{TEST_NGINX_USE_HUP} = 1;
+        undef $ENV{TEST_NGINX_USE_STAP};
+    }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->error_log && !$block->no_error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+
+    my $extra_init_worker_by_lua = $block->extra_init_worker_by_lua // "";
+    $extra_init_worker_by_lua .= <<_EOC_;
+        require("lib.test_redis").flush_all({password = "foobared"})
+_EOC_
+
+    $block->set_value("extra_init_worker_by_lua", $extra_init_worker_by_lua);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: routes pointing to the same redis but different databases must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 1,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 2,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.sleep(0.5)
+
+            -- alternate requests between the two routes so that the second
+            -- route is served by a connection put into the keepalive pool
+            -- by the first one if the pool is wrongly shared
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"])
+                end
+            end
+
+            -- each database must contain only its own route's counter,
+            -- decremented by exactly the 2 requests sent to that route
+            local redis = require "resty.redis"
+            for db = 1, 2 do
+                local red = redis:new()
+                red:set_timeout(1000)
+                local ok, err = red:connect("127.0.0.1", 6379)
+                if not ok then
+                    ngx.say("failed to connect: ", err)
+                    return
+                end
+                red:select(db)
+                local keys, err = red:keys("plugin-limit-count*")
+                if not keys then
+                    ngx.say("failed to get keys: ", err)
+                    return
+                end
+                ngx.say("db ", db, " keys: ", #keys)
+                for _, key in ipairs(keys) do
+                    local counter = red:get(key)
+                    ngx.say("db ", db, " counter: ", counter)
+                end
+                red:close()
+            end
+        }
+    }
+--- timeout: 10
+--- response_body
+200 remaining: 4
+200 remaining: 4
+200 remaining: 3
+200 remaining: 3
+db 1 keys: 1
+db 1 counter: 3
+db 2 keys: 1
+db 2 counter: 3
+
+
+
+=== TEST 2: routes with different credentials must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- the CI redis has a user alice with the same permissions
+            -- as the default user
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_username": "alice",
+                            "redis_password": "somepassword",
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.sleep(0.5)
+
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"])

Review Comment:
   Done in f0ef2dac7 — the header now falls back to "nil" and the redesigned TEST 2 asserts it for the failing route.



##########
t/plugin/limit-count-redis5.t:
##########
@@ -0,0 +1,298 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+    if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+        $SkipReason = "unavailable for the hup tests";
+
+    } else {
+        $ENV{TEST_NGINX_USE_HUP} = 1;
+        undef $ENV{TEST_NGINX_USE_STAP};
+    }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->error_log && !$block->no_error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+
+    my $extra_init_worker_by_lua = $block->extra_init_worker_by_lua // "";
+    $extra_init_worker_by_lua .= <<_EOC_;
+        require("lib.test_redis").flush_all({password = "foobared"})
+_EOC_
+
+    $block->set_value("extra_init_worker_by_lua", $extra_init_worker_by_lua);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: routes pointing to the same redis but different databases must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 1,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 2,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.sleep(0.5)
+
+            -- alternate requests between the two routes so that the second
+            -- route is served by a connection put into the keepalive pool
+            -- by the first one if the pool is wrongly shared
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"])
+                end
+            end
+
+            -- each database must contain only its own route's counter,
+            -- decremented by exactly the 2 requests sent to that route
+            local redis = require "resty.redis"
+            for db = 1, 2 do
+                local red = redis:new()
+                red:set_timeout(1000)
+                local ok, err = red:connect("127.0.0.1", 6379)
+                if not ok then
+                    ngx.say("failed to connect: ", err)
+                    return
+                end
+                red:select(db)
+                local keys, err = red:keys("plugin-limit-count*")
+                if not keys then
+                    ngx.say("failed to get keys: ", err)
+                    return
+                end
+                ngx.say("db ", db, " keys: ", #keys)
+                for _, key in ipairs(keys) do
+                    local counter = red:get(key)
+                    ngx.say("db ", db, " counter: ", counter)
+                end
+                red:close()
+            end
+        }
+    }
+--- timeout: 10
+--- response_body
+200 remaining: 4
+200 remaining: 4
+200 remaining: 3
+200 remaining: 3
+db 1 keys: 1
+db 1 counter: 3
+db 2 keys: 1
+db 2 counter: 3
+
+
+
+=== TEST 2: routes with different credentials must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- the CI redis has a user alice with the same permissions
+            -- as the default user
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_username": "alice",
+                            "redis_password": "somepassword",
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            code, body = t('/apisix/admin/routes/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello1",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 5,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_keepalive_pool": 1
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.sleep(0.5)
+
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"])
+                end
+            end
+        }
+    }
+--- timeout: 10
+--- response_body
+200 remaining: 4
+200 remaining: 4
+200 remaining: 3
+200 remaining: 3

Review Comment:
   Covered by the TEST 2 redesign in f0ef2dac7 — /hello1 is asserted to fail consistently (500) on both rounds, including after route 1 has warmed its own pool.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org