Skip to content

Commit

Permalink
lua-resty-auto-ssl-storage-adapter-consul (#25): Draft of prefixed_ke…
Browse files Browse the repository at this point in the history
…y(), _M.new, _M.get, _M.set, based on redis.lua and direct comparison with the API of lua-resty-consul
  • Loading branch information
fititnt committed Nov 28, 2019
1 parent dab478a commit ded064a
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 32 deletions.
159 changes: 130 additions & 29 deletions files/resty/auto-ssl/storage_adapters/consul.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
-- License: Public Domain

-- Requisites:
-- opm get hamishforbes/lua-resty-consul

------- How to test:
-- Copy this file to /usr/local/share/lua/5.1/resty/auto-ssl/storage_adapters/consul.lua. With ansible would be:
-- ansible -m copy -a "src=./consul.lua dest=/usr/local/share/lua/5.1/resty/auto-ssl/storage_adapters/consul.lua" aguia-pescadora-delta.etica.ai,aguia-pescadora-echo.etica.ai,aguia-pescadora-foxtrot.etica.ai
-- Them set the following on your OpenResty, at http context
-- auto_ssl:set("storage_adapter", "resty.auto-ssl.storage_adapters.consul")


print ('teste 123')

-- How to document Lua code:
-- - https://stevedonovan.github.io/ldoc/manual/doc.md.html
-- - https://keplerproject.github.io/luadoc/manual.html
-- - http://lua-users.org/wiki/LuaStyleGuide
-- - http://sputnik.freewisdom.org/en/Coding_Standard

-- I think the path would be /usr/local/share/lua/5.1/resty/auto-ssl/storage_adapters/consul.lua
-- to work with resty/auto-ssl
Expand All @@ -21,54 +23,153 @@ print ('teste 123')
-- Definitely an openresty guide/ Hello world https://www.staticshin.com/programming/definitely-an-open-resty-guide/#hello_world
-- Lua in 15 minutes http://tylerneylon.com/a/learn-lua/


-- @module storage_adapter_consul
local _M = {}

-- resty-auto-ssl file.lua/redis.lua have these exported funcions in common ----
--- Local helper function to, if options have prefix, return a prefixed key name
-- @param {self} self
-- @param {string} key The umprefixed key name
-- @return {string} The key prefixed
local function prefixed_key(self, key)
if self.options["prefix"] then
return self.options["prefix"] .. ":" .. key
else
return key
end
end

-- TODO: Discover what to type is the return of _M.new (fititnt, 2019-11-27 22:51 BRT)

--- Returns a stored Key Value from the Consul
-- @param {self} self
-- @param {string} key The umprefixed key name
-- @return ????
function _M.new(auto_ssl_instance)
local options = auto_ssl_instance:get("consul") or {}

if not options["host"] then
options["host"] = "127.0.0.1"
end

if not options["port"] then
options["port"] = 8500
end

if not options["connect_timeout"] then
options["connect_timeout"] = '60s'
end

if not options["read_timeout"] then
options["read_timeout"] = '60s'
end

if not options["ssl"] then
options["ssl"] = false
end

if not options["ssl_verify"] then
options["ssl_verify"] = true
end

return setmetatable({ options = options }, { __index = _M })
end

-- TODO: finish _M.get_connection (fititnt, 2019-27-23:01 BRT)
function _M.get_connection(self)
return connection
end

-- Note: _M.setup() on redis.lua is empty, no arguments, no return value
function _M.setup()
end

--- Returns a stored Key Value from the Consul
-- @param {self} self
-- @param {string} key The umprefixed key name
-- @return {string} The value of saved key (if exists)
function _M.get(self, key)
-- return content -- file.lua
return res, err -- redis.lua
local connection, connection_err = self:get_connection()
if connection_err then
return nil, connection_err
end

-- Redis use get, Consul use get_key
local res, err = connection:get_key(prefixed_key(self, key))
if res == ngx.null then
res = nil
end

return res, err
end

--- Save on Consul a key-value
-- @param {self} self
-- @param {string} key The umprefixed key name
-- @param {string} value The values
-- @return {string} The value of saved key (if exists)
function _M.set(self, key, value, options)
-- if err then -- file.lua
-- ngx.log(ngx.ERR, "auto-ssl: failed to open file for writing: ", err) -- file.lua
-- return false, err -- file.lua
-- ...
-- return true
return ok, err -- redis.lua
local connection, connection_err = self:get_connection()
if connection_err then
return false, connection_err
end

key = prefixed_key(self, key)

-- Redis use set, Consul use put_key:
local ok, err = connection:put_key(key, value)

-- Know issue: not implemented way to expire key at this moment.
-- The following was from redis.lua
-- if ok then
-- if options and options["exptime"] then
-- local _, expire_err = connection:expire(key, options["exptime"])
-- if expire_err then
-- ngx.log(ngx.ERR, "auto-ssl: failed to set expire: ", expire_err)
-- end
-- end
-- end

return ok, err
end

--- Delete a stored value
-- @param {self} self
-- @param {string} key The umprefixed key name
-- @return {string} The value of saved key (if exists)
function _M.delete(self, key)
-- local ok, err = os.remove(file_path(self, key)) -- file.lua
-- return false, err -- file.lua
-- return ok, err -- file.lua
return connection:del(prefixed_key(self, key)) -- redis.lua
local connection, connection_err = self:get_connection()
if connection_err then
return false, connection_err
end

-- Redis use del, Consul uses delete_key
return connection:delete_key(prefixed_key(self, key))
end

-- TODO: finish _M.keys_with_suffix (fititnt, 2019-27-23:01 BRT)
function _M.keys_with_suffix(self, suffix)
-- return keys -- file.lua
return keys, err -- redis.lua
end
local connection, connection_err = self:get_connection()
if connection_err then
return false, connection_err
end

-- resty-auto-ssl redis.lua only -----------------------------------------------
-- Redis use keys, ...
local keys, err = connection:keys(prefixed_key(self, "*" .. suffix))

function _M.get_connection(self)
return connection
end
if keys and self.options["prefix"] then
local unprefixed_keys = {}
-- First character past the prefix and a colon
local offset = string.len(self.options["prefix"]) + 2

-- Note: _M.setup() on redis.lua is empty, no arguments, no return value
function _M.setup()
end
for _, key in ipairs(keys) do
local unprefixed = string.sub(key, offset)
table.insert(unprefixed_keys, unprefixed)
end

keys = unprefixed_keys
end

-- resty-auto-ssl file.lua only ------------------------------------------------
function _M.setup_worker(self)
return keys, err
end

return _M
17 changes: 14 additions & 3 deletions tasks/openresty/openresty-luarocks-lua-resty-auto-ssl-install.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
---

# FILE: {{ role_path }}/tasks/openresty-luarocks-lua-resty-auto-ssl-install.yml
# LICENSE: Public Domain


## openresty? luarocks? lua-resty-auto-ssl? ____________________________________
- name: "openresty | openresty? (Safe to ignore error first time!)"
shell: openresty -v
Expand Down Expand Up @@ -59,6 +56,20 @@
update_cache: yes
when: is_luarocks is failed

## apt install -y luarocks _____________________________________________________

# TODO: manage better if already installed
#- name: "openresty | opm list | grep lua-resty-consul | wc -l? (Safe to ignore error first time!)"
# shell: opm list | grep lua-resty-consul | wc -l
# register: is_lua_resty_consul
# changed_when: false
# ignore_errors: true

- name: "openresty | opm get hamishforbes/lua-resty-consul"
shell: opm get hamishforbes/lua-resty-consul
when:
- alb_use_consul|bool

## luarocks install lua-resty-auto-ssl _________________________________________
# @see https://github.com/GUI/lua-resty-auto-ssl#installation

Expand Down

0 comments on commit ded064a

Please sign in to comment.