]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
Revert "rgw/lua: allow read access to object data" 47612/head
authorCasey Bodley <cbodley@redhat.com>
Mon, 15 Aug 2022 18:20:20 +0000 (14:20 -0400)
committerCasey Bodley <cbodley@redhat.com>
Mon, 15 Aug 2022 18:51:55 +0000 (14:51 -0400)
This reverts commit f4501f69701d74caa5c0da0b7247eed80c1f2a3f.

Signed-off-by: Casey Bodley <cbodley@redhat.com>
19 files changed:
doc/radosgw/lua-scripting.rst
src/rgw/CMakeLists.txt
src/rgw/rgw_admin.cc
src/rgw/rgw_common.h
src/rgw/rgw_lua.cc
src/rgw/rgw_lua.h
src/rgw/rgw_lua_background.cc
src/rgw/rgw_lua_background.h
src/rgw/rgw_lua_data_filter.cc [deleted file]
src/rgw/rgw_lua_data_filter.h [deleted file]
src/rgw/rgw_lua_request.cc
src/rgw/rgw_lua_request.h
src/rgw/rgw_lua_utils.cc
src/rgw/rgw_lua_utils.h
src/rgw/rgw_op.cc
src/rgw/rgw_op.h
src/rgw/rgw_process.cc
src/test/cli/radosgw-admin/help.t
src/test/rgw/test_rgw_lua.cc

index 9f42f20c18fcac905047aa46d0083e6033a9534d..750a91397ac25d4b5c6b70c805f7f217964de0c8 100644 (file)
@@ -6,26 +6,18 @@ Lua Scripting
 
 .. contents::
 
-This feature allows users to assign execution context to Lua scripts. The supported contexts are:
+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.
 
