Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVP of Consul storage adapter to lua-resty-auto-ssl #25

Open
fititnt opened this issue Nov 27, 2019 · 10 comments
Open

MVP of Consul storage adapter to lua-resty-auto-ssl #25

fititnt opened this issue Nov 27, 2019 · 10 comments
Labels

Comments

@fititnt
Copy link
Owner

fititnt commented Nov 27, 2019


I will try do some MVP of storage adapter for https://github.com/GUI/lua-resty-auto-ssl using https://github.com/hamishforbes/lua-resty-consul as library to talk with Consul.

GUI/lua-resty-auto-ssl does not have formal documentation on how to implement one adapter, but looking at redis.lua (134 lines) and file.lua (92 lines) is likely to be more easy learn the bare minimum of Lua to make a Consul adapter than the not-very-efficient way to create some way to synchronize both ways Consul with files in some folder.

If take too much time or get some hard issues I will prioritize other tasks. But this open issue here is to do try do some Minimal Viable Product that could just works.

@fititnt
Copy link
Owner Author

fititnt commented Nov 27, 2019

I'm trying to make VSCode show the code outline like the other languages. Having same issue as here microsoft/vscode#43131 (maybe some conflict with other extensions on my VSCode, since I have a lot).

Also here someone else also tould one specific extension of VSCode can do the outline microsoft/vscode#56209.

Anyway, I'm doing some reverse engineering of the code redis.lua and file.lua to discover what lua-resty-auto-ssl expect

