Skip to content

Commit

Permalink
Fix: lookup more specific DNS names directly (localstack#9815)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrw authored Dec 11, 2023
1 parent f98bf55 commit a4f0a22
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion localstack/dns/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@
psutil_cache = TTLCache(maxsize=100, ttl=10)


# TODO: update route53 provider to use this util
def normalise_dns_name(name: DNSLabel | str) -> str:
name = str(name)
if not name.endswith("."):
return f"{name}."

return name


@cached(cache=psutil_cache)
def list_network_interface_details() -> dict[str, list[snicaddr]]:
return psutil.net_if_addrs()
Expand Down Expand Up @@ -451,7 +460,7 @@ def _resolve_name_from_zones(
converter = RecordConverter(request, client_address)

# check for direct (not regex based) response
zone = self.zones.get(request.q.qname)
zone = self.zones.get(normalise_dns_name(request.q.qname))
if zone is not None:
for zone_records in zone:
rr = converter.to_record(zone_records).try_rr(request.q)
Expand Down Expand Up @@ -522,12 +531,14 @@ def flag(f):

def add_host(self, name: str, record: NameRecord):
LOG.debug("Adding host %s with record %s", name, record)
name = normalise_dns_name(name)
with self.lock:
self.zones.setdefault(name, [])
self.zones[name].append(record)

def delete_host(self, name: str, record: NameRecord):
LOG.debug("Deleting host %s with record %s", name, record)
name = normalise_dns_name(name)
with self.lock:
if not self.zones.get(name):
raise ValueError("Could not find entry %s for name %s in zones", record, name)
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_dns_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ def test_dns_server_wildcard_matching_with_skip(self, dns_server, query_dns):
assert answer.answer
assert "122.122.122.122" in answer.to_text()

def test_dns_server_specific_name_overrides_wildcard(self, dns_server, query_dns):
dns_server.add_host("*.example.org", TargetRecord("1.2.3.4", RecordType.A))
dns_server.add_host("foo.example.org", TargetRecord("5.6.7.8", RecordType.A))

answer = query_dns("foo.example.org", "A")

assert answer.answer
assert "5.6.7.8" in answer.to_text()
assert "1.2.3.4" not in answer.to_text()

def test_redirect_to_localstack_lifecycle(self, dns_server, query_dns):
"""Test adding records pointing to LS at all times"""
dns_server.add_host_pointing_to_localstack("*.example.org")
Expand Down

0 comments on commit a4f0a22

Please sign in to comment.