Redis Lua scripting implemented: how to play with it.
antirez.com
antirez.com
That said, if Redis can EVAL a script stored as a String or a Hash->Key, the client can deal with registering and versioning scripts.
Awesome work btw - been looking forward to scripting in Redis for a long time
Seems like a good idea to get the rest of it sorted before deciding on that bit though.
Basically, 1) does Lua have an "eval" statement and 2) can Redis' EVAL read keys (I assume it can, since that's pretty much what the post says)?
So it seems you can set up your own stored procedures.
Basically you give it a "-ext /path/to/waffles.lua" arg at startup to define a bunch of user defined functions.
Inflexible configuration mechanisms are one of the top pain points in cluster-environments (cloud or not). Kudos to antirez for realizing that and working against it.
and 2) this opens a whole new playground for NoSQL injections: I don't know a lot (well. anything) about lua embedding, but I could imagine that these lua scripts somehow could get access to the file system. This means that you have to be really, really careful not to expose redis to the outside world and not to have it execute injected commands.
Also, LUA is a very small at core and I don't think it by default includes things like file system access. You pretty much just get what the host applications gives you, which in this case seems to be just the Redis API.
Secondly, it does include filesystem access by default, and right now there is no decent protection against it. Hopefully, sandboxing will be implemented before this reaches anything related to stable.
Sandboxing Lua does seem quite easy though: http://stackoverflow.com/questions/1224708/how-can-i-create-...
1) Use loadstring() to compile and run a script from a Redis key. EVAL "return loadstring(redis('get', KEYS[1]))()" 1 foo
2) Set a function as a global value, and call that. EVAL "myfunc = function() return 42" 0 EVAL "return myfunc()" 0
antirez said in his previous post (http://antirez.com/post/redis-and-scripting.html) that storing scripts has some problems with regard to Redis Cluster, though.
the possibilities this brings is quite huge tho, so quite excited
local function accesscommand (t, key)
local f = function (...)
return redis(key, ...)
end
t[key] = f
return key
end
db = setmetatable({}, {__index = accesscommand})
function db.hmset (key, values)
local t = {}
for key, value in pairs(values) do
t[#t + 1] = key
t[#t + 1] = value
end
redis("hmset", unpack(t))
end
Any command without a function defined in the table would create a closure on the fly that calls that command like normal. (It also stores the closure in the table to speed up future calls.) Commands that could be implemented more elegantly using Lua semantics (like HMSET) have overrides defined in the db table directly, which prevents the closure-generating function from being called.So the whole point is to expose Redis in Lua in a way that is the most native related to the internals of Redis, and not the most comfortable/high-level.