-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(encryption): add kms key management
- Loading branch information
yujingwei
committed
Dec 19, 2023
1 parent
2a6cc71
commit 9921aa2
Showing
17 changed files
with
773 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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,87 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <openssl/rand.h> | ||
|
||
#include "absl/strings/escaping.h" | ||
#include "replica/key_provider.h" | ||
#include "utils/error_code.h" | ||
#include "utils/fmt_logging.h" | ||
|
||
namespace dsn { | ||
namespace security { | ||
|
||
class DefaultKeyProvider : public KeyProvider | ||
{ | ||
public: | ||
~DefaultKeyProvider() override {} | ||
dsn::error_s DecryptEncryptionKey(const std::string &encryption_key, | ||
const std::string & /*iv*/, | ||
const std::string & /*key_version*/, | ||
std::string *decrypted_key) override | ||
{ | ||
*decrypted_key = ::absl::HexStringToBytes(encryption_key); | ||
|
||
#ifdef __linux__ | ||
memfrob(decrypted_key->data(), decrypted_key->length()); | ||
#else | ||
// On Linux, memfrob() bitwise XORs the data with the magic number that is | ||
// the answer to the ultimate question of life, the universe, and | ||
// everything. On Mac, we do this manually. | ||
const uint8_t kMagic = 42; | ||
for (auto i = 0; i < decrypted_key->length(); ++i) { | ||
decrypted_key->data()[i] ^= kMagic; | ||
} | ||
#endif | ||
*decrypted_key = ::absl::BytesToHexString(*decrypted_key); | ||
return dsn::error_s::ok(); | ||
} | ||
|
||
dsn::error_s GenerateEncryptionKey(std::string *encryption_key, | ||
std::string *iv, | ||
std::string *key_version) override | ||
{ | ||
unsigned char key_bytes[32]; | ||
unsigned char iv_bytes[32]; | ||
int num_bytes = 16; | ||
RAND_bytes(key_bytes, num_bytes); | ||
std::string tmp = reinterpret_cast<const char *>(key_bytes); | ||
std::string *decrypted_key = &tmp; | ||
#ifdef __linux__ | ||
memfrob(decrypted_key->data(), decrypted_key->length()); | ||
#else | ||
// On Linux, memfrob() bitwise XORs the data with the magic number that is | ||
// the answer to the ultimate question of life, the universe, and | ||
// everything. On Mac, we do this manually. | ||
const uint8_t kMagic = 42; | ||
for (auto i = 0; i < decrypted_key->length(); ++i) { | ||
decrypted_key->data()[i] ^= kMagic; | ||
} | ||
#endif | ||
std::string dek = *decrypted_key; | ||
*encryption_key = ::absl::BytesToHexString(dek); | ||
RAND_bytes(iv_bytes, num_bytes); | ||
*iv = ::absl::BytesToHexString(reinterpret_cast<const char *>(iv_bytes)); | ||
*key_version = "encryptionkey@0"; | ||
return dsn::error_s::ok(); | ||
} | ||
}; | ||
} // namespace security | ||
} // namespace dsn |
This file contains 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,45 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "utils/errors.h" | ||
|
||
namespace dsn { | ||
namespace security { | ||
|
||
// An interface for encrypting and decrypting Pegasus's encryption keys. | ||
class KeyProvider | ||
{ | ||
public: | ||
virtual ~KeyProvider() = default; | ||
|
||
// Decrypts the encryption key. | ||
virtual dsn::error_s DecryptEncryptionKey(const std::string &encryption_key, | ||
const std::string &iv, | ||
const std::string &key_version, | ||
std::string *decrypted_key) = 0; | ||
|
||
// Generates an encryption key (the generated key is encrypted). | ||
virtual dsn::error_s GenerateEncryptionKey(std::string *encryption_key, | ||
std::string *iv, | ||
std::string *key_version) = 0; | ||
}; | ||
} // namespace security | ||
} // namespace dsn |
This file contains 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,42 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include <string> | ||
|
||
#include "replica/pegasus_kms_key_provider.h" | ||
#include "utils/errors.h" | ||
|
||
namespace dsn { | ||
namespace security { | ||
|
||
dsn::error_s PegasusKMSKeyProvider::DecryptEncryptionKey(const std::string &encryption_key, | ||
const std::string &iv, | ||
const std::string &key_version, | ||
std::string *decrypted_key) | ||
{ | ||
return client_.DecryptEncryptionKey(encryption_key, iv, key_version, decrypted_key); | ||
} | ||
|
||
dsn::error_s PegasusKMSKeyProvider::GenerateEncryptionKey(std::string *encryption_key, | ||
std::string *iv, | ||
std::string *key_version) | ||
{ | ||
return client_.GenerateEncryptionKey(encryption_key, iv, key_version); | ||
} | ||
|
||
} // namespace security | ||
} // namespace dsn |
This file contains 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,54 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "replica/key_provider.h" | ||
#include "runtime/security/kms_client.h" | ||
#include "utils/errors.h" | ||
|
||
namespace dsn { | ||
namespace security { | ||
class PegasusKMSKeyProvider : public KeyProvider | ||
{ | ||
public: | ||
~PegasusKMSKeyProvider() override {} | ||
|
||
PegasusKMSKeyProvider(const std::vector<std::string> &kms_url, std::string cluster_key_name) | ||
: client_(kms_url, std::move(cluster_key_name)) | ||
{ | ||
} | ||
|
||
// Decrypts the encryption key. | ||
dsn::error_s DecryptEncryptionKey(const std::string &encryption_key, | ||
const std::string &iv, | ||
const std::string &key_version, | ||
std::string *decrypted_key) override; | ||
|
||
// Generates an encryption key (the generated key is encrypted). | ||
dsn::error_s GenerateEncryptionKey(std::string *encryption_key, | ||
std::string *iv, | ||
std::string *key_version) override; | ||
|
||
private: | ||
KMSClient client_; | ||
}; | ||
} // namespace security | ||
} // namespace dsn |
This file contains 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
Oops, something went wrong.