Skip to content

Commit a62778c

Browse files
catbro666pintsized
authored andcommitted
error out directly for other cases as well and fix test
1 parent 310e78d commit a62778c

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

lib/resty/http_connect.lua

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,7 @@ local function connect(self, options)
179179
end
180180

181181
local cert_hash
182-
-- fallback to non-mTLS if it's not an error due to the caller
183-
repeat
184-
if not ssl or not ssl_client_cert or not ssl_client_priv_key then
185-
break
186-
end
187-
182+
if ssl and ssl_client_cert and ssl_client_priv_key then
188183
local cert_type = type(ssl_client_cert)
189184
local key_type = type(ssl_client_priv_key)
190185

@@ -197,15 +192,13 @@ local function connect(self, options)
197192
end
198193

199194
if not openssl_available then
200-
ngx_log(ngx_WARN, "module `resty.openssl.*` not available, falling back to non-mTLS:\n")
201-
break
195+
return nil, "module `resty.openssl.*` not available, mTLS isn't supported with lua-resty-openssl"
202196
end
203197

204198
-- convert from `void*` to `OPENSSL_STACK*`
205199
local cert_chain, err = lib_chain.dup(ffi_cast("OPENSSL_STACK*", ssl_client_cert))
206200
if not cert_chain then
207-
ngx_log(ngx_WARN, "failed to dup the ssl_client_cert, falling back to non-mTLS: ", err)
208-
break
201+
return nil, string_format("failed to dup the ssl_client_cert: %s", err)
209202
end
210203

211204
if #cert_chain < 1 then
@@ -214,15 +207,13 @@ local function connect(self, options)
214207

215208
local cert, err = lib_x509.dup(cert_chain[1].ctx)
216209
if not cert then
217-
ngx_log(ngx_WARN, "failed to dup the x509, falling back to non-mTLS: ", err)
218-
break
210+
return nil, string_format("failed to dup the x509: %s", err)
219211
end
220212

221213
-- convert from `void*` to `EVP_PKEY*`
222214
local key, err = lib_pkey.new(ffi_cast("EVP_PKEY*", ssl_client_priv_key))
223215
if not key then
224-
ngx_log(ngx_WARN, "failed to new the pkey, falling back to non-mTLS: ", err)
225-
break
216+
return nil, string_format("failed to new the pkey: %s": err)
226217
end
227218
-- should not free the cdata passed in
228219
ffi_gc(key.ctx, nil)
@@ -235,17 +226,10 @@ local function connect(self, options)
235226

236227
cert_hash, err = cert:digest("sha256")
237228
if not cert_hash then
238-
ngx_log(ngx_WARN, "failed to calculate the digest of the cert, falling back to non-mTLS: ", err)
239-
break
229+
return nil, string_format("failed to calculate the digest of the cert: %s", err)
240230
end
241231

242232
cert_hash = to_hex(cert_hash) -- convert to hex so that it's printable
243-
244-
until true
245-
246-
if not cert_hash then
247-
ssl_client_cert = nil
248-
ssl_client_priv_key = nil
249233
end
250234

251235
-- construct a poolname unique within proxy and ssl info
@@ -326,13 +310,13 @@ local function connect(self, options)
326310
-- Experimental mTLS support
327311
if ssl_client_cert and ssl_client_priv_key then
328312
if type(sock.setclientcert) ~= "function" then
329-
ngx_log(ngx_WARN, "cannot use SSL client cert and key without mTLS support")
313+
return nil, "cannot use SSL client cert and key without mTLS support"
330314

331315
else
332-
ok, err = sock:setclientcert(ssl_client_cert, ssl_client_priv_key)
333-
if not ok then
334-
ngx_log(ngx_WARN, "could not set client certificate: ", err)
335-
end
316+
ok, err = sock:setclientcert(ssl_client_cert, ssl_client_priv_key)
317+
if not ok then
318+
return nil, string_format("could not set client certificate: %s", err)
319+
end
336320
end
337321
end
338322

t/20-mtls.t

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ location /t {
137137
})
138138
139139
ngx.say(res:read_body())
140+
141+
else
142+
ngx.say("failed to connect: " .. err or "")
140143
end
141144
142145
httpc:close()
@@ -147,13 +150,15 @@ location /t {
147150
--- request
148151
GET /t
149152
--- error_code: 200
150-
--- error_log
151-
bad ssl_client_priv_key: cdata expected, got string
152-
--- response_body_unlike: hello, CN=foo@example.com,O=OpenResty,ST=California,C=US
153+
--- no_error_log
154+
[error]
155+
[warn]
156+
--- response_body
157+
failed to connect: bad ssl_client_priv_key: cdata expected, got string
153158
--- skip_nginx
154159
4: < 1.21.4
155160

156-
=== TEST 3: Connection succeeds with client cert and key. SKIP'd for CI until feature is merged.
161+
=== TEST 3: Connection succeeds with client cert and key.
157162
--- http_config eval: $::mtls_http_config
158163
--- config eval
159164
"

0 commit comments

Comments
 (0)