Skip to content

Commit

Permalink
dont proceed if a host is an apex domain
Browse files Browse the repository at this point in the history
Signed-off-by: craig <[email protected]>
  • Loading branch information
maleck13 committed Aug 21, 2024
1 parent 8a6812d commit 5656b94
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
17 changes: 15 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ func FindDNSZoneForHost(ctx context.Context, host string, zones []DNSZone) (*DNS
return z, err
}

func isApexDomain(host string, zones []DNSZone) (string, bool) {
for _, z := range zones {
if z.DNSName == host {
return z.ID, true
}
}
return "", false
}

// findDNSZoneForHost will take a host and look for a zone that patches the immediate parent of that host and will continue to step through parents until it either finds a zone or fails. Example *.example.com will look for example.com and other.domain.example.com will step through each subdomain until it hits example.com.
func findDNSZoneForHost(originalHost, host string, zones []DNSZone) (*DNSZone, string, error) {
if len(zones) == 0 {
return nil, "", fmt.Errorf("%w : %s", ErrNoZoneForHost, host)
Expand All @@ -92,12 +102,16 @@ func findDNSZoneForHost(originalHost, host string, zones []DNSZone) (*DNSZone, s
return nil, "", fmt.Errorf("no valid zone found for host: %v", originalHost)
}

// We do not currently support creating records for Apex domains, and a DNSZone represents an Apex domain we cannot setup dns for the host
if id, is := isApexDomain(originalHost, zones); is {
return nil, "", fmt.Errorf("host %s is an apex domain with zone id %s. Cannot configure DNS for apex domain as apex domains only support A records", originalHost, id)
}

hostParts := strings.SplitN(host, ".", 2)
if len(hostParts) < 2 {
return nil, "", fmt.Errorf("no valid zone found for host: %s", originalHost)
}
parentDomain := hostParts[1]

// We do not currently support creating records for Apex domains, and a DNSZone represents an Apex domain, as such
// we should never be trying to find a zone that matches the `originalHost` exactly. Instead, we just continue
// on to the next possible valid host to try i.e. the parent domain.
Expand All @@ -108,7 +122,6 @@ func findDNSZoneForHost(originalHost, host string, zones []DNSZone) (*DNSZone, s
matches := slices.DeleteFunc(slices.Clone(zones), func(zone DNSZone) bool {
return strings.ToLower(zone.DNSName) != host
})

if len(matches) > 0 {
if len(matches) > 1 {
return nil, "", fmt.Errorf("multiple zones found for host: %s", originalHost)
Expand Down
15 changes: 15 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ func Test_findDNSZoneForHost(t *testing.T) {
want1: "",
wantErr: true,
},
{
name: "apex domain",
host: "test.example.com",
zones: []DNSZone{
{
DNSName: "example.com",
},
{
DNSName: "test.example.com",
},
},
want: "",
want1: "",
wantErr: true,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 5656b94

Please sign in to comment.