Skip to content

Commit a7193b1

Browse files
committed
bugfix: some lua configurations (i.e. lua_ssl_trusted_certificate) were missing in the init_worker phase.
should use the top lua conf top_llcf for init_worker block. also renamed the variables from `*lcf` to `*scf`.
1 parent b1ba0e8 commit a7193b1

File tree

2 files changed

+101
-19
lines changed

2 files changed

+101
-19
lines changed

src/ngx_stream_lua_initworkerby.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ ngx_stream_lua_init_worker(ngx_cycle_t *cycle)
5151
ngx_conf_file_t *conf_file;
5252
ngx_stream_session_t *s;
5353

54-
ngx_stream_core_srv_conf_t *clcf, *top_clcf;
55-
ngx_stream_lua_srv_conf_t *llcf, *top_llcf;
54+
ngx_stream_core_srv_conf_t *cscf, *top_cscf;
55+
ngx_stream_lua_srv_conf_t *lscf, *top_lscf;
5656

5757
lmcf = ngx_stream_cycle_get_module_main_conf(cycle, ngx_stream_lua_module);
5858

@@ -98,8 +98,8 @@ ngx_stream_lua_init_worker(ngx_cycle_t *cycle)
9898
cycle->conf_ctx[ngx_stream_module.index];
9999
stream_ctx.main_conf = conf_ctx->main_conf;
100100

101-
top_clcf = conf_ctx->srv_conf[ngx_stream_core_module.ctx_index];
102-
top_llcf = conf_ctx->srv_conf[ngx_stream_lua_module.ctx_index];
101+
top_cscf = conf_ctx->srv_conf[ngx_stream_core_module.ctx_index];
102+
top_lscf = conf_ctx->srv_conf[ngx_stream_lua_module.ctx_index];
103103

104104
ngx_memzero(&conf, sizeof(ngx_conf_t));
105105

@@ -223,16 +223,24 @@ ngx_stream_lua_init_worker(ngx_cycle_t *cycle)
223223
stream_ctx.srv_conf[modules[i]->ctx_index] = cur;
224224

225225
if (modules[i]->ctx_index == ngx_stream_core_module.ctx_index) {
226-
clcf = cur;
226+
cscf = cur;
227227
/* just to silence the error in
228228
* ngx_stream_core_merge_srv_conf */
229-
clcf->handler = ngx_stream_lua_content_handler;
229+
cscf->handler = ngx_stream_lua_content_handler;
230230
}
231231

232232
if (module->merge_srv_conf) {
233-
prev = module->create_srv_conf(&conf);
234-
if (prev == NULL) {
235-
return NGX_ERROR;
233+
if (modules[i] == &ngx_stream_lua_module) {
234+
prev = top_lscf;
235+
236+
} else if (modules[i] == &ngx_stream_core_module) {
237+
prev = top_cscf;
238+
239+
} else {
240+
prev = module->create_srv_conf(&conf);
241+
if (prev == NULL) {
242+
return NGX_ERROR;
243+
}
236244
}
237245

238246
rv = module->merge_srv_conf(&conf, prev, cur);
@@ -262,24 +270,24 @@ ngx_stream_lua_init_worker(ngx_cycle_t *cycle)
262270
s->main_conf = stream_ctx.main_conf;
263271
s->srv_conf = stream_ctx.srv_conf;
264272

265-
clcf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module);
273+
cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module);
266274

267-
llcf = ngx_stream_get_module_srv_conf(s, ngx_stream_lua_module);
275+
lscf = ngx_stream_get_module_srv_conf(s, ngx_stream_lua_module);
268276

