File
Blob: acme/dns_config.go
| 1 | package acme |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "net/netip" |
| 7 | "sort" |
| 8 | "strings" |
| 9 | |
| 10 | "go.miragespace.co/specter/config" |
| 11 | "go.miragespace.co/specter/util" |
| 12 | |
| 13 | "github.com/miekg/dns" |
| 14 | ) |
| 15 | |
| 16 | const ( |
| 17 | DNSConfigName = "dns" |
| 18 | DNSConfigSchema = 1 |
| 19 | ) |
| 20 | |
| 21 | // DNSConfig contains the static records for the delegated ACME challenge zone. |
| 22 | // ACME challenge TXT records remain owned by the distributed KV provider. |
| 23 | type DNSConfig struct { |
| 24 | Zone string `json:"zone"` |
| 25 | Nameservers []DNSNameserver `json:"nameservers"` |
| 26 | } |
| 27 | |
| 28 | type DNSNameserver struct { |
| 29 | Host string `json:"host"` |
| 30 | Addresses []string `json:"addresses"` |
| 31 | } |
| 32 | |
| 33 | // DNSConfigError identifies the input field that prevents a DNS configuration |
| 34 | // from being used. Paths use zero-based indexes into the submitted document. |
| 35 | type DNSConfigError struct { |
| 36 | Field string |
| 37 | Message string |
| 38 | } |
| 39 | |
| 40 | func (e *DNSConfigError) Error() string { |
| 41 | return e.Field + ": " + e.Message |
| 42 | } |
| 43 | |
| 44 | // DecodeDNSConfig rejects unsupported schemas and fields rather than silently |
| 45 | // interpreting a newer document as an older configuration. The generic store |
| 46 | // can still retain and replicate that document without understanding it. |
| 47 | func DecodeDNSConfig(record config.Record, localZone string) (DNSConfig, error) { |
| 48 | if err := record.Validate(); err != nil { |
| 49 | return DNSConfig{}, fmt.Errorf("invalid shared DNS record: %w", err) |
| 50 | } |
| 51 | if record.Name != DNSConfigName { |
| 52 | return DNSConfig{}, fmt.Errorf("expected shared DNS configuration, got %q", record.Name) |
| 53 | } |
| 54 | if record.Version.Schema != DNSConfigSchema { |
| 55 | return DNSConfig{}, fmt.Errorf("unsupported DNS configuration schema %d (supported: %d)", record.Version.Schema, DNSConfigSchema) |
| 56 | } |
| 57 | var dnsConfig DNSConfig |
| 58 | if err := util.DecodeJSON(bytes.NewReader(record.Value), &dnsConfig); err != nil { |
| 59 | return DNSConfig{}, fmt.Errorf("invalid DNS configuration: %w", err) |
| 60 | } |
| 61 | return ValidateDNSConfig(dnsConfig, localZone) |
| 62 | } |
| 63 | |
| 64 | // ValidateDNSConfig returns a canonical copy. A nonempty localZone binds the |
| 65 | // document to this server's existing ACME zone; an initializer can leave it |
| 66 | // empty to validate a document before any server has been started. |
| 67 | func ValidateDNSConfig(dnsConfig DNSConfig, localZone string) (DNSConfig, error) { |
| 68 | zone, err := dnsConfigHostname(dnsConfig.Zone) |
| 69 | if err != nil { |
| 70 | return DNSConfig{}, &DNSConfigError{Field: "zone", Message: err.Error()} |
| 71 | } |
| 72 | if localZone != "" { |
| 73 | local, err := dnsConfigHostname(localZone) |
| 74 | if err != nil { |
| 75 | return DNSConfig{}, fmt.Errorf("invalid local ACME zone: %w", err) |
| 76 | } |
| 77 | if zone != local { |
| 78 | return DNSConfig{}, &DNSConfigError{Field: "zone", Message: fmt.Sprintf("must match the local ACME zone %s", local)} |
| 79 | } |
| 80 | } |
| 81 | if len(dnsConfig.Nameservers) == 0 { |
| 82 | return DNSConfig{}, &DNSConfigError{Field: "nameservers", Message: "at least one nameserver is required"} |
| 83 | } |
| 84 | validated := DNSConfig{Zone: zone, Nameservers: make([]DNSNameserver, 0, len(dnsConfig.Nameservers))} |
| 85 | hosts := make(map[string]struct{}, len(dnsConfig.Nameservers)) |
| 86 | for i, ns := range dnsConfig.Nameservers { |
| 87 | field := fmt.Sprintf("nameservers.%d", i) |
| 88 | host, err := dnsConfigHostname(ns.Host) |
| 89 | if err != nil { |
| 90 | return DNSConfig{}, &DNSConfigError{Field: field + ".host", Message: err.Error()} |
| 91 | } |
| 92 | if _, exists := hosts[host]; exists { |
| 93 | return DNSConfig{}, &DNSConfigError{Field: field + ".host", Message: "nameserver is listed more than once"} |
| 94 | } |
| 95 | hosts[host] = struct{}{} |
| 96 | if len(ns.Addresses) == 0 { |
| 97 | return DNSConfig{}, &DNSConfigError{Field: field + ".addresses", Message: "at least one IP address is required"} |
| 98 | } |
| 99 | addresses := make([]string, 0, len(ns.Addresses)) |
| 100 | seen := make(map[netip.Addr]struct{}, len(ns.Addresses)) |
| 101 | for j, raw := range ns.Addresses { |
| 102 | address, err := netip.ParseAddr(raw) |
| 103 | if err != nil || address.Zone() != "" { |
| 104 | return DNSConfig{}, &DNSConfigError{Field: fmt.Sprintf("%s.addresses.%d", field, j), Message: "must be an IPv4 or IPv6 address without a port or zone"} |
| 105 | } |
| 106 | address = address.Unmap() |
| 107 | if _, exists := seen[address]; exists { |
| 108 | return DNSConfig{}, &DNSConfigError{Field: fmt.Sprintf("%s.addresses.%d", field, j), Message: "address is listed more than once for this nameserver"} |
| 109 | } |
| 110 | seen[address] = struct{}{} |
| 111 | addresses = append(addresses, address.String()) |
| 112 | } |
| 113 | sort.Strings(addresses) |
| 114 | validated.Nameservers = append(validated.Nameservers, DNSNameserver{Host: host, Addresses: addresses}) |
| 115 | } |
| 116 | sort.Slice(validated.Nameservers, func(i, j int) bool { |
| 117 | return validated.Nameservers[i].Host < validated.Nameservers[j].Host |
| 118 | }) |
| 119 | return validated, nil |
| 120 | } |
| 121 | |
| 122 | func dnsConfigHostname(value string) (string, error) { |
| 123 | name := dns.CanonicalName(value) |
| 124 | if len(name) > 254 || name == "." { |
| 125 | return "", fmt.Errorf("must be a DNS hostname") |
| 126 | } |
| 127 | for _, label := range strings.Split(strings.TrimSuffix(name, "."), ".") { |
| 128 | if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' { |
| 129 | return "", fmt.Errorf("must contain DNS labels of 1 to 63 characters without leading or trailing hyphens") |
| 130 | } |
| 131 | for _, c := range label { |
| 132 | if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '-' { |
| 133 | return "", fmt.Errorf("must contain only ASCII letters, digits, hyphens, and dots") |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | if _, err := netip.ParseAddr(strings.TrimSuffix(name, ".")); err == nil { |
| 138 | return "", fmt.Errorf("must be a DNS hostname, not an IP address") |
| 139 | } |
| 140 | return name, nil |
| 141 | } |