- - ``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 certain fields, as well as the `Global RGW Table`_.
-The data context script can access the content of the object as well as the request fields and the `Global 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:
+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:
 
   - All packages in the allowlist are being re-installed using the luarocks package manager on radosgw restart. Therefore a restart is needed for adding or removing of packages to take effect 
-  - To add a package that contains C source code that needs to be compiled, use the ``--allow-compilation`` flag. In this case a C compiler needs to be available on the host
-  - Lua packages are installed in, and used from, a directory local to the radosgw. Meaning that Lua packages in the allowlist are separated from any Lua packages available on the host.
-    By default, this directory would be ``/tmp/luarocks/<entity name>``. Its prefix part (``/tmp/luarocks/``) could be set to a different location via the ``rgw_luarocks_location`` configuration parameter. 
-    Note that this parameter should not be set to one of the default locations where luarocks install packages (e.g. ``$HOME/.luarocks``, ``/usr/lib64/lua``, ``/usr/share/lua``).
+  - To add a package that contains C source code that needs to be compiled, use the `--allow-compilation` flag. In this case a C compiler needs to be available on the host
+  - Lua packages are installed in, and used from, a directory local to the radosgw. Meaning that lua packages in the allowlist are separated from any lua packages available on the host.
+    By default, this directory would be `/tmp/luarocks/<entity name>`. Its prefix part (`/tmp/luarocks/`) could be set to a different location via the `rgw_luarocks_location` configuration parameter. 
+    Note that this parameter should not be set to one of the default locations where luarocks install packages (e.g. `$HOME/.luarocks`, `/usr/lib64/lua`, `/usr/share/lua`)
        
 
 .. toctree::
@@ -40,7 +32,7 @@ To upload a script:
 
 ::
    
-   # radosgw-admin script put --infile={lua-file} --context={prerequest|postrequest|background|getdata|putdata} [--tenant={tenant-name}]
+   # radosgw-admin script put --infile={lua-file} --context={preRequest|postRequest|background} [--tenant={tenant-name}]
 
 
 * When uploading a script with the ``background`` context, a tenant name may not be specified.
@@ -50,14 +42,14 @@ To print the content of the script to standard output:
 
 ::
    
-   # radosgw-admin script get --context={prerequest|postrequest|background|getdata|putdata} [--tenant={tenant-name}]
+   # radosgw-admin script get --context={preRequest|postRequest|background} [--tenant={tenant-name}]
 
 
 To remove the script:
 
 ::
    
-   # radosgw-admin script rm --context={prerequest|postrequest|background|getdata|putdata} [--tenant={tenant-name}]
+   # radosgw-admin script rm --context={preRequest|postRequest|background} [--tenant={tenant-name}]
 
 
 Package Management via CLI
@@ -157,7 +149,7 @@ Request Fields
 +----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+
 | ``Request.Bucket.Tenant``                          | string   | tenant of the bucket                                         | no       | no        | yes      |
 +----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+
-| ``Request.Bucket.Name``                            | string   | bucket name (writeable only in ``prerequest`` context)       | no       | yes       | no       |
+| ``Request.Bucket.Name``                            | string   | bucket name (writeable only in `preRequest` context)         | no       | yes       | no       |
 +----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+
 | ``Request.Bucket.Marker``                          | string   | bucket marker (initial id)                                   | no       | no        | yes      |
 +----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+
@@ -314,32 +306,12 @@ 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 the ``postrequest`` context.
-
-- ``Request.Trace.SetAttribute(<key>, <value>)`` - sets the attribute for the request's trace.
-  The function takes two arguments: the first is the ``key``, which should be a string, and the second is the ``value``, which can either be a string or a number (integer or double).
-  You may then locate specific traces by using this attribute.
-
-- ``Request.Trace.AddEvent(<name>, <attributes>)`` - 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.
-  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 have the following fields:
-- ``Data`` which is read-only and iterable (byte by byte). In case that an object is uploaded or retrieved in multiple chunks, the ``Data`` field will hold data of one chunk at a time.
-- ``Offset`` which is holding the offset of the chunk within the entire object.
-- The ``Request`` fields and the background ``RGW`` table are also available in these contexts.
-
-Global RGW Table
+Global ``RGW`` Table
 --------------------
 The ``RGW`` Lua table is accessible from all contexts and saves data written to it
 during execution so that it may be read and used later during other executions, from the same context of a different one.
@@ -358,6 +330,19 @@ 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
 ----------------
 - Print information on source and destination objects in case of copy:
@@ -434,7 +419,7 @@ Lua Code Samples
 
 - Add metadata to objects that was not originally sent by the client:
 
-In the ``prerequest`` context we should add:
+In the `preRequest` context we should add:
 
 .. code-block:: lua
 
@@ -442,7 +427,7 @@ In the ``prerequest`` context we should add:
     Request.HTTP.Metadata["x-amz-meta-mydata"] = "my value"
   end
 
-In the ``postrequest`` context we look at the metadata:
+In the `postRequest` context we look at the metadata:
 
 .. code-block:: lua
 
@@ -461,7 +446,7 @@ First we should add the following packages to the allowlist:
   # radosgw-admin script-package add --package=luasocket --allow-compilation
 
 
-Then, do a restart for the radosgw and upload the following script to the ``postrequest`` context:
+Then, do a restart for the radosgw and upload the following script to the `postRequest` context:
 
 .. code-block:: lua
 
@@ -498,7 +483,7 @@ Tracing is disabled by default, so we should enable tracing for this specific bu
 
 
 If `tracing is enabled <https://docs.ceph.com/en/latest/jaegertracing/#how-to-enable-tracing-in-ceph/>`_ on the RGW, the value of Request.Trace.Enable is true, so we should disable tracing for all other requests that do not match the bucket name.
-In the ``prerequest`` context:
+In the `preRequest` context:
 
 .. code-block:: lua
 
@@ -506,12 +491,12 @@ In the ``prerequest`` context:
       Request.Trace.Enable = false
   end
 
-Note that changing ``Request.Trace.Enable`` does not change the tracer's state, but disables or enables the tracing for the request only.
+Note that changing `Request.Trace.Enable` does not change the tracer's state, but disables or enables the tracing for the request only.
 
 
 - Add Information for requests traces
 
