Skip to content

Commit

Permalink
Unknown rtypes should not result in panic (StackExchange#2775)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlimoncelli authored Jan 8, 2024
1 parent 6a8561f commit ce454c3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
12 changes: 11 additions & 1 deletion commands/getZones.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,12 @@ func GetZone(args GetZoneArgs) error {
}
}

ty := rec.Type
if rec.Type == "UNKNOWN" {
ty = rec.UnknownTypeName
}
fmt.Fprintf(w, "%s\t%s\t%d\tIN\t%s\t%s%s\n",
rec.NameFQDN, rec.Name, rec.TTL, rec.Type, rec.GetTargetCombinedFunc(nil), cfproxy)
rec.NameFQDN, rec.Name, rec.TTL, ty, rec.GetTargetCombinedFunc(nil), cfproxy)
}

default:
Expand Down Expand Up @@ -363,6 +367,8 @@ func formatDsl(zonename string, rec *models.RecordConfig, defaultTTL uint32) str
target = `"` + target + `"`
case "R53_ALIAS":
return makeR53alias(rec, ttl)
case "UNKNOWN":
return makeUknown(rec, ttl)
default:
target = `"` + target + `"`
}
Expand Down Expand Up @@ -399,3 +405,7 @@ func makeR53alias(rec *models.RecordConfig, ttl uint32) string {
}
return rec.Type + "(" + strings.Join(items, ", ") + ")"
}

func makeUknown(rc *models.RecordConfig, ttl uint32) string {
return fmt.Sprintf(`// %s("%s")`, rc.UnknownTypeName, rc.GetTargetField())
}
6 changes: 5 additions & 1 deletion models/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type RecordConfig struct {
Metadata map[string]string `json:"meta,omitempty"`
Original interface{} `json:"-"` // Store pointer to provider-specific record object. Used in diffing.

// If you add a field to this struct, also add it to the list on MarshalJSON.
// If you add a field to this struct, also add it to the list in the UnmarshalJSON function.
MxPreference uint16 `json:"mxpreference,omitempty"`
SrvPriority uint16 `json:"srvpriority,omitempty"`
SrvWeight uint16 `json:"srvweight,omitempty"`
Expand Down Expand Up @@ -129,6 +129,7 @@ type RecordConfig struct {
TlsaMatchingType uint8 `json:"tlsamatchingtype,omitempty"`
R53Alias map[string]string `json:"r53_alias,omitempty"`
AzureAlias map[string]string `json:"azure_alias,omitempty"`
UnknownTypeName string `json:"unknown_type_name,omitempty"`
}

// MarshalJSON marshals RecordConfig.
Expand Down Expand Up @@ -196,6 +197,7 @@ func (rc *RecordConfig) UnmarshalJSON(b []byte) error {
TlsaMatchingType uint8 `json:"tlsamatchingtype,omitempty"`
R53Alias map[string]string `json:"r53_alias,omitempty"`
AzureAlias map[string]string `json:"azure_alias,omitempty"`
UnknownTypeName string `json:"unknown_type_name,omitempty"`

EnsureAbsent bool `json:"ensure_absent,omitempty"` // Override NO_PURGE and delete this record

Expand Down Expand Up @@ -318,6 +320,8 @@ func (rc *RecordConfig) ToComparableNoTTL() string {
r := txtutil.EncodeQuoted(rc.target)
//fmt.Fprintf(os.Stdout, "DEBUG: ToComNoTTL cmp txts=%s q=%q\n", r, r)
return r
case "UNKNOWN":
return fmt.Sprintf("rtype=%s rdata=%s", rc.UnknownTypeName, rc.target)
}
return rc.GetTargetCombined()
}
Expand Down
4 changes: 2 additions & 2 deletions models/t_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func (rc *RecordConfig) PopulateFromStringFunc(rtype, contents, origin string, t
case "TLSA":
return rc.SetTargetTLSAString(contents)
default:
return fmt.Errorf("unknown rtype (%s) when parsing (%s) domain=(%s)",
rtype, contents, origin)
//return fmt.Errorf("unknown rtype (%s) when parsing (%s) domain=(%s)", rtype, contents, origin)
return MakeUnknown(rc, rtype, contents, origin)
}
}

Expand Down
4 changes: 4 additions & 0 deletions models/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (rc *RecordConfig) GetTargetCombinedFunc(encodeFn func(s string) string) st
// WARNING: How TXT records are handled is buggy but we can't change it because
// code depends on the bugs. Use Get GetTargetCombinedFunc() instead.
func (rc *RecordConfig) GetTargetCombined() string {

// Pseudo records:
if _, ok := dns.StringToType[rc.Type]; !ok {
switch rc.Type { // #rtype_variations
Expand All @@ -60,7 +61,10 @@ func (rc *RecordConfig) GetTargetCombined() string {
}
}

// Everything else
switch rc.Type {
case "UNKNOWN":
return fmt.Sprintf("rtype=%s rdata=%s", rc.UnknownTypeName, rc.target)
case "TXT":
return rc.zoneFileQuoted()
case "SOA":
Expand Down
9 changes: 9 additions & 0 deletions models/unknown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package models

func MakeUnknown(rc *RecordConfig, rtype string, contents string, origin string) error {
rc.Type = "UNKNOWN"
rc.UnknownTypeName = rtype
rc.target = contents

return nil
}
3 changes: 3 additions & 0 deletions pkg/prettyzone/prettyzone.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func (z *ZoneGenData) generateZoneFileHelper(w io.Writer) error {

// type
typeStr := rr.Type
if rr.Type == "UNKNOWN" {
typeStr = rr.UnknownTypeName
}

// the remaining line
target := rr.GetTargetCombinedFunc(txtutil.EncodeQuoted)
Expand Down

0 comments on commit ce454c3

Please sign in to comment.