From 4a90efe47aede3f9690ec75799633db1f781bc31 Mon Sep 17 00:00:00 2001 From: Oscar Mira Date: Mon, 13 May 2024 19:18:05 +0200 Subject: [PATCH] Remove unused 'tools' module from build-logic --- build-logic/plugins/build.gradle.kts | 1 - build-logic/settings.gradle.kts | 1 - build-logic/tools/build.gradle.kts | 19 ---- .../org/signal/buildtools/StaticIpResolver.kt | 100 ------------------ .../signal/buildtools/StaticIpResolverTest.kt | 59 ----------- 5 files changed, 180 deletions(-) delete mode 100644 build-logic/tools/build.gradle.kts delete mode 100644 build-logic/tools/src/main/java/org/signal/buildtools/StaticIpResolver.kt delete mode 100644 build-logic/tools/src/test/java/org/signal/buildtools/StaticIpResolverTest.kt diff --git a/build-logic/plugins/build.gradle.kts b/build-logic/plugins/build.gradle.kts index 9ab8a496de..7b42299039 100644 --- a/build-logic/plugins/build.gradle.kts +++ b/build-logic/plugins/build.gradle.kts @@ -21,7 +21,6 @@ dependencies { implementation(libs.kotlin.gradle.plugin) implementation(libs.android.library) implementation(libs.android.application) - implementation(project(":tools")) // These allow us to reference the dependency catalog inside of our compiled plugins implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location)) diff --git a/build-logic/settings.gradle.kts b/build-logic/settings.gradle.kts index 08b8672147..5d061ce386 100644 --- a/build-logic/settings.gradle.kts +++ b/build-logic/settings.gradle.kts @@ -18,6 +18,5 @@ dependencyResolutionManagement { rootProject.name = "build-logic" include(":plugins") -include(":tools") apply(from = "../dependencies.gradle.kts") diff --git a/build-logic/tools/build.gradle.kts b/build-logic/tools/build.gradle.kts deleted file mode 100644 index a23ecaa8b9..0000000000 --- a/build-logic/tools/build.gradle.kts +++ /dev/null @@ -1,19 +0,0 @@ -plugins { - id("org.jetbrains.kotlin.jvm") - id("java-library") -} - -val signalJavaVersion: JavaVersion by rootProject.extra - -java { - sourceCompatibility = signalJavaVersion - targetCompatibility = signalJavaVersion -} - -dependencies { - implementation(gradleApi()) - - implementation(libs.dnsjava) - testImplementation(testLibs.junit.junit) - testImplementation(testLibs.mockk) -} diff --git a/build-logic/tools/src/main/java/org/signal/buildtools/StaticIpResolver.kt b/build-logic/tools/src/main/java/org/signal/buildtools/StaticIpResolver.kt deleted file mode 100644 index 6117a40d93..0000000000 --- a/build-logic/tools/src/main/java/org/signal/buildtools/StaticIpResolver.kt +++ /dev/null @@ -1,100 +0,0 @@ -package org.signal.buildtools - -import org.xbill.DNS.ARecord -import org.xbill.DNS.Lookup -import org.xbill.DNS.Record -import org.xbill.DNS.SimpleResolver -import org.xbill.DNS.Type -import java.net.UnknownHostException -import kotlin.streams.toList - -/** - * A tool to resolve hostname to static IPs. - * Feeds into our custom DNS resolver to provide a static IP fallback for our services. - */ -class StaticIpResolver @JvmOverloads constructor( - private val recordFetcher: RecordFetcher = RealRecordFetcher -) { - - /** - * Resolves a hostname to a list of IPs, represented as a Java array declaration. e.g. - * - * ```java - * new String[]{"192.168.1.1", "192.168.1.2"} - * ``` - * - * This is intended to be injected as a BuildConfig. - */ - fun resolveToBuildConfig(hostName: String): String { - val ips: List = resolve(hostName) - val builder = StringBuilder() - - builder.append("new String[]{") - - ips.forEachIndexed { i, ip -> - builder.append("\"").append(ip).append("\"") - - if (i < ips.size - 1) { - builder.append(",") - } - } - - return builder.append("}").toString() - } - - private fun resolve(hostname: String): List { - val ips: MutableSet = mutableSetOf() - - // Run several resolves to mitigate DNS round robin - for (i in 1..10) { - ips.addAll(resolveOnce(hostname)) - } - - return ips.stream().sorted().toList() - } - - private fun resolveOnce(hostName: String): List { - try { - val records = recordFetcher.fetchRecords(hostName) - if (records != null) { - return records - .filter { it.type == Type.A } - .map { it as ARecord } - .map { it.address } - .map { it.hostAddress } - .filterNotNull() - } else { - throw IllegalStateException("Failed to resolve host! Lookup did not return any records.. $hostName") - } - } catch (e: UnknownHostException) { - throw IllegalStateException("Failed to resolve host! $hostName", e) - } - } - - interface RecordFetcher { - fun fetchRecords(hostName: String): Array? - } - - private object RealRecordFetcher : RecordFetcher { - override fun fetchRecords(hostName: String): Array? { - val resolver = SimpleResolver("1.1.1.1") - - resolver.setTCP(true) - - val lookup: Lookup = doLookup(hostName) - - lookup.setResolver(resolver) - - return lookup.run() - } - - @Throws(UnknownHostException::class) - private fun doLookup(hostname: String): Lookup { - try { - return Lookup(hostname) - } catch (e: Throwable) { - throw UnknownHostException() - } - } - } -} diff --git a/build-logic/tools/src/test/java/org/signal/buildtools/StaticIpResolverTest.kt b/build-logic/tools/src/test/java/org/signal/buildtools/StaticIpResolverTest.kt deleted file mode 100644 index f52d9df389..0000000000 --- a/build-logic/tools/src/test/java/org/signal/buildtools/StaticIpResolverTest.kt +++ /dev/null @@ -1,59 +0,0 @@ -package org.signal.buildtools - -import io.mockk.every -import io.mockk.mockk -import org.junit.Assert.assertEquals -import org.junit.Test -import org.xbill.DNS.ARecord -import org.xbill.DNS.DClass -import org.xbill.DNS.Name -import org.xbill.DNS.Record -import java.net.Inet4Address - -class StaticIpResolverTest { - - companion object { - const val SIGNAL_DOT_ORG = "www.signal.org" - val SIGNAL_IP = byteArrayOf(123, 45, 67, 89) - val STRINGIFIED_IP = SIGNAL_IP.joinToString(".") - } - - @Test - fun `Given a hostname with records, when I resolveToBuildConfig, then I expect a matching IP`() { - val staticIpResolver = StaticIpResolver( - FakeRecordFetcher( - mapOf( - SIGNAL_DOT_ORG to arrayOf( - ARecord( - Name.fromString("www."), - DClass.ANY, - 0L, - mockk { - every { address } returns SIGNAL_IP - every { hostAddress } returns STRINGIFIED_IP - } - ) - ) - ) - ) - ) - val actual = staticIpResolver.resolveToBuildConfig(SIGNAL_DOT_ORG) - val expected = """ - new String[]{"$STRINGIFIED_IP"} - """.trimIndent() - - assertEquals(expected, actual) - } - - @Test(expected = IllegalStateException::class) - fun `Given a hostname without records, when I resolveToBuildConfig, then I expect`() { - val staticIpResolver = StaticIpResolver(FakeRecordFetcher(emptyMap())) - staticIpResolver.resolveToBuildConfig(SIGNAL_DOT_ORG) - } - - private class FakeRecordFetcher(private val recordMap: Map?>) : StaticIpResolver.RecordFetcher { - override fun fetchRecords(hostName: String): Array? { - return recordMap[hostName] - } - } -}