-in ``postrequest`` context, we can add attributes and events to the request's trace.
+in `postRequest` context, we can add attributes and events to the request's trace.
 
 .. code-block:: lua
 
@@ -526,40 +511,3 @@ in ``postrequest`` context, we can add attributes and events to the request's tr
 
   Request.Trace.AddEvent("second event", event_attrs)
 
-- The entropy value of an object could be used to detect whether the object is encrypted. 
-  The following script calculates the entropy and size of uploaded objects and print to debug log
-
-in the ``putdata`` context, 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 .. " at offset:" .. tostring(Offset)  ..  " is: " .. tostring(object_entropy()))
-       RGWDebugLog("payload size of chunk of: " .. full_name .. " is: " .. #Data)
-
index 133e2cf34d4d179b04b8ce2d6c3ec7189b60036f..3ac721f0fd692d0b4d50bb8fe11934ca21698b6f 100644 (file)
@@ -162,11 +162,8 @@ 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)
@@ -277,6 +274,8 @@ 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
index e4add62293728a1d478a812459c6463e89f5a8f1..637dab47e42efa9ce2ef5c1869bd3d4dbcfd2324 100644 (file)
@@ -121,9 +121,6 @@ static inline int posix_errortrans(int r)
   return r;
 }
 
-
-static const std::string LUA_CONTEXT_LIST("prerequest, postrequest, background, getdata, putdata");
-
 void usage()
 {
   cout << "usage: radosgw-admin <cmd> [options...]" << std::endl;
@@ -482,7 +479,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: "+LUA_CONTEXT_LIST+"\n";
+  cout << "   --context                 context in which the script runs. one of: preRequest, postRequest, background\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";
@@ -10398,7 +10395,7 @@ next:
     }
     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: " << LUA_CONTEXT_LIST << std::endl;
+      cerr << "ERROR: invalid script context: " << *str_script_ctx << ". must be one of: preRequest, postRequest, background" << std::endl;
       return EINVAL;
     }
     if (script_ctx == rgw::lua::context::background && !tenant.empty()) {
@@ -10420,7 +10417,7 @@ next:
     }
     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: " << LUA_CONTEXT_LIST << std::endl;
+      cerr << "ERROR: invalid script context: " << *str_script_ctx << ". must be one of: preRequest, postRequest, background" << std::endl;
       return EINVAL;
     }
     auto lua_manager = store->get_lua_manager();
@@ -10444,7 +10441,7 @@ next:
     }
     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: " << LUA_CONTEXT_LIST << std::endl;
+      cerr << "ERROR: invalid script context: " << *str_script_ctx << ". must be one of: preRequest, postRequest, background" << std::endl;
       return EINVAL;
     }
     auto lua_manager = store->get_lua_manager();
index c146a531a44f1077dbae4a5b7bd31674d1f3d85e..4bc18b2413630dbb9aa9d5d1ba446acf63eb6c77 100644 (file)
@@ -51,10 +51,6 @@ namespace rgw::sal {
   using Attrs = std::map<std::string, ceph::buffer::list>;
 }
 
-namespace rgw::lua {
-  class Background;
-}
-
 using ceph::crypto::MD5;
 
 #define RGW_ATTR_PREFIX  "user.rgw."
@@ -1700,8 +1696,6 @@ struct req_state : DoutPrefixProvider {
   //Principal tags that come in as part of AssumeRoleWithWebIdentity
   std::vector<std::pair<std::string, std::string>> principal_tags;
 
-  rgw::lua::Background* lua_background = nullptr;
-
   req_state(CephContext* _cct, RGWEnv* e, uint64_t id);
   ~req_state();
 
index 9eb67592365038431a3aee7fa6fecfdee7c05b28..2d4833e2ebf4ddd2e0539a282dded90a3140b808 100644 (file)
@@ -25,12 +25,6 @@ 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;
 }
 
@@ -43,10 +37,6 @@ 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;
   }
