diff --git a/README.md b/README.md index baf5f67..903bbc4 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 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 073ab24..cd4dc1a 100644 --- a/lib/resty/http_connect.lua +++ b/lib/resty/http_connect.lua @@ -11,16 +11,13 @@ 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 +-- the warning below is only logged once per worker to avoid flooding the log +local openssl_warned --[[ A connection function that incorporates: @@ -193,6 +190,12 @@ local function connect(self, options) end if not openssl_available then + 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