Skip to content

Commit

Permalink
Force use dedicated url for ddi-client
Browse files Browse the repository at this point in the history
Following the modifications to the OkHttp request behaviors
 in commit 3357972, to differentiate requests between the old and new
 service, the server URL of the ddi-client is adjusted to include the
 "ddi" subdomain.
It is recommended to utilize the new ddi URLs in the format
"ddi.<tier>.updatefactory.io" for the service configuration.

Signed-off-by: Saeed Rezaee <saeed.rezaee@kynetics.it>
  • Loading branch information
SaeedRe committed May 7, 2024
1 parent 022448b commit 5ba2809
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import com.kynetics.uf.android.update.system.OtaUpdater
import com.kynetics.uf.ddiclient.HaraClientFactory
import com.kynetics.uf.ddiclient.TargetTokenFoundListener
import org.eclipse.hara.ddiclient.api.*
import org.jetbrains.annotations.TestOnly
import java.io.File
import java.net.URI
import java.util.*

data class ConfigurationHandler (
Expand Down Expand Up @@ -93,10 +95,12 @@ data class ConfigurationHandler (

fun getCurrentConfiguration(): UFServiceConfigurationV2 {
return with(sharedPreferences){
val url = getString(keys.sharedPreferencesServerUrlKey, "")!!
val newUrl = addDDISubdomainToTheUrl(url)
UFServiceConfigurationV2(
tenant = getString(keys.sharedPreferencesTenantKey, "")!!,
controllerId = getString(keys.sharedPreferencesControllerIdKey, "")!!,
url = getString(keys.sharedPreferencesServerUrlKey, "")!!,
url = newUrl,
targetToken = getTargetToken(),
gatewayToken = getString(keys.sharedPreferencesGatewayToken, "")!!,
isApiMode = getBoolean(keys.sharedPreferencesApiModeKey, true),
Expand Down Expand Up @@ -175,7 +179,7 @@ data class ConfigurationHandler (
): HaraClient {
return if(isUpdateFactoryServe){
HaraClientFactory.newUFClient(
toClientData(),
toClientData().addDDISubdomainToTheUrl(),
object : DirectoryForArtifactsProvider {
override fun directoryForArtifacts(): File = currentUpdateState.rootDir()
},
Expand Down Expand Up @@ -228,5 +232,37 @@ data class ConfigurationHandler (
ALWAYS
)!!, getString(keys.sharedPreferencesTimeWindowsDuration, "$DEFAULT_WINDOW_DURATION")!!.toLong())
}

private fun HaraClientData.addDDISubdomainToTheUrl(): HaraClientData {
val newUri = addDDISubdomainToTheUrl(serverUrl)
return HaraClientData(tenant, controllerId, newUri, gatewayToken, targetToken)
}

private fun addDDISubdomainToTheUrl(url: String): String {
if (url.isEmpty()) return url
runCatching {
val uri = URI.create(url)
val urlHost = uri.host
val regex = """^(stage|personal|business)\.((prod|test)\.)?updatefactory\.io${'$'}""".toRegex()

if (!urlHost.startsWith("ddi.") && regex.find(urlHost) != null) {
val temp = urlHost.replace(regex, "ddi.\$0")
URI(uri.scheme, uri.userInfo, temp,
uri.port, uri.path, uri.query, uri.fragment).toString()
} else {
url
}
}.onFailure {
Log.e(TAG, "Error adding DDI subdomain to the url", it)
}.onSuccess {
return it
}
return url
}

@TestOnly
internal fun testConfigurationUrlUpdate(configData: HaraClientData): HaraClientData {
return configData.addDDISubdomainToTheUrl()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.kynetics.uf.android.configuration

import org.eclipse.hara.ddiclient.api.HaraClientData
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class ConfigurationHandlerTest {

private val tenant = "tenant"
private val controllerId = "controllerId"
private val gatewayToken = "gatewayToken"

private val clientData = HaraClientData(
tenant = tenant,
controllerId = controllerId,
serverUrl = "https://stage.updatefactory.io",
gatewayToken = gatewayToken
)

private val urlsThatShouldChangeMap = mapOf(
"https://stage.updatefactory.io" to "https://ddi.stage.updatefactory.io",
"https://personal.updatefactory.io" to "https://ddi.personal.updatefactory.io",
"https://business.updatefactory.io" to "https://ddi.business.updatefactory.io",
"https://stage.prod.updatefactory.io" to "https://ddi.stage.prod.updatefactory.io",
"https://personal.prod.updatefactory.io" to "https://ddi.personal.prod.updatefactory.io",
"https://business.prod.updatefactory.io" to "https://ddi.business.prod.updatefactory.io",
"https://stage.test.updatefactory.io" to "https://ddi.stage.test.updatefactory.io",
"https://personal.test.updatefactory.io" to "https://ddi.personal.test.updatefactory.io",
"https://business.test.updatefactory.io" to "https://ddi.business.test.updatefactory.io",
)

private val urlsThatShouldNotChange = listOf(
"https://corporate.debug.updatefactory.io",
"https://ddi.business.prod.updatefactory.io",
"https://ddi.business.updatefactory.io",
"https://cdn.business.updatefactory.io",
"https://hawkbit.eclipseprojects.io",
)

@Test
fun testTheUrlsThatShouldChange() {
urlsThatShouldChangeMap.forEach { (oldUrl, newUrl) ->
clientData.copy(
serverUrl = oldUrl
).apply {
assertConfigurationUrlChangeTo(newUrl)
}
}
}

@Test
fun testTheUrlsThatShouldStayTheSame() {
urlsThatShouldNotChange.forEach { url ->
assertConfigurationUrlStayTheSame(url)
}
}

private fun HaraClientData.assertConfigurationUrlChangeTo(expectedUrl: String) {
val newClientData = ConfigurationHandler.testConfigurationUrlUpdate(this)
Assert.assertEquals(expectedUrl, newClientData.serverUrl)
}

private fun assertConfigurationUrlStayTheSame(serverUrl: String) {
clientData.copy(
serverUrl = serverUrl
).apply {
assertConfigurationUrlChangeTo(serverUrl)
}
}
}

0 comments on commit 5ba2809

Please sign in to comment.