Skip to content

Commit 1db2301

Browse files
committed
Add KeyManagedEncryptedEnv and AESBlockCipher (#4)
apache/incubator-pegasus#1575 Cherry-pick from tikv@113b363 Summary: Introduce `KeyManagedEncryptedEnv` which wraps around `EncryptedEnv` but provides an `KeyManager` API to enable key management per file. Also implements `AESBlockCipher` with OpenSSL. Test Plan: not tested yet. will update. Signed-off-by: Yi Wu <yiwu@pingcap.com> Signed-off-by: tabokie <xy.tao@outlook.com>
1 parent 4b0706f commit 1db2301

12 files changed

+759
-10
lines changed

.github/workflows/jobs-linux-run-tests.yml

+10-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
- uses: "./.github/actions/pre-steps"
7979
- run: mkdir build && cd build && cmake -DWITH_GFLAGS=1 -DWITH_BENCHMARK=1 .. && make V=1 -j5 && ctest -j5
8080
- uses: "./.github/actions/post-steps"
81-
build-linux-encrypted_env-no_compression:
81+
build-linux-encrypted_env-no_compression-no_openssl:
8282
runs-on: ubuntu-latest
8383
container:
8484
image: zjay437/rocksdb:0.6
@@ -88,3 +88,12 @@ jobs:
8888
- run: mkdir build && cd build && cmake -DWITH_SNAPPY=0 -DWITH_ZLIB=0 -DWITH_BZ2=0 -DWITH_LZ4=0 -DWITH_ZSTD=0 .. && make V=1 -j5 && ctest -j5 -V
8989
- run: "cd build/tools && ./sst_dump --help | grep -E -q 'Supported compression types: kNoCompression'"
9090
- uses: "./.github/actions/post-steps"
91+
build-linux-encrypted_env-openssl:
92+
runs-on: ubuntu-latest
93+
container:
94+
image: zjay437/rocksdb:0.6
95+
steps:
96+
- uses: actions/checkout@v3.5.0
97+
- uses: "./.github/actions/pre-steps"
98+
- run: mkdir build && cd build && cmake -DWITH_OPENSSL=1 -DENCRYPTED_ENV=1 .. && make V=1 -j5 && ctest -j5 -V
99+
- uses: "./.github/actions/post-steps"

CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ option(WITH_SNAPPY "build with SNAPPY" OFF)
7070
option(WITH_LZ4 "build with lz4" OFF)
7171
option(WITH_ZLIB "build with zlib" OFF)
7272
option(WITH_ZSTD "build with zstd" OFF)
73+
option(WITH_OPENSSL "build with openssl" OFF)
7374
option(WITH_WINDOWS_UTF8_FILENAMES "use UTF8 as characterset for opening files, regardles of the system code page" OFF)
7475
if (WITH_WINDOWS_UTF8_FILENAMES)
7576
add_definitions(-DROCKSDB_WINDOWS_UTF8_FILENAMES)
@@ -174,6 +175,14 @@ else()
174175
include_directories(${ZSTD_INCLUDE_DIR})
175176
list(APPEND THIRDPARTY_LIBS zstd::zstd)
176177
endif()
178+
179+
if(WITH_OPENSSL)
180+
find_package(OpenSSL REQUIRED)
181+
add_definitions(-DOPENSSL)
182+
include_directories(${OPENSSL_INCLUDE_DIR})
183+
# Only the crypto library is needed.
184+
list(APPEND THIRDPARTY_LIBS ${OPENSSL_CRYPTO_LIBRARIES})
185+
endif()
177186
endif()
178187

179188
option(WITH_MD_LIBRARY "build with MD" ON)
@@ -736,6 +745,7 @@ set(SOURCES
736745
db/write_controller.cc
737746
db/write_stall_stats.cc
738747
db/write_thread.cc
748+
encryption/encryption.cc
739749
env/composite_env.cc
740750
env/env.cc
741751
env/env_chroot.cc

TARGETS

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ cpp_library_wrapper(name="rocksdb_lib", srcs=[
108108
"db/write_controller.cc",
109109
"db/write_stall_stats.cc",
110110
"db/write_thread.cc",
111+
"encryption/encryption.cc",
111112
"env/composite_env.cc",
112113
"env/env.cc",
113114
"env/env_chroot.cc",

build_tools/build_detect_platform

+13
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,19 @@ EOF
478478
fi
479479
fi
480480

481+
if ! test $ROCKSDB_DISABLE_OPENSSL; then
482+
# Test whether OpenSSL library is installed
483+
$CXX $CFLAGS -x c++ - -o /dev/null 2>/dev/null <<EOF
484+
#include <openssl/crypto.h>
485+
int main() {}
486+
EOF
487+
if [ "$?" = 0 ]; then
488+
COMMON_FLAGS="$COMMON_FLAGS -DOPENSSL"
489+
PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lcrypto"
490+
JAVA_LDFLAGS="$JAVA_LDFLAGS -lcrypto"
491+
fi
492+
fi
493+
481494
if ! test $ROCKSDB_DISABLE_PTHREAD_MUTEX_ADAPTIVE_NP; then
482495
# Test whether PTHREAD_MUTEX_ADAPTIVE_NP mutex type is available
483496
$CXX $PLATFORM_CXXFLAGS -x c++ - -o test.o 2>/dev/null <<EOF

db/db_test_util.cc

+15-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ int64_t MaybeCurrentTime(Env* env) {
3131
}
3232
} // anonymous namespace
3333

34+
#ifdef OPENSSL
35+
const std::string TestKeyManager::default_key =
36+
"\x12\x34\x56\x78\x12\x34\x56\x78\x12\x34\x56\x78\x12\x34\x56\x78\x12\x34"
37+
"\x56\x78\x12\x34\x56\x78";
38+
const std::string TestKeyManager::default_iv =
39+
"\xaa\xbb\xcc\xdd\xaa\xbb\xcc\xdd\xaa\xbb\xcc\xdd\xaa\xbb\xcc\xdd";
40+
#endif
41+
3442
// Special Env used to delay background operations
3543

3644
SpecialEnv::SpecialEnv(Env* base, bool time_elapse_only_sleep)
@@ -71,15 +79,13 @@ DBTestBase::DBTestBase(const std::string path, bool env_do_fsync)
7179
mem_env_ = MockEnv::Create(base_env, base_env->GetSystemClock());
7280
}
7381
if (getenv("ENCRYPTED_ENV")) {
74-
std::shared_ptr<EncryptionProvider> provider;
75-
std::string provider_id = getenv("ENCRYPTED_ENV");
76-
if (provider_id.find("=") == std::string::npos &&
77-
!EndsWith(provider_id, "://test")) {
78-
provider_id = provider_id + "://test";
79-
}
80-
EXPECT_OK(EncryptionProvider::CreateFromString(ConfigOptions(), provider_id,
81-
&provider));
82-
encrypted_env_ = NewEncryptedEnv(mem_env_ ? mem_env_ : base_env, provider);
82+
#ifdef OPENSSL
83+
std::shared_ptr<encryption::KeyManager> key_manager(new TestKeyManager);
84+
encrypted_env_ = NewKeyManagedEncryptedEnv(Env::Default(), key_manager);
85+
#else
86+
fprintf(stderr, "EncryptedEnv is not available without OpenSSL.");
87+
assert(false);
88+
#endif
8389
}
8490
env_ = new SpecialEnv(encrypted_env_ ? encrypted_env_
8591
: (mem_env_ ? mem_env_ : base_env));

db/db_test_util.h

+36
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "rocksdb/compaction_filter.h"
3030
#include "rocksdb/convenience.h"
3131
#include "rocksdb/db.h"
32+
#include "rocksdb/encryption.h"
3233
#include "rocksdb/env.h"
3334
#include "rocksdb/file_system.h"
3435
#include "rocksdb/filter_policy.h"
@@ -55,6 +56,41 @@
5556
namespace ROCKSDB_NAMESPACE {
5657
class MockEnv;
5758

59+
// TODO(yiwu): Use InMemoryKeyManager instead for tests.
60+
#ifdef OPENSSL
61+
class TestKeyManager : public encryption::KeyManager {
62+
public:
63+
virtual ~TestKeyManager() = default;
64+
65+
static const std::string default_key;
66+
static const std::string default_iv;
67+
68+
Status GetFile(const std::string& /*fname*/,
69+
encryption::FileEncryptionInfo* file_info) override {
70+
file_info->method = encryption::EncryptionMethod::kAES192_CTR;
71+
file_info->key = default_key;
72+
file_info->iv = default_iv;
73+
return Status::OK();
74+
}
75+
76+
Status NewFile(const std::string& /*fname*/,
77+
encryption::FileEncryptionInfo* file_info) override {
78+
file_info->method = encryption::EncryptionMethod::kAES192_CTR;
79+
file_info->key = default_key;
80+
file_info->iv = default_iv;
81+
return Status::OK();
82+
}
83+
84+
Status DeleteFile(const std::string&) override { return Status::OK(); }
85+
Status LinkFile(const std::string&, const std::string&) override {
86+
return Status::OK();
87+
}
88+
Status RenameFile(const std::string&, const std::string&) override {
89+
return Status::OK();
90+
}
91+
};
92+
#endif
93+
5894
namespace anon {
5995
class AtomicCounter {
6096
public:

0 commit comments

Comments
 (0)