Fix Lets'encrypt plugin api and internal API

This commit is contained in:
Théophile Diot 2023-06-09 14:18:10 -04:00
parent 179a7aa34a
commit 6a1ff499c1
No known key found for this signature in database
GPG Key ID: E752C80DB72BB014
2 changed files with 7 additions and 8 deletions

View File

@ -221,17 +221,16 @@ function api:do_api_call()
end
end
end
local list, err = self.datastore:get("plugins")
local list, err = self.datastore:get("plugins", true)
if not list then
local status, resp = self:response(ngx.HTTP_INTERNAL_SERVER_ERROR, "error", "can't list loaded plugins : " .. err)
return false, resp["msg"], ngx.HTTP_INTERNAL_SERVER_ERROR, cjson.encode(resp)
end
list = cjson.decode(list)
for i, plugin in ipairs(list) do
if pcall(require, plugin.id .. "/" .. plugin.id) then
local plugin_lua = require(plugin.id .. "/" .. plugin.id)
if plugin_lua.api ~= nil then
local matched, status, resp = plugin_lua.api()
local matched, status, resp = plugin_lua:api(self.ctx)
if matched then
local ret = true
if status ~= ngx.HTTP_OK then

View File

@ -18,9 +18,9 @@ function letsencrypt:access()
return self:ret(true, "success")
end
function letsencrypt:api()
if not string.match(self.ctx.bw.uri, "^/lets%-encrypt/challenge$") or
(self.ctx.bw.request_method ~= "POST" and self.ctx.bw.request_method ~= "DELETE") then
function letsencrypt:api(ctx)
if not string.match(ctx.bw.uri, "^/lets%-encrypt/challenge$") or
(ctx.bw.request_method ~= "POST" and ctx.bw.request_method ~= "DELETE") then
return false, nil, nil
end
local acme_folder = "/var/tmp/bunkerweb/lets-encrypt/.well-known/acme-challenge/"
@ -30,7 +30,7 @@ function letsencrypt:api()
return true, ngx.HTTP_BAD_REQUEST, { status = "error", msg = "json body decoding failed" }
end
os.execute("mkdir -p " .. acme_folder)
if self.ctx.bw.request_method == "POST" then
if ctx.bw.request_method == "POST" then
local file, err = io.open(acme_folder .. data.token, "w+")
if not file then
return true, ngx.HTTP_INTERNAL_SERVER_ERROR, { status = "error", msg = "can't write validation token : " .. err }
@ -38,7 +38,7 @@ function letsencrypt:api()
file:write(data.validation)
file:close()
return true, ngx.HTTP_OK, { status = "success", msg = "validation token written" }
elseif self.ctx.bw.request_method == "DELETE" then
elseif ctx.bw.request_method == "DELETE" then
local ok, err = os.remove(acme_folder .. data.token)
if not ok then
return true, ngx.HTTP_INTERNAL_SERVER_ERROR, { status = "error", msg = "can't remove validation token : " .. err }