4 ms·
Writing an Nginx authentication module in Lua and Go
- casca 13y agoI'm also getting a lot of value from nginx+lua. It's amazing how many problems can be solved quickly and easily without going to another level of coding. And with the ability to call C libraries from within lua, integrating with 3rd-party binary libs has been another way to avoid another layer.
- jemeshsu 13y agoBeside the authentication highlighted by the article, any other use cases/problems that can be solved by nginx+lua?
- crawdog 13y agoi have been using lua for request manipulation and logging. here's a use case: you want to proxy request cookies from your application into 2 cookies (session/persistent), but you don't want to rewrite the legacy app. lua does a great job with request/response manipulation.
- GrinningFool 13y ago> can be solved quickly and easily without going to another level of coding We've recently done a refactor of a lot of complex load balancer rules into lua + redis. It really is a lot more maintainable. While it is more maintainable, don't let the illusion fool you - the complexity is still there. The solution may be quicker, but you still had to write code for the solution. It's just different code - and in a different place - from where you'd otherwise put it. In our case, it made sense - the logic was all already in the load balancer, it was just a lot more scattered. In other cases - such as splitting what is truly application logic between your LB and your application - it may lead to more headache than it's worth in exchange for a few ms of performance improvement. (I'll likely be writing this thought up in more detail in the near future.)
- pkulak 13y agoI don't think I've ever heard "not enough documentation" as a complaint about Go. It's always seemed fantastic to me. But, I don't use Lua and Python much, so maybe they are even better.
- StavrosK 13y agoI don't know, I couldn't find much documentation on doing HTTP basic auth, and something else which I forget now. I did find a project that had a code listing, though, and used that.
- ancarda 13y agoI have that personal gripe about Go's documentation. Compared to C# and even PHP, Go's documentation is very short, often being a single line. For a beginner, it's difficult to learn Go this way.[1] Since taking it up, I've found it more helpful to buy books on Go, whereas I've been able to learn PHP and C# almost solely off the documentation. I still don't know much about Go and I feel Golang could do a much better job of documenting the language and making it accessible to beginners. http://golang.org/pkg/io/#WriteString http://golang.org/pkg/io/#WriteString vs http://php.net/fwrite http://php.net/fwrite and http://msdn.microsoft.com/en-us/library/ms143376.aspx http://msdn.microsoft.com/en-us/library/ms143376.aspx [1] Edit: I'm aware that learning via documentation is probably not the right way to do it, but it's part of the learning process and thus, good documentation is important.
- moonboots 13y agoAlso, the if c1 ~= c2 branch is obviously not constant-time, but in practice it’s close enough that it won’t matter for our case. I’d prefer XORing the two strings together and seeing if the result is 0, but Lua doesn’t seem to include a bitwise XOR operator. If I’m wrong on this, I would appreciate a correction. LuaJIT provides bitwise operators out of the box [1], and the creator also provides an extension for regular lua [2]. [1] http://luajit.org/extensions.html http://luajit.org/extensions.html [2] http://bitop.luajit.org/ http://bitop.luajit.org/
- StavrosK 13y agoThanks, our nginx is linked with 5.2, it turns out, it's just my test box that had 5.1/didn't support bitwise. I'll change the code, thanks.
- pjmlp 13y agoWell, I remember writting proxy modules for Apache and IIS in C/TCL back in the day (~ 2000). So not much different.
- daurnimator 13y agoLua string comparison IS constant time (its always just a pointer comparison). The timing attack (i.e. memcmp) would happen at the string interning step; which cannot be avoided: but as a lua_State persists longer than a single request, I'm unsure if a timing attack here is plausible.
- stavros 13y agoI don't understand how you can compare two strings in constant time by doing a pointer comparison. Can you elaborate?
- harrytuttle 13y agoDoes Lua do string interning? If it does then every string the same will be at the same memory location so you can do a simple address comparison. edit: someone answered this in a sibling post with 'yes'.
- VMG 13y agoreference: http://www.luafaq.org/gotchas.html#T9 http://www.luafaq.org/gotchas.html#T9
- deleted 13y ago[deleted]
- johnpmayer 13y agoMaybe you could instantiate every string in a global radix tree?
- StavrosK 13y agoEven then, how would you know which string the one you have is, unless you compare it?
- sgolestane 13y agoStrings are interned in Lua which means identical strings have the same reference.
- deleted 13y ago[deleted]