From 1ae158e7d4f5e3855622c81391af75f6e232de9a Mon Sep 17 00:00:00 2001 From: Sebastian Parschauer Date: Tue, 17 Nov 2026 11:12:12 +0100 Subject: [PATCH 1/2] fix: Make lua-resty-openssl optional again lua-resty-openssl is only needed for mTLS (client certificate) connections. Everything else works without it, but a warning was logged at module load time whenever it was missing. This made every worker log a `[warn]` line for users who do not use mTLS. Only log the warning when mTLS is requested and the module is not available. The error return for that case is unchanged. Also fix the README: lua-resty-openssl is not a prerequisite for all HTTP connections. Drop it from the install command and document that it is only required for mTLS. --- README.md | 6 ++++-- lib/resty/http_connect.lua | 10 ++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index baf5f67..62689c3 100644 --- a/README.md +++ b/README.md @@ -59,10 +59,12 @@ https://luarocks.org/modules/pintsized/lua-resty-http Install it with: ``` -luarocks install lua-resty-http && luarocks install lua-resty-openssl +luarocks install lua-resty-http ``` -Appending the version to the `luarocks` installation command avoids migration conflicts. The `lua-resty-openssl` package is a prerequisite for all HTTP connections. +Appending the version to the `luarocks` installation command avoids migration conflicts. + +The `lua-resty-openssl` package is optional. It is only required for mTLS (client certificate) connections. Install it with `luarocks install lua-resty-openssl` if you need them. Without it, such connections fail with an error and a warning is logged. **Note**: The [OpenResty Package Manager (OPM) alternative](https://opm.openresty.org/package/pintsized/lua-resty-http/) is dead. No new versions can be uploaded there. Please migrate to `luarocks` instead. diff --git a/lib/resty/http_connect.lua b/lib/resty/http_connect.lua index 073ab24..b59c0fc 100644 --- a/lib/resty/http_connect.lua +++ b/lib/resty/http_connect.lua @@ -11,17 +11,12 @@ local ffi_cast = ffi.cast local type = type local lib_chain, lib_x509, lib_pkey -local openssl_available, res = xpcall(function() +local openssl_available, openssl_err = xpcall(function() lib_chain = require("resty.openssl.x509.chain") lib_x509 = require("resty.openssl.x509") lib_pkey = require("resty.openssl.pkey") end, debug.traceback) -if not openssl_available then - ngx_log(ngx_WARN, "failed to load module `resty.openssl.*`, \z - mTLS isn't supported without lua-resty-openssl:\n", res) -end - --[[ A connection function that incorporates: - tcp connect @@ -193,6 +188,9 @@ local function connect(self, options) end if not openssl_available then + ngx_log(ngx_WARN, "failed to load module `resty.openssl.*`, \z + mTLS isn't supported without lua-resty-openssl:\n", + openssl_err) return nil, "module `resty.openssl.*` not available, mTLS isn't supported without lua-resty-openssl" end From c32e49d19b473d9f7ce9a048fcf4120dba1b9d52 Mon Sep 17 00:00:00 2001 From: Sebastian Parschauer Date: Tue, 17 Nov 2026 12:48:17 +0100 Subject: [PATCH 2/2] fix: Log missing lua-resty-openssl only once per worker When mTLS is requested and lua-resty-openssl is not available, the warning including the traceback of the failed require was logged on every connect() call. The traceback lists all searched package paths, so a misconfigured deployment could flood the error log. Log the warning only once per worker. The error return is unchanged, so every mTLS attempt still fails with the same error. Add tests that verify the warning is logged exactly once when mTLS is requested without lua-resty-openssl and not at all without mTLS. --- README.md | 2 +- lib/resty/http_connect.lua | 11 +++-- t/20-mtls.t | 97 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 62689c3..903bbc4 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ luarocks install lua-resty-http Appending the version to the `luarocks` installation command avoids migration conflicts. -The `lua-resty-openssl` package is optional. It is only required for mTLS (client certificate) connections. Install it with `luarocks install lua-resty-openssl` if you need them. Without it, such connections fail with an error and a warning is logged. +The `lua-resty-openssl` package is optional. It is only required for mTLS (client certificate) connections. Install it with `luarocks install lua-resty-openssl` if you need them. Without it, such connections fail with an error and a warning is logged once per worker. **Note**: The [OpenResty Package Manager (OPM) alternative](https://opm.openresty.org/package/pintsized/lua-resty-http/) is dead. No new versions can be uploaded there. Please migrate to `luarocks` instead. diff --git a/lib/resty/http_connect.lua b/lib/resty/http_connect.lua index b59c0fc..cd4dc1a 100644 --- a/lib/resty/http_connect.lua +++ b/lib/resty/http_connect.lua @@ -16,6 +16,8 @@ local openssl_available, openssl_err = xpcall(function() lib_x509 = require("resty.openssl.x509") lib_pkey = require("resty.openssl.pkey") end, debug.traceback) +-- the warning below is only logged once per worker to avoid flooding the log +local openssl_warned --[[ A connection function that incorporates: @@ -188,9 +190,12 @@ local function connect(self, options) end if not openssl_available then - ngx_log(ngx_WARN, "failed to load module `resty.openssl.*`, \z - mTLS isn't supported without lua-resty-openssl:\n", - openssl_err) + if not openssl_warned then + openssl_warned = true + ngx_log(ngx_WARN, "failed to load module `resty.openssl.*`, \z + mTLS isn't supported without lua-resty-openssl:\n", + openssl_err) + end return nil, "module `resty.openssl.*` not available, mTLS isn't supported without lua-resty-openssl" end diff --git a/t/20-mtls.t b/t/20-mtls.t index 7154d6b..8d31c52 100644 --- a/t/20-mtls.t +++ b/t/20-mtls.t @@ -318,3 +318,100 @@ nil 400 --- skip_nginx 4: < 1.21.4 + + + +=== TEST 5: Missing lua-resty-openssl only logs a warning once per worker when mTLS is requested +--- http_config eval: $::mtls_http_config +--- config eval +" +lua_ssl_trusted_certificate $::HtmlDir/test.crt; + +location /t { + content_by_lua_block { + local f = assert(io.open('$::HtmlDir/mtls_client.crt')) + local cert_data = f:read('*a') + f:close() + + f = assert(io.open('$::HtmlDir/mtls_client.key')) + local key_data = f:read('*a') + f:close() + + local ssl = require('ngx.ssl') + + local cert = assert(ssl.parse_pem_cert(cert_data)) + local key = assert(ssl.parse_pem_priv_key(key_data)) + + -- simulate a missing lua-resty-openssl and reload the connect module + package.loaded['resty.openssl.x509.chain'] = nil + package.preload['resty.openssl.x509.chain'] = function() + error('module not found') + end + package.loaded['resty.http_connect'] = nil + + for i = 1, 3 do + local httpc = assert(require('resty.http').new()) + + local ok, err = httpc:connect { + scheme = 'https', + host = 'unix:$::HtmlDir/mtls.sock', + ssl_client_cert = cert, + ssl_client_priv_key = key, + } + + ngx.say(ok, ' ', err) + end + } +} +" +--- user_files eval: $::mtls_user_files +--- request +GET /t +--- response_body +nil module `resty.openssl.*` not available, mTLS isn't supported without lua-resty-openssl +nil module `resty.openssl.*` not available, mTLS isn't supported without lua-resty-openssl +nil module `resty.openssl.*` not available, mTLS isn't supported without lua-resty-openssl +--- grep_error_log eval: qr/failed to load module `resty\.openssl\.\*`/ +--- grep_error_log_out +failed to load module `resty.openssl.*` +--- no_error_log +[error] +--- skip_nginx +2: < 1.21.4 + + + +=== TEST 6: Missing lua-resty-openssl does not log anything without mTLS +--- http_config eval: $::mtls_http_config +--- config eval +" +lua_ssl_trusted_certificate $::HtmlDir/test.crt; + +location /t { + content_by_lua_block { + -- simulate a missing lua-resty-openssl and reload the connect module + package.loaded['resty.openssl.x509.chain'] = nil + package.preload['resty.openssl.x509.chain'] = function() + error('module not found') + end + package.loaded['resty.http_connect'] = nil + + local httpc = assert(require('resty.http').new()) + + local ok, err = httpc:connect { + scheme = 'https', + host = 'unix:$::HtmlDir/mtls.sock', + } + + ngx.say(ok, ' ', err) + httpc:close() + } +} +" +--- user_files eval: $::mtls_user_files +--- request +GET /t +--- no_error_log +[error] +[warn] +failed to load module