fititnt added a commit that referenced this issue Nov 27, 2019
…of file.lua & redis.lua (to try undestand the bare minimum to implement
@fititnt
Copy link
Owner Author

fititnt commented Nov 27, 2019

I'm trying to make VSCode show the code outline like the other languages. Having same issue as here microsoft/vscode#43131 (maybe some conflict with other extensions on my VSCode, since I have a lot).

About the error, I just had this outdated extension https://github.com/patrys/vscode-code-outline (discovered by using VSCode > Help > Toggle Developer Tools and inspecting the error). I just had to unnistal the extension (and even the author suggest this, since Code Outline is now a native feature.

@fititnt
Copy link
Owner Author

fititnt commented Nov 27, 2019

Ok, This VSCode extension seems to be very powerful to use with lua. The demos are better than ones for Python or PHP. But I will leave for other day configure such extension since I just want syntax highligth for now.

Will use some more simpler than a full development ambient. But yeah is very nice to know that have this type of extension for lua

fititnt added a commit that referenced this issue Nov 28, 2019
…y(), _M.new, _M.get, _M.set, based on redis.lua and direct comparison with the API of lua-resty-consul
fititnt added a commit that referenced this issue Nov 28, 2019
@fititnt
Copy link
Owner Author

fititnt commented Nov 28, 2019

There is another code style for lua at

fititnt added a commit that referenced this issue Nov 28, 2019
@fititnt
Copy link
Owner Author

fititnt commented Nov 28, 2019

Almost there. We're even already storing the keys on the Consul :o

Captura de tela de 2019-11-28 02-54-49

fititnt added a commit that referenced this issue Nov 28, 2019
…centralzed this time (not really need in the end product, but I need to inspect lua things without killing openresty
fititnt added a commit that referenced this issue Nov 28, 2019
…ore than once a minute calls from the same caller (way too many retrys will polute logs for debuggin)
@fititnt
Copy link
Owner Author

fititnt commented Nov 28, 2019

O get agora deve estar relativamente ok.

O Set ainda falta implementar clausula de expire para keys não ficarem para sempre armazenadas

fititnt added a commit that referenced this issue Nov 28, 2019
…s get return string or nil, but the Consul implementation returns a full lua-resty-http response object
@fititnt
Copy link
Owner Author

fititnt commented Nov 28, 2019

Humm... I guess we will also need to put some hardcoded prefix.

Redis equivalent at least the person have to choose betwen 16 databases, to no prefix does not make much differente. But Consul the scope is made using slashs "/".

Captura de tela de 2019-11-28 18-54-47

So, even the delete operations (the recursive ones) cannot be made without some prefix

@fititnt
Copy link
Owner Author

fititnt commented Nov 28, 2019

At consul.lua I was able to change the first prefix from : to /.
Captura de tela de 2019-11-28 20-08-43

Both redis.lua and file.lua can work with : but the batch operations for Consul assumes /.

The file that have references for this is this one https://github.com/GUI/lua-resty-auto-ssl/blob/86d09dcd98224639da1ed36d02bf0eda4b2f0baa/lib/resty/auto-ssl/storage.lua

Maybe I will leave this specific point to another issue.

fititnt added a commit that referenced this issue Nov 28, 2019
…resty-auto-ssl'; fixed prefix char to '/' instead of ':', moved helper functions temporary to the main file (they will be removed later)
@fititnt
Copy link
Owner Author

fititnt commented Nov 30, 2019

This is most a note to self: diff if using "/" instead of ":". Hardcoded, ideally should be configured. And is outside the target file.

diff --git a/lib/resty/auto-ssl/storage.lua b/lib/resty/auto-ssl/storage.lua
index 0f18f35..69f6673 100644
--- a/lib/resty/auto-ssl/storage.lua
+++ b/lib/resty/auto-ssl/storage.lua
@@ -12,19 +12,19 @@ function _M.new(options)
 end
 
 function _M.get_challenge(self, domain, path)
-  return self.adapter:get(domain .. ":challenge:" .. path)
+  return self.adapter:get(domain .. "/challenge/" .. path)
 end
 
 function _M.set_challenge(self, domain, path, value)
-  return self.adapter:set(domain .. ":challenge:" .. path, value)
+  return self.adapter:set(domain .. "/challenge/" .. path, value)
 end
 
 function _M.delete_challenge(self, domain, path)
-  return self.adapter:delete(domain .. ":challenge:" .. path)
+  return self.adapter:delete(domain .. "/challenge/" .. path)
 end
 
 function _M.get_cert(self, domain)
-  local json, err = self.adapter:get(domain .. ":latest")
+  local json, err = self.adapter:get(domain .. "/latest")
   if err then
     return nil, err
   elseif not json then
@@ -57,22 +57,22 @@ function _M.set_cert(self, domain, fullchain_pem, privkey_pem, cert_pem, expiry)
   end
 
   -- Store the cert under the "latest" alias, which is what this app will use.
-  return self.adapter:set(domain .. ":latest", string)
+  return self.adapter:set(domain .. "/latest", string)
 end
 
 function _M.delete_cert(self, domain)
-  return self.adapter:delete(domain .. ":latest")
+  return self.adapter:delete(domain .. "/latest")
 end
 
 function _M.all_cert_domains(self)
-  local keys, err = self.adapter:keys_with_suffix(":latest")
+  local keys, err = self.adapter:keys_with_suffix("/latest")
   if err then
     return nil, err
   end
 
   local domains = {}
   for _, key in ipairs(keys) do
-    local domain = ngx.re.sub(key, ":latest$", "", "jo")
+    local domain = ngx.re.sub(key, "/latest$", "", "jo")
     table.insert(domains, domain)
   end
 
@@ -91,7 +91,7 @@ end
 -- but in combination with resty-lock, it should prevent the vast majority of
 -- double requests.
 function _M.issue_cert_lock(self, domain)
-  local key = domain .. ":issue_cert_lock"
+  local key = domain .. "/issue_cert_lock"
   local lock_rand_value = str.to_hex(resty_random.bytes(32))
 
   -- Wait up to 30 seconds for any existing locks to be unlocked.
@@ -119,7 +119,7 @@ function _M.issue_cert_lock(self, domain)
 end
 
 function _M.issue_cert_unlock(self, domain, lock_rand_value)
-  local key = domain .. ":issue_cert_lock"
+  local key = domain .. "/issue_cert_lock"
 
   -- Remove the existing lock if it matches the expected value.
   local current_value, err = self.adapter:get(key)

@fititnt
Copy link
Owner Author

fititnt commented Dec 1, 2019

From https://github.com/GUI/lua-resty-auto-ssl/search?q=exptime&unscoped_q=exptime the exptime, when used, seems to be less than 24h. So the way it is implemented should work with Consul without need of using ngx.timer.

But for what I see, both file.lua and redis.lua actually does not set TTL or expiration, they just use ngx.timer and do it more manually. Humm...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant