dnsrecords

package
v1.5.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dnsrecords builds a provider-agnostic DNS record manager from whichever ACME DNS-01 provider a control plane already has credentials for (Cloudflare or Route53), reusing libdns's own interfaces instead of inventing a second one: github.com/libdns/cloudflare and github.com/libdns/route53, both already a dependency via internal/ingress's Caddy DNS-01 wiring, implement them already.

Index

Constants

View Source
const (
	WWWBoth     = "both"
	WWWApexOnly = "apex_only"
	WWWOnly     = "www_only"
)

WWW modes: which of apex and www get records.

View Source
const (
	CanonicalApex = "apex"
	CanonicalWWW  = "www"
)

Canonical values: which host visitors end up on.

View Source
const (
	OutcomeCreated   = "created"
	OutcomeUpdated   = "updated"
	OutcomeUnchanged = "unchanged"
	OutcomeConflict  = "conflict"
)

Outcome values a record plan or apply reports per domain.

Variables

View Source
var ErrNoTarget = errors.New("dnsrecords: no public address or CNAME target is known for this server")

ErrNoTarget means neither a public address nor a CNAME target is known.

View Source
var ErrZoneNotFound = errors.New("dnsrecords: no managed zone contains the domain")

ErrZoneNotFound means no zone the provider manages contains the domain.

Functions

func Candidates

func Candidates(domain string) []string

Candidates returns every zone name domain could belong to, longest first, down to two labels (a bare TLD is never a zone).

func CanonicalRedirect

func CanonicalRedirect(apex, mode, canonical string) (from, to string)

CanonicalRedirect names the 308 the plan implies: from the non-canonical host to the canonical one. Both are empty unless mode serves both hosts.

func ListedNames

func ListedNames(names []string) ([]string, int)

ListedNames returns up to maxListedZoneNames sorted unique names and how many were cut.

func MatchZone

func MatchZone(domain string, zones []string) (string, bool)

MatchZone returns the longest zone in zones that contains domain, with a trailing dot, or false when none does.

func NormalizeName

func NormalizeName(s string) string

NormalizeName lowercases, drops a trailing dot and converts an IDN name to its A-label form, so U-label and punycode spellings compare equal.

func RelativeName

func RelativeName(domain, zone string) string

RelativeName is domain's record name inside zone: "@" for the apex.

func TargetFor

func TargetFor(ips []string) (recType, value string, err error)

TargetFor picks the record type and value pointing at a server reachable at ips: an A record for the first IPv4 address, else AAAA for the first IPv6 one. ips that are not IP literals are ignored.

func ValidCanonical

func ValidCanonical(c string) bool

ValidCanonical reports whether c is a known domains.canonical value.

func ValidWWWMode

func ValidWWWMode(m string) bool

ValidWWWMode reports whether m is a known domains.www_mode value.

func ValidateTarget

func ValidateTarget(d Desired, provider string) error

ValidateTarget rejects a CNAME at a zone apex for providers that cannot flatten it.

Types

type ApexWWWItem

type ApexWWWItem struct {
	Desired Desired
	Plan    Plan
}

ApexWWWItem is one record of an apex and www plan.

func PlanApexWWW

func PlanApexWWW(apex, mode string, target Desired, existing []libdns.Record, replace bool, ttl time.Duration) ([]ApexWWWItem, error)

PlanApexWWW lists the records a zone-apex domain needs for mode. The apex gets the address record; www follows it with a CNAME so one change moves both, except in www_only mode where www carries the address itself.

type CloudflareAPI

type CloudflareAPI struct {
	Token   string
	BaseURL string
	HTTP    *http.Client
}

CloudflareAPI is the few direct Cloudflare calls libdns cannot make: an exact zone lookup and the proxied flag. The token is only ever sent as a bearer header and never appears in an error.

func (*CloudflareAPI) FindZone

func (c *CloudflareAPI) FindZone(ctx context.Context, domain string) (string, error)

FindZone asks Cloudflare for each candidate zone name, longest first.

func (*CloudflareAPI) Name

func (c *CloudflareAPI) Name() string

Name identifies this provider in edge status and purge history.

func (*CloudflareAPI) ProxyDrift

func (c *CloudflareAPI) ProxyDrift(ctx context.Context, zone, fqdn, recType, content string, proxied bool) (found, drifted int, err error)

ProxyDrift counts the matching records and how many differ from proxied, without changing anything.

func (*CloudflareAPI) PurgeHosts

func (c *CloudflareAPI) PurgeHosts(ctx context.Context, zone string, hosts []string) error

PurgeHosts clears Cloudflare's edge cache for every URL under each host in zone. It needs a token with the Cache Purge permission.

func (*CloudflareAPI) SSLMode

func (c *CloudflareAPI) SSLMode(ctx context.Context, zone string) (string, error)

SSLMode reads the zone's SSL/TLS encryption mode: off, flexible, full or strict. It needs a token with Zone Settings read.

func (*CloudflareAPI) SetProxied

func (c *CloudflareAPI) SetProxied(ctx context.Context, zone, fqdn, recType, content string, proxied bool) (int, error)

SetProxied sets the proxied flag on the records of recType at fqdn in zone whose content equals content, and returns how many it changed.

type Desired

type Desired struct {
	Name  string
	Type  string
	Value string
	TTL   time.Duration
}

Desired is the one record a domain should have.

type ListedZoneFinder

type ListedZoneFinder struct {
	P        dnszones.Provider
	Fallback ZoneFinder
	// Pinned is true when the operator pinned one hosted zone ID, so a lone
	// listed zone that misses is reported as the pinned zone.
	Pinned bool
	// Debug, when set, receives the list error that is otherwise swallowed.
	Debug func(msg string, err error)
}

ListedZoneFinder matches the domain against the provider's own zone list first, which works when the credentials can list zones but a record probe cannot (a record the library fails to parse, or a pinned hosted zone). It falls back to Fallback when the list errors or holds no match. Private zones never match, so a public zone of the same name wins.

func (ListedZoneFinder) FindZone

func (f ListedZoneFinder) FindZone(ctx context.Context, domain string) (string, error)

FindZone implements ZoneFinder.

type Manager

type Manager interface {
	GetRecords(ctx context.Context, zone string) ([]libdns.Record, error)
	AppendRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error)
	SetRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error)
	DeleteRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error)
}

Manager is the subset of libdns's interfaces a DNS records view needs: list, non-destructive add, replace-by-exact-match, and delete. Any provider implementing it already satisfies libdns.RecordGetter, libdns.RecordAppender, libdns.RecordSetter, and libdns.RecordDeleter.

func NewCloudflareManager

func NewCloudflareManager(apiToken string) Manager

NewCloudflareManager wraps github.com/libdns/cloudflare with the same scoped API token (Zone:DNS:Edit) already stored for Cloudflare ACME DNS-01, so records management needs no separate credential.

func NewGoogleCloudManager

func NewGoogleCloudManager(serviceAccountJSON string) (Manager, error)

NewGoogleCloudManager builds a Manager from a service account JSON key.

func NewRoute53Manager

func NewRoute53Manager(accessKeyID, secretAccessKey, region, hostedZoneID string) Manager

NewRoute53Manager wraps github.com/libdns/route53 with the same IAM access key pair already stored for Route53 ACME DNS-01. Region and hostedZoneID may be empty: the provider resolves them itself the same way it does for DNS-01 (see internal/reconcile/ingress's own use).

func NewRoute53ZoneManager

func NewRoute53ZoneManager(accessKeyID, secretAccessKey, region, hostedZoneID string) Manager

NewRoute53ZoneManager serves record automation through the Route 53 API. The libdns module lists the whole zone before every read and write and fails on one record it cannot parse, so only ACME DNS-01 keeps using it.

type ManagerZoneFinder

type ManagerZoneFinder struct {
	Mgr Manager
	// Debug, when set, receives each probe error that is otherwise skipped.
	Debug func(msg string, err error)
}

ManagerZoneFinder probes candidate zones through a Manager: a zone exists when it lists and its apex SOA is at "@". It is the only option for Route53, which libdns cannot enumerate zones for.

func (ManagerZoneFinder) FindZone

func (f ManagerZoneFinder) FindZone(ctx context.Context, domain string) (string, error)

FindZone tries each candidate from longest to shortest.

type Plan

type Plan struct {
	Outcome string
	// Replace holds the existing records an update removes first; for a
	// conflict it lists the records that block the create.
	Replace []libdns.RR
}

Plan is what applying Desired would do against a zone's existing records.

func PlanRecord

func PlanRecord(d Desired, existing []libdns.Record, replace bool) Plan

PlanRecord decides how to bring d into existing. An identical record is unchanged even next to siblings; a different A/AAAA/CNAME at the same name is a conflict unless replace is set, and a foreign TXT or MX is never touched.

type ZoneFinder

type ZoneFinder interface {
	// FindZone returns the zone with a trailing dot, or ErrZoneNotFound.
	FindZone(ctx context.Context, domain string) (string, error)
}

ZoneFinder resolves the zone a domain's records live in.

type ZoneManager

type ZoneManager struct{ P dnszones.Provider }

ZoneManager adapts a dnszones.Provider to Manager, so providers without a libdns module (Google Cloud DNS) serve record automation and DNS-01 TXT writes.

func (ZoneManager) AppendRecords

func (m ZoneManager) AppendRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error)

AppendRecords implements Manager.

func (ZoneManager) DeleteRecords

func (m ZoneManager) DeleteRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error)

DeleteRecords implements Manager. An empty Data on the input matches any value of that name and type.

func (ZoneManager) GetRecords

func (m ZoneManager) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error)

GetRecords implements Manager.

func (ZoneManager) SetRecords

func (m ZoneManager) SetRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error)

SetRecords implements Manager.

type ZoneMissError

type ZoneMissError struct {
	Domain string
	// Listed holds up to maxListedZoneNames public zone names, sorted.
	Listed []string
	// More counts the listed names left out of Listed.
	More int
	// Pinned is the pinned hosted zone name when it was the only zone visible.
	Pinned string
	// Private is a private zone that matched the domain and cannot be used.
	Private string
}

ZoneMissError is ErrZoneNotFound with the context an operator needs: the zone names the provider does list, a pinned zone that cannot hold the domain, or a private zone that matched. It carries names only.

func (*ZoneMissError) Error

func (e *ZoneMissError) Error() string

Error implements error.

func (*ZoneMissError) Unwrap

func (e *ZoneMissError) Unwrap() error

Unwrap lets errors.Is(err, ErrZoneNotFound) keep working.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL