Skip to content

Commit

Permalink
[CommCoreModule][native_rust_library] move constants to one place
Browse files Browse the repository at this point in the history
Summary:
Part of [ENG-9656](https://linear.app/comm/issue/ENG-9656/native-changes-for-minimal-version-of-backup)

I'm not sure why it was previously defined in two places. It makes it hard to maintain, so I'm cleaning this up. I added the missing comment.

Depends on D13937

Test Plan: Tested creating backup and secondary device login

Reviewers: bartek, tomek

Reviewed By: bartek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D14000
  • Loading branch information
xsanm committed Nov 25, 2024
1 parent 45497af commit 5a5a75a
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 15 deletions.
16 changes: 4 additions & 12 deletions native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ std::string SQLiteQueryExecutor::sqliteFilePath;
std::string SQLiteQueryExecutor::encryptionKey;
std::once_flag SQLiteQueryExecutor::initialized;
int SQLiteQueryExecutor::sqlcipherEncryptionKeySize = 64;
// Should match constant defined in `native_rust_library/src/constants.rs`
std::string SQLiteQueryExecutor::secureStoreEncryptionKeyID =
"comm.encryptionKey";
int SQLiteQueryExecutor::backupLogsEncryptionKeySize = 32;
std::string SQLiteQueryExecutor::secureStoreBackupLogsEncryptionKeyID =
"comm.backupLogsEncryptionKey";
std::string SQLiteQueryExecutor::backupLogsEncryptionKey;

#ifndef EMSCRIPTEN
Expand Down Expand Up @@ -2977,10 +2972,9 @@ void SQLiteQueryExecutor::initialize(std::string &databasePath) {
std::call_once(SQLiteQueryExecutor::initialized, [&databasePath]() {
SQLiteQueryExecutor::sqliteFilePath = databasePath;
folly::Optional<std::string> maybeEncryptionKey =
CommSecureStore::get(SQLiteQueryExecutor::secureStoreEncryptionKeyID);
CommSecureStore::get(CommSecureStore::encryptionKey);
folly::Optional<std::string> maybeBackupLogsEncryptionKey =
CommSecureStore::get(
SQLiteQueryExecutor::secureStoreBackupLogsEncryptionKeyID);
CommSecureStore::get(CommSecureStore::backupLogsEncryptionKey);

if (file_exists(databasePath) && maybeEncryptionKey &&
maybeBackupLogsEncryptionKey) {
Expand Down Expand Up @@ -3135,8 +3129,7 @@ void SQLiteQueryExecutor::createMainCompaction(std::string backupID) const {
void SQLiteQueryExecutor::generateFreshEncryptionKey() {
std::string encryptionKey = comm::crypto::Tools::generateRandomHexString(
SQLiteQueryExecutor::sqlcipherEncryptionKeySize);
CommSecureStore::set(
SQLiteQueryExecutor::secureStoreEncryptionKeyID, encryptionKey);
CommSecureStore::set(CommSecureStore::encryptionKey, encryptionKey);
SQLiteQueryExecutor::encryptionKey = encryptionKey;
SQLiteQueryExecutor::generateFreshBackupLogsEncryptionKey();
}
Expand All @@ -3146,8 +3139,7 @@ void SQLiteQueryExecutor::generateFreshBackupLogsEncryptionKey() {
comm::crypto::Tools::generateRandomHexString(
SQLiteQueryExecutor::backupLogsEncryptionKeySize);
CommSecureStore::set(
SQLiteQueryExecutor::secureStoreBackupLogsEncryptionKeyID,
backupLogsEncryptionKey);
CommSecureStore::backupLogsEncryptionKey, backupLogsEncryptionKey);
SQLiteQueryExecutor::backupLogsEncryptionKey = backupLogsEncryptionKey;
}

Expand Down
2 changes: 0 additions & 2 deletions native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class SQLiteQueryExecutor : public DatabaseQueryExecutor {

static std::once_flag initialized;
static int sqlcipherEncryptionKeySize;
static std::string secureStoreEncryptionKeyID;
static int backupLogsEncryptionKeySize;
static std::string secureStoreBackupLogsEncryptionKeyID;
static std::string backupLogsEncryptionKey;

#ifndef EMSCRIPTEN
Expand Down
4 changes: 4 additions & 0 deletions native/cpp/CommonCpp/Tools/CommSecureStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ class CommSecureStore {
public:
static void set(const std::string key, const std::string value);
static folly::Optional<std::string> get(const std::string key);
// Should match constant defined in `native_rust_library/src/constants.rs`
inline static const std::string commServicesAccessToken = "accessToken";
inline static const std::string userID = "userID";
inline static const std::string deviceID = "deviceID";
inline static const std::string encryptionKey = "comm.encryptionKey";
inline static const std::string backupLogsEncryptionKey =
"comm.backupLogsEncryptionKey";
};

} // namespace comm
1 change: 0 additions & 1 deletion native/native_rust_library/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub mod secure_store {
pub const COMM_SERVICES_ACCESS_TOKEN: &str = "accessToken";
pub const USER_ID: &str = "userID";
pub const DEVICE_ID: &str = "deviceID";
/// Should match constant defined in `SQLiteQueryExecutor.h`
pub const SECURE_STORE_ENCRYPTION_KEY_ID: &str = "comm.encryptionKey";
pub const SECURE_STORE_BACKUP_LOGS_ENCRYPTION_KEY_ID: &str =
"comm.backupLogsEncryptionKey";
Expand Down
Binary file modified web/shared-worker/_generated/comm_query_executor.wasm
Binary file not shown.

0 comments on commit 5a5a75a

Please sign in to comment.