How hashicorp/terraform-provider-dns works
Purpose and Entrypoint
This Go repository implements HashiCorp’s Terraform DNS provider. It exposes Terraform resources that create/update DNS records through RFC 2136 dynamic updates and data sources that query DNS records. The plugin executable begins at main.go; provider construction and Terraform registration are centered in internal/provider/provider.go and internal/provider/provider_framework.go.
Terraform Plugin Boundary
Terraform CLI launches the provider as a separate plugin process using Terraform Plugin Protocol v5. The provider translates Terraform configuration, planning, state, and CRUD/read requests into its internal DNS operations. Provider schema, resource/data-source registration, and framework integration are defined in:
• internal/provider/provider.go
• internal/provider/provider_framework.go
• internal/provider/config.go
Provider Configuration and Authentication
A shared provider configuration establishes DNS server connectivity and authentication used by all resources and data sources. It supports TSIG secret-key transaction authentication and Kerberos/GSS-TSIG. Configuration is transformed into DNS client behavior before request handling:
• internal/provider/config.go
• internal/provider/provider.go
• examples/provider/provider_gss_tsig.tf
This is the main security boundary: Terraform-supplied credentials and server settings become authenticated DNS transport configuration.
Record Resource Lifecycle
Mutable Terraform resources model individual records or record sets. Their create/update/delete lifecycle performs RFC 2136 DNS UPDATE operations, while read refreshes Terraform state from DNS. Record-specific implementations separate DNS semantics by type:
• internal/provider/resource_dns_a_record_set.go
• internal/provider/resource_dns_cname_record.go
• internal/provider/resource_dns_srv_record_set.go
The resource surface includes A/AAAA, CNAME, MX, NS, PTR, SRV, and TXT forms. State identity and collection stability rely on hashing where appropriate, including internal/hashcode/hashcode.go and internal/provider/hash_ip_string.go.
DNS Read Data Sources
Read-only data sources resolve existing DNS records and return results into Terraform expressions without modifying DNS. Type-specific schemas adapt lookup output to Terraform state:
• internal/provider/data_dns_a_record_set.go
• internal/provider/data_dns_cname_record_set.go
• internal/provider/data_dns_txt_record_set.go
The shared lookup layer is internal/provider/lookup.go, forming the boundary between record/data-source logic and DNS query execution.
Validation and DNS Naming Rules
Validation rejects malformed zones, record names, and provider inputs before external DNS calls. This protects the resource and data-source layers from invalid Terraform configuration:
• internal/provider/validators.go
• internal/validators/dnsvalidator/is_record_name_valid.go
• internal/validators/dnsvalidator/is_zone_name_valid.go
External Runtime, Test Infrastructure, and Delivery
Production runtime dependencies are Terraform CLI/plugin protocol, reachable authoritative DNS servers, and optionally TSIG or Kerberos infrastructure. Acceptance tests provision a Docker-based DNS/Kerberos environment and invoke real Terraform/provider processes:
• internal/provider/acceptance.sh
• internal/provider/testdata/Dockerfile
• internal/provider/testdata/named.conf.kerberos
GitHub Actions builds, tests, and publishes release artifacts; Terraform Registry documentation is generated into docs/ from templates and provider schemas:
• .github/workflows/build.yml
• GNUmakefile
• templates/index.md.tmpl
Main entry points: provider.go Provider Registration · resource_dns_a_record_set.go Record Resources · config.go Shared DNS Config · acceptance.sh Acceptance Environment
Components 17
Terraform Plugin
Provider process and Terraform-facing registration.
- Plugin Entrypoint
main.go - Provider Registration · Registers schemas, resources, and data sources.
internal/provider/provider.go - Framework Integration
internal/provider/provider_framework.go
DNS Operations
Authenticated update and lookup paths.
- Shared DNS Config · Converts provider settings and credentials into DNS client behavior.
internal/provider/config.go - Record Resources · Implements mutable A/AAAA, CNAME, MX, NS, PTR, SRV, and TXT records.
internal/provider/resource_dns_a_record_set.go - Record Type Semantics · Type-specific lifecycle behavior, including CNAME and SRV record sets.
internal/provider/resource_dns_srv_record_set.go - State Hashing · Stabilizes collection and record identity.
internal/hashcode/hashcode.go - DNS Data Sources · Maps DNS lookup results into Terraform state.
internal/provider/data_dns_a_record_set.go - DNS Lookup Layer · Executes shared DNS read queries.
internal/provider/lookup.go - TSIG / GSS-TSIG · Optional transaction authentication, including Kerberos.
examples/provider/provider_gss_tsig.tf
Validation & Delivery
Input safeguards, acceptance environment, and release workflow.
- Provider Validation · Rejects invalid provider and record inputs before DNS calls.
internal/provider/validators.go - DNS Name Rules · Validates record and zone names.
internal/validators/dnsvalidator/is_record_name_valid.go - Acceptance Environment · Docker DNS/Kerberos environment for real provider and Terraform runs.
internal/provider/acceptance.sh - GitHub Actions · Builds, tests, and publishes release artifacts.
.github/workflows/build.yml - Registry Documentation · Generates docs from templates and provider schemas.
templates/index.md.tmpl
Other components
- Terraform CLI · Launches the provider through Plugin Protocol v5.
- Authoritative DNS Server · Receives RFC 2136 updates and serves record queries.
Connections 21
- Terraform CLI → Plugin Entrypoint: launches
- Plugin Entrypoint → Provider Registration: starts
- Provider Registration → Framework Integration: integrates
- Provider Registration → Shared DNS Config: configures
- Provider Registration → Provider Validation: validates inputs
- Provider Validation → DNS Name Rules: applies naming rules
- Provider Registration → Record Resources: registers
- Record Resources → Record Type Semantics: dispatches to
- Record Resources → State Hashing: uses
- Provider Registration → DNS Data Sources: registers
- DNS Data Sources → DNS Lookup Layer: queries through
- Shared DNS Config → TSIG / GSS-TSIG: supports
- Shared DNS Config → Authoritative DNS Server: configures client for
- Record Resources → Authoritative DNS Server: RFC 2136 UPDATE
- DNS Lookup Layer → Authoritative DNS Server: DNS queries
- TSIG / GSS-TSIG → Authoritative DNS Server: authenticates updates
- Acceptance Environment → Terraform CLI: runs real processes
- Acceptance Environment → Authoritative DNS Server: provisions DNS/Kerberos
- GitHub Actions → Acceptance Environment: executes tests
- GitHub Actions → Registry Documentation: generates
- Provider Registration → Registry Documentation: supplies schemas