Skip to content

Commit

Permalink
feat(runloop): plugin:configure(configs) handler
Browse files Browse the repository at this point in the history
### Summary

Adds a new handler that plugins can implement:

```
 -- configs will be `nil` if there is no active configs for the plugin
function Plugin:configure(configs)
end
```

See the change in `acme` plugin.

Signed-off-by: Aapo Talvensaari <[email protected]>
  • Loading branch information
bungle committed Oct 6, 2023
1 parent e373295 commit 490d63e
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 140 deletions.
18 changes: 4 additions & 14 deletions kong/plugins/acme/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -508,20 +508,10 @@ local function renew_certificate_storage(conf)

end

local function renew_certificate(premature)
if premature then
return
end

for plugin, err in kong.db.plugins:each(1000) do
if err then
kong.log.warn("error fetching plugin: ", err)
end

if plugin.name == "acme" then
kong.log.info("renew storage configured in acme plugin: ", plugin.id)
renew_certificate_storage(plugin.config)
end
local function renew_certificate(configs)
for _, config in ipairs(configs) do
kong.log.info("renew storage configured in acme plugin: ", config.__plugin_id)
renew_certificate_storage(config)
end
end

Expand Down
20 changes: 19 additions & 1 deletion kong/plugins/acme/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,22 @@ local domains_matcher
-- expose it for use in api.lua
ACMEHandler.build_domain_matcher = build_domain_matcher


local CONFIGS


local function renew(premature)
if premature or not CONFIGS then
return
end
client.renew_certificate(CONFIGS)
end


function ACMEHandler:init_worker()
local worker_id = ngx.worker.id()
kong.log.info("acme renew timer started on worker ", worker_id)
ngx.timer.every(86400, client.renew_certificate)
ngx.timer.every(86400, renew)

-- handle cache updating of domains_matcher
kong.worker_events.register(function(data)
Expand All @@ -92,6 +104,12 @@ function ACMEHandler:init_worker()
end, "crud", "plugins")
end


function ACMEHandler:configure(configs)
CONFIGS = configs
end


local function check_domains(conf, host)
if not conf.enable_ipv4_common_name and string_find(host, "^(%d+)%.(%d+)%.(%d+)%.(%d+)$") then
return false
Expand Down
Loading

0 comments on commit 490d63e

Please sign in to comment.