Skip to content

Commit 942c007

Browse files
authored
feat(autossl) check if domain is whitelisted before cert renewal (#35)
1 parent ff17a74 commit 942c007

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ domain_whitelist = { "domain1.com", "domain2.com", "domain3.com" },
148148
To match a pattern in your domain name, for example all subdomains under `example.com`, use:
149149

150150
```lua
151-
domain_whitelist_callback = function(domain)
151+
domain_whitelist_callback = function(domain, is_new_cert_needed)
152152
return ngx.re.match(domain, [[\.example\.com$]], "jo")
153153
end
154154
```
@@ -158,7 +158,7 @@ It's possible to use cosocket API here. Do note that this will increase the SSL
158158
latency.
159159

160160
```lua
161-
domain_whitelist_callback = function(domain)
161+
domain_whitelist_callback = function(domain, is_new_cert_needed)
162162
-- send HTTP request
163163
local http = require("resty.http")
164164
local res, err = httpc:request_uri("http://example.com")
@@ -169,6 +169,8 @@ domain_whitelist_callback = function(domain)
169169
end}),
170170
```
171171

172+
`domain_whitelist_callback` function is provided with a second argument,
173+
which indicates whether the certificate is about to be served on incoming HTTP request (false) or new certificate is about to be requested (true). This allows to use cached values on hot path (serving requests) while fetching fresh data from storage for new certificates. One may also implement different logic, e.g. do extra checks before requesting new cert.
172174

173175
## tls-alpn-01 challenge
174176

@@ -300,7 +302,7 @@ All normal https traffic listens on `unix:/tmp/nginx-default.sock`.
300302

301303
```
302304
[stream server unix:/tmp/nginx-tls-alpn.sock ssl]
303-
Y /
305+
Y /
304306
[stream server 443] --- ALPN is acme-tls ?
305307
N \
306308
[http server unix:/tmp/nginx-default.sock ssl]

lib/resty/acme/autossl.lua

+15-5
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ function AUTOSSL.update_cert(data)
213213
AUTOSSL.client_initialized = true
214214
end
215215

216+
if not AUTOSSL.is_domain_whitelisted(data.domain, true) then
217+
return "cert update is not allowed for domain " .. data.domain
218+
end
219+
216220
-- Note that we lock regardless of key types
217221
-- Let's encrypt tends to have a (undocumented?) behaviour that if
218222
-- you submit an order with different CSR while the previous order is still pending
@@ -264,7 +268,7 @@ function AUTOSSL.check_renew()
264268
})
265269

266270
if err then
267-
log(ngx_ERR, "failed to renew certificate for domain ", domain)
271+
log(ngx_ERR, "failed to renew certificate for domain ", domain, " error: ", err)
268272
else
269273
log(ngx_INFO, "successfully renewed ", deserialized.type, " cert for domain ", domain)
270274
end
@@ -387,6 +391,15 @@ function AUTOSSL.serve_tls_alpn_challenge()
387391
AUTOSSL.client:serve_tls_alpn_challenge()
388392
end
389393

394+
function AUTOSSL.is_domain_whitelisted(domain, is_new_cert_needed)
395+
if domain_whitelist_callback then
396+
return domain_whitelist_callback(domain, is_new_cert_needed)
397+
elseif domain_whitelist then
398+
return domain_whitelist[domain]
399+
else
400+
return true
401+
end
402+
end
390403

391404
function AUTOSSL.ssl_certificate()
392405
local domain, err = ssl.server_name()
@@ -398,10 +411,7 @@ function AUTOSSL.ssl_certificate()
398411

399412
domain = string.lower(domain)
400413

401-
if domain_whitelist_callback and not domain_whitelist_callback(domain) then
402-
log(ngx_INFO, "domain ", domain, " does not pass whitelist_callback, skipping")
403-
return
404-
elseif domain_whitelist and not domain_whitelist[domain] then
414+
if not AUTOSSL.is_domain_whitelisted(domain, false) then
405415
log(ngx_INFO, "domain ", domain, " not in whitelist, skipping")
406416
return
407417
end

0 commit comments

Comments
 (0)