|
|
|
@@ -51,10 +51,10 @@ func init() {
|
|
|
|
// CloudflareDNSRes is a resource for managing DNS records in Cloudflare zones.
|
|
|
|
// CloudflareDNSRes is a resource for managing DNS records in Cloudflare zones.
|
|
|
|
// This resource uses the Cloudflare API to create, update, and delete DNS
|
|
|
|
// This resource uses the Cloudflare API to create, update, and delete DNS
|
|
|
|
// records in a specified zone. It supports various record types including A,
|
|
|
|
// 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
|
|
|
|
// AAAA, CNAME, MX, TXT, NS, and PTR records. The resource requires polling to
|
|
|
|
// to detect changes, as the Cloudflare API does not provide an event stream.
|
|
|
|
// detect changes, as the Cloudflare API does not provide an event stream. The
|
|
|
|
// The Purge functionality allows enforcing that only managed DNS records exist
|
|
|
|
// Purge functionality allows enforcing that only managed DNS records exist in
|
|
|
|
// in the zone, removing any unmanaged records.
|
|
|
|
// the zone, removing any unmanaged records.
|
|
|
|
type CloudflareDNSRes struct {
|
|
|
|
type CloudflareDNSRes struct {
|
|
|
|
traits.Base
|
|
|
|
traits.Base
|
|
|
|
traits.GraphQueryable
|
|
|
|
traits.GraphQueryable
|
|
|
|
@@ -74,14 +74,10 @@ type CloudflareDNSRes struct {
|
|
|
|
// Type (e.g., IP address for A records, hostname for CNAME records).
|
|
|
|
// Type (e.g., IP address for A records, hostname for CNAME records).
|
|
|
|
Content string `lang:"content"`
|
|
|
|
Content string `lang:"content"`
|
|
|
|
|
|
|
|
|
|
|
|
// Data is a value that's specific for SRV records, containing the priority,
|
|
|
|
|
|
|
|
// weight, port, and SRV targets.
|
|
|
|
|
|
|
|
Data *dns.SRVRecordData `lang:"srv_data"`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Priority is the priority value for records that support it (e.g., MX
|
|
|
|
// 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
|
|
|
|
// records). This is a pointer to distinguish between an unset value and
|
|
|
|
// a zero value.
|
|
|
|
// a zero value.
|
|
|
|
Priority *int64 `lang:"priority"`
|
|
|
|
Priority *float64 `lang:"priority"`
|
|
|
|
|
|
|
|
|
|
|
|
// Proxied specifies whether the record should be proxied through
|
|
|
|
// Proxied specifies whether the record should be proxied through
|
|
|
|
// Cloudflare's CDN. This is a pointer to distinguish between an unset
|
|
|
|
// Cloudflare's CDN. This is a pointer to distinguish between an unset
|
|
|
|
@@ -134,7 +130,7 @@ func (obj *CloudflareDNSRes) Validate() error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if obj.APIToken == "" {
|
|
|
|
if obj.APIToken == "" {
|
|
|
|
return fmt.Errorf("API token is required")
|
|
|
|
return fmt.Errorf("api token is required")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if obj.Type == "" {
|
|
|
|
if obj.Type == "" {
|
|
|
|
@@ -142,7 +138,7 @@ func (obj *CloudflareDNSRes) Validate() error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (obj.TTL < 60 || obj.TTL > 86400) && obj.TTL != 1 { // API requirement
|
|
|
|
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 == "" {
|
|
|
|
if obj.Zone == "" {
|
|
|
|
@@ -164,7 +160,7 @@ func (obj *CloudflareDNSRes) Validate() error {
|
|
|
|
// cloudflare accepts ~4req/s so this is safe enough even when managing lots
|
|
|
|
// cloudflare accepts ~4req/s so this is safe enough even when managing lots
|
|
|
|
// of records
|
|
|
|
// of records
|
|
|
|
if obj.MetaParams().Poll == 0 || obj.MetaParams().Poll < 60 {
|
|
|
|
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 fmt.Errorf("cloudflare:dns requires polling, set Meta:poll param (e.g., 300s), min. 60s")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
@@ -179,7 +175,6 @@ func (obj *CloudflareDNSRes) Init(init *engine.Init) error {
|
|
|
|
option.WithAPIToken(obj.APIToken),
|
|
|
|
option.WithAPIToken(obj.APIToken),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
//TODO: does it make more sense to check it here or in CheckApply()?
|
|
|
|
|
|
|
|
zoneListParams := zones.ZoneListParams{
|
|
|
|
zoneListParams := zones.ZoneListParams{
|
|
|
|
Name: cloudflare.F(obj.Zone),
|
|
|
|
Name: cloudflare.F(obj.Zone),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -365,14 +360,10 @@ func (obj *CloudflareDNSRes) Cmp(r engine.Res) error {
|
|
|
|
return fmt.Errorf("the priority param differs")
|
|
|
|
return fmt.Errorf("the priority param differs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if obj.Priority != nil && *obj.Priority != *obj.Priority {
|
|
|
|
if obj.Priority != nil && *obj.Priority != *res.Priority {
|
|
|
|
return fmt.Errorf("the priority param differs")
|
|
|
|
return fmt.Errorf("the priority param differs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if obj.Data != res.Data {
|
|
|
|
|
|
|
|
return fmt.Errorf("the data param differs")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -440,7 +431,7 @@ func (obj *CloudflareDNSRes) buildRecordParam() (any, error) {
|
|
|
|
param.Proxied = cloudflare.F(*obj.Proxied)
|
|
|
|
param.Proxied = cloudflare.F(*obj.Proxied)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if obj.Priority != nil { // required for MX record
|
|
|
|
if obj.Priority != nil { // required for MX record
|
|
|
|
param.Priority = cloudflare.F(float64(*obj.Priority))
|
|
|
|
param.Priority = cloudflare.F(*obj.Priority)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if obj.Comment != "" {
|
|
|
|
if obj.Comment != "" {
|
|
|
|
param.Comment = cloudflare.F(obj.Comment)
|
|
|
|
param.Comment = cloudflare.F(obj.Comment)
|
|
|
|
@@ -464,23 +455,8 @@ func (obj *CloudflareDNSRes) buildRecordParam() (any, error) {
|
|
|
|
|
|
|
|
|
|
|
|
case "NS":
|
|
|
|
case "NS":
|
|
|
|
param := dns.NSRecordParam{
|
|
|
|
param := dns.NSRecordParam{
|
|
|
|
Name: cloudflare.F(obj.RecordName),
|
|
|
|
|
|
|
|
Type: cloudflare.F(dns.NSRecordTypeNS),
|
|
|
|
|
|
|
|
Data: cloudflare.F(obj.Data),
|
|
|
|
|
|
|
|
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 "SRV":
|
|
|
|
|
|
|
|
param := dns.SRVRecordParam{
|
|
|
|
|
|
|
|
Name: cloudflare.F(obj.RecordName),
|
|
|
|
Name: cloudflare.F(obj.RecordName),
|
|
|
|
Type: cloudflare.F(dns.SRVRecordTypeSRV),
|
|
|
|
Type: cloudflare.F(dns.NSRecordTypeNS),
|
|
|
|
Content: cloudflare.F(obj.Content),
|
|
|
|
Content: cloudflare.F(obj.Content),
|
|
|
|
TTL: cloudflare.F(ttl),
|
|
|
|
TTL: cloudflare.F(ttl),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -593,16 +569,16 @@ func (obj *CloudflareDNSRes) needsUpdate(record dns.RecordResponse) bool {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if obj.Priority != nil {
|
|
|
|
if obj.Priority != nil {
|
|
|
|
if float64(*obj.Priority) != record.Priority {
|
|
|
|
if *obj.Priority != record.Priority {
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if obj.Comment != record.Comment {
|
|
|
|
if obj.Comment != "" && obj.Comment != record.Comment {
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO add more checks?
|
|
|
|
//TODO: add more checks?
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
|
@@ -656,7 +632,7 @@ func (obj *CloudflareDNSRes) purgeCheckApply(ctx context.Context, apply bool) (b
|
|
|
|
cfRes.Content)
|
|
|
|
cfRes.Content)
|
|
|
|
if cfRes.Priority != nil {
|
|
|
|
if cfRes.Priority != nil {
|
|
|
|
// corner case for MX records which require priority set
|
|
|
|
// corner case for MX records which require priority set
|
|
|
|
recordKey = fmt.Sprintf("%s:%d", recordKey, *cfRes.Priority)
|
|
|
|
recordKey = fmt.Sprintf("%s:%g", recordKey, *cfRes.Priority)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
excludes[recordKey] = true
|
|
|
|
excludes[recordKey] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -667,6 +643,9 @@ func (obj *CloudflareDNSRes) purgeCheckApply(ctx context.Context, apply bool) (b
|
|
|
|
for _, record := range allRecords {
|
|
|
|
for _, record := range allRecords {
|
|
|
|
recordKey := fmt.Sprintf("%s:%s:%s", record.Name, record.Type,
|
|
|
|
recordKey := fmt.Sprintf("%s:%s:%s", record.Name, record.Type,
|
|
|
|
record.Content)
|
|
|
|
record.Content)
|
|
|
|
|
|
|
|
if record.Priority != 0 {
|
|
|
|
|
|
|
|
recordKey = fmt.Sprintf("%s:%g", recordKey, record.Priority)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if excludes[recordKey] {
|
|
|
|
if excludes[recordKey] {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
@@ -700,22 +679,24 @@ func (obj *CloudflareDNSRes) GraphQueryAllowed(opts ...engine.GraphQueryableOpti
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// matchesRecordName checks if a record name from the API matches our desired record name.
|
|
|
|
// matchesRecordName checks if a record name from the API matches our desired
|
|
|
|
// Handles both FQDN (www.example.com) and short form (www) comparisons.
|
|
|
|
// record name. Handles both FQDN (www.example.com) and short form (www)
|
|
|
|
|
|
|
|
// comparisons.
|
|
|
|
func (obj *CloudflareDNSRes) matchesRecordName(apiRecordName string) bool {
|
|
|
|
func (obj *CloudflareDNSRes) matchesRecordName(apiRecordName string) bool {
|
|
|
|
desired := obj.normalizeRecordName(obj.RecordName)
|
|
|
|
desired := obj.normalizeRecordName(obj.RecordName)
|
|
|
|
actual := obj.normalizeRecordName(apiRecordName)
|
|
|
|
actual := obj.normalizeRecordName(apiRecordName)
|
|
|
|
return desired == actual
|
|
|
|
return desired == actual
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// normalizeRecordName converts a record name to a consistent format for comparison.
|
|
|
|
// normalizeRecordName converts a record name to a consistent format for
|
|
|
|
// Converts to FQDN format (e.g., "www" -> "www.example.com", "@" -> "example.com")
|
|
|
|
// comparison. Converts to FQDN format (e.g., "www" -> "www.example.com", "@" ->
|
|
|
|
|
|
|
|
// "example.com")
|
|
|
|
func (obj *CloudflareDNSRes) normalizeRecordName(name string) string {
|
|
|
|
func (obj *CloudflareDNSRes) normalizeRecordName(name string) string {
|
|
|
|
if name == "@" || name == obj.Zone {
|
|
|
|
if name == "@" || name == obj.Zone {
|
|
|
|
return obj.Zone
|
|
|
|
return obj.Zone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if strings.HasSuffix(name, "."+obj.Zone) || name == obj.Zone {
|
|
|
|
if strings.HasSuffix(name, "."+obj.Zone) {
|
|
|
|
return name
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|