index 405492809f29c7e311a53c84a38b72c41c86f665..562a80c6c3d3330975635b9d645a0592141389a4 100644 (file)
@@ -7,7 +7,6 @@
 #include "common/dout.h"
 #include "rgw_sal_fwd.h"
 
-class DoutPrefixProvider;
 class lua_State;
 class rgw_user;
 class DoutPrefixProvider;
@@ -22,8 +21,6 @@ enum class context {
   preRequest,
   postRequest,
   background,
-  getData,
-  putData,
   none
 };
 
index 88f4391e2d07a35849d6cc0b45c15731ac2d786e..a8d31c8ab3615c128e09b9906af54bd009e5ac3a 100644 (file)
@@ -177,5 +177,5 @@ void Background::create_background_metatable(lua_State* L) {
   create_metatable<rgw::lua::RGWTable>(L, true, &rgw_map, &table_mutex);
 }
 
-} //namespace rgw::lua
+} //namespace lua
 
index 671495b3c553fa8705918b271a8ff886eb2eac93..878baf3dba0f6f9311cf931040e04c2273b04ab6 100644 (file)
@@ -226,5 +226,5 @@ public:
     void resume(rgw::sal::Store* _store) override;
 };
 
-} //namepsace rgw::lua
+} //namepsace lua
 
diff --git a/src/rgw/rgw_lua_data_filter.cc b/src/rgw/rgw_lua_data_filter.cc
deleted file mode 100644 (file)
index 45cb877..0000000
+++ /dev/null
@@ -1,142 +0,0 @@
-#include "rgw_lua_data_filter.h"
-#include "rgw_lua_utils.h"
-#include "rgw_lua_request.h"
-#include "rgw_lua_background.h"
-#include <lua.hpp>
-
-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<bufferlist*>(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<bufferlist*>(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<bufferlist*>(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<bufferlist*>(lua_touserdata(L, lua_upvalueindex(1)));
-
-    lua_pushinteger(L, bl->length());
-
-    return ONE_RETURNVAL;
-  }
-};
-
-int RGWObjFilter::execute(bufferlist& bl, off_t offset, 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 the "Data" table
-  create_metatable<BufferlistMetaTable>(L, true, &bl);
-  lua_getglobal(L, BufferlistMetaTable::TableName().c_str());
-  ceph_assert(lua_istable(L, -1));
-
-  // create the "Request" table
-  request::create_top_metatable(L, s, op_name);
-
-  // create the "Offset" variable
-  lua_pushinteger(L, offset);
-  lua_setglobal(L, "Offset");
-
-  if (s->lua_background) {
-    // create the "RGW" table
-    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, bl_ofs, "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, logical_offset, "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
deleted file mode 100644 (file)
index 75596b6..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-#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, off_t offset, 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
-
index a6f56b5f96d524f15f6cdc432c9114c408fae043..3e9e0521b27ead7a996782956737fe0856b29e9b 100644 (file)
@@ -839,19 +839,14 @@ struct RequestMetaTable : public EmptyMetaTable {
   }
 };
 
-void create_top_metatable(lua_State* L, req_state* s, const char* op_name) {
-  create_metatable<RequestMetaTable>(L, true, s, const_cast<char*>(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)
+    const std::string& script,
+    rgw::lua::Background* background)
 
 {
   auto L = luaL_newstate();
@@ -878,8 +873,8 @@ int execute(
   lua_pushcclosure(L, RequestLog, FOUR_UPVALS);
   lua_rawset(L, -3);
   
-  if (s->lua_background) {
-    s->lua_background->create_background_metatable(L);
+  if (background) {
+    background->create_background_metatable(L);
     lua_getglobal(L, rgw::lua::RGWTable::TableName().c_str());
     ceph_assert(lua_istable(L, -1));
   }
@@ -903,5 +898,4 @@ int execute(
   return rc;
 }
 
-} // namespace rgw::lua::request
-
+}
index 908d160eb3e9b184a19f940b79ac668d140e2d59..f7569a3e764d7c4ad8c012a30936c7a9b21bef77 100644 (file)
@@ -4,16 +4,15 @@
 #include "include/common_fwd.h"
 #include "rgw_sal_fwd.h"
 
-struct lua_State;
-class req_state;
+struct 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,
@@ -21,7 +20,8 @@ int execute(
     OpsLogSink* olog,
     req_state *s, 
     const char* op_name,
-    const std::string& script);
+    const std::string& script,
+    rgw::lua::Background* background = nullptr);
 
-} // namespace rgw::lua::request
+}
 
index 3ffe23662116eb1cbf48d04416324dc078c2b3be..9b116269d9ead60f0019df1eb89882a0b725aa51 100644 (file)
@@ -73,5 +73,4 @@ void open_standard_libs(lua_State* L) {
   lua_settable(L, -3);
 }
 
-} // namespace rgw::lua
-
+}
index cc77dae7a896af214acf174be6586b84a4c4813d..0071b71c2b47f051ecb7399f3e0e3e9ca8e4dc2d 100644 (file)
@@ -310,6 +310,5 @@ struct StringMapMetaTable : public EmptyMetaTable {
     return ONE_RETURNVAL;
   }
 };
