Skip to content

Commit ebfa5d3

Browse files
committed
Expose the redis adapter's redis connection in the allow_domain callback
The auto_ssl instance is now passed as the second argument to the `allow_domain` callback. If using the Redis storage adapter, then now the Redis connection can be accessed by `auto_ssl.storage.adapter:get_connection()`. This allows for more easily accessing the same redis connection in this callback function (or anywhere else the auto_ssl instance is available). This rolls back the change in 9703684 to rename the storage adapter instance inside the storage library, so it continues to just be `adapter` (instead of `storage_adapter`). This makes this access a bit easier and also keeps backwards compatibility with the previous release. See: #38
1 parent 4aed490 commit ebfa5d3

File tree

13 files changed

+244
-75
lines changed

13 files changed

+244
-75
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Added
66
- Allow for the Redis `db` number to be configured. Thanks to [@RainFlying](https://github.com/RainFlying). ([#103](https://github.com/GUI/lua-resty-auto-ssl/pull/103))
7+
- Expose the storage adapter instance in the `allow_domain` callback so the Redis connection can be reused. ([#38](https://github.com/GUI/lua-resty-auto-ssl/issues/38))
78
- Add `generate_certs` option to allow for disabling SSL certification generation within specific server blocks. Thanks to [@mklauber](https://github.com/mklauber). ([#91](https://github.com/GUI/lua-resty-auto-ssl/issues/91), [#92](https://github.com/GUI/lua-resty-auto-ssl/pull/92))
89
- Add `json_adapter` option for choosing a different JSON encoder/decoder library. Thanks to [@meyskens](https://github.com/meyskens). ([#85](https://github.com/GUI/lua-resty-auto-ssl/pull/85), [#84](https://github.com/GUI/lua-resty-auto-ssl/issues/84))
910

@@ -12,6 +13,9 @@
1213
- Only call the `allow_domain` callback if a certificate is not present in shared memory. This may improve efficiency in cases where the `allow_domain` callback is more costly or takes longer. Thanks to [@gohai](https://github.com/gohai). ([#107](https://github.com/GUI/lua-resty-auto-ssl/pull/107))
1314
- Upgrade dehydrated to latest version from master to fix redirect issues on the Let's Encrypt staging server.
1415

16+
### Deprecated
17+
- If accessing the storage object off of the auto-ssl instance, use `auto_ssl.storage` instead of `auto_ssl:get("storage")`.
18+
1519
### Fixed
1620
- Fix renewals when using the file adapter and too many certificate files were present for shell globbing ([#109](https://github.com/GUI/lua-resty-auto-ssl/issues/109))
1721

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,18 @@ http {
138138
Additional configuration options can be set on the `auto_ssl` instance that is created:
139139

140140
### `allow_domain`
141-
*Default:* `function(domain) return false end`
141+
*Default:* `function(domain, auto_ssl) return false end`
142142

143143
A function that determines whether the incoming domain should automatically issue a new SSL certificate.
144144

145145
By default, resty-auto-ssl will not perform any SSL registrations until you define the `allow_domain` function. You may return `true` to handle all possible domains, but be aware that bogus SNI hostnames can then be used to trigger an indefinite number of SSL registration attempts (which will be rejected). A better approach may be to whitelist the allowed domains in some way.
146146

147+
When using the Redis storage adapter, you can access the current Redis connection inside the `allow_domain` callback by accessing `auto_ssl.storage.adapter:get_connection()`.
148+
147149
*Example:*
148150

149151
```lua
150-
auto_ssl:set("allow_domain", function(domain)
152+
auto_ssl:set("allow_domain", function(domain, auto_ssl)
151153
return ngx.re.match(domain, "^(example.com|example.net)$", "ijo")
152154
end)
153155
```

lib/resty/auto-ssl.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,25 @@ function _M.new(options)
5252
end
5353

5454
function _M.set(self, key, value)
55+
if key == "storage" then
56+
ngx.log(ngx.ERR, "auto-ssl: DEPRECATED: Don't use auto_ssl:set() for the 'storage' instance. Set directly with auto_ssl.storage.")
57+
self.storage = value
58+
return
59+
end
60+
5561
self.options[key] = value
5662
end
5763

5864
function _M.get(self, key)
65+
if key == "storage" then
66+
ngx.log(ngx.ERR, "auto-ssl: DEPRECATED: Don't use auto_ssl:get() for the 'storage' instance. Get directly with auto_ssl.storage.")
67+
return self.storage
68+
end
69+
5970
return self.options[key]
6071
end
6172

62-
function _M.allow_domain(domain) -- luacheck: ignore
73+
function _M.allow_domain(domain, auto_ssl) -- luacheck: ignore
6374
return false
6475
end
6576

lib/resty/auto-ssl/init_master.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ local function setup_storage(auto_ssl_instance)
9191

9292
local storage = require "resty.auto-ssl.storage"
9393
local storage_instance = storage.new({
94-
storage_adapter = storage_adapter_instance,
94+
adapter = storage_adapter_instance,
9595
json_adapter = json_adapter_instance,
9696
})
97-
auto_ssl_instance:set("storage", storage_instance)
97+
auto_ssl_instance.storage = storage_instance
9898
end
9999

100100
return function(auto_ssl_instance)

lib/resty/auto-ssl/init_worker.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ return function(auto_ssl_instance)
2525
-- background process, which would be nice.
2626
start_sockproc()
2727

28-
local storage = auto_ssl_instance:get("storage")
29-
local storage_adapter = storage.storage_adapter
28+
local storage = auto_ssl_instance.storage
29+
local storage_adapter = storage.adapter
3030
if storage_adapter.setup_worker then
3131
storage_adapter:setup_worker()
3232
end

lib/resty/auto-ssl/jobs/renewal.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ end
136136

137137
local function renew_all_domains(auto_ssl_instance)
138138
-- Loop through all known domains and check to see if they should be renewed.
139-
local storage = auto_ssl_instance:get("storage")
139+
local storage = auto_ssl_instance.storage
140140
local domains, domains_err = storage:all_cert_domains()
141141
if domains_err then
142142
ngx.log(ngx.ERR, "auto-ssl: failed to fetch all certificate domains: ", domains_err)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ return function(auto_ssl_instance)
1515

1616
-- Return the challenge value for this token if it's found.
1717
local domain = ngx.var.host
18-
local storage = auto_ssl_instance:get("storage")
18+
local storage = auto_ssl_instance.storage
1919
local value = storage:get_challenge(domain, token_filename)
2020
if value then
2121
ngx.say(value)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ return function(auto_ssl_instance)
1717
end
1818

1919
local path = ngx.var.request_uri
20-
local storage = auto_ssl_instance:get("storage")
20+
local storage = auto_ssl_instance.storage
2121
if path == "/deploy-challenge" then
2222
assert(params["domain"])
2323
assert(params["token_filename"])

lib/resty/auto-ssl/ssl_certificate.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ local function get_cert_der(auto_ssl_instance, domain, ssl_options)
122122
-- We may want to consider caching the results of allow_domain lookups
123123
-- (including negative caching or disallowed domains).
124124
local allow_domain = auto_ssl_instance:get("allow_domain")
125-
if not allow_domain(domain) then
125+
if not allow_domain(domain, auto_ssl_instance) then
126126
return nil, "domain not allowed"
127127
end
128128

129129
-- Next, look for the certificate in permanent storage (which can be shared
130130
-- across servers depending on the storage).
131-
local storage = auto_ssl_instance:get("storage")
131+
local storage = auto_ssl_instance.storage
132132
local cert, get_cert_err = storage:get_cert(domain)
133133
if get_cert_err then
134134
ngx.log(ngx.ERR, "auto-ssl: error fetching certificate from storage for ", domain, ": ", get_cert_err)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function _M.issue_cert(auto_ssl_instance, domain)
4646

4747
-- The result of running that command should result in the certs being
4848
-- populated in our storage (due to the deploy_cert hook triggering).
49-
local storage = auto_ssl_instance:get("storage")
49+
local storage = auto_ssl_instance.storage
5050
local cert, get_cert_err = storage:get_cert(domain)
5151
if get_cert_err then
5252
ngx.log(ngx.ERR, "auto-ssl: error fetching certificate from storage for ", domain, ": ", get_cert_err)

0 commit comments

Comments
 (0)