Skip to content

Commit

Permalink
fix(*): drop luasocket in cli
Browse files Browse the repository at this point in the history
This is the follow-up PR of #11127

Changing the socket type from luasocket to openresty cosocket causes some test fail weirdly. After investigating, it's mainly because the cosocket support yield and setkeepalive. See the comments in tests.

https://konghq.atlassian.net/browse/FTI-4937
  • Loading branch information
catbro666 committed Jul 25, 2023
1 parent 5c7a7b8 commit 86c374c
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 67 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@
Thanks [@backjo](https://github.com/backjo) for contributing this change.
[#10723](https://github.com/Kong/kong/pull/10729)
- Make `kong vault get` CLI command work in dbless mode by injecting the necessary directives into the kong cli nginx.conf.
Meanwhile, the following Kong configurations cannot reference vaults as they are required for vaults initializing:
`prefix`, `vaults`, `database`, `lmdb_environment_path`, `lmdb_map_size`, `lua_ssl_trusted_certificate`, `lua_ssl_protocols`, `nginx_http_lua_ssl_protocols`, `nginx_stream_lua_ssl_protocols`, `vault_*`.
Meanwhile, the following Kong configurations cannot reference vaults as they are required for vaults initializing:
`prefix`, `vaults`, `database`, `lmdb_environment_path`, `lmdb_map_size`, `lua_ssl_trusted_certificate`, `lua_ssl_protocols`, `nginx_http_lua_ssl_protocols`, `nginx_stream_lua_ssl_protocols`, `vault_*`.
[#10675](https://github.com/Kong/kong/pull/10675)
- Drop luasocket in cli
[#11177](https://github.com/Kong/kong/pull/11177)

#### Admin API

Expand Down
8 changes: 8 additions & 0 deletions bin/busted
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ require("kong.globalpatches")({
rbusted = true
})

-- some libraries used in test like spec/helpers
-- calls cosocket in module level, and as LuaJIT's
-- `require` is implemented in C, this throws
-- "attempt to yield across C-call boundary" error
-- the following pure-lua implementation is to bypass
-- this limitation, without need to modify all tests
_G.require = require "spec.require".require

-- Busted command-line runner
require 'busted.runner'({ standalone = false })

Expand Down
3 changes: 3 additions & 0 deletions kong/cmd/utils/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ local function reset(schema_state, db, ttl)
-- failed to acquire locks - maybe locks table was dropped?
log.error(err .. " - retrying without cluster lock")
log("Resetting database...")
-- ROLLBACK in order to solve this error
-- ERROR: current transaction is aborted, commands ignored until end of transaction block
assert(db.connector:query("ROLLBACK;"))
assert(db:schema_reset())
log("Database successfully reset")
return true
Expand Down
7 changes: 2 additions & 5 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ local setkeepalive

local function reconnect(config)
local phase = get_phase()
if phase == "init" or phase == "init_worker" or ngx.IS_CLI then
-- Force LuaSocket usage in the CLI in order to allow for self-signed
-- certificates to be trusted (via opts.cafile) in the resty-cli
-- interpreter (no way to set lua_ssl_trusted_certificate).
if phase == "init" or phase == "init_worker" then
config.socket_type = "luasocket"

else
Expand Down Expand Up @@ -298,7 +295,7 @@ function _mt:init()
local res, err = self:query("SHOW server_version_num;")
local ver = tonumber(res and res[1] and res[1].server_version_num)
if not ver then
return nil, "failed to retrieve PostgreSQL server_version_num: " .. err
return nil, "failed to retrieve PostgreSQL server_version_num: " .. (err or "")
end

local major = floor(ver / 10000)
Expand Down
4 changes: 4 additions & 0 deletions spec/02-integration/02-cmd/10-migrations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ for _, strategy in helpers.each_strategy() do

local db = assert(DB.new(tmp_conf, strategy))
assert(db:init_connector())
-- in spec/helpers.lua, db has already been init'ed
-- the stored connection will be reused here,
-- so we need to set schema explicitly to 'kong_migrations_tests'
assert(db:connect())
assert(db.connector:query("SET SCHEMA 'kong_migrations_tests';\n"))
finally(function()
db.connector:close()
end)
Expand Down
75 changes: 38 additions & 37 deletions spec/02-integration/03-db/01-db_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ for _, strategy in helpers.each_strategy() do
assert.is_nil(err)
assert.is_table(db)

-- the schema is only set when the socket is new
-- but here in tests because in spec/helpers.lua the db is init'ed and
-- setkeepalive, so the connection may be reused,
-- so we need to close the original connection and connect a new one
assert(db:connect())
assert(db:close())
assert(db:init_connector())

local infos = db.infos
Expand Down Expand Up @@ -236,12 +242,7 @@ for _, strategy in helpers.each_strategy() do
assert(db:close())
end)

it("returns opened connection when using cosockets", function()
-- bin/busted runs with ngx.IS_CLI = true, which forces luasocket to
-- be used in the DB connector (for custom CAs to work)
-- Disable this behavior for this test, especially considering we
-- are running within resty-cli, and thus within timer_by_lua (which
-- can support cosockets).
it("returns opened connection when IS_CLI=false", function()
ngx.IS_CLI = false

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -264,7 +265,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

it("returns opened connection when using luasocket", function()
it("returns opened connection when IS_CLI=true", function()
ngx.IS_CLI = true

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -278,15 +279,15 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_false(db.connector:get_stored_connection().config.ssl)

end

db:close()
end)

postgres_only("returns opened connection with ssl (cosockets)", function()
postgres_only("returns opened connection with ssl (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -312,7 +313,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("returns opened connection with ssl (luasocket)", function()
postgres_only("returns opened connection with ssl (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -330,15 +331,15 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_true(db.connector:get_stored_connection().config.ssl)

end

db:close()
end)

postgres_only("connects to postgres with readonly account (cosockets)", function()
postgres_only("connects to postgres with readonly account (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand Down Expand Up @@ -366,7 +367,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("connects to postgres with readonly account (luasocket)", function()
postgres_only("connects to postgres with readonly account (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -385,13 +386,13 @@ for _, strategy in helpers.each_strategy() do

assert.is_nil(db.connector:get_stored_connection("read"))

assert.equal("luasocket", db.connector:get_stored_connection("write").sock_type)
assert.equal("nginx", db.connector:get_stored_connection("write").sock_type)

if strategy == "portgres" then
assert.is_false(db.connector:get_stored_connection("write").config.ssl)
end

assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)

if strategy == "portgres" then
assert.is_false(db.connector:get_stored_connection("write").config.ssl)
Expand All @@ -407,7 +408,7 @@ for _, strategy in helpers.each_strategy() do
helpers.get_db_utils(strategy, {})
end)

it("returns true when there is a stored connection (cosockets)", function()
it("returns true when there is a stored connection (IS_CLI=false)", function()
ngx.IS_CLI = false

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -432,7 +433,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

it("returns true when there is a stored connection (luasocket)", function()
it("returns true when there is a stored connection (IS_CLI=true)", function()
ngx.IS_CLI = true

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -446,7 +447,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_false(db.connector:get_stored_connection().config.ssl)

end
Expand All @@ -457,7 +458,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("returns true when there is a stored connection with ssl (cosockets)", function()
postgres_only("returns true when there is a stored connection with ssl (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand Down Expand Up @@ -485,7 +486,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("returns true when there is a stored connection with ssl (luasocket)", function()
postgres_only("returns true when there is a stored connection with ssl (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -503,7 +504,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_true(db.connector:get_stored_connection().config.ssl)

end
Expand All @@ -514,7 +515,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

it("returns true when there is no stored connection (cosockets)", function()
it("returns true when there is no stored connection (IS_CLI=false)", function()
ngx.IS_CLI = false

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -527,7 +528,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:setkeepalive())
end)

it("returns true when there is no stored connection (luasocket)", function()
it("returns true when there is no stored connection (IS_CLI=true)", function()
ngx.IS_CLI = true

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -540,7 +541,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:setkeepalive())
end)

postgres_only("keepalives both read only and write connection (cosockets)", function()
postgres_only("keepalives both read only and write connection (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand Down Expand Up @@ -577,7 +578,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("connects and keepalives only write connection (luasocket)", function()
postgres_only("connects and keepalives only write connection (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -598,7 +599,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_nil(err)
assert.is_table(conn)

assert.equal("luasocket", db.connector:get_stored_connection("write").sock_type)
assert.equal("nginx", db.connector:get_stored_connection("write").sock_type)
assert.is_nil(db.connector:get_stored_connection("read"))

if strategy == "postgres" then
Expand All @@ -620,7 +621,7 @@ for _, strategy in helpers.each_strategy() do
helpers.get_db_utils(strategy, {})
end)

it("returns true when there is a stored connection (cosockets)", function()
it("returns true when there is a stored connection (IS_CLI=false)", function()
ngx.IS_CLI = false

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -643,7 +644,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:close())
end)

it("returns true when there is a stored connection (luasocket)", function()
it("returns true when there is a stored connection (IS_CLI=true)", function()
ngx.IS_CLI = true

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -657,7 +658,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_false(db.connector:get_stored_connection().config.ssl)

end
Expand All @@ -666,7 +667,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:close())
end)

postgres_only("returns true when there is a stored connection with ssl (cosockets)", function()
postgres_only("returns true when there is a stored connection with ssl (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -692,7 +693,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:close())
end)

postgres_only("returns true when there is a stored connection with ssl (luasocket)", function()
postgres_only("returns true when there is a stored connection with ssl (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -710,15 +711,15 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(conn)

if strategy == "postgres" then
assert.equal("luasocket", db.connector:get_stored_connection().sock_type)
assert.equal("nginx", db.connector:get_stored_connection().sock_type)
assert.is_true(db.connector:get_stored_connection().config.ssl)

end

assert.is_true(db:close())
end)

it("returns true when there is no stored connection (cosockets)", function()
it("returns true when there is no stored connection (IS_CLI=false)", function()
ngx.IS_CLI = false

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -731,7 +732,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_true(db:close())
end)

it("returns true when there is no stored connection (luasocket)", function()
it("returns true when there is no stored connection (IS_CLI=true)", function()
ngx.IS_CLI = true

local db, err = DB.new(helpers.test_conf, strategy)
Expand All @@ -744,7 +745,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, db:close())
end)

postgres_only("returns true when both read-only and write connection exists (cosockets)", function()
postgres_only("returns true when both read-only and write connection exists (IS_CLI=false)", function()
ngx.IS_CLI = false

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand Down Expand Up @@ -781,7 +782,7 @@ for _, strategy in helpers.each_strategy() do
db:close()
end)

postgres_only("returns true when both read-only and write connection exists (luasocket)", function()
postgres_only("returns true when both read-only and write connection exists (IS_CLI=true)", function()
ngx.IS_CLI = true

local conf = utils.cycle_aware_deep_copy(helpers.test_conf)
Expand All @@ -801,7 +802,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_nil(err)
assert.is_table(conn)

assert.equal("luasocket", db.connector:get_stored_connection("write").sock_type)
assert.equal("nginx", db.connector:get_stored_connection("write").sock_type)
assert.is_nil(db.connector:get_stored_connection("read"))

if strategy == "postgres" then
Expand Down
Loading

0 comments on commit 86c374c

Please sign in to comment.