diff --git a/actor-tests/src/test/scala/org/apache/pekko/io/dns/DnsSettingsSpec.scala b/actor-tests/src/test/scala/org/apache/pekko/io/dns/DnsSettingsSpec.scala index c658106a23..199d67edf5 100644 --- a/actor-tests/src/test/scala/org/apache/pekko/io/dns/DnsSettingsSpec.scala +++ b/actor-tests/src/test/scala/org/apache/pekko/io/dns/DnsSettingsSpec.scala @@ -19,6 +19,7 @@ import scala.concurrent.duration._ import org.apache.pekko import pekko.actor.ExtendedActorSystem +import pekko.io.dns.internal.ResolvConfParser import pekko.testkit.PekkoSpec import com.typesafe.config.ConfigFactory @@ -97,6 +98,50 @@ class DnsSettingsSpec extends PekkoSpec { dnsSettings.SearchDomains shouldEqual List("example.com", "example.net") } + "ignore a root search domain from the host configuration" in { + val hostConfig = ResolvConfParser.parseLines(Iterator("search example.com", "search .")) + val config = ConfigFactory.parseString("search-domains = \"default\"") + + DnsSettings.configuredSearchDomains(config, hostConfig.search) shouldEqual Nil + } + + "ignore a single configured root search domain without changing ndots" in { + val dnsSettings = new DnsSettings( + eas, + ConfigFactory.parseString(""" + nameservers = "127.0.0.1" + search-domains = "." + ndots = 5 + """).withFallback(defaultConfig)) + + dnsSettings.SearchDomains shouldEqual Nil + dnsSettings.NDots shouldEqual 5 + } + + "normalize search suffixes from the host configuration after parsing" in { + val hostConfig = ResolvConfParser.parseLines(Iterator("search .example.net example.org example.net")) + val config = ConfigFactory.parseString("search-domains = \"default\"") + + DnsSettings.configuredSearchDomains(config, hostConfig.search) shouldEqual List("example.net", "example.org") + } + + "normalize search suffixes in a single configured domain without removing a trailing dot" in { + val config = ConfigFactory.parseString("search-domains = \".example.net.\"") + + DnsSettings.configuredSearchDomains(config, Nil) shouldEqual List("example.net.") + } + + "normalize search suffixes without changing first-occurrence order" in { + val dnsSettings = new DnsSettings( + eas, + ConfigFactory.parseString(""" + nameservers = "127.0.0.1" + search-domains = [ ".", ".example.net", "", "example.org.", "example.net", "", "." ] + """).withFallback(defaultConfig)) + + dnsSettings.SearchDomains shouldEqual List("example.net", "", "example.org.") + } + "use host ndots if set to default" in { val dnsSettings = new DnsSettings( eas, diff --git a/actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala b/actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala index fde89e6c81..85a82169ed 100644 --- a/actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala +++ b/actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala @@ -125,18 +125,7 @@ private[dns] final class DnsSettings(system: ExtendedActorSystem, c: Config) { } else None } - val SearchDomains: List[String] = { - c.getValue("search-domains").valueType() match { - case ConfigValueType.STRING => - c.getString("search-domains") match { - case "default" => resolvConf.map(_.search).getOrElse(Nil) - case single => List(single) - } - case ConfigValueType.LIST => - c.getStringList("search-domains").asScala.toList - case _ => throw new IllegalArgumentException("Invalid type for search-domains. Must be a string or string list.") - } - } + val SearchDomains: List[String] = DnsSettings.configuredSearchDomains(c, resolvConf.map(_.search).getOrElse(Nil)) val NDots: Int = { c.getValue("ndots").valueType() match { @@ -169,6 +158,22 @@ private[dns] final class DnsSettings(system: ExtendedActorSystem, c: Config) { object DnsSettings { + private[dns] def configuredSearchDomains(config: Config, hostSearchDomains: => List[String]): List[String] = { + val configured = config.getValue("search-domains").valueType() match { + case ConfigValueType.STRING => + config.getString("search-domains") match { + case "default" => hostSearchDomains + case single => List(single) + } + case ConfigValueType.LIST => + config.getStringList("search-domains").asScala.toList + case _ => throw new IllegalArgumentException("Invalid type for search-domains. Must be a string or string list.") + } + // Some resolv.conf providers use the root to indicate that there is no search suffix. + // Keep the first occurrence and preserve explicit empty suffixes and trailing dots. + configured.iterator.filterNot(_ == ".").map(_.stripPrefix(".")).distinct.toList + } + private final val DnsFallbackPort = 53 private val inetSocketAddress = """(.*?)(?::(\d+))?""".r private val publicLookup = MethodHandles.publicLookup() diff --git a/docs/src/main/paradox/io-dns.md b/docs/src/main/paradox/io-dns.md index 11f39cd10b..0bc6f2a2cd 100644 --- a/docs/src/main/paradox/io-dns.md +++ b/docs/src/main/paradox/io-dns.md @@ -54,6 +54,13 @@ There are currently two implementations: `inet-address` is the default implementation as it pre-dates `async-dns`, `async-dns` may become the default in a future release. +For both `/etc/resolv.conf` and `pekko.io.dns.async-dns.search-domains`, `async-dns` ignores the exact +root marker `.`. If `.` is the only entry, the resolver does not append any search suffixes. A single +leading dot is removed from other search suffixes: `.example.org` is treated as `example.org`. +Identical entries after normalization are used once, in first-occurrence order. An explicitly +empty entry still adds no suffix at its position in that order. Trailing dots and `ndots` +are unchanged. + DNS lookups can be done via the `DNS` extension: Scala