1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

In preparation of #1457, warn if cred-names contain colon (#1480)

This commit is contained in:
Tom Limoncelli
2022-04-04 15:33:59 -04:00
committed by GitHub
parent 17feddb157
commit e2536ad406
4 changed files with 87 additions and 4 deletions

View File

@@ -14,8 +14,26 @@ import (
"github.com/DisposaBoy/JsonConfigReader"
"github.com/TomOnTime/utfutil"
"golang.org/x/exp/maps"
)
func quotedList(l []string) string {
if len(l) == 0 {
return ""
}
return `"` + strings.Join(l, `", "`) + `"`
}
func keysWithColons(list []string) []string {
var r []string
for _, k := range list {
if strings.Contains(k, ":") {
r = append(r, k)
}
}
return r
}
// LoadProviderConfigs will open or execute the specified file name, and parse its contents. It will replace environment variables it finds if any value matches $[A-Za-z_-0-9]+
func LoadProviderConfigs(fname string) (map[string]map[string]string, error) {
var results = map[string]map[string]string{}
@@ -57,6 +75,14 @@ func LoadProviderConfigs(fname string) (map[string]map[string]string, error) {
if err = replaceEnvVars(results); err != nil {
return nil, err
}
ckeys := keysWithColons(maps.Keys(results))
if len(ckeys) != 0 {
fmt.Printf(`WARNING: In the future, colons in cred entry names will have meaning.`+
` Our best advice is to remove the colons for now to avoid future compatibility issues.`+
` Specifically these keys: %v` + "\n",
quotedList(ckeys))
}
return results, nil
}

View File

@@ -0,0 +1,51 @@
package config
import (
"reflect"
"testing"
)
func Test_keysWithColons(t *testing.T) {
type args struct {
list []string
}
tests := []struct {
name string
args args
want []string
}{
{"0", args{list: []string{""}}, nil},
{"1", args{list: []string{"none"}}, nil},
{"2", args{list: []string{"a:b"}}, []string{"a:b"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := keysWithColons(tt.args.list); !reflect.DeepEqual(got, tt.want) {
t.Errorf("keysWithColons() = %v, want %v", got, tt.want)
}
})
}
}
func Test_quotedList(t *testing.T) {
type args struct {
l []string
}
tests := []struct {
name string
args args
want string
}{
{"none", args{}, ""},
{"single", args{l: []string{"one"}}, `"one"`},
{"two", args{l: []string{"ricky", "lucy"}}, `"ricky", "lucy"`},
{"three", args{l: []string{"manny", "moe", "jack"}}, `"manny", "moe", "jack"`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := quotedList(tt.args.l); got != tt.want {
t.Errorf("quotedList() = %v, want %v", got, tt.want)
}
})
}
}