-
Notifications
You must be signed in to change notification settings - Fork 128
feat(dns): derive PTR records from machine interface addresses #2691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chet
wants to merge
1
commit into
NVIDIA:main
Choose a base branch
from
chet:gh-issue-2637
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
crates/api-db/migrations/20260618070345_reverse_dns_ptr_records.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| -- Reverse DNS (PTR) support. Convert an inet address to its PTR query name in | ||
| -- the in-addr.arpa (IPv4) or ip6.arpa (IPv6) zone, and expose PTR records | ||
| -- derived from machine interface addresses. The IPv6 expansion mirrors | ||
| -- nico_inet_to_dns_hostname so the two address-derived helpers stay in agreement. | ||
|
|
||
| CREATE OR REPLACE FUNCTION nico_ip_to_arpa_qname(address inet) | ||
| RETURNS text | ||
| LANGUAGE plpgsql | ||
| IMMUTABLE | ||
| STRICT | ||
| AS $$ | ||
| DECLARE | ||
| octets text[]; | ||
| address_text text; | ||
| parts text[]; | ||
| left_groups text[] := ARRAY[]::text[]; | ||
| right_groups text[] := ARRAY[]::text[]; | ||
| groups text[] := ARRAY[]::text[]; | ||
| embedded_ipv4_text text; | ||
| embedded_ipv4_parts text[]; | ||
| missing_groups integer; | ||
| nibbles text; | ||
| BEGIN | ||
| IF family(address) = 4 THEN | ||
| -- Reverse the four octets and append the in-addr.arpa zone. | ||
| octets := string_to_array(host(address), '.'); | ||
| RETURN octets[4] || '.' || octets[3] || '.' || octets[2] || '.' | ||
| || octets[1] || '.in-addr.arpa.'; | ||
| END IF; | ||
|
|
||
| -- IPv6: expand to eight zero-padded hextets (same handling as | ||
| -- nico_inet_to_dns_hostname), concatenate to 32 hex nibbles, then reverse | ||
| -- and dot-separate them under the ip6.arpa zone. | ||
| address_text := host(address); | ||
| embedded_ipv4_text := substring( | ||
| address_text FROM '([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$' | ||
| ); | ||
| IF embedded_ipv4_text IS NOT NULL THEN | ||
| embedded_ipv4_parts := string_to_array(embedded_ipv4_text, '.'); | ||
| address_text := regexp_replace( | ||
| address_text, | ||
| '([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$', | ||
| to_hex(embedded_ipv4_parts[1]::integer * 256 + embedded_ipv4_parts[2]::integer) | ||
| || ':' | ||
| || to_hex(embedded_ipv4_parts[3]::integer * 256 + embedded_ipv4_parts[4]::integer) | ||
| ); | ||
| END IF; | ||
|
|
||
| parts := regexp_split_to_array(address_text, '::'); | ||
|
|
||
| IF parts[1] IS NOT NULL AND parts[1] != '' THEN | ||
| left_groups := string_to_array(parts[1], ':'); | ||
| END IF; | ||
|
|
||
| IF cardinality(parts) > 1 AND parts[2] != '' THEN | ||
| right_groups := string_to_array(parts[2], ':'); | ||
| END IF; | ||
|
|
||
| missing_groups := 8 - cardinality(left_groups) - cardinality(right_groups); | ||
| IF missing_groups < 0 THEN | ||
| RAISE EXCEPTION 'invalid IPv6 address expansion for %', address_text; | ||
| END IF; | ||
|
|
||
| groups := left_groups; | ||
| IF missing_groups > 0 THEN | ||
| groups := groups || array_fill('0'::text, ARRAY[missing_groups]); | ||
| END IF; | ||
| groups := groups || right_groups; | ||
|
|
||
| IF cardinality(groups) != 8 THEN | ||
| RAISE EXCEPTION 'invalid IPv6 address expansion for %', address_text; | ||
| END IF; | ||
|
|
||
| SELECT string_agg(lpad(lower(group_text), 4, '0'), '' ORDER BY group_index) | ||
| INTO nibbles | ||
| FROM unnest(groups) WITH ORDINALITY AS expanded(group_text, group_index); | ||
|
|
||
| RETURN regexp_replace(reverse(nibbles), '(.)', '\1.', 'g') || 'ip6.arpa.'; | ||
| END; | ||
| $$; | ||
|
|
||
| -- PTR records for machine interface addresses. The qname is the address in its | ||
| -- arpa form; the answer is the same FQDN the forward shortname view publishes | ||
| -- for that interface, so a forward A/AAAA record and its PTR round-trip. The | ||
| -- WHERE clause mirrors dns_records_shortname_combined exactly (primary or BMC | ||
| -- interfaces) so every PTR has a matching forward record and vice versa. | ||
| CREATE VIEW dns_records_ptr AS | ||
| SELECT | ||
| nico_ip_to_arpa_qname(mia.address) AS q_name, | ||
| concat(mi.hostname, '.', d.name, '.') AS ptr_content, | ||
| 'PTR'::varchar(10) AS q_type, | ||
| COALESCE(meta.ttl, 300) AS ttl, | ||
| d.id AS domain_id | ||
| FROM | ||
| machine_interfaces mi | ||
| JOIN machine_interface_addresses mia ON mia.interface_id = mi.id | ||
| JOIN domains d ON d.id = mi.domain_id | ||
| LEFT JOIN dns_record_metadata meta ON meta.id = mi.id | ||
| WHERE | ||
| mi.primary_interface = TRUE | ||
| OR mi.interface_type = 'Bmc'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.