2023-05-24 15:27:47 +00:00
|
|
|
#include <Disks/DiskEncryptedTransaction.h>
|
2023-05-25 10:01:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
#if USE_SSL
|
2023-05-24 15:27:47 +00:00
|
|
|
#include <IO/FileEncryptionCommon.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <boost/algorithm/hex.hpp>
|
|
|
|
#include <IO/ReadBufferFromEncryptedFile.h>
|
2024-02-13 11:02:46 +00:00
|
|
|
#include <IO/ReadBufferFromFileDecorator.h>
|
2023-05-24 15:27:47 +00:00
|
|
|
#include <IO/ReadBufferFromString.h>
|
|
|
|
#include <IO/WriteBufferFromEncryptedFile.h>
|
2023-05-25 15:37:09 +00:00
|
|
|
#include <Common/quoteString.h>
|
2023-05-24 15:27:47 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int DATA_ENCRYPTION_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
FileEncryption::Header readHeader(ReadBufferFromFileBase & read_buffer)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
FileEncryption::Header header;
|
|
|
|
header.read(read_buffer);
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
e.addMessage("While reading the header of encrypted file " + quoteString(read_buffer.getFileName()));
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:11:10 +00:00
|
|
|
String DiskEncryptedSettings::findKeyByFingerprint(UInt128 key_fingerprint, const String & path_for_logs) const
|
2023-05-24 15:27:47 +00:00
|
|
|
{
|
2023-05-31 11:11:10 +00:00
|
|
|
auto it = all_keys.find(key_fingerprint);
|
|
|
|
if (it == all_keys.end())
|
|
|
|
{
|
2023-05-24 15:27:47 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::DATA_ENCRYPTION_ERROR,
|
2023-05-31 11:11:10 +00:00
|
|
|
"Not found an encryption key required to decipher file {}",
|
|
|
|
quoteString(path_for_logs));
|
|
|
|
}
|
|
|
|
return it->second;
|
2023-05-24 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 12:29:37 +00:00
|
|
|
void DiskEncryptedTransaction::copyFile(const std::string & from_file_path, const std::string & to_file_path, const ReadSettings & read_settings, const WriteSettings & write_settings)
|
2023-05-24 15:27:47 +00:00
|
|
|
{
|
|
|
|
auto wrapped_from_path = wrappedPath(from_file_path);
|
|
|
|
auto wrapped_to_path = wrappedPath(to_file_path);
|
2023-08-11 12:29:37 +00:00
|
|
|
delegate_transaction->copyFile(wrapped_from_path, wrapped_to_path, read_settings, write_settings);
|
2023-05-24 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 11:02:40 +00:00
|
|
|
std::unique_ptr<WriteBufferFromFileBase> DiskEncryptedTransaction::writeFile( // NOLINT
|
2023-05-24 15:27:47 +00:00
|
|
|
const std::string & path,
|
|
|
|
size_t buf_size,
|
|
|
|
WriteMode mode,
|
|
|
|
const WriteSettings & settings,
|
|
|
|
bool autocommit)
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
|
|
|
FileEncryption::Header header;
|
|
|
|
String key;
|
|
|
|
UInt64 old_file_size = 0;
|
2023-05-25 14:38:19 +00:00
|
|
|
if (mode == WriteMode::Append && delegate_disk->exists(wrapped_path))
|
2023-05-24 15:27:47 +00:00
|
|
|
{
|
2023-05-25 14:38:19 +00:00
|
|
|
size_t size = delegate_disk->getFileSize(wrapped_path);
|
|
|
|
old_file_size = size > FileEncryption::Header::kSize ? (size - FileEncryption::Header::kSize) : 0;
|
2023-05-24 15:27:47 +00:00
|
|
|
if (old_file_size)
|
|
|
|
{
|
|
|
|
/// Append mode: we continue to use the same header.
|
|
|
|
auto read_buffer = delegate_disk->readFile(wrapped_path, ReadSettings().adjustBufferSize(FileEncryption::Header::kSize));
|
|
|
|
header = readHeader(*read_buffer);
|
2023-05-31 11:11:10 +00:00
|
|
|
key = current_settings.findKeyByFingerprint(header.key_fingerprint, path);
|
2023-05-24 15:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!old_file_size)
|
|
|
|
{
|
|
|
|
/// Rewrite mode: we generate a new header.
|
|
|
|
header.algorithm = current_settings.current_algorithm;
|
2023-05-31 11:11:10 +00:00
|
|
|
key = current_settings.current_key;
|
|
|
|
header.key_fingerprint = current_settings.current_key_fingerprint;
|
2023-05-24 15:27:47 +00:00
|
|
|
header.init_vector = FileEncryption::InitVector::random();
|
|
|
|
}
|
|
|
|
auto buffer = delegate_transaction->writeFile(wrapped_path, buf_size, mode, settings, autocommit);
|
|
|
|
return std::make_unique<WriteBufferFromEncryptedFile>(buf_size, std::move(buffer), key, header, old_file_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-05-25 10:01:24 +00:00
|
|
|
|
|
|
|
#endif
|