-
-} // namespace rgw::lua
+}
 
index d5ef76adc4f2081e0a433184c78a2ff7d9705811..7625ebd2ead4f8c278c68d58e6c93e81f4cd7ffd 100644 (file)
@@ -52,8 +52,6 @@
 #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"
@@ -2086,20 +2084,6 @@ 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<RGWGetObj_Filter>* 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*/
@@ -2151,7 +2135,6 @@ void RGWGetObj::execute(optional_yield y)
   RGWGetObj_Filter* filter = (RGWGetObj_Filter *)&cb;
   boost::optional<RGWGetObj_Decompress> decompress;
   std::unique_ptr<RGWGetObj_Filter> decrypt;
-  std::unique_ptr<RGWGetObj_Filter> run_lua;
   map<string, bufferlist>::iterator attr_iter;
 
   perfcounter->inc(l_rgw_get);
@@ -2219,15 +2202,6 @@ void RGWGetObj::execute(optional_yield y)
   }
   /* end gettorrent */
 
-  // run lua script on decompressed and decrypted data - first filter runs last
-  op_ret = get_lua_filter(&run_lua, filter);
-  if (run_lua != nullptr) {
-    filter = run_lua.get();
-  }
-  if (op_ret < 0) {
-    goto done_err;
-  }
-
   op_ret = rgw_compression_info_from_attrset(attrs, need_decompress, cs_info);
   if (op_ret < 0) {
     ldpp_dout(this, 0) << "ERROR: failed to decode compression info, cannot decompress" << dendl;
@@ -2310,7 +2284,6 @@ void RGWGetObj::execute(optional_yield y)
     goto done_err;
   }
 
-
   if (!get_data || ofs > end) {
     send_response_data(bl, 0, 0);
     return;
@@ -3869,20 +3842,6 @@ static CompressorRef get_compressor_plugin(const req_state *s,
   return Compressor::create(s->cct, alg);
 }
 
-int RGWPutObj::get_lua_filter(std::unique_ptr<rgw::sal::DataProcessor>* 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];
@@ -4080,7 +4039,6 @@ void RGWPutObj::execute(optional_yield y)
   boost::optional<RGWPutObj_Compress> compressor;
 
   std::unique_ptr<rgw::sal::DataProcessor> encrypt;
-  std::unique_ptr<rgw::sal::DataProcessor> run_lua;
 
   if (!append) { // compression and encryption only apply to full object uploads
     op_ret = get_encrypt_filter(&encrypt, filter);
@@ -4101,14 +4059,6 @@ void RGWPutObj::execute(optional_yield y)
         s->object->set_compressed();
       }
     }
