Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.
Merged
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
15 changes: 12 additions & 3 deletions localstack-core/localstack/dns/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,22 @@ def _skip_local_resolution(self, request) -> bool:
return True
return False

def _find_matching_aliases(self, question: DNSQuestion) -> list[AliasTarget] | None:
"""
Find aliases matching the question, supporting wildcards.
"""
qlabel = DNSLabel(to_bytes(question.qname))
qtype = RecordType[QTYPE[question.qtype]]
for (label, rtype), targets in self.aliases.items():
if rtype == qtype and qlabel.matchWildcard(label):
return targets
return None

def _resolve_alias(
self, request: DNSRecord, reply: DNSRecord, client_address: ClientAddress
) -> bool:
if request.q.qtype in (QTYPE.A, QTYPE.AAAA, QTYPE.CNAME):
key = (DNSLabel(to_bytes(request.q.qname)), RecordType[QTYPE[request.q.qtype]])
# check if we have aliases defined for our given qname/qtype pair
if aliases := self.aliases.get(key):
if aliases := self._find_matching_aliases(request.q):
for alias in aliases:
# if there is no health check, or the healthcheck is successful, we will consider this alias
# take the first alias passing this check
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_dns_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ def test_dns_server_add_multiple_hosts(self, dns_server, query_dns):
assert answer.answer
assert "123.123.123.123" in answer.to_text()

def test_dns_server_resolves_alias_wildcards(self, dns_server, query_dns):
"""Check if server resolves aliases with wildcards"""
dns_server.add_host("example.org", TargetRecord("1.1.1.1", RecordType.A))
answer = query_dns("subdomain1.example.org", "A")
assert len(answer.answer) == 0

dns_server.add_alias(
source_name="*.example.org",
record_type=RecordType.A,
target=AliasTarget(target="example.org"),
)
answer = query_dns("subdomain1.example.org", "A")
assert answer.answer
assert "1.1.1.1" in answer.to_text()
answer = query_dns("subdomain2.example.org", "A")
assert answer.answer
assert "1.1.1.1" in answer.to_text()

def test_overriding_with_dns_resolve_ip(self, dns_server, query_dns, monkeypatch):
monkeypatch.setattr(config, "DNS_RESOLVE_IP", "2.2.2.2")

Expand Down
Loading