Skip to content

Commit

Permalink
feat: use case to change cipher_profile [WPB-14826] (#3163)
Browse files Browse the repository at this point in the history
* feat: use case to change cipher_profile [WPB-14826]

* change cipher_profile value to be platform specific

* fix detekt
  • Loading branch information
saleniuk authored Dec 11, 2024
1 parent abcd037 commit 0667f9b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,7 @@ class UserSessionScope internal constructor(
legalHoldHandler,
notificationTokenRepository,
this,
userStorage,
userScopedLogger,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.feature.debug

import com.wire.kalium.logic.di.UserStorage

class ChangeProfilingUseCase(
private val userStorage: UserStorage,
) {
/**
* Changes the profiling of the database (cipher_profile) if the profile is specified and the database is encrypted
* @param enabled true to enable profiling, false to disable
*/
operator fun invoke(enabled: Boolean) {
userStorage.database.changeProfiling(enabled)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.wire.kalium.logic.data.prekey.PreKeyRepository
import com.wire.kalium.logic.data.sync.SlowSyncRepository
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.di.UserStorage
import com.wire.kalium.logic.feature.message.MLSMessageCreator
import com.wire.kalium.logic.feature.message.MLSMessageCreatorImpl
import com.wire.kalium.logic.feature.message.MessageEnvelopeCreator
Expand Down Expand Up @@ -92,6 +93,7 @@ class DebugScope internal constructor(
private val legalHoldHandler: LegalHoldHandler,
private val notificationTokenRepository: NotificationTokenRepository,
private val scope: CoroutineScope,
userStorage: UserStorage,
logger: KaliumLogger,
internal val dispatcher: KaliumDispatcher = KaliumDispatcherImpl,
) {
Expand Down Expand Up @@ -224,4 +226,6 @@ class DebugScope internal constructor(
clientRepository,
notificationTokenRepository,
)

val changeProfiling: ChangeProfilingUseCase = ChangeProfilingUseCase(userStorage)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ actual fun userDatabaseBuilder(
) {
isWALEnabled = enableWAL
}
return UserDatabaseBuilder(userId, driver, dispatcher, platformDatabaseData, isEncryptionEnabled)
return UserDatabaseBuilder(
userId = userId,
sqlDriver = driver,
dispatcher = dispatcher,
platformDatabaseData = platformDatabaseData,
isEncrypted = isEncryptionEnabled,
cipherProfile = "logcat",
)
}

actual fun userDatabaseDriverByPath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ internal expect fun userDatabaseDriverByPath(
enableWAL: Boolean
): SqlDriver

@Suppress("LongParameterList")
class UserDatabaseBuilder internal constructor(
private val userId: UserIDEntity,
internal val sqlDriver: SqlDriver,
dispatcher: CoroutineDispatcher,
private val platformDatabaseData: PlatformDatabaseData,
private val isEncrypted: Boolean,
private val queriesContext: CoroutineContext = KaliumDispatcherImpl.io
private val queriesContext: CoroutineContext = KaliumDispatcherImpl.io,
private val cipherProfile: String? = null,
) {

internal val database: UserDatabase = UserDatabase(
Expand Down Expand Up @@ -316,6 +318,25 @@ class UserDatabaseBuilder internal constructor(
*/
fun dbFileLocation(): String? = getDatabaseAbsoluteFileLocation(platformDatabaseData, userId)

/**
* Changes the profiling of the database (cipher_profile) if the profile is specified and the database is encrypted
* @param enabled true to enable profiling, false to disable
*/
fun changeProfiling(enabled: Boolean) {
if (isEncrypted && cipherProfile != null) {
val cipherProfileValue = if (enabled) cipherProfile else "off"
sqlDriver.executeQuery(
identifier = null,
sql = "PRAGMA cipher_profile='$cipherProfileValue'",
mapper = {
it.next()
it.getLong(0).let { QueryResult.Value<Long?>(it) }
},
parameters = 0,
)
}
}

/**
* drops DB connection and delete the DB file
*/
Expand Down

0 comments on commit 0667f9b

Please sign in to comment.