-    // run lua script before data is compressed and encrypted - last filter runs first
-    op_ret = get_lua_filter(&run_lua, filter);
-    if (op_ret < 0) {
-      return;
-    }
-    if (run_lua) {
-      filter = &*run_lua;
-    }
   }
   tracepoint(rgw_op, before_data_transfer, s->req_id.c_str());
   do {
index 9b6f110e5556a6142b6969a3b52b6c7547c9333d..75508c0660493ae286f56420b2974db54519978e 100644 (file)
@@ -437,11 +437,6 @@ public:
     *filter = nullptr;
     return 0;
   }
-
-  // get lua script to run as a "get object" filter 
-  int get_lua_filter(std::unique_ptr<RGWGetObj_Filter>* filter, 
-      RGWGetObj_Filter* cb);
-
   dmc::client_id dmclock_client() override { return dmc::client_id::data; }
 };
 
@@ -1281,10 +1276,6 @@ public:
     return 0;
   }
 
-  // get lua script to run as a "put object" filter 
-  int get_lua_filter(std::unique_ptr<rgw::sal::DataProcessor>* 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);
 
index 74ad1f59a0af75b19652138c6d3d5a3e11a3915d..14f1ee41fe6d3bab1632de0bc36d3788e1df06d8 100644 (file)
@@ -328,7 +328,6 @@ 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;
@@ -338,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);
+      rc = rgw::lua::request::execute(store, rest, olog, s, op->name(), script, lua_background);
       if (rc < 0) {
         ldpp_dout(op, 5) << "WARNING: failed to execute pre request script. error: " << rc << dendl;
       }
@@ -423,7 +422,7 @@ done:
     } 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);
+      rc = rgw::lua::request::execute(store, rest, olog, s, op->name(), script, lua_background);
       if (rc < 0) {
         ldpp_dout(op, 5) << "WARNING: failed to execute post request script. error: " << rc << dendl;
       }
index bdff2abc4c2e83d49cb5d9eabebc23025aadb8d4..07a4ac1b3f74eb5468c2e41ef318159cb7f63952 100644 (file)
      --event-id                event id in a pubsub subscription
   
   Script options:
-     --context                 context in which the script runs. one of: prerequest, postrequest, background, getdata, putdata
+     --context                 context in which the script runs. one of: preRequest, postRequest, background
      --package                 name of the lua package that should be added/removed to/from the allowlist
      --allow-compilation       package is allowed to compile C code as part of its installation
   
index c6e24eea6faa0652c16bc10f276416dc68486b77..aed870269251832482f9937603a1684bd4f5528b 100644 (file)
@@ -6,7 +6,6 @@
 #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;
@@ -771,11 +770,10 @@ 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);
+  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
   EXPECT_EQ(get_table_value<std::string>(lua_background, "hello"), "from request");
   // now we resume and let the background set the value
@@ -922,9 +920,8 @@ TEST(TestRGWLuaBackground, TableValues)
   )";
 
   DEFINE_REQ_STATE;
-  s.lua_background = &lua_background;
 
-  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
   EXPECT_EQ(get_table_value<std::string>(lua_background, "key1"), "string value");
   EXPECT_EQ(get_table_value<long long int>(lua_background, "key2"), 42);
@@ -943,9 +940,8 @@ TEST(TestRGWLuaBackground, TablePersist)
   )";
 
   DEFINE_REQ_STATE;
-  s.lua_background = &lua_background;
 
-  auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
   EXPECT_EQ(get_table_value<std::string>(lua_background, "key1"), "string value");
   EXPECT_EQ(get_table_value<long long int>(lua_background, "key2"), 42);
@@ -955,7 +951,7 @@ TEST(TestRGWLuaBackground, TablePersist)
     RGW["key4"] = RGW["key2"]
   )";
   
-  rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
   EXPECT_EQ(get_table_value<std::string>(lua_background, "key1"), "string value");
   EXPECT_EQ(get_table_value<long long int>(lua_background, "key2"), 42);
@@ -977,14 +973,12 @@ 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);
+  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
   EXPECT_EQ(get_table_value<long long int>(lua_background, "key1"), -99);
   EXPECT_EQ(get_table_value<std::string>(lua_background, "key2"), "hi");
