From b1b2a2a3b69df779bdd241be2f61fea0e16fd736 Mon Sep 17 00:00:00 2001 From: Tom Limoncelli Date: Mon, 13 Jan 2025 13:43:03 -0500 Subject: [PATCH] cleanup(all) Merge lint from main (#3358) Co-authored-by: Asif Nawaz <107853964+AsifNawaz-cnic@users.noreply.github.com> --- integrationTest/integration_test.go | 2 +- models/cloudflare_from.go | 32 +++++++++++++++++++ models/record.go | 1 - pkg/rtypecontrol/pave.go | 47 ++++++++++++++++++++++++++++ pkg/txtutil/txtcode.go | 4 +-- pkg/txtutil/txtutil.go | 2 +- providers/cloudns/cloudnsProvider.go | 4 ++- providers/hexonet/records.go | 9 +++++- 8 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 pkg/rtypecontrol/pave.go diff --git a/integrationTest/integration_test.go b/integrationTest/integration_test.go index 1e3e9a5d13..74c2e524bb 100644 --- a/integrationTest/integration_test.go +++ b/integrationTest/integration_test.go @@ -710,8 +710,8 @@ func makeRec(name, target, typ string) *models.RecordConfig { func manyA(namePattern, target string, n int) []*models.RecordConfig { recs := []*models.RecordConfig{} for i := range n { - //recs = append(recs, makeRec(fmt.Sprintf(namePattern, i), target, "A")) recs = append(recs, a(fmt.Sprintf(namePattern, i), target)) + //recs = append(recs, makeRec(fmt.Sprintf(namePattern, i), target, "A")) } return recs } diff --git a/models/cloudflare_from.go b/models/cloudflare_from.go index 81a7eaef2a..d9b0dc8b12 100644 --- a/models/cloudflare_from.go +++ b/models/cloudflare_from.go @@ -8,6 +8,38 @@ import ( // MakeSingleRedirectFromRawRec updates a RecordConfig to be a // SINGLEREDIRECT using the data from a RawRecord. func MakeSingleRedirectFromRawRec(rc *RecordConfig, code uint16, name, when, then string) error { + //// MakePageRule updates a RecordConfig to be a PAGE_RULE using PAGE_RULE data. + //func MakePageRule(rc *models.RecordConfig, priority int, code uint16, when, then string) error { + // if rc == nil { + // return errors.New("RecordConfig cannot be nil") + // } + // if when == "" || then == "" { + // return errors.New("when and then parameters cannot be empty") + // } + // + // display := mkPageRuleBlob(priority, code, when, then) + // + // rc.Type = "PAGE_RULE" + // rc.TTL = 1 + // rc.CloudflareRedirect = &models.CloudflareSingleRedirectConfig{ + // Code: code, + // // + // PRWhen: when, + // PRThen: then, + // PRPriority: priority, + // PRDisplay: display, + // } + // return rc.SetTarget(display) + //} + // + //// mkPageRuleBlob creates the 1,301,when,then string used in displays. + //func mkPageRuleBlob(priority int, code uint16, when, then string) string { + // return fmt.Sprintf("%d,%03d,%s,%s", priority, code, when, then) + //} + // + //// makeSingleRedirectFromRawRec updates a RecordConfig to be a + //// SINGLEREDIRECT using the data from a RawRecord. + //func makeSingleRedirectFromRawRec(rc *models.RecordConfig, code uint16, name, when, then string) error { target := targetFromRaw(name, code, when, then) rc.Type = "CF_SINGLE_REDIRECT" diff --git a/models/record.go b/models/record.go index 13e6ba789d..f63581f03f 100644 --- a/models/record.go +++ b/models/record.go @@ -354,7 +354,6 @@ func (rc *RecordConfig) SetLabel3(short, subdomain, origin string) error { // fqdn may have a trailing "." but it is not required. // origin may not have a trailing dot. func (rc *RecordConfig) SetLabelFromFQDN(fqdn, origin string) { - // Assertions that make sure the function is being used correctly: if strings.HasSuffix(origin, ".") { panic(fmt.Errorf("origin (%s) is not supposed to end with a dot", origin)) diff --git a/pkg/rtypecontrol/pave.go b/pkg/rtypecontrol/pave.go new file mode 100644 index 0000000000..b30ae3f6de --- /dev/null +++ b/pkg/rtypecontrol/pave.go @@ -0,0 +1,47 @@ +package rtypecontrol + +import ( + "fmt" + "strconv" +) + +// PaveArgs converts each arg to its desired type, or returns an error if conversion fails or if the number of arguments is wrong. +// argTypes is a string where each rune specifies the desired type of the arg in the same position: +// 'i': uinet16 (will convert strings, truncate floats, etc) +// 's': Valid only if string. +func PaveArgs(args []any, argTypes string) error { + if len(args) != len(argTypes) { + return fmt.Errorf("wrong number of arguments. Expected %v, got %v", len(argTypes), len(args)) + } + + for i, at := range argTypes { + arg := args[i] + switch at { + case 'i': // uint16 + if s, ok := arg.(string); ok { // Is this a string-encoded int? + ni, err := strconv.Atoi(s) + if err != nil { + return fmt.Errorf("value %q is not a number (uint16 wanted)", arg) + } + args[i] = uint16(ni) + } else if _, ok := arg.(float64); ok { + args[i] = uint16(arg.(float64)) + } else if _, ok := arg.(uint16); ok { + args[i] = arg.(uint16) + } else if _, ok := arg.(int); ok { + args[i] = uint16(arg.(int)) + } else { + return fmt.Errorf("value %q is type %T, expected uint16", arg, arg) + } + + case 's': + if _, ok := arg.(string); ok { + args[i] = arg.(string) + } else { + args[i] = fmt.Sprintf("%v", arg) + } + } + } + + return nil +} diff --git a/pkg/txtutil/txtcode.go b/pkg/txtutil/txtcode.go index 00b2807078..c20ec63405 100644 --- a/pkg/txtutil/txtcode.go +++ b/pkg/txtutil/txtcode.go @@ -70,10 +70,10 @@ func txtDecode(s string) (string, error) { /* BNF: - txttarget := `""`` | item | item ` ` item* + txttarget := `""`` | item | item ` item* item := quoteditem | unquoteditem quoteditem := quote innertxt quote - quote := `"` + := `"` innertxt := (escaped | printable )* escaped := `\\` | `\"` printable := (printable ASCII chars) diff --git a/pkg/txtutil/txtutil.go b/pkg/txtutil/txtutil.go index a6caf5e05e..4a859b1bb9 100644 --- a/pkg/txtutil/txtutil.go +++ b/pkg/txtutil/txtutil.go @@ -13,7 +13,7 @@ func splitChunks(buf string, lim int) []string { chunks = append(chunks, chunk) } if len(buf) > 0 { - chunks = append(chunks, buf[:]) + chunks = append(chunks, buf) } return chunks } diff --git a/providers/cloudns/cloudnsProvider.go b/providers/cloudns/cloudnsProvider.go index 5b47193839..d218925fa6 100644 --- a/providers/cloudns/cloudnsProvider.go +++ b/providers/cloudns/cloudnsProvider.go @@ -308,7 +308,9 @@ func toRc(domain string, r *domainRecord) (*models.RecordConfig, error) { case "TXT": err = rc.SetTargetTXT(r.Target) case "CNAME", "DNAME", "MX", "NS", "SRV", "ALIAS", "PTR": - err = rc.SetTarget(dnsutil.AddOrigin(r.Target+".", domain)) + if err := rc.SetTarget(dnsutil.AddOrigin(r.Target+".", domain)); err != nil { + return nil, err + } case "CAA": caaFlag, _ := strconv.ParseUint(r.CaaFlag, 10, 8) rc.CaaFlag = uint8(caaFlag) diff --git a/providers/hexonet/records.go b/providers/hexonet/records.go index d8fb27145c..37b2c1b382 100644 --- a/providers/hexonet/records.go +++ b/providers/hexonet/records.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "math" "strconv" "strings" @@ -206,7 +207,13 @@ func (n *HXClient) getRecords(domain string) ([]*HXRecord, error) { record.Fqdn = spl[0] + "." + record.Fqdn } if record.Type == "MX" || record.Type == "SRV" { - prio, _ := strconv.ParseUint(spl[4], 10, 32) + prio, err := strconv.ParseUint(spl[4], 10, 32) + if err != nil { + return nil, fmt.Errorf("failed to parse priority: %w", err) + } + if prio > math.MaxUint16 { + return nil, fmt.Errorf("priority value exceeds uint16 maximum: %s", spl[4]) + } record.Priority = uint32(prio) record.Answer = strings.Join(spl[5:], " ") } else {