mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
ROUTE53 cleanups
This commit is contained in:
@@ -3,11 +3,12 @@ package commands
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/google/go-cmp/cmp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
@@ -178,7 +179,7 @@ func TestShellCompletionCommand(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range shellsAndCompletionScripts {
|
for _, tt := range shellsAndCompletionScripts {
|
||||||
testCases[0].expected = append(testCases[0].expected, tt.shellName)
|
testCases[0].expected = append(testCases[0].expected, tt.shellName)
|
||||||
for i, _ := range tt.shellName {
|
for i := range tt.shellName {
|
||||||
testCases = append(testCases, testCase{
|
testCases = append(testCases, testCase{
|
||||||
shellArg: tt.shellName[:i+1],
|
shellArg: tt.shellName[:i+1],
|
||||||
expected: []string{tt.shellName},
|
expected: []string{tt.shellName},
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ func AuditRecords(records []*models.RecordConfig) []error {
|
|||||||
|
|
||||||
a.Add("R53_ALIAS", rejectifTargetEqualsLabel) // Last verified 2023-03-01
|
a.Add("R53_ALIAS", rejectifTargetEqualsLabel) // Last verified 2023-03-01
|
||||||
|
|
||||||
a.Add("TXT", rejectif.TxtIsEmpty) // Last verified 2023-10-28
|
|
||||||
|
|
||||||
return a.Audit(records)
|
return a.Audit(records)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/StackExchange/dnscontrol/v4/pkg/printer"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type State int
|
type State int
|
||||||
@@ -25,19 +23,14 @@ func isRemaining(s string, i, r int) bool {
|
|||||||
return (len(s) - 1 - i) > r
|
return (len(s) - 1 - i) > r
|
||||||
}
|
}
|
||||||
|
|
||||||
// txtDecode is like strings.Fields except individual fields
|
// txtDecode decodes TXT strings received from ROUTE53.
|
||||||
// might be quoted using `"`.
|
|
||||||
func txtDecode(s string) (string, error) {
|
func txtDecode(s string) (string, error) {
|
||||||
printer.Printf("DEBUG: route53 txt inboundv=%v\n", s)
|
|
||||||
// Parse according to RFC1035 zonefile specifications.
|
// Parse according to RFC1035 zonefile specifications.
|
||||||
// "foo" -> one string: `foo``
|
// "foo" -> one string: `foo``
|
||||||
// "foo" "bar" -> two strings: `foo` and `bar`
|
// "foo" "bar" -> two strings: `foo` and `bar`
|
||||||
|
// quotes and backslashes are escaped using \
|
||||||
|
|
||||||
// if s == `""` {
|
//printer.Printf("DEBUG: route53 txt inboundv=%v\n", s)
|
||||||
// r := []string{}
|
|
||||||
// printer.Printf("DEBUG: route53 txt Z decodedv=%v\n", r)
|
|
||||||
// return r, nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
b := &bytes.Buffer{}
|
b := &bytes.Buffer{}
|
||||||
state := StateStart
|
state := StateStart
|
||||||
@@ -106,18 +99,13 @@ func txtDecode(s string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r := b.String()
|
r := b.String()
|
||||||
printer.Printf("DEBUG: route53 txt decodedv=%v\n", r)
|
//printer.Printf("DEBUG: route53 txt decodedv=%v\n", r)
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// txtEncode encodes TXT strings as expected by ROUTE53.
|
||||||
func txtEncode(ts []string) string {
|
func txtEncode(ts []string) string {
|
||||||
printer.Printf("DEBUG: route53 txt outboundv=%v\n", ts)
|
//printer.Printf("DEBUG: route53 txt outboundv=%v\n", ts)
|
||||||
|
|
||||||
if len(ts) == 0 {
|
|
||||||
t := `""`
|
|
||||||
printer.Printf("DEBUG: route53 txt Z encodedv=%v\n", t)
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range ts {
|
for i := range ts {
|
||||||
ts[i] = strings.ReplaceAll(ts[i], `\`, `\\`)
|
ts[i] = strings.ReplaceAll(ts[i], `\`, `\\`)
|
||||||
@@ -125,6 +113,6 @@ func txtEncode(ts []string) string {
|
|||||||
}
|
}
|
||||||
t := `"` + strings.Join(ts, `" "`) + `"`
|
t := `"` + strings.Join(ts, `" "`) + `"`
|
||||||
|
|
||||||
printer.Printf("DEBUG: route53 txt encodedv=%v\n", t)
|
//printer.Printf("DEBUG: route53 txt encodedv=%v\n", t)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ func TestTxtDecode(t *testing.T) {
|
|||||||
data string
|
data string
|
||||||
expected []string
|
expected []string
|
||||||
}{
|
}{
|
||||||
|
{``, []string{``}},
|
||||||
|
{`""`, []string{``}},
|
||||||
{`foo`, []string{`foo`}},
|
{`foo`, []string{`foo`}},
|
||||||
{`"foo"`, []string{`foo`}},
|
{`"foo"`, []string{`foo`}},
|
||||||
{`"foo bar"`, []string{`foo bar`}},
|
{`"foo bar"`, []string{`foo bar`}},
|
||||||
@@ -51,3 +53,29 @@ func TestTxtDecode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTxtEncode(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
data []string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{[]string{``}, `""`},
|
||||||
|
{[]string{`foo`}, `"foo"`},
|
||||||
|
{[]string{`aaa`, `bbb`}, `"aaa" "bbb"`},
|
||||||
|
{[]string{`ccc`, `ddd`, `eee`}, `"ccc" "ddd" "eee"`},
|
||||||
|
{[]string{`a"a`, `bbb`}, `"a\"a" "bbb"`},
|
||||||
|
{[]string{`quo'te`}, "\"quo'te\""},
|
||||||
|
{[]string{"blah`blah"}, "\"blah`blah\""},
|
||||||
|
{[]string{`quo"te`}, "\"quo\\\"te\""},
|
||||||
|
{[]string{`q"uo"te`}, "\"q\\\"uo\\\"te\""},
|
||||||
|
{[]string{`backs\lash`}, "\"backs\\\\lash\""},
|
||||||
|
}
|
||||||
|
for i, test := range tests {
|
||||||
|
got := txtEncode(test.data)
|
||||||
|
|
||||||
|
want := test.expected
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("%v: expected TxtStrings=v(%v) got (%v)", i, want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user