-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support Proteus federation if MLS not supported by backend (WPB-14250) #3126
Merged
MohamadJaara
merged 6 commits into
release/candidate
from
feat/support-proteus-federation-if-mls-disabled
Nov 27, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
28cc966
chore: use case template
yamilmedina 70e0a0e
feat: usecase for allowing federated search
yamilmedina 12895f6
feat: add usecase to check for federation search allowance
yamilmedina c96a33b
feat: cleanup
yamilmedina 1d465b5
feat: adjust tests to new implementation
yamilmedina 3efc683
feat: adjust tests to new implementation
yamilmedina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...ommonMain/kotlin/com/wire/kalium/logic/feature/search/IsFederationSearchAllowedUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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.search | ||
|
||
import com.wire.kalium.logic.data.conversation.Conversation | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.mls.MLSPublicKeys | ||
import com.wire.kalium.logic.data.mlspublickeys.MLSPublicKeysRepository | ||
import com.wire.kalium.logic.data.user.SupportedProtocol | ||
import com.wire.kalium.logic.feature.conversation.GetConversationProtocolInfoUseCase | ||
import com.wire.kalium.logic.feature.user.GetDefaultProtocolUseCase | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.util.KaliumDispatcher | ||
import com.wire.kalium.util.KaliumDispatcherImpl | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* Check if FederatedSearchIsAllowed according to MLS configuration of the backend | ||
* and the conversation protocol if a [ConversationId] is provided. | ||
*/ | ||
interface IsFederationSearchAllowedUseCase { | ||
suspend operator fun invoke(conversationId: ConversationId?): Boolean | ||
} | ||
|
||
@Suppress("FunctionNaming") | ||
internal fun IsFederationSearchAllowedUseCase( | ||
mlsPublicKeysRepository: MLSPublicKeysRepository, | ||
getDefaultProtocol: GetDefaultProtocolUseCase, | ||
getConversationProtocolInfo: GetConversationProtocolInfoUseCase, | ||
dispatcher: KaliumDispatcher = KaliumDispatcherImpl | ||
) = object : IsFederationSearchAllowedUseCase { | ||
|
||
override suspend operator fun invoke(conversationId: ConversationId?): Boolean = withContext(dispatcher.io) { | ||
val isMlsConfiguredForBackend = hasMLSKeysConfiguredForBackend() | ||
when (isMlsConfiguredForBackend) { | ||
true -> isConversationProtocolAbleToFederate(conversationId) | ||
false -> true | ||
} | ||
} | ||
|
||
private suspend fun hasMLSKeysConfiguredForBackend(): Boolean { | ||
return when (val mlsKeysResult = mlsPublicKeysRepository.getKeys()) { | ||
is Either.Left -> false | ||
is Either.Right -> { | ||
val mlsKeys: MLSPublicKeys = mlsKeysResult.value | ||
mlsKeys.removal != null && mlsKeys.removal?.isNotEmpty() == true | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* MLS is enabled, then we need to check if the protocol for the conversation is able to federate. | ||
*/ | ||
private suspend fun isConversationProtocolAbleToFederate(conversationId: ConversationId?): Boolean { | ||
val isProteusTeam = getDefaultProtocol() == SupportedProtocol.PROTEUS | ||
val isOtherDomainAllowed: Boolean = conversationId?.let { | ||
when (val result = getConversationProtocolInfo(it)) { | ||
is GetConversationProtocolInfoUseCase.Result.Failure -> !isProteusTeam | ||
|
||
is GetConversationProtocolInfoUseCase.Result.Success -> | ||
!isProteusTeam && result.protocolInfo !is Conversation.ProtocolInfo.Proteus | ||
} | ||
} ?: !isProteusTeam | ||
return isOtherDomainAllowed | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
...nTest/kotlin/com/wire/kalium/logic/feature/search/IsFederationSearchAllowedUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* 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.search | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.data.mls.MLSPublicKeys | ||
import com.wire.kalium.logic.data.mlspublickeys.MLSPublicKeysRepository | ||
import com.wire.kalium.logic.data.user.SupportedProtocol | ||
import com.wire.kalium.logic.feature.conversation.GetConversationProtocolInfoUseCase | ||
import com.wire.kalium.logic.feature.user.GetDefaultProtocolUseCase | ||
import com.wire.kalium.logic.framework.TestConversation | ||
import com.wire.kalium.logic.framework.TestConversation.PROTEUS_PROTOCOL_INFO | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.util.KaliumDispatcherImpl | ||
import io.mockative.Mock | ||
import io.mockative.any | ||
import io.mockative.coEvery | ||
import io.mockative.coVerify | ||
import io.mockative.every | ||
import io.mockative.mock | ||
import io.mockative.once | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class IsFederationSearchAllowedUseCaseTest { | ||
|
||
@Test | ||
fun givenMLSIsNotConfigured_whenInvokingIsFederationSearchAllowed_thenReturnTrue() = runTest { | ||
val (arrangement, isFederationSearchAllowedUseCase) = Arrangement() | ||
.withMLSConfiguredForBackend(isConfigured = false) | ||
.arrange() | ||
|
||
val isAllowed = isFederationSearchAllowedUseCase(conversationId = null) | ||
|
||
assertEquals(true, isAllowed) | ||
coVerify { arrangement.mlsPublicKeysRepository.getKeys() }.wasInvoked(once) | ||
coVerify { arrangement.getDefaultProtocol.invoke() }.wasNotInvoked() | ||
coVerify { arrangement.getConversationProtocolInfo.invoke(any()) }.wasNotInvoked() | ||
} | ||
|
||
@Test | ||
fun givenMLSIsConfiguredAndAMLSTeamWithEmptyKeys_whenInvokingIsFederationSearchAllowed_thenReturnTrue() = runTest { | ||
val (arrangement, isFederationSearchAllowedUseCase) = Arrangement() | ||
.withEmptyMlsKeys() | ||
.arrange() | ||
|
||
val isAllowed = isFederationSearchAllowedUseCase(conversationId = null) | ||
|
||
assertEquals(true, isAllowed) | ||
coVerify { arrangement.mlsPublicKeysRepository.getKeys() }.wasInvoked(once) | ||
coVerify { arrangement.getDefaultProtocol.invoke() }.wasNotInvoked() | ||
coVerify { arrangement.getConversationProtocolInfo.invoke(any()) }.wasNotInvoked() | ||
} | ||
|
||
@Test | ||
fun givenMLSIsConfiguredAndAMLSTeam_whenInvokingIsFederationSearchAllowed_thenReturnTrue() = runTest { | ||
val (arrangement, isFederationSearchAllowedUseCase) = Arrangement() | ||
.withMLSConfiguredForBackend(isConfigured = true) | ||
.withDefaultProtocol(SupportedProtocol.MLS) | ||
.arrange() | ||
|
||
val isAllowed = isFederationSearchAllowedUseCase(conversationId = null) | ||
|
||
assertEquals(true, isAllowed) | ||
coVerify { arrangement.mlsPublicKeysRepository.getKeys() }.wasInvoked(once) | ||
coVerify { arrangement.getDefaultProtocol.invoke() }.wasInvoked(once) | ||
coVerify { arrangement.getConversationProtocolInfo.invoke(any()) }.wasNotInvoked() | ||
} | ||
|
||
@Test | ||
fun givenMLSIsConfiguredAndAMLSTeamAndProteusProtocol_whenInvokingIsFederationSearchAllowed_thenReturnFalse() = runTest { | ||
val (arrangement, isFederationSearchAllowedUseCase) = Arrangement() | ||
.withMLSConfiguredForBackend(isConfigured = true) | ||
.withDefaultProtocol(SupportedProtocol.MLS) | ||
.withConversationProtocolInfo(GetConversationProtocolInfoUseCase.Result.Success(PROTEUS_PROTOCOL_INFO)) | ||
.arrange() | ||
|
||
val isAllowed = isFederationSearchAllowedUseCase(conversationId = TestConversation.ID) | ||
|
||
assertEquals(false, isAllowed) | ||
coVerify { arrangement.mlsPublicKeysRepository.getKeys() }.wasInvoked(once) | ||
coVerify { arrangement.getDefaultProtocol.invoke() }.wasInvoked(once) | ||
coVerify { arrangement.getConversationProtocolInfo.invoke(any()) }.wasInvoked(once) | ||
} | ||
|
||
@Test | ||
fun givenMLSIsConfiguredAndAProteusTeamAndProteusProtocol_whenInvokingIsFederationSearchAllowed_thenReturnFalse() = runTest { | ||
val (arrangement, isFederationSearchAllowedUseCase) = Arrangement() | ||
.withMLSConfiguredForBackend(isConfigured = true) | ||
.withDefaultProtocol(SupportedProtocol.PROTEUS) | ||
.withConversationProtocolInfo(GetConversationProtocolInfoUseCase.Result.Success(PROTEUS_PROTOCOL_INFO)) | ||
.arrange() | ||
|
||
val isAllowed = isFederationSearchAllowedUseCase(conversationId = TestConversation.ID) | ||
|
||
assertEquals(false, isAllowed) | ||
coVerify { arrangement.mlsPublicKeysRepository.getKeys() }.wasInvoked(once) | ||
coVerify { arrangement.getDefaultProtocol.invoke() }.wasInvoked(once) | ||
coVerify { arrangement.getConversationProtocolInfo.invoke(any()) }.wasInvoked(once) | ||
} | ||
|
||
private class Arrangement { | ||
|
||
@Mock | ||
val mlsPublicKeysRepository = mock(MLSPublicKeysRepository::class) | ||
|
||
@Mock | ||
val getDefaultProtocol = mock(GetDefaultProtocolUseCase::class) | ||
|
||
@Mock | ||
val getConversationProtocolInfo = mock(GetConversationProtocolInfoUseCase::class) | ||
|
||
private val MLS_PUBLIC_KEY = MLSPublicKeys( | ||
removal = mapOf( | ||
"ed25519" to "gRNvFYReriXbzsGu7zXiPtS8kaTvhU1gUJEV9rdFHVw=" | ||
) | ||
) | ||
|
||
fun withDefaultProtocol(protocol: SupportedProtocol) = apply { | ||
every { getDefaultProtocol.invoke() }.returns(protocol) | ||
} | ||
|
||
suspend fun withConversationProtocolInfo(protocolInfo: GetConversationProtocolInfoUseCase.Result) = apply { | ||
coEvery { getConversationProtocolInfo(any()) }.returns(protocolInfo) | ||
} | ||
|
||
suspend fun withMLSConfiguredForBackend(isConfigured: Boolean = true) = apply { | ||
coEvery { mlsPublicKeysRepository.getKeys() }.returns( | ||
if (isConfigured) { | ||
Either.Right(MLS_PUBLIC_KEY) | ||
} else { | ||
Either.Left(CoreFailure.Unknown(RuntimeException("MLS is not configured"))) | ||
} | ||
) | ||
} | ||
|
||
suspend fun withEmptyMlsKeys() = apply { | ||
coEvery { mlsPublicKeysRepository.getKeys() }.returns(Either.Right(MLSPublicKeys(emptyMap()))) | ||
} | ||
|
||
fun arrange() = this to IsFederationSearchAllowedUseCase( | ||
mlsPublicKeysRepository = mlsPublicKeysRepository, | ||
getDefaultProtocol = getDefaultProtocol, | ||
getConversationProtocolInfo = getConversationProtocolInfo, | ||
dispatcher = KaliumDispatcherImpl | ||
) | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Logic migrated as-is to preserve retro compatibility with, plus added tests for this here too