@@ -1007,11 +1001,10 @@ 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);
+  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_NE(rc, 0);
   EXPECT_EQ(get_table_value<std::string>(lua_background, "key1"), "val1");
   EXPECT_EQ(get_table_value<long long int>(lua_background, "key2"), 42);
@@ -1033,9 +1026,8 @@ TEST(TestRGWLuaBackground, TableErase)
   )";
 
   DEFINE_REQ_STATE;
-  s.lua_background = &lua_background;
 
-  auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
   EXPECT_EQ(get_table_value<std::string>(lua_background, "key1"), "string value");
   EXPECT_EQ(get_table_value<long long int>(lua_background, "key2"), 42);
@@ -1050,7 +1042,7 @@ TEST(TestRGWLuaBackground, TableErase)
     RGW["size"] = #RGW
   )";
   
-  rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
   EXPECT_EQ(get_table_value<std::string>(lua_background, "key1"), "");
   EXPECT_EQ(get_table_value<long long int>(lua_background, "key2"), 42);
@@ -1075,9 +1067,8 @@ TEST(TestRGWLuaBackground, TableIterate)
   )";
 
   DEFINE_REQ_STATE;
-  s.lua_background = &lua_background;
 
-  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
   EXPECT_EQ(get_table_value<std::string>(lua_background, "key1"), "string value");
   EXPECT_EQ(get_table_value<long long int>(lua_background, "key2"), 42);
@@ -1101,9 +1092,8 @@ TEST(TestRGWLuaBackground, TableIncrement)
   )";
 
   DEFINE_REQ_STATE;
-  s.lua_background = &lua_background;
 
-  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
 }
 
@@ -1124,9 +1114,8 @@ TEST(TestRGWLuaBackground, TableIncrementBy)
   )";
 
   DEFINE_REQ_STATE;
-  s.lua_background = &lua_background;
 
-  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
 }
 
@@ -1145,9 +1134,8 @@ TEST(TestRGWLuaBackground, TableDecrement)
   )";
 
   DEFINE_REQ_STATE;
-  s.lua_background = &lua_background;
 
-  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
 }
 
@@ -1168,9 +1156,8 @@ TEST(TestRGWLuaBackground, TableDecrementBy)
   )";
 
   DEFINE_REQ_STATE;
-  s.lua_background = &lua_background;
 
-  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_EQ(rc, 0);
 }
 
@@ -1186,9 +1173,8 @@ TEST(TestRGWLuaBackground, TableIncrementValueError)
   )";
 
   DEFINE_REQ_STATE;
-  s.lua_background = &lua_background;
 
-  auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_NE(rc, 0);
   
   request_script = R"(
@@ -1197,7 +1183,7 @@ TEST(TestRGWLuaBackground, TableIncrementValueError)
     RGW.increment("key1")
   )";
 
-  rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_NE(rc, 0);
   
   request_script = R"(
@@ -1206,7 +1192,7 @@ TEST(TestRGWLuaBackground, TableIncrementValueError)
     RGW.increment("key1", "kaboom")
   )";
 
-  rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_NE(rc, 0);
 }
 
@@ -1222,9 +1208,8 @@ TEST(TestRGWLuaBackground, TableIncrementError)
   )";
 
   DEFINE_REQ_STATE;
-  s.lua_background = &lua_background;
 
-  auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_NE(rc, 0);
   
   request_script = R"(
@@ -1232,7 +1217,7 @@ TEST(TestRGWLuaBackground, TableIncrementError)
     RGW.increment = 11
   )";
 
-  rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script);
+  rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "", request_script, &lua_background);
   ASSERT_NE(rc, 0);
 }
 
@@ -1283,52 +1268,3 @@ 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")
-    assert(Offset == 12345678)
-  )";
-
-  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");
-  off_t offset = 12345678;
-  const auto rc = filter.execute(bl, offset, "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, 0, "put_obj");
-  ASSERT_NE(rc, 0);
-}
-