diff --git a/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.h b/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.h --- a/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.h +++ b/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.h @@ -8,6 +8,10 @@ namespace comm { class DatabaseManager { + // Constant key sizes + static const int backupDataKeySize; + static const int backupLogDataKeySize; + // Indicate that at least one instance of SQLiteQueryExecutor was created, // which is identical to finishing the migration process and having a fully // operational database that can be used by application logic. @@ -34,6 +38,14 @@ static void initializeQueryExecutor(std::string &databasePath); static bool checkIfDatabaseNeedsDeletion(); static void reportDBOperationsFailure(); + + // Set SQLite keys to keep using the previous User Data keys. It is required + // to upload User Keys during restoring RPC and be able to restore again even + // if User Data upload fails (which is not part of a single RPC). + // By default, it is applied to the main database. + static void setUserDataKeys( + const std::string &backupDataKey, + const std::string &backupLogDataKey); }; } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp b/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/DatabaseManager.cpp @@ -9,6 +9,9 @@ namespace comm { +const int DatabaseManager::backupDataKeySize = 64; +const int DatabaseManager::backupLogDataKeySize = 32; + std::once_flag DatabaseManager::queryExecutorCreationIndicated; std::once_flag DatabaseManager::sqliteQueryExecutorPropertiesInitialized; @@ -91,14 +94,14 @@ std::string DatabaseManager::generateBackupDataKey() { std::string backupDataKey = comm::crypto::Tools::generateRandomHexString( - SQLiteQueryExecutor::backupDataKeySize); + DatabaseManager::backupDataKeySize); CommSecureStore::set(CommSecureStore::backupDataKey, backupDataKey); return backupDataKey; } std::string DatabaseManager::generateBackupLogDataKey() { std::string backupLogDataKey = comm::crypto::Tools::generateRandomHexString( - SQLiteQueryExecutor::backupLogDataKeySize); + DatabaseManager::backupLogDataKeySize); CommSecureStore::set(CommSecureStore::backupLogDataKey, backupLogDataKey); return backupLogDataKey; } @@ -148,4 +151,33 @@ CommSecureStore::set(DATABASE_MANAGER_STATUS_KEY, DB_OPERATIONS_FAILURE); } +void DatabaseManager::setUserDataKeys( + const std::string &backupDataKey, + const std::string &backupLogDataKey) { + if (SQLiteQueryExecutor::backupDataKey.empty()) { + throw std::runtime_error("backupDataKey is not set"); + } + + if (SQLiteQueryExecutor::backupLogDataKey.empty()) { + throw std::runtime_error("backupLogDataKey is not set"); + } + + if (backupDataKey.size() != DatabaseManager::backupDataKeySize) { + throw std::runtime_error("invalid backupDataKey size"); + } + + if (backupLogDataKey.size() != DatabaseManager::backupLogDataKeySize) { + throw std::runtime_error("invalid backupLogDataKey size"); + } + + SQLiteUtils::rekeyDatabase( + SQLiteQueryExecutor::getConnection(), backupDataKey); + + CommSecureStore::set(CommSecureStore::backupDataKey, backupDataKey); + SQLiteQueryExecutor::backupDataKey = backupDataKey; + + CommSecureStore::set(CommSecureStore::backupLogDataKey, backupLogDataKey); + SQLiteQueryExecutor::backupLogDataKey = backupLogDataKey; +} + } // namespace comm diff --git a/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h --- a/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/DatabaseQueryExecutor.h @@ -203,9 +203,6 @@ virtual void createMainCompaction(std::string backupID) const = 0; virtual void captureBackupLogs() const = 0; virtual void triggerBackupFileUpload() const = 0; - virtual void setUserDataKeys( - const std::string &backupDataKey, - const std::string &backupLogDataKey) const = 0; #endif }; diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h @@ -51,10 +51,6 @@ static WebSQLiteConnectionManager connectionManager; #endif - // Constant key sizes - static int backupDataKeySize; - static int backupLogDataKeySize; - SQLiteQueryExecutor(); ~SQLiteQueryExecutor(); SQLiteQueryExecutor(std::string sqliteFilePath); @@ -225,9 +221,6 @@ void createMainCompaction(std::string backupID) const override; void captureBackupLogs() const override; void triggerBackupFileUpload() const override; - void setUserDataKeys( - const std::string &backupDataKey, - const std::string &backupLogDataKey) const override; #endif }; diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp @@ -23,7 +23,6 @@ #ifndef EMSCRIPTEN #include "../CryptoTools/CryptoModule.h" #include "../Tools/ServicesUtils.h" -#include "CommSecureStore.h" #include "PlatformSpecificTools.h" #include "StaffUtils.h" #include "lib.rs.h" @@ -37,10 +36,8 @@ std::string SQLiteQueryExecutor::sqliteFilePath; std::string SQLiteQueryExecutor::backupDataKey; -int SQLiteQueryExecutor::backupDataKeySize = 64; std::string SQLiteQueryExecutor::backupLogDataKey; -int SQLiteQueryExecutor::backupLogDataKeySize = 32; #ifndef EMSCRIPTEN NativeSQLiteConnectionManager SQLiteQueryExecutor::connectionManager; @@ -1832,34 +1829,6 @@ ::triggerBackupFileUpload(); } -void SQLiteQueryExecutor::setUserDataKeys( - const std::string &backupDataKey, - const std::string &backupLogDataKey) const { - if (SQLiteQueryExecutor::backupDataKey.empty()) { - throw std::runtime_error("backupDataKey is not set"); - } - - if (SQLiteQueryExecutor::backupLogDataKey.empty()) { - throw std::runtime_error("invalid backupLogDataKey size"); - } - - if (backupDataKey.size() != SQLiteQueryExecutor::backupDataKeySize) { - throw std::runtime_error("invalid backupDataKey size"); - } - - if (backupLogDataKey.size() != SQLiteQueryExecutor::backupLogDataKeySize) { - throw std::runtime_error("invalid backupLogDataKey size"); - } - - SQLiteUtils::rekeyDatabase( - SQLiteQueryExecutor::getConnection(), backupDataKey); - - CommSecureStore::set(CommSecureStore::backupDataKey, backupDataKey); - SQLiteQueryExecutor::backupDataKey = backupDataKey; - - CommSecureStore::set(CommSecureStore::backupLogDataKey, backupLogDataKey); - SQLiteQueryExecutor::backupLogDataKey = backupLogDataKey; -} #endif void SQLiteQueryExecutor::copyTablesDataUsingAttach( diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp @@ -2657,7 +2657,7 @@ taskType job = [=]() { std::string error; try { - DatabaseManager::getQueryExecutor().setUserDataKeys( + DatabaseManager::setUserDataKeys( backupDataKeyCpp, backupLogDataKeyCpp); } catch (std::system_error &e) { error = e.what();