2021-05-13 06:37:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-07-11 09:22:30 +00:00
|
|
|
#include <Common/config.h>
|
2021-05-18 20:48:16 +00:00
|
|
|
|
2021-07-11 09:22:30 +00:00
|
|
|
#if USE_SSL
|
2021-05-13 06:37:05 +00:00
|
|
|
#include <Disks/IDisk.h>
|
2021-05-17 16:58:51 +00:00
|
|
|
#include <Disks/DiskDecorator.h>
|
2021-07-27 08:38:22 +00:00
|
|
|
#include <Common/MultiVersion.h>
|
2021-05-13 06:37:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-07-11 09:22:30 +00:00
|
|
|
class ReadBufferFromFileBase;
|
|
|
|
class WriteBufferFromFileBase;
|
2021-07-17 13:35:15 +00:00
|
|
|
namespace FileEncryption { enum class Algorithm; }
|
2021-05-13 16:21:45 +00:00
|
|
|
|
2021-07-27 08:38:22 +00:00
|
|
|
struct DiskEncryptedSettings
|
|
|
|
{
|
|
|
|
DiskPtr wrapped_disk;
|
|
|
|
String disk_path;
|
|
|
|
std::unordered_map<UInt64, String> keys;
|
|
|
|
UInt64 current_key_id;
|
|
|
|
FileEncryption::Algorithm current_algorithm;
|
|
|
|
};
|
|
|
|
|
2021-07-11 19:26:39 +00:00
|
|
|
/// Encrypted disk ciphers all written files on the fly and writes the encrypted files to an underlying (normal) disk.
|
|
|
|
/// And when we read files from an encrypted disk it deciphers them automatically,
|
|
|
|
/// so we can work with a encrypted disk like it's a normal disk.
|
2021-05-14 21:52:51 +00:00
|
|
|
class DiskEncrypted : public DiskDecorator
|
2021-05-13 06:37:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-07-27 08:38:22 +00:00
|
|
|
DiskEncrypted(const String & name_, const Poco::Util::AbstractConfiguration & config_, const String & config_prefix_, const DisksMap & map_);
|
|
|
|
DiskEncrypted(const String & name_, std::unique_ptr<const DiskEncryptedSettings> settings_);
|
2021-05-13 06:37:05 +00:00
|
|
|
|
|
|
|
const String & getName() const override { return name; }
|
2021-05-13 16:21:45 +00:00
|
|
|
const String & getPath() const override { return disk_absolute_path; }
|
2021-05-13 06:37:05 +00:00
|
|
|
|
|
|
|
ReservationPtr reserve(UInt64 bytes) override;
|
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
bool exists(const String & path) const override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
return delegate->exists(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isFile(const String & path) const override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
return delegate->isFile(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
bool isDirectory(const String & path) const override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
return delegate->isDirectory(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-14 21:52:51 +00:00
|
|
|
size_t getFileSize(const String & path) const override;
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void createDirectory(const String & path) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->createDirectory(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void createDirectories(const String & path) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->createDirectories(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void clearDirectory(const String & path) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->clearDirectory(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void moveDirectory(const String & from_path, const String & to_path) override
|
|
|
|
{
|
|
|
|
auto wrapped_from_path = wrappedPath(from_path);
|
|
|
|
auto wrapped_to_path = wrappedPath(to_path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->moveDirectory(wrapped_from_path, wrapped_to_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2022-06-13 19:07:10 +00:00
|
|
|
DirectoryIteratorPtr iterateDirectory(const String & path) const override
|
2021-05-13 16:21:45 +00:00
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
return delegate->iterateDirectory(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-19 22:06:25 +00:00
|
|
|
void createFile(const String & path) override
|
|
|
|
{
|
2021-05-13 16:21:45 +00:00
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->createFile(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void moveFile(const String & from_path, const String & to_path) override
|
|
|
|
{
|
|
|
|
auto wrapped_from_path = wrappedPath(from_path);
|
|
|
|
auto wrapped_to_path = wrappedPath(to_path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->moveFile(wrapped_from_path, wrapped_to_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void replaceFile(const String & from_path, const String & to_path) override
|
|
|
|
{
|
|
|
|
auto wrapped_from_path = wrappedPath(from_path);
|
|
|
|
auto wrapped_to_path = wrappedPath(to_path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->replaceFile(wrapped_from_path, wrapped_to_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2022-06-13 19:07:10 +00:00
|
|
|
void listFiles(const String & path, std::vector<String> & file_names) const override
|
2021-05-13 16:21:45 +00:00
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->listFiles(wrapped_path, file_names);
|
|
|
|
}
|
|
|
|
|
2021-07-11 19:26:39 +00:00
|
|
|
void copy(const String & from_path, const std::shared_ptr<IDisk> & to_disk, const String & to_path) override;
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2022-04-21 12:39:12 +00:00
|
|
|
void copyDirectoryContent(const String & from_dir, const std::shared_ptr<IDisk> & to_disk, const String & to_dir) override;
|
|
|
|
|
2021-05-13 06:37:05 +00:00
|
|
|
std::unique_ptr<ReadBufferFromFileBase> readFile(
|
|
|
|
const String & path,
|
2021-08-16 00:00:32 +00:00
|
|
|
const ReadSettings & settings,
|
2022-01-03 18:36:34 +00:00
|
|
|
std::optional<size_t> read_hint,
|
|
|
|
std::optional<size_t> file_size) const override;
|
2021-05-13 06:37:05 +00:00
|
|
|
|
|
|
|
std::unique_ptr<WriteBufferFromFileBase> writeFile(
|
|
|
|
const String & path,
|
|
|
|
size_t buf_size,
|
2022-03-14 18:40:53 +00:00
|
|
|
WriteMode mode,
|
|
|
|
const WriteSettings & settings) override;
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void removeFile(const String & path) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->removeFile(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void removeFileIfExists(const String & path) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->removeFileIfExists(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void removeDirectory(const String & path) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->removeDirectory(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void removeRecursive(const String & path) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->removeRecursive(wrapped_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeSharedFile(const String & path, bool flag) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
|
|
|
delegate->removeSharedFile(wrapped_path, flag);
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:01:30 +00:00
|
|
|
void removeSharedRecursive(const String & path, bool keep_all_batch_data, const NameSet & file_names_remove_metadata_only) override
|
2021-05-14 21:52:51 +00:00
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2022-04-19 12:01:30 +00:00
|
|
|
delegate->removeSharedRecursive(wrapped_path, keep_all_batch_data, file_names_remove_metadata_only);
|
2022-04-18 23:09:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 12:01:30 +00:00
|
|
|
void removeSharedFiles(const RemoveBatchRequest & files, bool keep_all_batch_data, const NameSet & file_names_remove_metadata_only) override
|
2022-04-18 23:09:09 +00:00
|
|
|
{
|
|
|
|
for (const auto & file : files)
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(file.path);
|
2022-04-19 12:01:30 +00:00
|
|
|
bool keep = keep_all_batch_data || file_names_remove_metadata_only.contains(fs::path(file.path).filename());
|
2022-04-18 23:09:09 +00:00
|
|
|
if (file.if_exists)
|
|
|
|
delegate->removeSharedFileIfExists(wrapped_path, keep);
|
|
|
|
else
|
|
|
|
delegate->removeSharedFile(wrapped_path, keep);
|
|
|
|
}
|
2021-05-14 21:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void removeSharedFileIfExists(const String & path, bool flag) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
|
|
|
delegate->removeSharedFileIfExists(wrapped_path, flag);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void setLastModified(const String & path, const Poco::Timestamp & timestamp) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->setLastModified(wrapped_path, timestamp);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2022-06-13 19:07:10 +00:00
|
|
|
Poco::Timestamp getLastModified(const String & path) const override
|
2021-05-13 16:21:45 +00:00
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
return delegate->getLastModified(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2022-06-13 22:47:03 +00:00
|
|
|
time_t getLastChanged(const String & path) const override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
|
|
|
return delegate->getLastChanged(wrapped_path);
|
|
|
|
}
|
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void setReadOnly(const String & path) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->setReadOnly(wrapped_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
2021-05-13 16:21:45 +00:00
|
|
|
void createHardLink(const String & src_path, const String & dst_path) override
|
|
|
|
{
|
|
|
|
auto wrapped_src_path = wrappedPath(src_path);
|
|
|
|
auto wrapped_dst_path = wrappedPath(dst_path);
|
2021-05-14 21:52:51 +00:00
|
|
|
delegate->createHardLink(wrapped_src_path, wrapped_dst_path);
|
2021-05-13 16:21:45 +00:00
|
|
|
}
|
2021-05-13 06:37:05 +00:00
|
|
|
|
|
|
|
void truncateFile(const String & path, size_t size) override;
|
|
|
|
|
2021-05-14 21:52:51 +00:00
|
|
|
String getUniqueId(const String & path) const override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
|
|
|
return delegate->getUniqueId(wrapped_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onFreeze(const String & path) override
|
|
|
|
{
|
|
|
|
auto wrapped_path = wrappedPath(path);
|
2021-05-18 20:20:51 +00:00
|
|
|
delegate->onFreeze(wrapped_path);
|
2021-05-14 21:52:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 05:36:50 +00:00
|
|
|
void applyNewSettings(const Poco::Util::AbstractConfiguration & config, ContextPtr context, const String & config_prefix, const DisksMap & map) override;
|
2021-05-14 21:52:51 +00:00
|
|
|
|
2021-08-24 22:24:47 +00:00
|
|
|
DiskType getType() const override { return DiskType::Encrypted; }
|
2021-08-24 23:27:20 +00:00
|
|
|
bool isRemote() const override { return delegate->isRemote(); }
|
2021-05-13 06:37:05 +00:00
|
|
|
|
|
|
|
SyncGuardPtr getDirectorySyncGuard(const String & path) const override;
|
|
|
|
|
|
|
|
private:
|
2021-05-14 21:52:51 +00:00
|
|
|
String wrappedPath(const String & path) const
|
|
|
|
{
|
|
|
|
// if path starts_with disk_path -> got already wrapped path
|
2021-07-11 09:22:30 +00:00
|
|
|
if (!disk_path.empty() && path.starts_with(disk_path))
|
2021-05-14 21:52:51 +00:00
|
|
|
return path;
|
|
|
|
return disk_path + path;
|
|
|
|
}
|
|
|
|
|
2021-07-27 08:38:22 +00:00
|
|
|
const String name;
|
|
|
|
const String disk_path;
|
|
|
|
const String disk_absolute_path;
|
|
|
|
MultiVersion<DiskEncryptedSettings> current_settings;
|
2021-05-13 06:37:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-05-18 20:48:16 +00:00
|
|
|
|
|
|
|
#endif
|