From 12ae2626f271931f35f8cf0baa3c57ec21d3d56a Mon Sep 17 00:00:00 2001 From: Yuval Lifshitz Date: Tue, 7 Jun 2022 14:27:53 +0300 Subject: [PATCH] rgw/lua: allow read access to object data Signed-off-by: Yuval Lifshitz --- doc/radosgw/lua-scripting.rst | 88 ++++++++++++++++----- src/rgw/CMakeLists.txt | 5 +- src/rgw/rgw_admin.cc | 8 +- src/rgw/rgw_common.h | 6 ++ src/rgw/rgw_lua.cc | 10 +++ src/rgw/rgw_lua.h | 3 + src/rgw/rgw_lua_background.cc | 2 +- src/rgw/rgw_lua_background.h | 2 +- src/rgw/rgw_lua_data_filter.cc | 135 +++++++++++++++++++++++++++++++++ src/rgw/rgw_lua_data_filter.h | 52 +++++++++++++ src/rgw/rgw_lua_request.cc | 16 ++-- src/rgw/rgw_lua_request.h | 14 ++-- src/rgw/rgw_lua_utils.cc | 3 +- src/rgw/rgw_lua_utils.h | 3 +- src/rgw/rgw_op.cc | 48 ++++++++++++ src/rgw/rgw_op.h | 9 +++ src/rgw/rgw_process.cc | 5 +- src/test/rgw/test_rgw_lua.cc | 98 +++++++++++++++++++----- 18 files changed, 446 insertions(+), 61 deletions(-) create mode 100644 src/rgw/rgw_lua_data_filter.cc create mode 100644 src/rgw/rgw_lua_data_filter.h diff --git a/doc/radosgw/lua-scripting.rst b/doc/radosgw/lua-scripting.rst index 750a91397ac25d..f8f6cb289d5e18 100644 --- a/doc/radosgw/lua-scripting.rst +++ b/doc/radosgw/lua-scripting.rst @@ -6,10 +6,18 @@ Lua Scripting .. contents:: -This feature allows users to assign execution context to Lua scripts. The three supported contexts are ``preRequest``" which will execute a script before each -operation is performed, ``postRequest`` which will execute after each operation is performed, and ``background`` which will execute within a specified time interval. -A request context script may be constrained to operations belonging to a specific tenant's users. -The request context script can also access fields in the request and modify some fields. All Lua language features can be used. +This feature allows users to assign execution context to Lua scripts. The supported contexts are: + + - ``preRequest`` which will execute a script before each operation is performed + - ``postRequest`` which will execute after each operation is performed + - ``background`` which will execute within a specified time interval + - ``getData`` which will execute on objects' data when objects are downloaded + - ``putData`` which will execute on objects' data when objects are uploaded + +A request (pre or post) or data (get or put) context script may be constrained to operations belonging to a specific tenant's users. +The request context script can also access fields in the request and modify some fields, as well as the background ``RGW`` table. +The data context script can access the content of the object as well as the request fields an the background ``RGW`` table. +All Lua language features can be used in all contexts. By default, all lua standard libraries are available in the script, however, in order to allow for other lua modules to be used in the script, we support adding packages to an allowlist: @@ -32,7 +40,7 @@ To upload a script: :: - # radosgw-admin script put --infile={lua-file} --context={preRequest|postRequest|background} [--tenant={tenant-name}] + # radosgw-admin script put --infile={lua-file} --context={preRequest|postRequest|background|getData|putdata} [--tenant={tenant-name}] * When uploading a script with the ``background`` context, a tenant name may not be specified. @@ -42,14 +50,14 @@ To print the content of the script to standard output: :: - # radosgw-admin script get --context={preRequest|postRequest|background} [--tenant={tenant-name}] + # radosgw-admin script get --context={preRequest|postRequest|background|getData} [--tenant={tenant-name}] To remove the script: :: - # radosgw-admin script rm --context={preRequest|postRequest|background} [--tenant={tenant-name}] + # radosgw-admin script rm --context={preRequest|postRequest|background|putData} [--tenant={tenant-name}] Package Management via CLI @@ -306,11 +314,29 @@ Operations Log ~~~~~~~~~~~~~~ The ``Request.Log()`` function prints the requests into the operations log. This function has no parameters. It returns 0 for success and an error code if it fails. +Tracing +~~~~~~~ +Tracing functions can be used only in `postRequest` context. + +- ``Request.Trace.SetAttribute()`` - sets the attribute for the request's trace. + Takes two arguments. The first is the `key`, which should be a string. The second is the value, which can either be a string or a number. + Using the attribute, you can locate specific traces. + +- ``Request.Trace.AddEvent()`` - adds an event to the first span of the request's trace + An event is defined by event name, event time, and zero or more event attributes. + Therefore, the function accepts one or two arguments. A string containing the event name should be the first argument, followed by the event attributes, which is optional for events without attributes. + An event's attributes must be a table of strings. + Background Context -------------------- The ``background`` context may be used for purposes that include analytics, monitoring, caching data for other context executions. - Background script execution default interval is 5 seconds. +Data Context +-------------------- +Both ``getData`` and ``putData`` contexts has a single field named ``Data`` which is read only, optional and iterable (byte by byte). +The ``Request`` fields and the background ``RGW`` table are also available in these contexts. + Global ``RGW`` Table -------------------- The ``RGW`` Lua table is accessible from all contexts and saves data written to it @@ -330,18 +356,6 @@ to atomically increment and decrement numeric values in it. For that the followi - if we try to increment or decrement by non-numeric values, the execution of the script would fail -Tracing -~~~~~~~ -Tracing functions can be used only in `postRequest` context. - -- ``Request.Trace.SetAttribute()`` - sets the attribute for the request's trace. - Takes two arguments. The first is the `key`, which should be a string. The second is the value, which can either be a string or a number. - Using the attribute, you can locate specific traces. - -- ``Request.Trace.AddEvent()`` - adds an event to the first span of the request's trace - An event is defined by event name, event time, and zero or more event attributes. - Therefore, the function accepts one or two arguments. A string containing the event name should be the first argument, followed by the event attributes, which is optional for events without attributes. - An event's attributes must be a table of strings. Lua Code Samples ---------------- @@ -511,3 +525,39 @@ in `postRequest` context, we can add attributes and events to the request's trac Request.Trace.AddEvent("second event", event_attrs) +- Calculate the entropy and size of uploaded objects and print to debug log + +in `putData` ccontext, add the following script + +.. code-block:: lua + + function object_entropy() + local byte_hist = {} + local byte_hist_size = 256 + for i = 1,byte_hist_size do + byte_hist[i] = 0 + end + local total = 0 + + for i, c in pairs(Data) do + local byte = c:byte() + 1 + byte_hist[byte] = byte_hist[byte] + 1 + total = total + 1 + end + + entropy = 0 + + for _, count in ipairs(byte_hist) do + if count ~= 0 then + local p = 1.0 * count / total + entropy = entropy - (p * math.log(p)/math.log(byte_hist_size)) + end + end + + return entropy + end + + local full_name = Request.Bucket.Name.."\\"..Request.Object.Name + RGWDebugLog("entropy of chunk of: " .. full_name .. " is: " .. tostring(object_entropy())) + RGWDebugLog("payload size of chunk of: " .. full_name .. " is: " .. #Data) + diff --git a/src/rgw/CMakeLists.txt b/src/rgw/CMakeLists.txt index a127b54a622395..ed483cd481f235 100644 --- a/src/rgw/CMakeLists.txt +++ b/src/rgw/CMakeLists.txt @@ -161,8 +161,11 @@ set(librgw_common_srcs rgw_datalog.cc rgw_datalog_notify.cc cls_fifo_legacy.cc + rgw_log.cc + rgw_lua_request.cc rgw_lua_utils.cc rgw_lua.cc + rgw_lua_data_filter.cc rgw_bucket_encryption.cc rgw_tracer.cc rgw_lua_background.cc) @@ -276,8 +279,6 @@ set(rgw_a_srcs rgw_frontend.cc rgw_http_client_curl.cc rgw_loadgen.cc - rgw_log.cc - rgw_lua_request.cc rgw_period_pusher.cc rgw_realm_reloader.cc rgw_realm_watcher.cc diff --git a/src/rgw/rgw_admin.cc b/src/rgw/rgw_admin.cc index 19cf667ecf4af7..e32f3d8a15158e 100644 --- a/src/rgw/rgw_admin.cc +++ b/src/rgw/rgw_admin.cc @@ -477,7 +477,7 @@ void usage() cout << " --subscription pubsub subscription name\n"; cout << " --event-id event id in a pubsub subscription\n"; cout << "\nScript options:\n"; - cout << " --context context in which the script runs. one of: preRequest, postRequest, background\n"; + cout << " --context context in which the script runs. one of: preRequest, postRequest, background, getData, putData\n"; cout << " --package name of the lua package that should be added/removed to/from the allowlist\n"; cout << " --allow-compilation package is allowed to compile C code as part of its installation\n"; cout << "\nradoslist options:\n"; @@ -10377,7 +10377,7 @@ int main(int argc, const char **argv) } const rgw::lua::context script_ctx = rgw::lua::to_context(*str_script_ctx); if (script_ctx == rgw::lua::context::none) { - cerr << "ERROR: invalid script context: " << *str_script_ctx << ". must be one of: preRequest, postRequest, background" << std::endl; + cerr << "ERROR: invalid script context: " << *str_script_ctx << ". must be one of: preRequest, postRequest, background, getData, putData" << std::endl; return EINVAL; } if (script_ctx == rgw::lua::context::background && !tenant.empty()) { @@ -10398,7 +10398,7 @@ int main(int argc, const char **argv) } const rgw::lua::context script_ctx = rgw::lua::to_context(*str_script_ctx); if (script_ctx == rgw::lua::context::none) { - cerr << "ERROR: invalid script context: " << *str_script_ctx << ". must be one of: preRequest, postRequest, background" << std::endl; + cerr << "ERROR: invalid script context: " << *str_script_ctx << ". must be one of: preRequest, postRequest, background, getData, putData" << std::endl; return EINVAL; } std::string script; @@ -10421,7 +10421,7 @@ int main(int argc, const char **argv) } const rgw::lua::context script_ctx = rgw::lua::to_context(*str_script_ctx); if (script_ctx == rgw::lua::context::none) { - cerr << "ERROR: invalid script context: " << *str_script_ctx << ". must be one of: preRequest, postRequest, background" << std::endl; + cerr << "ERROR: invalid script context: " << *str_script_ctx << ". must be one of: preRequest, postRequest, background, getData, putData" << std::endl; return EINVAL; } const auto rc = rgw::lua::delete_script(dpp(), store, tenant, null_yield, script_ctx); diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index c9a831df53eb09..bfd6168ff820aa 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -51,6 +51,10 @@ namespace rgw::sal { using Attrs = std::map; } +namespace rgw::lua { + class Background; +} + using ceph::crypto::MD5; #define RGW_ATTR_PREFIX "user.rgw." @@ -1697,6 +1701,8 @@ struct req_state : DoutPrefixProvider { //Principal tags that come in as part of AssumeRoleWithWebIdentity std::vector> principal_tags; + rgw::lua::Background* lua_background = nullptr; + req_state(CephContext* _cct, RGWEnv* e, uint64_t id); ~req_state(); diff --git a/src/rgw/rgw_lua.cc b/src/rgw/rgw_lua.cc index 323cc5ed0f62de..48ad5283734446 100644 --- a/src/rgw/rgw_lua.cc +++ b/src/rgw/rgw_lua.cc @@ -26,6 +26,12 @@ context to_context(const std::string& s) if (strcasecmp(s.c_str(), "background") == 0) { return context::background; } + if (strcasecmp(s.c_str(), "getData") == 0) { + return context::getData; + } + if (strcasecmp(s.c_str(), "putData") == 0) { + return context::putData; + } return context::none; } @@ -38,6 +44,10 @@ std::string to_string(context ctx) return "postrequest"; case context::background: return "background"; + case context::getData: + return "getdata"; + case context::putData: + return "putdata"; case context::none: break; } diff --git a/src/rgw/rgw_lua.h b/src/rgw/rgw_lua.h index 1eb14c605ffd44..ecc583bd41ba7a 100644 --- a/src/rgw/rgw_lua.h +++ b/src/rgw/rgw_lua.h @@ -4,6 +4,7 @@ #include "common/async/yield_context.h" #include "rgw_sal_fwd.h" +class DoutPrefixProvider; class lua_State; class rgw_user; namespace rgw::sal { @@ -16,6 +17,8 @@ enum class context { preRequest, postRequest, background, + getData, + putData, none }; diff --git a/src/rgw/rgw_lua_background.cc b/src/rgw/rgw_lua_background.cc index ea85193edf7c4a..bfd6d6d59ef743 100644 --- a/src/rgw/rgw_lua_background.cc +++ b/src/rgw/rgw_lua_background.cc @@ -176,5 +176,5 @@ void Background::create_background_metatable(lua_State* L) { create_metatable(L, true, &rgw_map, &table_mutex); } -} //namespace lua +} //namespace rgw::lua diff --git a/src/rgw/rgw_lua_background.h b/src/rgw/rgw_lua_background.h index 5d7eafa39befdb..5cac2c52d8722b 100644 --- a/src/rgw/rgw_lua_background.h +++ b/src/rgw/rgw_lua_background.h @@ -223,5 +223,5 @@ class Background : public RGWRealmReloader::Pauser { void resume(rgw::sal::Store* _store) override; }; -} //namepsace lua +} //namepsace rgw::lua diff --git a/src/rgw/rgw_lua_data_filter.cc b/src/rgw/rgw_lua_data_filter.cc new file mode 100644 index 00000000000000..a9286fd1b21e55 --- /dev/null +++ b/src/rgw/rgw_lua_data_filter.cc @@ -0,0 +1,135 @@ +#include "rgw_lua_data_filter.h" +#include "rgw_lua_utils.h" +#include "rgw_lua_request.h" +#include "rgw_lua_background.h" +#include + +namespace rgw::lua { + +void push_bufferlist_byte(lua_State* L, bufferlist::iterator& it) { + char byte[1]; + it.copy(1, byte); + lua_pushlstring(L, byte, 1); +} + +struct BufferlistMetaTable : public EmptyMetaTable { + + static std::string TableName() {return "Data";} + static std::string Name() {return TableName() + "Meta";} + + static int IndexClosure(lua_State* L) { + auto bl = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); + const auto index = luaL_checkinteger(L, 2); + if (index <= 0 || index > bl->length()) { + // lua arrays start from 1 + lua_pushnil(L); + return ONE_RETURNVAL; + } + auto it = bl->begin(index-1); + if (it != bl->end()) { + push_bufferlist_byte(L, it); + } else { + lua_pushnil(L); + } + + return ONE_RETURNVAL; + } + + static int PairsClosure(lua_State* L) { + auto bl = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); + ceph_assert(bl); + lua_pushlightuserdata(L, bl); + lua_pushcclosure(L, stateless_iter, ONE_UPVAL); // push the stateless iterator function + lua_pushnil(L); // indicate this is the first call + // return stateless_iter, nil + + return TWO_RETURNVALS; + } + + static int stateless_iter(lua_State* L) { + // based on: http://lua-users.org/wiki/GeneralizedPairsAndIpairs + auto bl = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); + lua_Integer index; + if (lua_isnil(L, -1)) { + index = 1; + } else { + index = luaL_checkinteger(L, -1) + 1; + } + + // lua arrays start from 1 + auto it = bl->begin(index-1); + + if (index > bl->length()) { + // index of the last element was provided + lua_pushnil(L); + lua_pushnil(L); + // return nil, nil + } else { + lua_pushinteger(L, index); + push_bufferlist_byte(L, it); + // return key, value + } + + return TWO_RETURNVALS; + } + + static int LenClosure(lua_State* L) { + const auto bl = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); + + lua_pushinteger(L, bl->length()); + + return ONE_RETURNVAL; + } +}; + +int RGWObjFilter::execute(bufferlist& bl, const char* op_name) const { + auto L = luaL_newstate(); + lua_state_guard lguard(L); + + open_standard_libs(L); + + create_debug_action(L, s->cct); + + create_metatable(L, true, &bl); + lua_getglobal(L, BufferlistMetaTable::TableName().c_str()); + ceph_assert(lua_istable(L, -1)); + + request::create_top_metatable(L, s, op_name); + + if (s->lua_background) { + s->lua_background->create_background_metatable(L); + lua_getglobal(L, rgw::lua::RGWTable::TableName().c_str()); + ceph_assert(lua_istable(L, -1)); + } + + try { + // execute the lua script + if (luaL_dostring(L, script.c_str()) != LUA_OK) { + const std::string err(lua_tostring(L, -1)); + ldpp_dout(s, 1) << "Lua ERROR: " << err << dendl; + return -EINVAL; + } + } catch (const std::runtime_error& e) { + ldpp_dout(s, 1) << "Lua ERROR: " << e.what() << dendl; + return -EINVAL; + } + + return 0; +} + +int RGWGetObjFilter::handle_data(bufferlist& bl, + off_t bl_ofs, + off_t bl_len) { + filter.execute(bl, "get_obj"); + // return value is ignored since we don't want to fail execution if lua script fails + return RGWGetObj_Filter::handle_data(bl, bl_ofs, bl_len); +} + +int RGWPutObjFilter::process(bufferlist&& data, uint64_t logical_offset) { + filter.execute(data, "put_obj"); + // return value is ignored since we don't want to fail execution if lua script fails + return rgw::putobj::Pipe::process(std::move(data), logical_offset); +} + +} // namespace rgw::lua + diff --git a/src/rgw/rgw_lua_data_filter.h b/src/rgw/rgw_lua_data_filter.h new file mode 100644 index 00000000000000..abc28243034982 --- /dev/null +++ b/src/rgw/rgw_lua_data_filter.h @@ -0,0 +1,52 @@ +#pragma once + +#include "rgw_op.h" + +class DoutPrefixProvider; + +namespace rgw::lua { + +class RGWObjFilter { + req_state* const s; + const std::string script; + +public: + RGWObjFilter(req_state* s, + const std::string& script) : + s(s), script(script) {} + + int execute(bufferlist& bl, const char* op_name) const; +}; + +class RGWGetObjFilter : public RGWGetObj_Filter { + const RGWObjFilter filter; + +public: + RGWGetObjFilter(req_state* s, + const std::string& script, + RGWGetObj_Filter* next) : RGWGetObj_Filter(next), filter(s, script) + {} + + ~RGWGetObjFilter() override = default; + + int handle_data(bufferlist& bl, + off_t bl_ofs, + off_t bl_len) override; + +}; + +class RGWPutObjFilter : public rgw::putobj::Pipe { + const RGWObjFilter filter; + +public: + RGWPutObjFilter(req_state* s, + const std::string& script, + rgw::sal::DataProcessor* next) : rgw::putobj::Pipe(next), filter(s, script) + {} + + ~RGWPutObjFilter() override = default; + + int process(bufferlist&& data, uint64_t logical_offset) override; +}; +} // namespace rgw::lua + diff --git a/src/rgw/rgw_lua_request.cc b/src/rgw/rgw_lua_request.cc index 3e9e0521b27ead..a6f56b5f96d524 100644 --- a/src/rgw/rgw_lua_request.cc +++ b/src/rgw/rgw_lua_request.cc @@ -839,14 +839,19 @@ struct RequestMetaTable : public EmptyMetaTable { } }; +void create_top_metatable(lua_State* L, req_state* s, const char* op_name) { + create_metatable(L, true, s, const_cast(op_name)); + lua_getglobal(L, RequestMetaTable::TableName().c_str()); + ceph_assert(lua_istable(L, -1)); +} + int execute( rgw::sal::Store* store, RGWREST* rest, OpsLogSink* olog, req_state* s, const char* op_name, - const std::string& script, - rgw::lua::Background* background) + const std::string& script) { auto L = luaL_newstate(); @@ -873,8 +878,8 @@ int execute( lua_pushcclosure(L, RequestLog, FOUR_UPVALS); lua_rawset(L, -3); - if (background) { - background->create_background_metatable(L); + if (s->lua_background) { + s->lua_background->create_background_metatable(L); lua_getglobal(L, rgw::lua::RGWTable::TableName().c_str()); ceph_assert(lua_istable(L, -1)); } @@ -898,4 +903,5 @@ int execute( return rc; } -} +} // namespace rgw::lua::request + diff --git a/src/rgw/rgw_lua_request.h b/src/rgw/rgw_lua_request.h index f7569a3e764d7c..908d160eb3e9b1 100644 --- a/src/rgw/rgw_lua_request.h +++ b/src/rgw/rgw_lua_request.h @@ -4,15 +4,16 @@ #include "include/common_fwd.h" #include "rgw_sal_fwd.h" -struct req_state; +struct lua_State; +class req_state; class RGWREST; class OpsLogSink; -namespace rgw::lua { - class Background; -} namespace rgw::lua::request { +// create the request metatable +void create_top_metatable(lua_State* L, req_state* s, const char* op_name); + // execute a lua script in the Request context int execute( rgw::sal::Store* store, @@ -20,8 +21,7 @@ int execute( OpsLogSink* olog, req_state *s, const char* op_name, - const std::string& script, - rgw::lua::Background* background = nullptr); + const std::string& script); -} +} // namespace rgw::lua::request diff --git a/src/rgw/rgw_lua_utils.cc b/src/rgw/rgw_lua_utils.cc index 9b116269d9ead6..3ffe23662116eb 100644 --- a/src/rgw/rgw_lua_utils.cc +++ b/src/rgw/rgw_lua_utils.cc @@ -73,4 +73,5 @@ void open_standard_libs(lua_State* L) { lua_settable(L, -3); } -} +} // namespace rgw::lua + diff --git a/src/rgw/rgw_lua_utils.h b/src/rgw/rgw_lua_utils.h index 0071b71c2b47f0..cc77dae7a896af 100644 --- a/src/rgw/rgw_lua_utils.h +++ b/src/rgw/rgw_lua_utils.h @@ -310,5 +310,6 @@ struct StringMapMetaTable : public EmptyMetaTable { return ONE_RETURNVAL; } }; -} + +} // namespace rgw::lua diff --git a/src/rgw/rgw_op.cc b/src/rgw/rgw_op.cc index 7afcbc4444836b..5de3766454eb47 100644 --- a/src/rgw/rgw_op.cc +++ b/src/rgw/rgw_op.cc @@ -52,6 +52,8 @@ #include "rgw_notify_event_type.h" #include "rgw_sal.h" #include "rgw_sal_rados.h" +#include "rgw_lua_data_filter.h" +#include "rgw_lua.h" #include "services/svc_zone.h" #include "services/svc_quota.h" @@ -2087,6 +2089,20 @@ int RGWGetObj::get_data_cb(bufferlist& bl, off_t bl_ofs, off_t bl_len) return send_response_data(bl, bl_ofs, bl_len); } +int RGWGetObj::get_lua_filter(std::unique_ptr* filter, RGWGetObj_Filter* cb) { + std::string script; + const auto rc = rgw::lua::read_script(s, store, s->bucket_tenant, s->yield, rgw::lua::context::getData, script); + if (rc == -ENOENT) { + // no script, nothing to do + return 0; + } else if (rc < 0) { + ldpp_dout(this, 5) << "WARNING: failed to read data script. error: " << rc << dendl; + return rc; + } + filter->reset(new rgw::lua::RGWGetObjFilter(s, script, cb)); + return 0; +} + bool RGWGetObj::prefetch_data() { /* HEAD request, stop prefetch*/ @@ -2138,6 +2154,7 @@ void RGWGetObj::execute(optional_yield y) RGWGetObj_Filter* filter = (RGWGetObj_Filter *)&cb; boost::optional decompress; std::unique_ptr decrypt; + std::unique_ptr run_lua; map::iterator attr_iter; perfcounter->inc(l_rgw_get); @@ -2287,6 +2304,15 @@ void RGWGetObj::execute(optional_yield y) goto done_err; } + // run lua script on decompressed and decrypted data + op_ret = get_lua_filter(&run_lua, filter); + if (run_lua != nullptr) { + filter = run_lua.get(); + } + if (op_ret < 0) { + goto done_err; + } + if (!get_data || ofs > end) { send_response_data(bl, 0, 0); return; @@ -3845,6 +3871,20 @@ static CompressorRef get_compressor_plugin(const req_state *s, return Compressor::create(s->cct, alg); } +int RGWPutObj::get_lua_filter(std::unique_ptr* filter, rgw::sal::DataProcessor* cb) { + std::string script; + const auto rc = rgw::lua::read_script(s, store, s->bucket_tenant, s->yield, rgw::lua::context::putData, script); + if (rc == -ENOENT) { + // no script, nothing to do + return 0; + } else if (rc < 0) { + ldpp_dout(this, 5) << "WARNING: failed to read data script. error: " << rc << dendl; + return rc; + } + filter->reset(new rgw::lua::RGWPutObjFilter(s, script, cb)); + return 0; +} + void RGWPutObj::execute(optional_yield y) { char supplied_md5_bin[CEPH_CRYPTO_MD5_DIGESTSIZE + 1]; @@ -4042,8 +4082,16 @@ void RGWPutObj::execute(optional_yield y) boost::optional compressor; std::unique_ptr encrypt; + std::unique_ptr run_lua; if (!append) { // compression and encryption only apply to full object uploads + op_ret = get_lua_filter(&run_lua, filter); + if (op_ret < 0) { + return; + } + if (run_lua) { + filter = &*run_lua; + } op_ret = get_encrypt_filter(&encrypt, filter); if (op_ret < 0) { return; diff --git a/src/rgw/rgw_op.h b/src/rgw/rgw_op.h index 9176d2389350a0..f99ea23edc40ca 100644 --- a/src/rgw/rgw_op.h +++ b/src/rgw/rgw_op.h @@ -437,6 +437,11 @@ class RGWGetObj : public RGWOp { *filter = nullptr; return 0; } + + // get lua script to run as a "get object" filter + int get_lua_filter(std::unique_ptr* filter, + RGWGetObj_Filter* cb); + dmc::client_id dmclock_client() override { return dmc::client_id::data; } }; @@ -1276,6 +1281,10 @@ class RGWPutObj : public RGWOp { return 0; } + // get lua script to run as a "put object" filter + int get_lua_filter(std::unique_ptr* filter, + rgw::sal::DataProcessor* cb); + int get_data_cb(bufferlist& bl, off_t bl_ofs, off_t bl_len); int get_data(const off_t fst, const off_t lst, bufferlist& bl); diff --git a/src/rgw/rgw_process.cc b/src/rgw/rgw_process.cc index 1d4c8014c1fdd9..dc6a26b832fe29 100644 --- a/src/rgw/rgw_process.cc +++ b/src/rgw/rgw_process.cc @@ -327,6 +327,7 @@ int process_request(rgw::sal::Store* const store, abort_early(s, NULL, -ERR_METHOD_NOT_ALLOWED, handler, yield); goto done; } + s->lua_background = lua_background; { s->trace_enabled = tracing::rgw::tracer.is_enabled(); std::string script; @@ -336,7 +337,7 @@ int process_request(rgw::sal::Store* const store, } else if (rc < 0) { ldpp_dout(op, 5) << "WARNING: failed to read pre request script. error: " << rc << dendl; } else { - rc = rgw::lua::request::execute(store, rest, olog, s, op->name(), script, lua_background); + rc = rgw::lua::request::execute(store, rest, olog, s, op->name(), script); if (rc < 0) { ldpp_dout(op, 5) << "WARNING: failed to execute pre request script. error: " << rc << dendl; } @@ -419,7 +420,7 @@ int process_request(rgw::sal::Store* const store, } else if (rc < 0) { ldpp_dout(op, 5) << "WARNING: failed to read post request script. error: " << rc << dendl; } else { - rc = rgw::lua::request::execute(store, rest, olog, s, op->name(), script, lua_background); + rc = rgw::lua::request::execute(store, rest, olog, s, op->name(), script); if (rc < 0) { ldpp_dout(op, 5) << "WARNING: failed to execute post request script. error: " << rc << dendl; } diff --git a/src/test/rgw/test_rgw_lua.cc b/src/test/rgw/test_rgw_lua.cc index 110e6addd84cd8..0dbf3bd51a60ff 100644 --- a/src/test/rgw/test_rgw_lua.cc +++ b/src/test/rgw/test_rgw_lua.cc @@ -6,6 +6,7 @@ #include "rgw/rgw_sal_rados.h" #include "rgw/rgw_lua_request.h" #include "rgw/rgw_lua_background.h" +#include "rgw/rgw_lua_data_filter.h" using namespace std; using namespace rgw; @@ -764,10 +765,11 @@ TEST(TestRGWLuaBackground, RequestScript) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; // to make sure test is consistent we have to puase the background lua_background.pause(); - const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); EXPECT_EQ(get_table_value(lua_background, "hello"), "from request"); // now we resume and let the background set the value @@ -908,8 +910,9 @@ TEST(TestRGWLuaBackground, TableValues) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; - const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); EXPECT_EQ(get_table_value(lua_background, "key1"), "string value"); EXPECT_EQ(get_table_value(lua_background, "key2"), 42); @@ -927,8 +930,9 @@ TEST(TestRGWLuaBackground, TablePersist) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; - auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); EXPECT_EQ(get_table_value(lua_background, "key1"), "string value"); EXPECT_EQ(get_table_value(lua_background, "key2"), 42); @@ -938,7 +942,7 @@ TEST(TestRGWLuaBackground, TablePersist) RGW["key4"] = RGW["key2"] )"; - rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); EXPECT_EQ(get_table_value(lua_background, "key1"), "string value"); EXPECT_EQ(get_table_value(lua_background, "key2"), 42); @@ -959,12 +963,14 @@ TEST(TestRGWLuaBackground, TableValuesFromRequest) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; + s.tagset.add_tag("key1", "val1"); s.tagset.add_tag("key2", "val1"); s.err.ret = -99; s.err.message = "hi"; - const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); EXPECT_EQ(get_table_value(lua_background, "key1"), -99); EXPECT_EQ(get_table_value(lua_background, "key2"), "hi"); @@ -986,10 +992,11 @@ TEST(TestRGWLuaBackground, TableInvalidValue) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; s.tagset.add_tag("key1", "val1"); s.tagset.add_tag("key2", "val2"); - const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_NE(rc, 0); EXPECT_EQ(get_table_value(lua_background, "key1"), "val1"); EXPECT_EQ(get_table_value(lua_background, "key2"), 42); @@ -1010,8 +1017,9 @@ TEST(TestRGWLuaBackground, TableErase) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; - auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); EXPECT_EQ(get_table_value(lua_background, "key1"), "string value"); EXPECT_EQ(get_table_value(lua_background, "key2"), 42); @@ -1026,7 +1034,7 @@ TEST(TestRGWLuaBackground, TableErase) RGW["size"] = #RGW )"; - rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); EXPECT_EQ(get_table_value(lua_background, "key1"), ""); EXPECT_EQ(get_table_value(lua_background, "key2"), 42); @@ -1050,8 +1058,9 @@ TEST(TestRGWLuaBackground, TableIterate) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; - const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); EXPECT_EQ(get_table_value(lua_background, "key1"), "string value"); EXPECT_EQ(get_table_value(lua_background, "key2"), 42); @@ -1074,8 +1083,9 @@ TEST(TestRGWLuaBackground, TableIncrement) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; - const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); } @@ -1095,8 +1105,9 @@ TEST(TestRGWLuaBackground, TableIncrementBy) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; - const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); } @@ -1114,8 +1125,9 @@ TEST(TestRGWLuaBackground, TableDecrement) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; - const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); } @@ -1135,8 +1147,9 @@ TEST(TestRGWLuaBackground, TableDecrementBy) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; - const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_EQ(rc, 0); } @@ -1151,8 +1164,9 @@ TEST(TestRGWLuaBackground, TableIncrementValueError) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; - auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_NE(rc, 0); request_script = R"( @@ -1161,7 +1175,7 @@ TEST(TestRGWLuaBackground, TableIncrementValueError) RGW.increment("key1") )"; - rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_NE(rc, 0); request_script = R"( @@ -1170,7 +1184,7 @@ TEST(TestRGWLuaBackground, TableIncrementValueError) RGW.increment("key1", "kaboom") )"; - rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_NE(rc, 0); } @@ -1185,8 +1199,9 @@ TEST(TestRGWLuaBackground, TableIncrementError) )"; DEFINE_REQ_STATE; + s.lua_background = &lua_background; - auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_NE(rc, 0); request_script = R"( @@ -1194,7 +1209,7 @@ TEST(TestRGWLuaBackground, TableIncrementError) RGW.increment = 11 )"; - rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background); + rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script); ASSERT_NE(rc, 0); } @@ -1245,3 +1260,50 @@ TEST(TestRGWLua, TracingAddEvent) const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "put_obj", script); ASSERT_EQ(rc, 0); } + +TEST(TestRGWLua, Data) +{ + const std::string script = R"( + local expected = "The quick brown fox jumps over the lazy dog" + local actual = "" + RGW["key1"] = 0 + + for i, c in pairs(Data) do + actual = actual .. c + RGW.increment("key1") + end + assert(expected == actual) + assert(#Data == #expected); + assert(RGW["key1"] == #Data) + assert(Request.RGWId == "foo") + )"; + + TestBackground lua_background(""); + DEFINE_REQ_STATE; + s.host_id = "foo"; + s.lua_background = &lua_background; + lua::RGWObjFilter filter(&s, script); + bufferlist bl; + bl.append("The quick brown fox jumps over the lazy dog"); + const auto rc = filter.execute(bl, "put_obj"); + ASSERT_EQ(rc, 0); +} + +TEST(TestRGWLua, WriteDataFail) +{ + const std::string script = R"( + Data[1] = "h" + Data[2] = "e" + Data[3] = "l" + Data[4] = "l" + Data[5] = "o" + )"; + + DEFINE_REQ_STATE; + lua::RGWObjFilter filter(&s, script); + bufferlist bl; + bl.append("The quick brown fox jumps over the lazy dog"); + const auto rc = filter.execute(bl, "put_obj"); + ASSERT_NE(rc, 0); +} +