-
Notifications
You must be signed in to change notification settings - Fork 4k
libroach: add key manager #20670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
libroach: add key manager #20670
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Bump the version below when changing cryptopp configure flags. Search for "BUILD | ||
| ARTIFACT CACHING" in build/common.mk for rationale. | ||
|
|
||
| 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| Bump the version below when changing libroach CMake flags. Search for "BUILD | ||
| ARTIFACT CACHING" in build/common.mk for rationale. | ||
|
|
||
| 1 | ||
| 2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,22 @@ | ||
| #include <gtest/gtest.h> | ||
| // Copyright 2017 The Cockroach Authors. | ||
| // | ||
| // Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
| // License (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
|
||
| #include "../db.h" | ||
| #include "../testutils.h" | ||
|
|
||
| TEST(LibroachCCL, DBOpenHook) { | ||
| DBOptions db_opts; | ||
|
|
||
| // Try an empty extra_options. | ||
| db_opts.extra_options = ToDBSlice(""); | ||
| EXPECT_TRUE(DBOpenHook(db_opts).ok()); | ||
| EXPECT_OK(DBOpenHook(db_opts)); | ||
|
|
||
| // Try extra_options with bad data. | ||
| db_opts.extra_options = ToDBSlice("blah"); | ||
| EXPECT_STREQ("failed to parse extra options", DBOpenHook(db_opts).getState()); | ||
| EXPECT_ERR(DBOpenHook(db_opts), "failed to parse extra options"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright 2017 The Cockroach Authors. | ||
| // | ||
| // Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
| // License (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
|
||
| #include "key.h" | ||
| #include <cryptopp/filters.h> | ||
| #include <cryptopp/hex.h> | ||
| #include <cryptopp/sha.h> | ||
|
|
||
| std::string KeyHash(const std::string& k) { | ||
| std::string value; | ||
| CryptoPP::SHA256 hash; | ||
|
|
||
| CryptoPP::StringSource ss( | ||
| k, true /* PumpAll */, | ||
| new CryptoPP::HashFilter(hash, new CryptoPP::HexEncoder(new CryptoPP::StringSink(value), false /* uppercase */))); | ||
|
|
||
| return value; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright 2017 The Cockroach Authors. | ||
| // | ||
| // Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
| // License (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| typedef std::string KeyID; | ||
| typedef std::string RawKey; | ||
|
|
||
| enum EncryptionType { PLAIN, AES }; | ||
|
|
||
| // EncryptionKey represents a key: type (PLAIN or AES) and actual key contents. | ||
| // TODO(mberhault): we would like to be able to mlock and madvise(MADV_DONTDUMP) the | ||
| // memory handling keys. This includes the permanent EncryptionKey and any temporary | ||
| // buffers (eg: when reading files). | ||
| struct EncryptionKey { | ||
| EncryptionType type; | ||
| // The Key ID is a sha-256 hash of the key contents. It is lowercase, without groupings or terminator. | ||
| // **WARNING**: the key ID is persisted to disk, do not change the format. | ||
| KeyID id; | ||
| RawKey key; | ||
| // source describes the source of the key. This could be a filename or | ||
| // simply a description of the generator (eg: "data key manager"). | ||
| std::string source; | ||
| }; | ||
|
|
||
| // KeyHash returns the sha-256 hash of the string. Returned value is in hexadecimal format. | ||
| std::string KeyHash(const std::string& k); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Copyright 2017 The Cockroach Authors. | ||
| // | ||
| // Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
| // License (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
|
||
| #include "key_manager.h" | ||
| #include <cryptopp/modes.h> | ||
| #include <google/protobuf/stubs/stringprintf.h> | ||
| #include "../fmt.h" | ||
|
|
||
| static std::string kFilenamePlain = "plain"; | ||
|
|
||
| static rocksdb::Status KeyFromFile(rocksdb::Env* env, const std::string& path, EncryptionKey* key) { | ||
| if (path == kFilenamePlain) { | ||
| // plaintext placeholder. | ||
| key->type = PLAIN; | ||
| key->id = ""; | ||
| key->key = ""; | ||
| key->source = kFilenamePlain; | ||
| return rocksdb::Status::OK(); | ||
| } | ||
|
|
||
| // Real file, try to read it. | ||
| std::string contents; | ||
| auto status = rocksdb::ReadFileToString(env, path, &contents); | ||
| if (!status.ok()) { | ||
| return status; | ||
| } | ||
|
|
||
| // Check that the length is valid for AES. | ||
| auto key_length = contents.size(); | ||
| if (key_length != 16 && key_length != 24 && key_length != 32) { | ||
| return rocksdb::Status::InvalidArgument(fmt::StringPrintf( | ||
| "key in file %s is %llu bytes long, AES keys can be 16, 24, or 32 bytes", path.c_str(), key_length)); | ||
| } | ||
|
|
||
| // Fill in the key. | ||
| key->type = AES; | ||
| key->id = KeyHash(contents); | ||
| key->key = contents; | ||
| key->source = path; | ||
|
|
||
| return rocksdb::Status::OK(); | ||
| } | ||
|
|
||
| rocksdb::Status FileKeyManager::LoadKeys() { | ||
| std::unique_ptr<EncryptionKey> active(new EncryptionKey()); | ||
| rocksdb::Status status = KeyFromFile(env_, active_key_path_, active.get()); | ||
| if (!status.ok()) { | ||
| return status; | ||
| } | ||
|
|
||
| std::unique_ptr<EncryptionKey> old(new EncryptionKey()); | ||
| status = KeyFromFile(env_, old_key_path_, old.get()); | ||
| if (!status.ok()) { | ||
| return status; | ||
| } | ||
|
|
||
| active_key_.swap(active); | ||
| old_key_.swap(old); | ||
| return rocksdb::Status::OK(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c-deps/cryptopp-rebuild should get this same explanatory comment (sorry, GH won't let me comment there)
Thanks for remembering to bump this one!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uh. oops. I thought I had it. Turns out mine was blank.