-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #991 from DataDog/xgouchet/RUMM-2327/rotate_ip_dns
RUMM-2327 add internal DNS resolver
- Loading branch information
Showing
5 changed files
with
204 additions
and
3 deletions.
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
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
69 changes: 69 additions & 0 deletions
69
dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/net/RotatingDnsResolver.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,69 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.core.internal.net | ||
|
||
import java.net.InetAddress | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.minutes | ||
import kotlin.time.Duration.Companion.nanoseconds | ||
import okhttp3.Dns | ||
|
||
internal class RotatingDnsResolver( | ||
private val delegate: Dns = Dns.SYSTEM, | ||
private val ttl: Duration = TTL_30_MIN | ||
) : Dns { | ||
|
||
data class ResolvedHost( | ||
val hostname: String, | ||
val addresses: MutableList<InetAddress> | ||
) { | ||
private val resolutionTimestamp: Long = System.nanoTime() | ||
|
||
fun getAge(): Duration { | ||
return (System.nanoTime() - resolutionTimestamp).nanoseconds | ||
} | ||
|
||
fun rotate() { | ||
val first = addresses.removeFirstOrNull() | ||
if (first != null) { | ||
addresses.add(first) | ||
} | ||
} | ||
} | ||
|
||
private val knownHosts = mutableMapOf<String, ResolvedHost>() | ||
|
||
// region Dns | ||
|
||
override fun lookup(hostname: String): MutableList<InetAddress> { | ||
val knownHost = knownHosts[hostname] | ||
|
||
return if (knownHost != null && isValid(knownHost)) { | ||
knownHost.rotate() | ||
knownHost.addresses.toMutableList() | ||
} else { | ||
@Suppress("UnsafeThirdPartyFunctionCall") // handled by caller | ||
val result = delegate.lookup(hostname) | ||
knownHosts[hostname] = ResolvedHost(hostname, result.toMutableList()) | ||
result | ||
} | ||
} | ||
|
||
// endregion | ||
|
||
// region Internal | ||
|
||
private fun isValid(knownHost: ResolvedHost): Boolean { | ||
return knownHost.getAge() < ttl && knownHost.addresses.isNotEmpty() | ||
} | ||
|
||
// endregion | ||
|
||
companion object { | ||
val TTL_30_MIN: Duration = 30.minutes | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
...-android/src/test/kotlin/com/datadog/android/core/internal/net/RotatingDnsResolverTest.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,117 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.core.internal.net | ||
|
||
import com.datadog.android.utils.forge.Configurator | ||
import com.nhaarman.mockitokotlin2.doReturn | ||
import com.nhaarman.mockitokotlin2.mock | ||
import com.nhaarman.mockitokotlin2.whenever | ||
import fr.xgouchet.elmyr.Forge | ||
import fr.xgouchet.elmyr.annotation.StringForgery | ||
import fr.xgouchet.elmyr.junit5.ForgeConfiguration | ||
import fr.xgouchet.elmyr.junit5.ForgeExtension | ||
import java.net.InetAddress | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import okhttp3.Dns | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.junit.jupiter.api.extension.Extensions | ||
import org.mockito.Mock | ||
import org.mockito.junit.jupiter.MockitoExtension | ||
import org.mockito.junit.jupiter.MockitoSettings | ||
import org.mockito.quality.Strictness | ||
|
||
@Extensions( | ||
ExtendWith(MockitoExtension::class), | ||
ExtendWith(ForgeExtension::class) | ||
) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
@ForgeConfiguration(Configurator::class) | ||
internal class RotatingDnsResolverTest { | ||
|
||
lateinit var testedDns: Dns | ||
|
||
@Mock | ||
lateinit var mockDelegate: Dns | ||
|
||
@StringForgery | ||
lateinit var fakeHostname: String | ||
|
||
lateinit var fakeInetAddresses: List<InetAddress> | ||
|
||
@BeforeEach | ||
fun `set up`(forge: Forge) { | ||
fakeInetAddresses = forge.aList { mock() } | ||
|
||
testedDns = RotatingDnsResolver(mockDelegate, TEST_TTL_MS) | ||
} | ||
|
||
@Test | ||
fun `𝕄 return delegate result 𝕎 lookup {unknown hostname}`() { | ||
// Given | ||
whenever(mockDelegate.lookup(fakeHostname)) doReturn fakeInetAddresses | ||
|
||
// When | ||
val result = testedDns.lookup(fakeHostname) | ||
|
||
// Then | ||
assertThat(result).containsExactlyElementsOf(fakeInetAddresses) | ||
} | ||
|
||
@Test | ||
fun `𝕄 rotate known result 𝕎 lookup {known hostname}`() { | ||
// Given | ||
whenever(mockDelegate.lookup(fakeHostname)) doReturn fakeInetAddresses | ||
val result = mutableListOf<InetAddress>() | ||
|
||
// When | ||
for (i in fakeInetAddresses.indices) { | ||
result.add(testedDns.lookup(fakeHostname).first()) | ||
} | ||
|
||
// Then | ||
assertThat(result).containsExactlyElementsOf(fakeInetAddresses) | ||
} | ||
|
||
@Test | ||
fun `𝕄 renew result 𝕎 lookup {expired hostname}`( | ||
forge: Forge | ||
) { | ||
// Given | ||
val fakeInetAddresses2: List<InetAddress> = forge.aList { mock() } | ||
whenever(mockDelegate.lookup(fakeHostname)).doReturn(fakeInetAddresses, fakeInetAddresses2) | ||
|
||
// When | ||
val result = testedDns.lookup(fakeHostname) | ||
Thread.sleep(TEST_TTL_MS.inWholeMilliseconds) | ||
val result2 = testedDns.lookup(fakeHostname) | ||
|
||
// Then | ||
assertThat(result).containsExactlyElementsOf(fakeInetAddresses) | ||
assertThat(result2).containsExactlyElementsOf(fakeInetAddresses2) | ||
} | ||
|
||
@Test | ||
fun `𝕄 renew result 𝕎 lookup {empty hostname}`() { | ||
// Given | ||
whenever(mockDelegate.lookup(fakeHostname)).doReturn(emptyList(), fakeInetAddresses) | ||
|
||
// When | ||
val result = testedDns.lookup(fakeHostname) | ||
val result2 = testedDns.lookup(fakeHostname) | ||
|
||
// Then | ||
assertThat(result).isEmpty() | ||
assertThat(result2).containsExactlyElementsOf(fakeInetAddresses) | ||
} | ||
|
||
companion object { | ||
internal val TEST_TTL_MS = 250.milliseconds | ||
} | ||
} |
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