269-
if (top_llcf->log_socket_errors != NGX_CONF_UNSET) {
270-
llcf->log_socket_errors = top_llcf->log_socket_errors;
277+
if (top_lscf->log_socket_errors != NGX_CONF_UNSET) {
278+
lscf->log_socket_errors = top_lscf->log_socket_errors;
271279
}
272280

273-
if (top_clcf->resolver != NULL) {
274-
clcf->resolver = top_clcf->resolver;
281+
if (top_cscf->resolver != NULL) {
282+
cscf->resolver = top_cscf->resolver;
275283
}
276284

277-
if (top_clcf->resolver_timeout != NGX_CONF_UNSET_MSEC) {
278-
clcf->resolver_timeout = top_clcf->resolver_timeout;
285+
if (top_cscf->resolver_timeout != NGX_CONF_UNSET_MSEC) {
286+
cscf->resolver_timeout = top_cscf->resolver_timeout;
279287
}
280288

281289
#if defined(nginx_version) && nginx_version >= 1009000
282-
ngx_set_connection_log(s->connection, clcf->error_log);
290+
ngx_set_connection_log(s->connection, cscf->error_log);
283291

284292
#else
285293
#endif

t/124-init-worker.t

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@ use Test::Nginx::Socket::Lua::Stream;
88

99
repeat_each(1);
1010

11-
plan tests => repeat_each() * (blocks() * 4 - 1);
11+
plan tests => repeat_each() * (blocks() * 4 + 1);
1212

1313
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
1414
$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8';
1515

16+
sub read_file {
17+
my $infile = shift;
18+
open my $in, $infile
19+
or die "cannot open $infile for reading: $!";
20+
my $cert = do { local $/; <$in> };
21+
close $in;
22+
$cert;
23+
}
24+
25+
our $DSTRootCertificate = read_file("t/cert/dst-ca.crt");
1626
our $ServerRoot = server_root();
1727

1828
#no_diff();
@@ -699,3 +709,67 @@ This also affects merge_loc_conf
699709
ok
700710
--- no_error_log
701711
[error]
712+
713+
714+
715+
=== TEST 20: lua_ssl_trusted_certificate
716+
--- stream_config
717+
resolver $TEST_NGINX_RESOLVER ipv6=off;
718+
lua_ssl_trusted_certificate ../html/trusted.crt;
719+
lua_ssl_verify_depth 2;
720+
721+
init_worker_by_lua_block {
722+
local semaphore = require "ngx.semaphore"
723+
local sem = semaphore:new(0)
724+
package.loaded.sem = sem
725+
726+
local function test_ssl_verify()
727+
local sock = ngx.socket.tcp()
728+
sock:settimeout(2000)
729+
local ok, err = sock:connect("openresty.org", 443)
730+
if not ok then
731+
ngx.log(ngx.ERR, "failed to connect: ", err)
732+
return
733+
end
734+
735+
ngx.log(ngx.WARN, "connected: ", ok)
736+
737+
local session, err = sock:sslhandshake(nil, "openresty.org", true)
738+
if not session then
739+
ngx.log(ngx.ERR, "failed to do SSL handshake: ", err)
740+
return
741+
end
742+
743+
ngx.log(ngx.WARN, "ssl handshake: ", type(session))
744+
745+
local ok, err = sock:close()
746+
ngx.log(ngx.WARN, "close: ", ok, " ", err)
747+
748+
sem:post(1)
749+
end
750+
751+
ngx.timer.at(0, test_ssl_verify)
752+
}
753+
754+
--- stream_server_config
755+
content_by_lua_block {
756+
local sem = package.loaded.sem
757+
local ok, err = sem:wait(3)
758+
if not ok then
759+
ngx.say("wait test_ssl_verify failed: ", err)
760+
end
761+
762+
ngx.say('ok')
763+
}
764+
--- user_files eval
765+
">>> trusted.crt
766+
$::DSTRootCertificate"
767+
768+
--- stream_response
769+
ok
770+
--- no_error_log
771+
[error]
772+
--- error_log
773+
connected: 1
774+
ssl handshake: userdata
775+
close: 1 nil

0 commit comments

Comments
 (0)