Skip to content

Commit 15b180d

Browse files
authored
Merge pull request #68 from luto/fix-66
Store hook secret in shared dict, don't overwrite it on reload
2 parents cee47f9 + 278e5e5 commit 15b180d

File tree

14 files changed

+134
-9
lines changed

14 files changed

+134
-9
lines changed

.luacheckrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
globals = {
2-
"AUTO_SSL_HOOK_SECRET",
32
"ngx",
43
}
54

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ http {
4949
# hold your certificate data. 1MB of storage holds certificates for
5050
# approximately 100 separate domains.
5151
lua_shared_dict auto_ssl 1m;
52+
# The "auto_ssl" shared dict is used to temporarily store various settings
53+
# like the secret used by the hook server on port 8999. Do not change or
54+
# omit it.
55+
lua_shared_dict auto_ssl_settings 64k;
5256
5357
# A DNS resolver must be defined for OCSP stapling to function.
5458
#

lib/resty/auto-ssl/init_master.lua

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,21 @@ end
2828
-- secret token is an extra precaution to ensure the server is not accidentally
2929
-- opened up or proxied to the outside world.
3030
local function generate_hook_sever_secret()
31+
if ngx.shared.auto_ssl_settings:get("hook_server:secret") then
32+
-- if we've already got a secret token, do not overwrite it, as this causes
33+
-- problems in reload-heavy envrionments.
34+
-- See https://github.com/GUI/lua-resty-auto-ssl/issues/66
35+
return
36+
end
37+
3138
-- Generate the secret token.
3239
local random = resty_random.bytes(32)
33-
AUTO_SSL_HOOK_SECRET = str.to_hex(random)
40+
local _, set_err, set_forcible = ngx.shared.auto_ssl_settings:set("hook_server:secret", str.to_hex(random))
41+
if set_err then
42+
ngx.log(ngx.ERR, "auto-ssl: failed to set shdict for hook_server:secret: ", set_err)
43+
elseif set_forcible then
44+
ngx.log(ngx.ERR, "auto-ssl: 'lua_shared_dict auto_ssl_settings' might be too small - consider increasing its configured size (old entries were removed while adding hook_server:secret)")
45+
end
3446
end
3547

3648
local function generate_config(auto_ssl_instance)
@@ -82,6 +94,10 @@ local function setup_storage(auto_ssl_instance)
8294
end
8395

8496
return function(auto_ssl_instance)
97+
if not ngx.shared.auto_ssl_settings then
98+
ngx.log(ngx.ERR, "auto-ssl: dict auto_ssl_settings could not be found. Please add it to your configuration: `lua_shared_dict auto_ssl_settings 64k;`")
99+
end
100+
85101
check_dependencies()
86102
generate_hook_sever_secret()
87103
generate_config(auto_ssl_instance)

lib/resty/auto-ssl/servers/hook.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ return function(auto_ssl_instance)
88
local path = ngx.var.request_uri
99
local params = ngx.req.get_post_args()
1010

11-
if ngx.var.http_x_hook_secret ~= AUTO_SSL_HOOK_SECRET then
11+
if ngx.var.http_x_hook_secret ~= ngx.shared.auto_ssl_settings:get("hook_server:secret") then
1212
return ngx.exit(ngx.HTTP_FORBIDDEN)
1313
end
1414

lib/resty/auto-ssl/ssl_providers/lets_encrypt.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function _M.issue_cert(auto_ssl_instance, domain)
1515
assert(type(hook_port) == "number", "hook_port must be a number")
1616
assert(hook_port <= 65535, "hook_port must be below 65536")
1717

18-
local hook_secret = AUTO_SSL_HOOK_SECRET
18+
local hook_secret = ngx.shared.auto_ssl_settings:get("hook_server:secret")
1919
assert(type(hook_secret) == "string", "hook_server:secret must be a string")
2020

2121
local env_vars =

lib/resty/auto-ssl/utils/shell_execute.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ return function(command)
66
-- (since it's started by only a single worker in init_worker, it's possible
77
-- other workers have already finished their init_worker phases before the
88
-- process is actually started).
9-
if not ngx.shared.auto_ssl:get("sockproc_started") then
9+
if not ngx.shared.auto_ssl_settings:get("sockproc_started") then
1010
start_sockproc()
1111

1212
local wait_time = 0
1313
local sleep_time = 0.01
1414
local max_time = 5
15-
while not ngx.shared.auto_ssl:get("sockproc_started") do
15+
while not ngx.shared.auto_ssl_settings:get("sockproc_started") do
1616
ngx.sleep(sleep_time)
1717
wait_time = wait_time + sleep_time
1818

lib/resty/auto-ssl/utils/start_sockproc.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ local function start()
55
local exit_status = os.execute("umask 0022 && " .. auto_ssl.lua_root .. "/bin/resty-auto-ssl/start_sockproc")
66
-- Lua 5.2+ returns boolean. Prior versions return status code.
77
if exit_status == 0 or exit_status == true then
8-
local _, set_err, set_forcible = ngx.shared.auto_ssl:set("sockproc_started", true)
8+
local _, set_err, set_forcible = ngx.shared.auto_ssl_settings:set("sockproc_started", true)
99
if set_err then
1010
ngx.log(ngx.ERR, "auto-ssl: failed to set shdict for sockproc_started: ", set_err)
1111
elseif set_forcible then
12-
ngx.log(ngx.ERR, "auto-ssl: 'lua_shared_dict auto_ssl' might be too small - consider increasing its configured size (old entries were removed while adding sockproc_started)")
12+
ngx.log(ngx.ERR, "auto-ssl: 'lua_shared_dict auto_ssl_settings' might be too small - consider increasing its configured size (old entries were removed while adding sockproc_started)")
1313
end
1414
else
1515
ngx.log(ngx.ERR, "auto-ssl: failed to start sockproc")
1616
end
1717
end
1818

1919
return function(force)
20-
if ngx.shared.auto_ssl:get("sockproc_started") and not force then
20+
if ngx.shared.auto_ssl_settings:get("sockproc_started") and not force then
2121
return
2222
end
2323

t/file.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ __DATA__
2020
--- http_config
2121
resolver $TEST_NGINX_RESOLVER;
2222
lua_shared_dict auto_ssl 1m;
23+
lua_shared_dict auto_ssl_settings 1m;
2324
2425
init_by_lua_block {
2526
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -144,6 +145,7 @@ auto-ssl: issuing new certificate for
144145
--- http_config
145146
resolver $TEST_NGINX_RESOLVER;
146147
lua_shared_dict auto_ssl 1m;
148+
lua_shared_dict auto_ssl_settings 1m;
147149
148150
init_by_lua_block {
149151
auto_ssl = (require "lib.resty.auto-ssl").new({

t/hook_secret.t

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use strict;
2+
use warnings;
3+
use Test::Nginx::Socket::Lua;
4+
require "./t/inc/setup.pl";
5+
AutoSsl::setup();
6+
7+
repeat_each(1);
8+
9+
plan tests => repeat_each() * (5 + 6);
10+
11+
no_long_string();
12+
no_shuffle();
13+
14+
run_tests();
15+
16+
__DATA__
17+
18+
=== TEST 1: writes a friendly error message when auto_ssl_settings dict is missing
19+
--- http_config
20+
resolver $TEST_NGINX_RESOLVER;
21+
lua_shared_dict auto_ssl 1m;
22+
23+
init_by_lua_block {
24+
auto_ssl = (require "lib.resty.auto-ssl").new({
25+
dir = "$TEST_NGINX_RESTY_AUTO_SSL_DIR",
26+
ca = "https://acme-staging.api.letsencrypt.org/directory",
27+
})
28+
auto_ssl:init()
29+
}
30+
--- config
31+
--- timeout: 30s
32+
--- request
33+
GET /
34+
--- must_die
35+
--- ignore_response
36+
--- error_log
37+
auto-ssl: dict auto_ssl_settings could not be found. Please add it to your configuration: `lua_shared_dict auto_ssl_settings 64k;`
38+
--- no_error_log
39+
[warn]
40+
[alert]
41+
[emerg]
42+
43+
=== TEST 2: doesn't change the hook secret after reloading
44+
--- http_config
45+
resolver $TEST_NGINX_RESOLVER;
46+
lua_shared_dict auto_ssl 1m;
47+
lua_shared_dict auto_ssl_settings 1m;
48+
49+
init_by_lua_block {
50+
auto_ssl = (require "lib.resty.auto-ssl").new({
51+
dir = "$TEST_NGINX_RESTY_AUTO_SSL_DIR",
52+
ca = "https://acme-staging.api.letsencrypt.org/directory",
53+
})
54+
auto_ssl:init()
55+
}
56+
57+
init_worker_by_lua_block {
58+
auto_ssl:init_worker()
59+
}
60+
61+
--- config
62+
location /t {
63+
content_by_lua_block {
64+
local secret1 = ngx.shared.auto_ssl_settings:get("hook_server:secret")
65+
auto_ssl:init()
66+
local secret2 = ngx.shared.auto_ssl_settings:get("hook_server:secret")
67+
68+
if secret1 == secret2 then
69+
ngx.say("OK")
70+
else
71+
ngx.say("NOPE")
72+
end
73+
}
74+
}
75+
--- timeout: 30s
76+
--- request
77+
GET /t
78+
--- response_body
79+
OK
80+
--- error_log
81+
--- no_error_log
82+
[warn]
83+
[error]
84+
[alert]
85+
[emerg]

t/memory.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ __DATA__
1919
--- http_config
2020
resolver $TEST_NGINX_RESOLVER;
2121
lua_shared_dict auto_ssl 1m;
22+
lua_shared_dict auto_ssl_settings 1m;
2223
2324
init_by_lua_block {
2425
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -127,6 +128,7 @@ auto-ssl: issuing new certificate for
127128
--- http_config
128129
resolver $TEST_NGINX_RESOLVER;
129130
lua_shared_dict auto_ssl 1m;
131+
lua_shared_dict auto_ssl_settings 1m;
130132
131133
init_by_lua_block {
132134
auto_ssl = (require "lib.resty.auto-ssl").new({

t/multiple_workers.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ $TEST_NGINX_USER
3838
--- http_config
3939
resolver $TEST_NGINX_RESOLVER;
4040
lua_shared_dict auto_ssl 1m;
41+
lua_shared_dict auto_ssl_settings 1m;
4142
lua_shared_dict test_counts 128k;
4243
4344
init_by_lua_block {

t/redis.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ __DATA__
2626
--- http_config
2727
resolver $TEST_NGINX_RESOLVER;
2828
lua_shared_dict auto_ssl 1m;
29+
lua_shared_dict auto_ssl_settings 1m;
2930
3031
init_by_lua_block {
3132
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -158,6 +159,7 @@ auto-ssl: issuing new certificate for
158159
--- http_config
159160
resolver $TEST_NGINX_RESOLVER;
160161
lua_shared_dict auto_ssl 1m;
162+
lua_shared_dict auto_ssl_settings 1m;
161163
162164
init_by_lua_block {
163165
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -287,6 +289,7 @@ issuing new certificate for
287289
--- http_config
288290
resolver $TEST_NGINX_RESOLVER;
289291
lua_shared_dict auto_ssl 1m;
292+
lua_shared_dict auto_ssl_settings 1m;
290293
291294
init_by_lua_block {
292295
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -420,6 +423,7 @@ dehydrated succeeded, but certs still missing from storage
420423
--- http_config
421424
resolver $TEST_NGINX_RESOLVER;
422425
lua_shared_dict auto_ssl 1m;
426+
lua_shared_dict auto_ssl_settings 1m;
423427
424428
init_by_lua_block {
425429
auto_ssl = (require "lib.resty.auto-ssl").new({

t/sanity.t

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ __DATA__
2020
--- http_config
2121
resolver $TEST_NGINX_RESOLVER;
2222
lua_shared_dict auto_ssl 1m;
23+
lua_shared_dict auto_ssl_settings 1m;
2324
2425
init_by_lua_block {
2526
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -132,6 +133,7 @@ auto-ssl: issuing new certificate for
132133
--- http_config
133134
resolver $TEST_NGINX_RESOLVER;
134135
lua_shared_dict auto_ssl 1m;
136+
lua_shared_dict auto_ssl_settings 1m;
135137
136138
init_by_lua_block {
137139
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -244,6 +246,7 @@ issuing new certificate for
244246
--- http_config
245247
resolver $TEST_NGINX_RESOLVER;
246248
lua_shared_dict auto_ssl 1m;
249+
lua_shared_dict auto_ssl_settings 1m;
247250
248251
init_by_lua_block {
249252
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -350,6 +353,7 @@ lua ssl certificate verify error: (18: self signed certificate)
350353
--- http_config
351354
resolver $TEST_NGINX_RESOLVER;
352355
lua_shared_dict auto_ssl 1m;
356+
lua_shared_dict auto_ssl_settings 1m;
353357
354358
init_by_lua_block {
355359
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -453,6 +457,7 @@ lua ssl certificate verify error: (18: self signed certificate)
453457
--- http_config
454458
resolver $TEST_NGINX_RESOLVER;
455459
lua_shared_dict auto_ssl 1m;
460+
lua_shared_dict auto_ssl_settings 1m;
456461
457462
init_by_lua_block {
458463
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -565,6 +570,7 @@ lua ssl certificate verify error: (18: self signed certificate)
565570
--- http_config
566571
resolver $TEST_NGINX_RESOLVER;
567572
lua_shared_dict auto_ssl 1m;
573+
lua_shared_dict auto_ssl_settings 1m;
568574
569575
init_by_lua_block {
570576
auto_ssl = (require "lib.resty.auto-ssl").new()
@@ -676,6 +682,7 @@ lua ssl certificate verify error: (18: self signed certificate)
676682
--- http_config
677683
resolver $TEST_NGINX_RESOLVER;
678684
lua_shared_dict auto_ssl 1m;
685+
lua_shared_dict auto_ssl_settings 1m;
679686
680687
init_by_lua_block {
681688
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -784,6 +791,7 @@ lua ssl certificate verify error: (18: self signed certificate)
784791
--- http_config
785792
resolver $TEST_NGINX_RESOLVER;
786793
lua_shared_dict auto_ssl 1m;
794+
lua_shared_dict auto_ssl_settings 1m;
787795
788796
init_by_lua_block {
789797
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -892,6 +900,7 @@ lua ssl certificate verify error: (18: self signed certificate)
892900
--- http_config
893901
resolver $TEST_NGINX_RESOLVER;
894902
lua_shared_dict auto_ssl 1m;
903+
lua_shared_dict auto_ssl_settings 1m;
895904
896905
init_by_lua_block {
897906
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -1051,6 +1060,7 @@ lua ssl certificate verify error: (18: self signed certificate)
10511060
--- http_config
10521061
resolver $TEST_NGINX_RESOLVER;
10531062
lua_shared_dict auto_ssl 1m;
1063+
lua_shared_dict auto_ssl_settings 1m;
10541064
10551065
init_by_lua_block {
10561066
auto_ssl = (require "lib.resty.auto-ssl").new({
@@ -1171,6 +1181,7 @@ auto-ssl: dehydrated succeeded, but certs still missing from storage - trying to
11711181
--- http_config
11721182
resolver $TEST_NGINX_RESOLVER;
11731183
lua_shared_dict auto_ssl 1m;
1184+
lua_shared_dict auto_ssl_settings 1m;
11741185
11751186
init_by_lua_block {
11761187
auto_ssl = (require "lib.resty.auto-ssl").new({

t/worker_file_permissions.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ user $TEST_NGINX_NOBODY_USER $TEST_NGINX_NOBODY_GROUP;
2828
--- http_config
2929
resolver $TEST_NGINX_RESOLVER;
3030
lua_shared_dict auto_ssl 1m;
31+
lua_shared_dict auto_ssl_settings 1m;
3132
3233
init_by_lua_block {
3334
auto_ssl = (require "lib.resty.auto-ssl").new({

0 commit comments

Comments
 (0)