Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 17 additions & 12 deletions actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edge case: the root marker is filtered before the leading dot is stripped, so an entry of .. gets past the filter and is then turned into .. That leaves the root marker in SearchDomains, and AsyncDnsResolver builds name.. from it, which is what this change is meant to avoid.

It is an unlikely value, but could the root marker be filtered again after the strip, e.g.

configured.iterator.filterNot(_ == ".").map(_.stripPrefix(".")).filterNot(_ == ".").distinct.toList

Keeping the first filter matters, since stripping a plain . would otherwise turn it into an explicit empty entry. A test case with .. in the list would cover it.

}

private final val DnsFallbackPort = 53
private val inetSocketAddress = """(.*?)(?::(\d+))?""".r
private val publicLookup = MethodHandles.publicLookup()
Expand Down
7 changes: 7 additions & 0 deletions docs/src/main/paradox/io-dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the comment on search-domains in actor/src/main/resources/reference.conf is not updated, so this normalization is only described here. Could a short note be added there as well (the root marker . is ignored, a leading dot is removed and duplicates are used once)? That comment is where most users will look when setting the value.


DNS lookups can be done via the `DNS` extension:

Scala
Expand Down