Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ https://luarocks.org/modules/pintsized/lua-resty-http

Install it with:
```
luarocks install lua-resty-http <version> && luarocks install lua-resty-openssl
luarocks install lua-resty-http <version>
```

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.

Expand Down
15 changes: 9 additions & 6 deletions lib/resty/http_connect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
97 changes: 97 additions & 0 deletions t/20-mtls.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading