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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
