fix encryption provider

This commit is contained in:
Scott E. Graves 2023-12-13 13:34:21 -06:00
parent 0e83d84360
commit d6d4b579c9
2 changed files with 67 additions and 51 deletions

View File

@ -259,48 +259,46 @@ auto encrypt_provider::get_directory_items(const std::string &api_path,
return api_error::item_exists; return api_error::item_exists;
} }
auto result = db::db_select{*db_, source_table} auto cfg = config_.get_encrypt_config();
.column("source_path") std::string source_path{api_path};
.where("api_path") if (api_path != "/") {
.equals(api_path) res =
.go(); utils::encryption::decrypt_file_path(cfg.encryption_token, source_path);
std::optional<db::db_select::row> row; if (res != api_error::success) {
if (not(result.get_row(row) && row.has_value())) { return res;
}
}
source_path =
utils::path::absolute(utils::path::combine(cfg.path, {source_path}));
if (source_path != cfg.path &&
not source_path.starts_with(cfg.path +
utils::path::directory_seperator)) {
return api_error::directory_not_found; return api_error::directory_not_found;
} }
auto source_path = row->get_column("source_path").get_value<std::string>();
result = db::db_select{*db_, directory_table} if (not utils::file::is_directory(source_path)) {
.column("api_path")
.where("source_path")
.equals(source_path)
.go();
if (not result.has_row()) {
return api_error::directory_not_found; return api_error::directory_not_found;
} }
try { try {
for (const auto &dir_entry : auto iter = std::filesystem::directory_iterator(source_path);
std::filesystem::directory_iterator(source_path)) { for (const auto &dir_entry : iter) {
try { try {
std::string current_api_path{}; std::string current_api_path{};
if (dir_entry.is_directory()) { if (dir_entry.is_directory()) {
result = db::db_select{*db_, directory_table} auto result = db::db_select{*db_, directory_table}
.column("api_path") .column("api_path")
.where("source_path") .where("source_path")
.equals(dir_entry.path().string()) .equals(dir_entry.path().string())
.go(); .go();
row.reset(); std::optional<db::db_select::row> row;
if (result.get_row(row) && row.has_value()) { if (result.get_row(row) && row.has_value()) {
current_api_path = current_api_path =
row->get_column("api_path").get_value<std::string>(); row->get_column("api_path").get_value<std::string>();
} }
if (current_api_path.empty()) { if (current_api_path.empty()) {
const auto cfg = config_.get_encrypt_config(); process_directory_entry(dir_entry, cfg, current_api_path);
for (const auto &child_dir_entry :
std::filesystem::directory_iterator(dir_entry.path())) {
process_directory_entry(child_dir_entry, cfg, current_api_path);
}
result = db::db_select{*db_, directory_table} result = db::db_select{*db_, directory_table}
.column("api_path") .column("api_path")
@ -317,18 +315,17 @@ auto encrypt_provider::get_directory_items(const std::string &api_path,
} }
} else { } else {
std::string api_path_data{}; std::string api_path_data{};
result = db::db_select{*db_, file_table} auto result = db::db_select{*db_, file_table}
.column("data") .column("data")
.where("source_path") .where("source_path")
.equals(dir_entry.path().string()) .equals(dir_entry.path().string())
.go(); .go();
row.reset(); std::optional<db::db_select::row> row;
if (result.get_row(row) && row.has_value()) { if (result.get_row(row) && row.has_value()) {
api_path_data = row->get_column("data").get_value<std::string>(); api_path_data = row->get_column("data").get_value<std::string>();
} }
if (api_path_data.empty()) { if (api_path_data.empty()) {
const auto cfg = config_.get_encrypt_config();
if (not process_directory_entry(dir_entry, cfg, current_api_path)) { if (not process_directory_entry(dir_entry, cfg, current_api_path)) {
continue; continue;
} }
@ -414,7 +411,7 @@ auto encrypt_provider::process_directory_entry(
auto encrypted_parts = utils::string::split( auto encrypted_parts = utils::string::split(
utils::path::create_api_path(dir_path.string()), '/', false); utils::path::create_api_path(dir_path.string()), '/', false);
for (std::size_t part_idx = 0U; part_idx < encrypted_parts.size(); for (std::size_t part_idx = 1U; part_idx < encrypted_parts.size();
part_idx++) { part_idx++) {
data_buffer encrypted_data; data_buffer encrypted_data;
utils::encryption::encrypt_data( utils::encryption::encrypt_data(
@ -425,7 +422,7 @@ auto encrypt_provider::process_directory_entry(
encrypted_parts[part_idx] = utils::to_hex_string(encrypted_data); encrypted_parts[part_idx] = utils::to_hex_string(encrypted_data);
} }
std::size_t current_idx{}; std::size_t current_idx{1U};
std::string current_encrypted_path{}; std::string current_encrypted_path{};
std::string current_source_path{cfg.path}; std::string current_source_path{cfg.path};
for (const auto &part : dir_path) { for (const auto &part : dir_path) {
@ -436,7 +433,7 @@ auto encrypt_provider::process_directory_entry(
current_source_path = current_source_path =
utils::path::combine(current_source_path, {part.string()}); utils::path::combine(current_source_path, {part.string()});
std::string parent_api_path{}; std::string current_api_path{};
auto result = db::db_select{*db_, directory_table} auto result = db::db_select{*db_, directory_table}
.column("api_path") .column("api_path")
.where("source_path") .where("source_path")
@ -444,33 +441,35 @@ auto encrypt_provider::process_directory_entry(
.go(); .go();
std::optional<db::db_select::row> row; std::optional<db::db_select::row> row;
if (result.get_row(row) && row.has_value()) { if (result.get_row(row) && row.has_value()) {
parent_api_path = row->get_column("api_path").get_value<std::string>(); current_api_path = row->get_column("api_path").get_value<std::string>();
} }
if (parent_api_path.empty()) { if (current_api_path.empty()) {
parent_api_path = utils::path::create_api_path( current_api_path = utils::path::create_api_path(
current_encrypted_path + '/' + encrypted_parts.at(current_idx)); current_encrypted_path + '/' + encrypted_parts.at(current_idx));
auto ins_res = db::db_insert{*db_, directory_table} auto ins_res = db::db_insert{*db_, directory_table}
.column_value("source_path", current_source_path) .column_value("source_path", current_source_path)
.column_value("api_path", parent_api_path) .column_value("api_path", current_api_path)
.go(); .go();
// TODO handle error // TODO handle error
ins_res = db::db_insert{*db_, source_table} ins_res = db::db_insert{*db_, source_table}
.column_value("api_path", parent_api_path) .column_value("api_path", current_api_path)
.column_value("source_path", current_source_path) .column_value("source_path", current_source_path)
.go(); .go();
// TODO handle error // TODO handle error
event_system::instance().raise<filesystem_item_added>( event_system::instance().raise<filesystem_item_added>(
parent_api_path, utils::path::get_parent_api_path(parent_api_path), current_api_path,
true); utils::path::get_parent_api_path(current_api_path), true);
} else { } else {
encrypted_parts[current_idx] = encrypted_parts[current_idx] =
utils::string::split(parent_api_path, '/', false)[current_idx]; utils::string::split(current_api_path, '/', false)[current_idx];
} }
current_encrypted_path = utils::path::create_api_path( current_encrypted_path = utils::path::create_api_path(
current_encrypted_path + '/' + encrypted_parts[current_idx++]); current_encrypted_path + '/' + encrypted_parts.at(current_idx++));
std::cout << current_source_path << ':' << current_encrypted_path
<< std::endl;
} }
return current_encrypted_path; return current_encrypted_path;

View File

@ -22,25 +22,42 @@
#include "test_common.hpp" #include "test_common.hpp"
#include "app_config.hpp" #include "app_config.hpp"
#include "file_manager/file_manager.hpp"
#include "events/consumers/console_consumer.hpp" #include "events/consumers/console_consumer.hpp"
#include "providers/encrypt/encrypt_provider.hpp" #include "providers/encrypt/encrypt_provider.hpp"
#include "utils/path_utils.hpp" #include "utils/path_utils.hpp"
namespace repertory { namespace repertory {
/* TEST(encrypt_provider, can_construct_encrypt_provider) { /*
TEST(encrypt_provider, can_construct_encrypt_provider) {
ASSERT_TRUE( ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test")); utils::file::delete_directory_recursively("./encrypt_provider_test"));
// console_consumer consumer{};
// event_system::instance().start();
{ {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::encrypt, "./encrypt_provider_test"); app_config cfg(provider_type::encrypt, "./encrypt_provider_test");
EXPECT_FALSE(
cfg.set_value_by_name("EncryptConfig.Path", "c:\\src").empty());
encrypt_provider provider(cfg); encrypt_provider provider(cfg);
file_manager mgr(cfg, provider);
mgr.start();
event_system::instance().stop(); EXPECT_TRUE(provider.start(
[&provider](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
&mgr));
directory_item_list list{};
EXPECT_EQ(api_error::success, provider.get_directory_items("/", list));
provider.stop();
mgr.stop();
} }
// event_system::instance().stop();
ASSERT_TRUE( ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test")); utils::file::delete_directory_recursively("./encrypt_provider_test"));
} }