Skip to content
Closed
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
5 changes: 5 additions & 0 deletions api/v1alpha/domain_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ const (
// was encountered during verification.
DomainReasonVerificationInternalError = "InternalError"

// DomainReasonAddressNotPublic indicates that the HTTP token URL, or a
// redirect it returned, led to a non-public address, which verification
// refuses to connect to.
DomainReasonAddressNotPublic = "AddressNotPublic"

// DomainReasonVerified indicates domain ownership has been successfully
// verified
DomainReasonVerified = "Verified"
Expand Down
1 change: 1 addition & 0 deletions docs/agent/skills/domain-verification.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ do not reformat them, and do not add quotes.
| `RecordNotFound` | Datum looked and found nothing | Check the exact name, and that it is publicly visible |
| `VerificationRecordContentMismatch` | The record is there, the value is wrong | Replace the value exactly. Remove any older record from a previous attempt |
| `UnexpectedResponse` | Something answered, but not as expected | Check the domain resolves at all and nothing is answering in front of it |
| `AddressNotPublic` | The HTTP token URL, or a redirect from it, leads to a non-public address | Serve the token from a public address, or prove ownership with the DNS record |
| `InternalError` | Datum's own fault | Say so, say to raise it, and do not suggest changes to their DNS |
| `InvalidApex` | Not a registrable domain | They gave a public suffix like `com` or `co.uk`. Use the domain they registered |

Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/domain-verification-by-delegation.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ status, so neither waits on its own output.
| Domain reconciler | `internal/controller/domain_controller.go` | Watches `Domain` and `DNSZone`. Runs the zone check, then TXT and HTTP. |
| Zone verdict | `attemptDNSZoneVerification` | Decides `VerifiedDNSZone` from served zones and the delegation. |
| Delegation resolver | `internal/delegation` | Reads delegations from the parent zone's servers, with an RDAP fallback. |
| TXT and HTTP checks | `attemptDNSVerification`, `attemptHTTPVerification` | Verify a `Domain` from a TXT record or an HTTP token. |
| TXT and HTTP checks | `attemptDNSVerification`, `attemptHTTPVerification` | Verify a `Domain` from a TXT record or an HTTP token. The HTTP check connects only to public addresses, on every redirect. |
| Registration client | `internal/registrydata` | Registration data, RDAP nameservers for the fallback, and IP registrant enrichment. |
| Hostname gate | `wildcard_ownership.go`, `gateway_controller.go`, and its copy in `internal/cmd/alb/spec/verification.go` | Admits a hostname on a verified `Domain`, and a wildcard on `VerifiedDNS` or `VerifiedDNSZone`. |
| Gateway DNS | `gateway_dns_controller.go` | Writes records when the zone has DNS authority, from `status.nameservers`. |
Expand Down
11 changes: 11 additions & 0 deletions internal/agent/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,17 @@ var domainCatalog = []ReasonInfo{
"and no trailing spaces. If you have an older record from a previous attempt, remove it.",
Skill: SkillDomainVerification,
},
{
Reason: networkingv1alpha.DomainReasonAddressNotPublic,
ConditionType: networkingv1alpha.DomainConditionVerified,
Actionability: ActionabilityUser,
Scope: ScopeOneHostname,
Explanation: "Datum did not fetch the HTTP token, because the token URL, or a redirect it " +
"returned, leads to a private or other non-public address.",
Remediation: "Serve the token from a public address, and keep any redirect from it on public " +
"addresses. If the site is reachable only privately, verify with the DNS record instead.",
Skill: SkillDomainVerification,
},
{
Reason: networkingv1alpha.DomainReasonVerificationUnexpectedResponse,
ConditionType: networkingv1alpha.DomainConditionVerified,
Expand Down
58 changes: 18 additions & 40 deletions internal/controller/domain_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"slices"
Expand Down Expand Up @@ -556,7 +555,13 @@ func (r *DomainReconciler) attemptHTTPVerification(
logger.Info("looking for HTTP token", "url", domainStatus.Verification.HTTPToken.URL)
responseBody, httpResponse, err := r.httpGet(ctx, domainStatus.Verification.HTTPToken.URL)
if err != nil {
if strings.Contains(err.Error(), "no such host") || strings.Contains(err.Error(), "connection refused") {
if errors.Is(err, errVerificationAddressNotPublic) {
logger.Info("refused to fetch the HTTP token from a non-public address", "error", err.Error())
verifiedHTTPCondition.Reason = networkingv1alpha.DomainReasonAddressNotPublic
verifiedHTTPCondition.Message = "The HTTP token URL, or a redirect it returned, leads to a non-public address, " +
"and verification connects only to public addresses. " +
"Serve the token from a public address, or verify with the TXT record."
} else if strings.Contains(err.Error(), "no such host") || strings.Contains(err.Error(), "connection refused") {
verifiedHTTPCondition.Reason = networkingv1alpha.DomainReasonPendingVerification
verifiedHTTPCondition.Message = "unable to establish connection with HTTP token endpoint"
} else if strings.Contains(err.Error(), "timeout") || strings.Contains(err.Error(), "deadline exceeded") {
Expand All @@ -575,13 +580,20 @@ func (r *DomainReconciler) attemptHTTPVerification(

expectedContent := domainStatus.Verification.HTTPToken.Body
actualContent := strings.TrimSpace(string(responseBody))
if actualContent == expectedContent {
switch {
case actualContent == expectedContent:
verifiedHTTPCondition.Status = metav1.ConditionTrue
verifiedHTTPCondition.Reason = networkingv1alpha.DomainReasonVerified
verifiedHTTPCondition.Message = "HTTP token verification successful"
} else {
case strings.Contains(actualContent, expectedContent):
verifiedHTTPCondition.Reason = networkingv1alpha.DomainReasonVerificationRecordContentMismatch
verifiedHTTPCondition.Message = fmt.Sprintf(
"HTTP token content mismatch. The response body contains %q with other content around it; serve only the token.",
expectedContent)
default:
verifiedHTTPCondition.Reason = networkingv1alpha.DomainReasonVerificationRecordContentMismatch
verifiedHTTPCondition.Message = fmt.Sprintf("HTTP token content mismatch. Expected %q, got %q", expectedContent, actualContent)
verifiedHTTPCondition.Message = fmt.Sprintf(
"HTTP token content mismatch. Expected the response body to be %q.", expectedContent)
}
logger.Info(verifiedHTTPCondition.Message)
} else {
Expand All @@ -590,40 +602,6 @@ func (r *DomainReconciler) attemptHTTPVerification(
}
}

// defaultHTTPGet is the default HTTP GET implementation for verification
func defaultHTTPGet(ctx context.Context, url string) ([]byte, *http.Response, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, fmt.Errorf("creating request: %w", err)
}

resp, err := client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("executing request: %w", err)
}
defer func() {
// Avoid linter violation and exception
_ = resp.Body.Close()
}()

if resp.StatusCode != http.StatusOK {
return nil, resp, nil
}

// Limit response body to 1KB to prevent malicious responses
limitedReader := io.LimitReader(resp.Body, 1024)
body, err := io.ReadAll(limitedReader)
if err != nil {
return nil, resp, fmt.Errorf("reading response body: %w", err)
}

return body, resp, nil
}

// reconcileRegistration keeps Status.Registration fresh and schedules independent timers.
// Returns the next registration refresh time (zero if none).
func (r *DomainReconciler) reconcileRegistration(ctx context.Context, d *networkingv1alpha.Domain, apex string) time.Time {
Expand Down Expand Up @@ -733,7 +711,7 @@ func (r *DomainReconciler) SetupWithManager(mgr mcmanager.Manager) error {
r.mgr = mgr

r.timeNow = time.Now
r.httpGet = defaultHTTPGet
r.httpGet = newPublicHTTPVerificationGet()
r.lookupTXT = net.DefaultResolver.LookupTXT

registryCfg := r.Config.DomainRegistration.RegistryData
Expand Down
85 changes: 85 additions & 0 deletions internal/controller/domain_http_verification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: AGPL-3.0-only

package controller

import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/netip"
"syscall"
"time"

"go.datum.net/network-services-operator/internal/validation"
)

const (
httpVerificationTimeout = 10 * time.Second
httpVerificationBodyLimit = 1024
)

var errVerificationAddressNotPublic = errors.New("verification address is not public")

func refuseNonPublicAddress(address netip.AddrPort) error {
if r, ok := validation.NonPublicAddressRange(address.Addr().String()); ok {
return fmt.Errorf("%w: %s is in the %s range %s", errVerificationAddressNotPublic, address, r.Name, r.Prefix)
}
return nil
}

func newPublicHTTPVerificationGet() func(ctx context.Context, url string) ([]byte, *http.Response, error) {
return newHTTPVerificationGet(refuseNonPublicAddress)
}

func newHTTPVerificationGet(
refuse func(netip.AddrPort) error,
) func(ctx context.Context, url string) ([]byte, *http.Response, error) {
dialer := &net.Dialer{
Timeout: httpVerificationTimeout,
ControlContext: func(_ context.Context, _, address string, _ syscall.RawConn) error {
addrPort, err := netip.ParseAddrPort(address)
if err != nil {
return fmt.Errorf("%w: cannot parse %q", errVerificationAddressNotPublic, address)
}
return refuse(addrPort)
},
}
client := &http.Client{
Timeout: httpVerificationTimeout,
Transport: &http.Transport{
Proxy: nil,
DialContext: dialer.DialContext,
TLSHandshakeTimeout: httpVerificationTimeout,
DisableKeepAlives: true,
},
}

return func(ctx context.Context, url string) ([]byte, *http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, fmt.Errorf("creating request: %w", err)
}

resp, err := client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("executing request: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()

if resp.StatusCode != http.StatusOK {
return nil, resp, nil
}

body, err := io.ReadAll(io.LimitReader(resp.Body, httpVerificationBodyLimit))
if err != nil {
return nil, resp, fmt.Errorf("reading response body: %w", err)
}

return body, resp, nil
}
}
Loading
Loading