From 70c8c8110c339fcb6bc68ad2f2bd0166a144692d Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Wed, 10 Oct 2018 15:54:13 -0400 Subject: [PATCH] rgw: encryption filter uses rgw::putobj::Pipe Signed-off-by: Casey Bodley --- src/rgw/rgw_crypt.cc | 70 ++++++++++++--------------------- src/rgw/rgw_crypt.h | 20 +++------- src/test/rgw/test_rgw_crypto.cc | 31 +++------------ 3 files changed, 37 insertions(+), 84 deletions(-) diff --git a/src/rgw/rgw_crypt.cc b/src/rgw/rgw_crypt.cc index 139f2832987..5a1b454a2d0 100644 --- a/src/rgw/rgw_crypt.cc +++ b/src/rgw/rgw_crypt.cc @@ -661,69 +661,49 @@ int RGWGetObj_BlockDecrypt::flush() { } RGWPutObj_BlockEncrypt::RGWPutObj_BlockEncrypt(CephContext* cct, - RGWPutObjDataProcessor* next, - std::unique_ptr crypt): - RGWPutObj_Filter(next), + rgw::putobj::DataProcessor *next, + std::unique_ptr crypt) + : Pipe(next), cct(cct), crypt(std::move(crypt)), - ofs(0), - cache() + block_size(this->crypt->get_block_size()) { - block_size = this->crypt->get_block_size(); } -RGWPutObj_BlockEncrypt::~RGWPutObj_BlockEncrypt() { -} +int RGWPutObj_BlockEncrypt::process(bufferlist&& data, uint64_t logical_offset) +{ + ldout(cct, 25) << "Encrypt " << data.length() << " bytes" << dendl; -int RGWPutObj_BlockEncrypt::handle_data(bufferlist& bl, - off_t in_ofs, - void **phandle, - rgw_raw_obj *pobj, - bool *again) { - int res = 0; - ldout(cct, 25) << "Encrypt " << bl.length() << " bytes" << dendl; - - if (*again) { - bufferlist no_data; - res = next->handle_data(no_data, in_ofs, phandle, pobj, again); - //if *again is not set to false, we will have endless loop - //drop info on log - if (*again) { - ldout(cct, 20) << "*again==true" << dendl; - } - return res; - } + // adjust logical offset to beginning of cached data + ceph_assert(logical_offset >= cache.length()); + logical_offset -= cache.length(); - cache.append(bl); - off_t proc_size = cache.length() & ~(block_size - 1); - if (bl.length() == 0) { + const bool flush = (data.length() == 0); + cache.claim_append(data); + + uint64_t proc_size = cache.length() & ~(block_size - 1); + if (flush) { proc_size = cache.length(); } if (proc_size > 0) { - bufferlist data; - if (! crypt->encrypt(cache, 0, proc_size, data, ofs) ) { + bufferlist in, out; + cache.splice(0, proc_size, &in); + if (!crypt->encrypt(in, 0, proc_size, out, logical_offset)) { return -ERR_INTERNAL_ERROR; } - res = next->handle_data(data, ofs, phandle, pobj, again); - ofs += proc_size; - cache.splice(0, proc_size); - if (res < 0) - return res; + int r = Pipe::process(std::move(out), logical_offset); + logical_offset += proc_size; + if (r < 0) + return r; } - if (bl.length() == 0) { + if (flush) { /*replicate 0-sized handle_data*/ - res = next->handle_data(bl, ofs, phandle, pobj, again); + return Pipe::process({}, logical_offset); } - return res; + return 0; } -int RGWPutObj_BlockEncrypt::throttle_data(void *handle, - const rgw_raw_obj& obj, - uint64_t size, - bool need_to_wait) { - return next->throttle_data(handle, obj, size, need_to_wait); -} std::string create_random_key_selector(CephContext * const cct) { char random[AES_256_KEYSIZE]; diff --git a/src/rgw/rgw_crypt.h b/src/rgw/rgw_crypt.h index db88d835ec0..1cc379c74e7 100644 --- a/src/rgw/rgw_crypt.h +++ b/src/rgw/rgw_crypt.h @@ -9,6 +9,7 @@ #include #include #include +#include "rgw_putobj.h" #include /** @@ -111,28 +112,19 @@ public: }; /* RGWGetObj_BlockDecrypt */ -class RGWPutObj_BlockEncrypt : public RGWPutObj_Filter +class RGWPutObj_BlockEncrypt : public rgw::putobj::Pipe { CephContext* cct; std::unique_ptr crypt; /**< already configured stateless BlockCrypt for operations when enough data is accumulated */ - off_t ofs; /**< stream offset of data we expect to show up next through \ref handle_data */ bufferlist cache; /**< stores extra data that could not (yet) be processed by BlockCrypt */ - size_t block_size; /**< snapshot of \ref BlockCrypt.get_block_size() */ + const size_t block_size; /**< snapshot of \ref BlockCrypt.get_block_size() */ public: RGWPutObj_BlockEncrypt(CephContext* cct, - RGWPutObjDataProcessor* next, + rgw::putobj::DataProcessor *next, std::unique_ptr crypt); - virtual ~RGWPutObj_BlockEncrypt(); - virtual int handle_data(bufferlist& bl, - off_t ofs, - void **phandle, - rgw_raw_obj *pobj, - bool *again) override; - virtual int throttle_data(void *handle, - const rgw_raw_obj& obj, - uint64_t size, - bool need_to_wait) override; + + int process(bufferlist&& data, uint64_t logical_offset) override; }; /* RGWPutObj_BlockEncrypt */ diff --git a/src/test/rgw/test_rgw_crypto.cc b/src/test/rgw/test_rgw_crypto.cc index bcf02ab1b97..51b81f9e35d 100644 --- a/src/test/rgw/test_rgw_crypto.cc +++ b/src/test/rgw/test_rgw_crypto.cc @@ -44,20 +44,13 @@ public: } }; -class ut_put_sink: public RGWPutObjDataProcessor +class ut_put_sink: public rgw::putobj::DataProcessor { std::stringstream sink; public: - ut_put_sink(){} - virtual ~ut_put_sink(){} - int handle_data(bufferlist& bl, off_t ofs, void **phandle, rgw_raw_obj *pobj, bool *again) override + int process(bufferlist&& bl, uint64_t ofs) override { sink << boost::string_ref(bl.c_str(),bl.length()); - *again = false; - return 0; - } - int throttle_data(void *handle, const rgw_raw_obj& obj, uint64_t size, bool need_to_wait) override - { return 0; } std::string get_sink() @@ -562,18 +555,11 @@ TEST(TestRGWCrypto, verify_RGWPutObj_BlockEncrypt_chunks) bufferlist bl; bl.append(input.c_str()+pos, size); - void* handle; - bool again = false; - rgw_raw_obj ro; - encrypt.handle_data(bl, 0, &handle, nullptr, &again); - encrypt.throttle_data(handle, ro, size, false); + encrypt.process(std::move(bl), pos); pos = pos + size; } while (pos < test_size); - bufferlist bl; - void* handle; - bool again = false; - encrypt.handle_data(bl, 0, &handle, nullptr, &again); + encrypt.process({}, pos); ASSERT_EQ(put_sink.get_sink().length(), static_cast(test_size)); @@ -619,13 +605,8 @@ TEST(TestRGWCrypto, verify_Encrypt_Decrypt) AES_256_CBC_create(g_ceph_context, &key[0], 32) ); bufferlist bl; bl.append((char*)test_in, test_size); - void* handle; - bool again = false; - rgw_raw_obj ro; - encrypt.handle_data(bl, 0, &handle, nullptr, &again); - encrypt.throttle_data(handle, ro, test_size, false); - bl.clear(); - encrypt.handle_data(bl, 0, &handle, nullptr, &again); + encrypt.process(std::move(bl), 0); + encrypt.process({}, test_size); ASSERT_EQ(put_sink.get_sink().length(), test_size); bl.append(put_sink.get_sink().data(), put_sink.get_sink().length()); -- 2.39.5