Skip to content

Commit 86d3c94

Browse files
committed
Merge branch 'Cargo-has_certificate' into master
2 parents 247f375 + 0f7bfd5 commit 86d3c94

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,17 @@ auto_ssl:set("http_proxy_options", {
315315
})
316316
```
317317

318-
## `ssl_certificate` Configuration
318+
## API
319+
320+
<a id="ssl_certificate-configuration"></a>
321+
### `ssl_certificate`
322+
*Syntax:* `auto_ssl:ssl_certificate(options)`
319323

320324
The `ssl_certificate` function accepts an optional table of configuration options. These options can be used to customize and control the SSL behavior on a per nginx `server` basis. Some built-in options may control the default behavior of lua-resty-auto-ssl, but any other custom data can be given as options, which will then be passed along to the [`allow_domain`](#allow_domain) and [`request_domain`](#request_domain) callback functions.
321325

322326
Built-in configuration options:
323327

324-
### `generate_certs`
328+
#### `generate_certs`
325329
*Default:* true
326330

327331
This variable can be used to disable generating certs on a per server block location.
@@ -337,7 +341,26 @@ server {
337341
}
338342
```
339343

340-
### Advanced Let's Encrypt Configuration
344+
### `has_certificate`
345+
*Syntax:* `exists = auto_ssl:has_certificate(domain, shmem_only?)`
346+
347+
The `has_certificate` function returns a boolean value for whether or not a certificate exists for the given `domain`. This is first looked up in the local shared memory cache, and then falls back to fetching from storage.
348+
349+
The optional `shmem_only` parameter can be set to true in order to only check the local shared memory cache for the presence of the certificate, and not check the storage engine.
350+
351+
*Example:*
352+
353+
```nginx
354+
rewrite_by_lua_block {
355+
local has_cert = auto_ssl:has_certificate(ngx.var.host)
356+
if has_cert then
357+
local https_uri = "https://" .. ngx.var.host .. ngx.var.request_uri
358+
ngx.redirect(https_uri, 301)
359+
end
360+
}
361+
```
362+
363+
## Advanced Let's Encrypt Configuration
341364

342365
Internally, lua-resty-auto-ssl uses [dehydrated](https://github.com/lukas2511/dehydrated) as it's Let's Encrypt client. If you'd like to adjust lower-level settings, like the private key size, public key algorithm, or your registration e-mail, these settings can be configured in a custom dehydrated configuration file.
343366

lib/resty/auto-ssl.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ function _M.challenge_server(self)
9999
server(self)
100100
end
101101

102+
function _M.has_certificate(self, domain, shmem_only)
103+
local has_certificate = require "resty.auto-ssl.utils.has_certificate"
104+
return has_certificate(self, domain, shmem_only)
105+
end
106+
102107
function _M.hook_server(self)
103108
local server = require "resty.auto-ssl.servers.hook"
104109
server(self)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
return function(auto_ssl_instance, domain, shmem_only)
2+
local shmem = ngx.shared.auto_ssl:get("domain:fullchain_der:" .. domain)
3+
if shmem then
4+
return true
5+
elseif shmem_only then
6+
return false
7+
end
8+
9+
local storage = auto_ssl_instance.storage
10+
local cert = storage:get_cert(domain)
11+
if cert then
12+
return true
13+
end
14+
15+
return false
16+
end

0 commit comments

Comments
 (0)