From 89ef51a81bc4264a8fe4fc4efa090702bfb3f445 Mon Sep 17 00:00:00 2001 From: Yuval Lifshitz Date: Sun, 12 Jun 2022 20:58:40 +0300 Subject: [PATCH] rgw/lua: allow bucket name override in pre request Signed-off-by: Yuval Lifshitz --- doc/radosgw/lua-scripting.rst | 2 +- src/rgw/rgw_lua_request.cc | 15 +++++++++++++++ src/test/rgw/test_rgw_lua.cc | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/doc/radosgw/lua-scripting.rst b/doc/radosgw/lua-scripting.rst index dbfd4b016e5..750a91397ac 100644 --- a/doc/radosgw/lua-scripting.rst +++ b/doc/radosgw/lua-scripting.rst @@ -149,7 +149,7 @@ Request Fields +----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+ | ``Request.Bucket.Tenant`` | string | tenant of the bucket | no | no | yes | +----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+ -| ``Request.Bucket.Name`` | string | bucket name | no | no | 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 | +----------------------------------------------------+----------+--------------------------------------------------------------+----------+-----------+----------+ diff --git a/src/rgw/rgw_lua_request.cc b/src/rgw/rgw_lua_request.cc index 1974a2b8bc9..3e9e0521b27 100644 --- a/src/rgw/rgw_lua_request.cc +++ b/src/rgw/rgw_lua_request.cc @@ -324,6 +324,21 @@ struct BucketMetaTable : public EmptyMetaTable { } return ONE_RETURNVAL; } + + static int NewIndexClosure(lua_State* L) { + const auto s = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(FIRST_UPVAL))); + const auto bucket = s->bucket.get(); + + const char* index = luaL_checkstring(L, 2); + + if (rgw::sal::Bucket::empty(bucket)) { + if (strcasecmp(index, "Name") == 0) { + s->init_state.url_bucket = luaL_checkstring(L, 3); + return NO_RETURNVAL; + } + } + return error_unknown_field(L, index, TableName()); + } }; struct ObjectMetaTable : public EmptyMetaTable { diff --git a/src/test/rgw/test_rgw_lua.cc b/src/test/rgw/test_rgw_lua.cc index eeca2a09267..b09848e53b4 100644 --- a/src/test/rgw/test_rgw_lua.cc +++ b/src/test/rgw/test_rgw_lua.cc @@ -343,6 +343,39 @@ TEST(TestRGWLua, Bucket) ASSERT_EQ(rc, 0); } +TEST(TestRGWLua, WriteBucket) +{ + const std::string script = R"( + assert(Request.Bucket) + assert(Request.Bucket.Name == "myname") + Request.Bucket.Name = "othername" + )"; + + DEFINE_REQ_STATE; + s.init_state.url_bucket = "myname"; + + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "put_obj", script); + ASSERT_EQ(rc, 0); + ASSERT_EQ(s.init_state.url_bucket, "othername"); +} + +TEST(TestRGWLua, WriteBucketFail) +{ + const std::string script = R"( + assert(Request.Bucket) + assert(Request.Bucket.Name == "myname") + Request.Bucket.Name = "othername" + )"; + + DEFINE_REQ_STATE; + rgw_bucket b; + b.name = "myname"; + s.bucket.reset(new sal::RadosBucket(nullptr, b)); + + const auto rc = lua::request::execute(nullptr, nullptr, nullptr, &s, "put_obj", script); + ASSERT_NE(rc, 0); +} + TEST(TestRGWLua, GenericAttributes) { const std::string script = R"(