From d0d11ccd26b083c8343ab04f1c37ccdbc3400559 Mon Sep 17 00:00:00 2001 From: Mohamad Jaara Date: Mon, 19 Feb 2024 17:36:22 +0100 Subject: [PATCH] fix: some end points are not routed through the proxy server v4.5 (#2485) * fix: some end points are not routed through the proxy server (#2400) * fix: some end points are not routed through the proxy server * fix cli * fix android sample * fix test service * detekt * fix ios target * fix tests * refactor: reimplement update api versions usecase to use proxy (#2414) * refactor: implement UpdateApiVersionsUseCaseImpl to be proxy safe * feat: add proxy URL and need auth indication to custom server dialogs * fix tests (cherry picked from commit b1f14574e945e276fe275acce468346d755a97a3) * fix cherry pick issues * detekt * trigger CI * fix import * fix test (cherry picked from commit 5651bb125732c2901157fd225b00e9a42aa5129e) --- .../kalium/logic/feature/UserSessionScope.kt | 2 ++ .../ObserveIfAppUpdateRequiredUseCase.kt | 2 +- .../auth/AddAuthenticatedUserUseCase.kt | 1 + .../logic/feature/auth/AuthenticationScope.kt | 14 +++++++++-- .../logic/feature/auth/DomainLookupUseCase.kt | 10 +++----- .../AutoVersionAuthScopeUseCase.kt | 1 + .../server/ServerConfigForAccountUseCase.kt | 1 + .../server/UpdateApiVersionsUseCase.kt | 1 + .../feature/auth/DomainLookupUseCaseTest.kt | 25 +++++++++---------- .../UnauthenticatedNetworkContainer.kt | 3 +-- .../UnboundNetworkContainer.kt | 6 ++--- .../kotlin/PocIntegrationTest.kt | 2 +- .../testservice/managed/InstanceService.kt | 17 +++++++++---- 13 files changed, 51 insertions(+), 34 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt index a12cecf444f..3fea51b4d32 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt @@ -1680,6 +1680,8 @@ class UserSessionScope internal constructor( messages.sendConfirmation, renamedConversationHandler, qualifiedIdMapper, + team.isSelfATeamMember, + authenticationScope.serverConfigRepository, authenticationScope.serverConfigRepository, userStorage, userPropertyRepository, diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/appVersioning/ObserveIfAppUpdateRequiredUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/appVersioning/ObserveIfAppUpdateRequiredUseCase.kt index 90d1ee0f77f..499aa144921 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/appVersioning/ObserveIfAppUpdateRequiredUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/appVersioning/ObserveIfAppUpdateRequiredUseCase.kt @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ -@file:Suppress("konsist.useCasesShouldNotAccessNetworkLayerDirectly") +@file:Suppress("konsist.useCasesShouldNotAccessNetworkLayerDirectly", "konsist.useCasesShouldNotAccessDaoLayerDirectly") package com.wire.kalium.logic.feature.appVersioning diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/AddAuthenticatedUserUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/AddAuthenticatedUserUseCase.kt index 0ff1d673067..566ba4fecaf 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/AddAuthenticatedUserUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/AddAuthenticatedUserUseCase.kt @@ -15,6 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ +@file:Suppress("konsist.useCasesShouldNotAccessDaoLayerDirectly",) package com.wire.kalium.logic.feature.auth diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/AuthenticationScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/AuthenticationScope.kt index 3dd286a14fb..e7fa684a00f 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/AuthenticationScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/AuthenticationScope.kt @@ -15,13 +15,15 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ -@file:Suppress("konsist.useCasesShouldNotAccessNetworkLayerDirectly") +@file:Suppress("konsist.useCasesShouldNotAccessDaoLayerDirectly", "konsist.useCasesShouldNotAccessNetworkLayerDirectly") package com.wire.kalium.logic.feature.auth import co.touchlab.stately.collections.ConcurrentMutableMap import com.wire.kalium.logic.configuration.appVersioning.AppVersionRepository import com.wire.kalium.logic.configuration.appVersioning.AppVersionRepositoryImpl +import com.wire.kalium.logic.configuration.server.CustomServerConfigDataSource +import com.wire.kalium.logic.configuration.server.CustomServerConfigRepository import com.wire.kalium.logic.configuration.server.ServerConfig import com.wire.kalium.logic.configuration.server.ServerConfigDataSource import com.wire.kalium.logic.configuration.server.ServerConfigRepository @@ -108,6 +110,14 @@ class AuthenticationScope internal constructor( get() = RegisterAccountDataSource( unauthenticatedNetworkContainer.registerApi ) + + private val customServerConfigRepository: CustomServerConfigRepository + get() = CustomServerConfigDataSource( + unauthenticatedNetworkContainer.serverConfigApi, + kaliumConfigs.developmentApiEnabled, + globalDatabase.serverConfigurationDAO + ) + internal val ssoLoginRepository: SSOLoginRepository get() = SSOLoginRepositoryImpl(unauthenticatedNetworkContainer.sso, unauthenticatedNetworkContainer.domainLookupApi) @@ -140,7 +150,7 @@ class AuthenticationScope internal constructor( val domainLookup: DomainLookupUseCase get() = DomainLookupUseCase( - serverConfigApi = unauthenticatedNetworkContainer.serverConfigApi, + serverConfigApi = customServerConfigRepository, ssoLoginRepository = ssoLoginRepository ) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/DomainLookupUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/DomainLookupUseCase.kt index ef2d66f16eb..a0dec3dd306 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/DomainLookupUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/DomainLookupUseCase.kt @@ -18,6 +18,7 @@ package com.wire.kalium.logic.feature.auth import com.wire.kalium.logic.CoreFailure +import com.wire.kalium.logic.configuration.server.CustomServerConfigRepository import com.wire.kalium.logic.configuration.server.ServerConfig import com.wire.kalium.logic.configuration.server.ServerConfigMapper import com.wire.kalium.logic.data.auth.login.SSOLoginRepository @@ -35,9 +36,8 @@ import com.wire.kalium.network.api.base.unbound.configuration.ServerConfigApi * @param email the email to lookup */ class DomainLookupUseCase internal constructor( - private val serverConfigApi: ServerConfigApi, - private val ssoLoginRepository: SSOLoginRepository, - private val serverConfigMapper: ServerConfigMapper = MapperProvider.serverConfigMapper() + private val customServerConfigRepository: CustomServerConfigRepository, + private val ssoLoginRepository: SSOLoginRepository ) { suspend operator fun invoke(email: String): Result { val domain = email.substringAfterLast('@').ifBlank { @@ -46,9 +46,7 @@ class DomainLookupUseCase internal constructor( } return ssoLoginRepository.domainLookup(domain).flatMap { - wrapApiRequest { serverConfigApi.fetchServerConfig(it.configJsonUrl) } - .map { serverConfigMapper.fromDTO(it) } - + customServerConfigRepository.fetchRemoteConfig(it.configJsonUrl) }.fold(Result::Failure, Result::Success) } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/autoVersioningAuth/AutoVersionAuthScopeUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/autoVersioningAuth/AutoVersionAuthScopeUseCase.kt index 1eeed39f4fd..53d0a2de35b 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/autoVersioningAuth/AutoVersionAuthScopeUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/auth/autoVersioningAuth/AutoVersionAuthScopeUseCase.kt @@ -15,6 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ +@file:Suppress("konsist.useCasesShouldNotAccessNetworkLayerDirectly") package com.wire.kalium.logic.feature.auth.autoVersioningAuth diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/server/ServerConfigForAccountUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/server/ServerConfigForAccountUseCase.kt index a776285a231..dbc6018526f 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/server/ServerConfigForAccountUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/server/ServerConfigForAccountUseCase.kt @@ -15,6 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ +@file:Suppress("konsist.useCasesShouldNotAccessDaoLayerDirectly") package com.wire.kalium.logic.feature.server diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/server/UpdateApiVersionsUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/server/UpdateApiVersionsUseCase.kt index 66f7b29f055..0d6eb9dea57 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/server/UpdateApiVersionsUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/server/UpdateApiVersionsUseCase.kt @@ -15,6 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ +@file:Suppress("konsist.useCasesShouldNotAccessDaoLayerDirectly") package com.wire.kalium.logic.feature.server diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/auth/DomainLookupUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/auth/DomainLookupUseCaseTest.kt index 2f7a52efbbc..967ba8fc61c 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/auth/DomainLookupUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/auth/DomainLookupUseCaseTest.kt @@ -18,6 +18,8 @@ package com.wire.kalium.logic.feature.auth import com.wire.kalium.logic.NetworkFailure +import com.wire.kalium.logic.configuration.server.CustomServerConfigRepository +import com.wire.kalium.logic.configuration.server.ServerConfig import com.wire.kalium.logic.data.auth.login.DomainLookupResult import com.wire.kalium.logic.data.auth.login.SSOLoginRepository import com.wire.kalium.logic.di.MapperProvider @@ -41,7 +43,6 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertIs -@OptIn(ExperimentalCoroutinesApi::class) class DomainLookupUseCaseTest { @Test @@ -83,8 +84,8 @@ class DomainLookupUseCaseTest { .arrange() useCases(userEmail) - verify(arrangement.serverConfigApi) - .suspendFunction(arrangement.serverConfigApi::fetchServerConfig) + verify(arrangement.customServerConfigRepository) + .suspendFunction(arrangement.customServerConfigRepository::fetchRemoteConfig) .with(eq("https://wire.com")) .wasInvoked(exactly = once) } @@ -94,9 +95,6 @@ class DomainLookupUseCaseTest { val userEmail = "cool-person@wire.com" val expectedServerLinks = newServerConfig(1).links - val expextedDTO = expectedServerLinks.let { - MapperProvider.serverConfigMapper().toDTO(it) - } val (arrangement, useCases) = Arrangement() .withDomainLookupResult(Either.Right(DomainLookupResult("https://wire.com", "https://wire.com"))) .withFetchServerConfigResult(NetworkResponse.Success(expextedDTO, emptyMap(), 200,)) @@ -112,8 +110,8 @@ class DomainLookupUseCaseTest { .with(eq("wire.com")) .wasInvoked(exactly = once) - verify(arrangement.serverConfigApi) - .suspendFunction(arrangement.serverConfigApi::fetchServerConfig) + verify(arrangement.customServerConfigRepository) + .suspendFunction(arrangement.customServerConfigRepository::fetchRemoteConfig) .with(eq("https://wire.com")) .wasInvoked(exactly = once) } @@ -124,10 +122,10 @@ class DomainLookupUseCaseTest { val ssoLoginRepository: SSOLoginRepository = mock(SSOLoginRepository::class) @Mock - val serverConfigApi: ServerConfigApi = mock(ServerConfigApi::class) + val customServerConfigRepository: CustomServerConfigRepository = mock(CustomServerConfigRepository::class) private val useCases = DomainLookupUseCase( - serverConfigApi, + customServerConfigRepository, ssoLoginRepository ) @@ -138,9 +136,10 @@ class DomainLookupUseCaseTest { .thenReturn(result) } - fun withFetchServerConfigResult(result: NetworkResponse) = apply { - given(serverConfigApi) - .suspendFunction(serverConfigApi::fetchServerConfig) + fun withFetchServerConfigResult(result: + Either) = apply { + given(customServerConfigRepository) + .suspendFunction(customServerConfigRepository::fetchRemoteConfig) .whenInvokedWith(any()) .thenReturn(result) } diff --git a/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/UnauthenticatedNetworkContainer.kt b/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/UnauthenticatedNetworkContainer.kt index 3b69dcd75c6..fe04e9292fa 100644 --- a/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/UnauthenticatedNetworkContainer.kt +++ b/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/UnauthenticatedNetworkContainer.kt @@ -93,8 +93,7 @@ interface UnauthenticatedNetworkContainer { // this is intentional since we should drop support for api v3 // and we default back to v2 - 3 -> UnauthenticatedNetworkContainerV2( - developmentApiEnabled, + 3 -> UnauthenticatedNetworkContainerV2( developmentApiEnabled, networkStateObserver, serverConfigDTO, proxyCredentials = proxyCredentials, diff --git a/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/UnboundNetworkContainer.kt b/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/UnboundNetworkContainer.kt index fb304c47c9d..6f037741fb8 100644 --- a/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/UnboundNetworkContainer.kt +++ b/network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/UnboundNetworkContainer.kt @@ -75,15 +75,13 @@ internal class UnboundClearTextTrafficNetworkClientProviderImpl( class UnboundNetworkContainerCommon( networkStateObserver: NetworkStateObserver, userAgent: String, - private val ignoreSSLCertificates: Boolean, certificatePinning: CertificatePinning, mockEngine: HttpClientEngine? ) : UnboundNetworkContainer, UnboundNetworkClientProvider by UnboundNetworkClientProviderImpl( - networkStateObserver = networkStateObserver, - userAgent = userAgent, + networkStateObserver, + userAgent, engine = mockEngine ?: defaultHttpEngine( - ignoreSSLCertificates = ignoreSSLCertificates, certificatePinning = certificatePinning, proxyCredentials = null, serverConfigDTOApiProxy = null diff --git a/tango-tests/src/integrationTest/kotlin/PocIntegrationTest.kt b/tango-tests/src/integrationTest/kotlin/PocIntegrationTest.kt index 54757cf16fc..43a8a86e996 100644 --- a/tango-tests/src/integrationTest/kotlin/PocIntegrationTest.kt +++ b/tango-tests/src/integrationTest/kotlin/PocIntegrationTest.kt @@ -118,7 +118,7 @@ class PocIntegrationTest { isOnPremises = true, apiProxy = null ) - ).invoke() + ).invoke(null) if (result !is AutoVersionAuthScopeUseCase.Result.Success) { error("Failed getting AuthScope: $result") } diff --git a/testservice/src/main/kotlin/com/wire/kalium/testservice/managed/InstanceService.kt b/testservice/src/main/kotlin/com/wire/kalium/testservice/managed/InstanceService.kt index 0acf8901f07..207bbfa0abf 100644 --- a/testservice/src/main/kotlin/com/wire/kalium/testservice/managed/InstanceService.kt +++ b/testservice/src/main/kotlin/com/wire/kalium/testservice/managed/InstanceService.kt @@ -25,6 +25,7 @@ import com.wire.kalium.logger.KaliumLogger import com.wire.kalium.logic.CoreLogger import com.wire.kalium.logic.CoreLogic import com.wire.kalium.logic.configuration.server.ServerConfig +import com.wire.kalium.logic.data.auth.login.ProxyCredentials import com.wire.kalium.logic.data.client.ClientType import com.wire.kalium.logic.data.client.DeleteClientParam import com.wire.kalium.logic.data.conversation.ClientId @@ -172,7 +173,7 @@ class InstanceService( } } - val loginResult = provideVersionedAuthenticationScope(coreLogic, serverConfig) + val loginResult = provideVersionedAuthenticationScope(coreLogic, serverConfig, null) .login( instanceRequest.email, instanceRequest.password, true, secondFactorVerificationCode = instanceRequest.verificationCode @@ -306,15 +307,21 @@ class InstanceService( // close the stream files.close() } catch (e: IOException) { - log.warn("Instance ${instance.instanceId}: Could not delete directory ${instance.instancePath}: " - + e.message) + log.warn( + "Instance ${instance.instanceId}: Could not delete directory ${instance.instancePath}: " + + e.message + ) } } }, deleteLocalFilesTimeoutInMinutes.toMinutes(), TimeUnit.MINUTES) } - private suspend fun provideVersionedAuthenticationScope(coreLogic: CoreLogic, serverLinks: ServerConfig.Links): AuthenticationScope = - when (val result = coreLogic.versionedAuthenticationScope(serverLinks).invoke()) { + private suspend fun provideVersionedAuthenticationScope( + coreLogic: CoreLogic, + serverLinks: ServerConfig.Links, + proxyCredentials: ProxyCredentials? + ): AuthenticationScope = + when (val result = coreLogic.versionedAuthenticationScope(serverLinks).invoke(proxyCredentials)) { is AutoVersionAuthScopeUseCase.Result.Failure.Generic -> throw WebApplicationException("failed to create authentication scope: ${result.genericFailure}")