|
|
|
|
@@ -32,6 +32,7 @@ package resources
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/purpleidea/mgmt/engine"
|
|
|
|
|
"github.com/purpleidea/mgmt/engine/traits"
|
|
|
|
|
@@ -50,10 +51,10 @@ func init() {
|
|
|
|
|
// CloudflareDNSRes is a resource for managing DNS records in Cloudflare zones.
|
|
|
|
|
// This resource uses the Cloudflare API to create, update, and delete DNS
|
|
|
|
|
// records in a specified zone. It supports various record types including A,
|
|
|
|
|
// AAAA, CNAME, MX, TXT, NS, SRV, and PTR records. The resource requires polling
|
|
|
|
|
// to detect changes, as the Cloudflare API does not provide an event stream.
|
|
|
|
|
// The Purge functionality allows enforcing that only managed DNS records exist
|
|
|
|
|
// in the zone, removing any unmanaged records.
|
|
|
|
|
// AAAA, CNAME, MX, TXT, NS, and PTR records. The resource requires polling to
|
|
|
|
|
// detect changes, as the Cloudflare API does not provide an event stream. The
|
|
|
|
|
// Purge functionality allows enforcing that only managed DNS records exist in
|
|
|
|
|
// the zone, removing any unmanaged records.
|
|
|
|
|
type CloudflareDNSRes struct {
|
|
|
|
|
traits.Base
|
|
|
|
|
traits.GraphQueryable
|
|
|
|
|
@@ -76,7 +77,7 @@ type CloudflareDNSRes struct {
|
|
|
|
|
// Priority is the priority value for records that support it (e.g., MX
|
|
|
|
|
// records). This is a pointer to distinguish between an unset value and
|
|
|
|
|
// a zero value.
|
|
|
|
|
Priority *int64 `lang:"priority"`
|
|
|
|
|
Priority *float64 `lang:"priority"`
|
|
|
|
|
|
|
|
|
|
// Proxied specifies whether the record should be proxied through
|
|
|
|
|
// Cloudflare's CDN. This is a pointer to distinguish between an unset
|
|
|
|
|
@@ -129,7 +130,7 @@ func (obj *CloudflareDNSRes) Validate() error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if obj.APIToken == "" {
|
|
|
|
|
return fmt.Errorf("API token is required")
|
|
|
|
|
return fmt.Errorf("api token is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if obj.Type == "" {
|
|
|
|
|
@@ -137,7 +138,7 @@ func (obj *CloudflareDNSRes) Validate() error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (obj.TTL < 60 || obj.TTL > 86400) && obj.TTL != 1 { // API requirement
|
|
|
|
|
return fmt.Errorf("TTL must be between 60s and 86400s, or set to 1")
|
|
|
|
|
return fmt.Errorf("ttl must be between 60s and 86400s, or set to 1")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if obj.Zone == "" {
|
|
|
|
|
@@ -152,8 +153,14 @@ func (obj *CloudflareDNSRes) Validate() error {
|
|
|
|
|
return fmt.Errorf("content is required when state is 'exists'")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if obj.MetaParams().Poll == 0 || obj.MetaParams().Poll < 1 { // CF accepts ~4req/s so this is good enough
|
|
|
|
|
return fmt.Errorf("cloudflare:dns requires polling, set Meta:poll param (e.g., 60 seconds), min. 1s")
|
|
|
|
|
if obj.Type == "MX" && obj.Priority == nil {
|
|
|
|
|
return fmt.Errorf("priority is required for MX records")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cloudflare accepts ~4req/s so this is safe enough even when managing lots
|
|
|
|
|
// of records
|
|
|
|
|
if obj.MetaParams().Poll == 0 || obj.MetaParams().Poll < 60 {
|
|
|
|
|
return fmt.Errorf("cloudflare:dns requires polling, set Meta:poll param (e.g., 300s), min. 60s")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
@@ -168,7 +175,6 @@ func (obj *CloudflareDNSRes) Init(init *engine.Init) error {
|
|
|
|
|
option.WithAPIToken(obj.APIToken),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//TODO: does it make more sense to check it here or in CheckApply()?
|
|
|
|
|
zoneListParams := zones.ZoneListParams{
|
|
|
|
|
Name: cloudflare.F(obj.Zone),
|
|
|
|
|
}
|
|
|
|
|
@@ -207,21 +213,6 @@ func (obj *CloudflareDNSRes) Watch(context.Context) error {
|
|
|
|
|
// if necessary. If apply is false, it only checks if changes are needed. If
|
|
|
|
|
// Purge is enabled, it will first delete any unmanaged records in the zone.
|
|
|
|
|
func (obj *CloudflareDNSRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
|
|
|
|
|
zone, err := obj.client.Zones.List(ctx, zones.ZoneListParams{
|
|
|
|
|
Name: cloudflare.F(obj.Zone),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(zone.Result) == 0 {
|
|
|
|
|
return false, fmt.Errorf("there's no zone registered with name %s", obj.Zone)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(zone.Result) > 1 {
|
|
|
|
|
return false, fmt.Errorf("there's more than one zone with name %s", obj.Zone)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We start by checking the need for purging
|
|
|
|
|
if obj.Purge {
|
|
|
|
|
checkOK, err := obj.purgeCheckApply(ctx, apply)
|
|
|
|
|
@@ -233,7 +224,8 @@ func (obj *CloudflareDNSRes) CheckApply(ctx context.Context, apply bool) (bool,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List existing records
|
|
|
|
|
// we're using `contains` so as to get the candidates, as `exact` might not
|
|
|
|
|
// give the expected results depending on how the user specified it.
|
|
|
|
|
listParams := dns.RecordListParams{
|
|
|
|
|
ZoneID: cloudflare.F(obj.zoneID),
|
|
|
|
|
Name: cloudflare.F(dns.RecordListParamsName{
|
|
|
|
|
@@ -247,10 +239,15 @@ func (obj *CloudflareDNSRes) CheckApply(ctx context.Context, apply bool) (bool,
|
|
|
|
|
return false, errwrap.Wrapf(err, "failed to list DNS records")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
recordExists := len(recordList.Result) > 0
|
|
|
|
|
// here we filter to find the exact match
|
|
|
|
|
recordExists := false
|
|
|
|
|
var record dns.RecordResponse
|
|
|
|
|
if recordExists {
|
|
|
|
|
record = recordList.Result[0]
|
|
|
|
|
for _, r := range recordList.Result {
|
|
|
|
|
if obj.matchesRecordName(r.Name) {
|
|
|
|
|
record = r
|
|
|
|
|
recordExists = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch obj.State {
|
|
|
|
|
@@ -319,7 +316,11 @@ func (obj *CloudflareDNSRes) Cmp(r engine.Res) error {
|
|
|
|
|
return fmt.Errorf("apitoken differs")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if obj.Proxied != res.Proxied {
|
|
|
|
|
if (obj.Proxied == nil) != (res.Proxied == nil) {
|
|
|
|
|
return fmt.Errorf("proxied values differ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if obj.Proxied != nil && *obj.Proxied != *res.Proxied {
|
|
|
|
|
return fmt.Errorf("proxied values differ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -355,7 +356,11 @@ func (obj *CloudflareDNSRes) Cmp(r engine.Res) error {
|
|
|
|
|
return fmt.Errorf("content param differs")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if obj.Priority != res.Priority {
|
|
|
|
|
if (obj.Priority == nil) != (res.Priority == nil) {
|
|
|
|
|
return fmt.Errorf("the priority param differs")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if obj.Priority != nil && *obj.Priority != *res.Priority {
|
|
|
|
|
return fmt.Errorf("the priority param differs")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -426,7 +431,7 @@ func (obj *CloudflareDNSRes) buildRecordParam() (any, error) {
|
|
|
|
|
param.Proxied = cloudflare.F(*obj.Proxied)
|
|
|
|
|
}
|
|
|
|
|
if obj.Priority != nil { // required for MX record
|
|
|
|
|
param.Priority = cloudflare.F(float64(*obj.Priority))
|
|
|
|
|
param.Priority = cloudflare.F(*obj.Priority)
|
|
|
|
|
}
|
|
|
|
|
if obj.Comment != "" {
|
|
|
|
|
param.Comment = cloudflare.F(obj.Comment)
|
|
|
|
|
@@ -463,20 +468,6 @@ func (obj *CloudflareDNSRes) buildRecordParam() (any, error) {
|
|
|
|
|
}
|
|
|
|
|
return param, nil
|
|
|
|
|
|
|
|
|
|
case "SRV":
|
|
|
|
|
param := dns.SRVRecordParam{
|
|
|
|
|
Name: cloudflare.F(obj.RecordName),
|
|
|
|
|
Type: cloudflare.F(dns.SRVRecordTypeSRV),
|
|
|
|
|
TTL: cloudflare.F(ttl),
|
|
|
|
|
}
|
|
|
|
|
if obj.Proxied != nil {
|
|
|
|
|
param.Proxied = cloudflare.F(*obj.Proxied)
|
|
|
|
|
}
|
|
|
|
|
if obj.Comment != "" {
|
|
|
|
|
param.Comment = cloudflare.F(obj.Comment)
|
|
|
|
|
}
|
|
|
|
|
return param, nil
|
|
|
|
|
|
|
|
|
|
case "PTR":
|
|
|
|
|
param := dns.PTRRecordParam{
|
|
|
|
|
Name: cloudflare.F(obj.RecordName),
|
|
|
|
|
@@ -578,16 +569,16 @@ func (obj *CloudflareDNSRes) needsUpdate(record dns.RecordResponse) bool {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if obj.Priority != nil {
|
|
|
|
|
if float64(*obj.Priority) != record.Priority {
|
|
|
|
|
if *obj.Priority != record.Priority {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if obj.Comment != record.Comment {
|
|
|
|
|
if obj.Comment != "" && obj.Comment != record.Comment {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO add more checks?
|
|
|
|
|
//TODO: add more checks?
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
@@ -637,7 +628,12 @@ func (obj *CloudflareDNSRes) purgeCheckApply(ctx context.Context, apply bool) (b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cfRes.Zone == obj.Zone {
|
|
|
|
|
recordKey := fmt.Sprintf("%s:%s", cfRes.RecordName, cfRes.Type)
|
|
|
|
|
recordKey := fmt.Sprintf("%s:%s:%s", cfRes.RecordName, cfRes.Type,
|
|
|
|
|
cfRes.Content)
|
|
|
|
|
if cfRes.Priority != nil {
|
|
|
|
|
// corner case for MX records which require priority set
|
|
|
|
|
recordKey = fmt.Sprintf("%s:%g", recordKey, *cfRes.Priority)
|
|
|
|
|
}
|
|
|
|
|
excludes[recordKey] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -645,7 +641,11 @@ func (obj *CloudflareDNSRes) purgeCheckApply(ctx context.Context, apply bool) (b
|
|
|
|
|
checkOK := true
|
|
|
|
|
|
|
|
|
|
for _, record := range allRecords {
|
|
|
|
|
recordKey := fmt.Sprintf("%s:%s", record.Name, record.Type)
|
|
|
|
|
recordKey := fmt.Sprintf("%s:%s:%s", record.Name, record.Type,
|
|
|
|
|
record.Content)
|
|
|
|
|
if record.Priority != 0 {
|
|
|
|
|
recordKey = fmt.Sprintf("%s:%g", recordKey, record.Priority)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if excludes[recordKey] {
|
|
|
|
|
continue
|
|
|
|
|
@@ -678,3 +678,27 @@ func (obj *CloudflareDNSRes) GraphQueryAllowed(opts ...engine.GraphQueryableOpti
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// matchesRecordName checks if a record name from the API matches our desired
|
|
|
|
|
// record name. Handles both FQDN (www.example.com) and short form (www)
|
|
|
|
|
// comparisons.
|
|
|
|
|
func (obj *CloudflareDNSRes) matchesRecordName(apiRecordName string) bool {
|
|
|
|
|
desired := obj.normalizeRecordName(obj.RecordName)
|
|
|
|
|
actual := obj.normalizeRecordName(apiRecordName)
|
|
|
|
|
return desired == actual
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// normalizeRecordName converts a record name to a consistent format for
|
|
|
|
|
// comparison. Converts to FQDN format (e.g., "www" -> "www.example.com", "@" ->
|
|
|
|
|
// "example.com")
|
|
|
|
|
func (obj *CloudflareDNSRes) normalizeRecordName(name string) string {
|
|
|
|
|
if name == "@" || name == obj.Zone {
|
|
|
|
|
return obj.Zone
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.HasSuffix(name, "."+obj.Zone) {
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return name + "." + obj.Zone
|
|
|
|
|
}
|
|
|
|
|
|