Skip to content

Commit 879852c

Browse files
authored
upgrade to yaml.v3 (#1148)
* upgrade to yaml.v3 The main nice fix here is that maps unmarshal into `map[string]any` instead of `map[any]any`, so it cleans things up a bit. * add config.AsBool Since yaml.v3 doesn't automatically convert yes to bool now, for backwards compat * use type aliases for m * more cleanup * more cleanup * more cleanup * go mod cleanup
1 parent 75faa5f commit 879852c

36 files changed

+257
-258
lines changed

Diff for: allow_list.go

+12-26
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type AllowListNameRule struct {
3636

3737
func NewLocalAllowListFromConfig(c *config.C, k string) (*LocalAllowList, error) {
3838
var nameRules []AllowListNameRule
39-
handleKey := func(key string, value interface{}) (bool, error) {
39+
handleKey := func(key string, value any) (bool, error) {
4040
if key == "interfaces" {
4141
var err error
4242
nameRules, err = getAllowListInterfaces(k, value)
@@ -70,7 +70,7 @@ func NewRemoteAllowListFromConfig(c *config.C, k, rangesKey string) (*RemoteAllo
7070

7171
// If the handleKey func returns true, the rest of the parsing is skipped
7272
// for this key. This allows parsing of special values like `interfaces`.
73-
func newAllowListFromConfig(c *config.C, k string, handleKey func(key string, value interface{}) (bool, error)) (*AllowList, error) {
73+
func newAllowListFromConfig(c *config.C, k string, handleKey func(key string, value any) (bool, error)) (*AllowList, error) {
7474
r := c.Get(k)
7575
if r == nil {
7676
return nil, nil
@@ -81,8 +81,8 @@ func newAllowListFromConfig(c *config.C, k string, handleKey func(key string, va
8181

8282
// If the handleKey func returns true, the rest of the parsing is skipped
8383
// for this key. This allows parsing of special values like `interfaces`.
84-
func newAllowList(k string, raw interface{}, handleKey func(key string, value interface{}) (bool, error)) (*AllowList, error) {
85-
rawMap, ok := raw.(map[interface{}]interface{})
84+
func newAllowList(k string, raw any, handleKey func(key string, value any) (bool, error)) (*AllowList, error) {
85+
rawMap, ok := raw.(map[string]any)
8686
if !ok {
8787
return nil, fmt.Errorf("config `%s` has invalid type: %T", k, raw)
8888
}
@@ -100,12 +100,7 @@ func newAllowList(k string, raw interface{}, handleKey func(key string, value in
100100
rules4 := allowListRules{firstValue: true, allValuesMatch: true, defaultSet: false}
101101
rules6 := allowListRules{firstValue: true, allValuesMatch: true, defaultSet: false}
102102

103-
for rawKey, rawValue := range rawMap {
104-
rawCIDR, ok := rawKey.(string)
105-
if !ok {
106-
return nil, fmt.Errorf("config `%s` has invalid key (type %T): %v", k, rawKey, rawKey)
107-
}
108-
103+
for rawCIDR, rawValue := range rawMap {
109104
if handleKey != nil {
110105
handled, err := handleKey(rawCIDR, rawValue)
111106
if err != nil {
@@ -116,7 +111,7 @@ func newAllowList(k string, raw interface{}, handleKey func(key string, value in
116111
}
117112
}
118113

119-
value, ok := rawValue.(bool)
114+
value, ok := config.AsBool(rawValue)
120115
if !ok {
121116
return nil, fmt.Errorf("config `%s` has invalid value (type %T): %v", k, rawValue, rawValue)
122117
}
@@ -173,22 +168,18 @@ func newAllowList(k string, raw interface{}, handleKey func(key string, value in
173168
return &AllowList{cidrTree: tree}, nil
174169
}
175170

176-
func getAllowListInterfaces(k string, v interface{}) ([]AllowListNameRule, error) {
171+
func getAllowListInterfaces(k string, v any) ([]AllowListNameRule, error) {
177172
var nameRules []AllowListNameRule
178173

179-
rawRules, ok := v.(map[interface{}]interface{})
174+
rawRules, ok := v.(map[string]any)
180175
if !ok {
181176
return nil, fmt.Errorf("config `%s.interfaces` is invalid (type %T): %v", k, v, v)
182177
}
183178

184179
firstEntry := true
185180
var allValues bool
186-
for rawName, rawAllow := range rawRules {
187-
name, ok := rawName.(string)
188-
if !ok {
189-
return nil, fmt.Errorf("config `%s.interfaces` has invalid key (type %T): %v", k, rawName, rawName)
190-
}
191-
allow, ok := rawAllow.(bool)
181+
for name, rawAllow := range rawRules {
182+
allow, ok := config.AsBool(rawAllow)
192183
if !ok {
193184
return nil, fmt.Errorf("config `%s.interfaces` has invalid value (type %T): %v", k, rawAllow, rawAllow)
194185
}
@@ -224,16 +215,11 @@ func getRemoteAllowRanges(c *config.C, k string) (*bart.Table[*AllowList], error
224215

225216
remoteAllowRanges := new(bart.Table[*AllowList])
226217

227-
rawMap, ok := value.(map[interface{}]interface{})
218+
rawMap, ok := value.(map[string]any)
228219
if !ok {
229220
return nil, fmt.Errorf("config `%s` has invalid type: %T", k, value)
230221
}
231-
for rawKey, rawValue := range rawMap {
232-
rawCIDR, ok := rawKey.(string)
233-
if !ok {
234-
return nil, fmt.Errorf("config `%s` has invalid key (type %T): %v", k, rawKey, rawKey)
235-
}
236-
222+
for rawCIDR, rawValue := range rawMap {
237223
allowList, err := newAllowList(fmt.Sprintf("%s.%s", k, rawCIDR), rawValue, nil)
238224
if err != nil {
239225
return nil, err

Diff for: allow_list_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ import (
1515
func TestNewAllowListFromConfig(t *testing.T) {
1616
l := test.NewLogger()
1717
c := config.NewC(l)
18-
c.Settings["allowlist"] = map[interface{}]interface{}{
18+
c.Settings["allowlist"] = map[string]any{
1919
"192.168.0.0": true,
2020
}
2121
r, err := newAllowListFromConfig(c, "allowlist", nil)
2222
require.EqualError(t, err, "config `allowlist` has invalid CIDR: 192.168.0.0. netip.ParsePrefix(\"192.168.0.0\"): no '/'")
2323
assert.Nil(t, r)
2424

25-
c.Settings["allowlist"] = map[interface{}]interface{}{
25+
c.Settings["allowlist"] = map[string]any{
2626
"192.168.0.0/16": "abc",
2727
}
2828
r, err = newAllowListFromConfig(c, "allowlist", nil)
2929
require.EqualError(t, err, "config `allowlist` has invalid value (type string): abc")
3030

31-
c.Settings["allowlist"] = map[interface{}]interface{}{
31+
c.Settings["allowlist"] = map[string]any{
3232
"192.168.0.0/16": true,
3333
"10.0.0.0/8": false,
3434
}
3535
r, err = newAllowListFromConfig(c, "allowlist", nil)
3636
require.EqualError(t, err, "config `allowlist` contains both true and false rules, but no default set for 0.0.0.0/0")
3737

38-
c.Settings["allowlist"] = map[interface{}]interface{}{
38+
c.Settings["allowlist"] = map[string]any{
3939
"0.0.0.0/0": true,
4040
"10.0.0.0/8": false,
4141
"10.42.42.0/24": true,
@@ -45,7 +45,7 @@ func TestNewAllowListFromConfig(t *testing.T) {
4545
r, err = newAllowListFromConfig(c, "allowlist", nil)
4646
require.EqualError(t, err, "config `allowlist` contains both true and false rules, but no default set for ::/0")
4747

48-
c.Settings["allowlist"] = map[interface{}]interface{}{
48+
c.Settings["allowlist"] = map[string]any{
4949
"0.0.0.0/0": true,
5050
"10.0.0.0/8": false,
5151
"10.42.42.0/24": true,
@@ -55,7 +55,7 @@ func TestNewAllowListFromConfig(t *testing.T) {
5555
assert.NotNil(t, r)
5656
}
5757

58-
c.Settings["allowlist"] = map[interface{}]interface{}{
58+
c.Settings["allowlist"] = map[string]any{
5959
"0.0.0.0/0": true,
6060
"10.0.0.0/8": false,
6161
"10.42.42.0/24": true,
@@ -70,25 +70,25 @@ func TestNewAllowListFromConfig(t *testing.T) {
7070

7171
// Test interface names
7272

73-
c.Settings["allowlist"] = map[interface{}]interface{}{
74-
"interfaces": map[interface{}]interface{}{
73+
c.Settings["allowlist"] = map[string]any{
74+
"interfaces": map[string]any{
7575
`docker.*`: "foo",
7676
},
7777
}
7878
lr, err := NewLocalAllowListFromConfig(c, "allowlist")
7979
require.EqualError(t, err, "config `allowlist.interfaces` has invalid value (type string): foo")
8080

81-
c.Settings["allowlist"] = map[interface{}]interface{}{
82-
"interfaces": map[interface{}]interface{}{
81+
c.Settings["allowlist"] = map[string]any{
82+
"interfaces": map[string]any{
8383
`docker.*`: false,
8484
`eth.*`: true,
8585
},
8686
}
8787
lr, err = NewLocalAllowListFromConfig(c, "allowlist")
8888
require.EqualError(t, err, "config `allowlist.interfaces` values must all be the same true/false value")
8989

90-
c.Settings["allowlist"] = map[interface{}]interface{}{
91-
"interfaces": map[interface{}]interface{}{
90+
c.Settings["allowlist"] = map[string]any{
91+
"interfaces": map[string]any{
9292
`docker.*`: false,
9393
},
9494
}

Diff for: cert/cert_v1.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type detailsV1 struct {
4141
curve Curve
4242
}
4343

44-
type m map[string]interface{}
44+
type m = map[string]any
4545

4646
func (c *certificateV1) Version() Version {
4747
return Version1

Diff for: cmd/nebula-cert/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (he *helpError) Error() string {
1717
return he.s
1818
}
1919

20-
func newHelpErrorf(s string, v ...interface{}) error {
20+
func newHelpErrorf(s string, v ...any) error {
2121
return &helpError{s: fmt.Sprintf(s, v...)}
2222
}
2323

Diff for: config/config.go

+33-17
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ import (
1717

1818
"dario.cat/mergo"
1919
"github.com/sirupsen/logrus"
20-
"gopkg.in/yaml.v2"
20+
"gopkg.in/yaml.v3"
2121
)
2222

2323
type C struct {
2424
path string
2525
files []string
26-
Settings map[interface{}]interface{}
27-
oldSettings map[interface{}]interface{}
26+
Settings map[string]any
27+
oldSettings map[string]any
2828
callbacks []func(*C)
2929
l *logrus.Logger
3030
reloadLock sync.Mutex
3131
}
3232

3333
func NewC(l *logrus.Logger) *C {
3434
return &C{
35-
Settings: make(map[interface{}]interface{}),
35+
Settings: make(map[string]any),
3636
l: l,
3737
}
3838
}
@@ -92,8 +92,8 @@ func (c *C) HasChanged(k string) bool {
9292
}
9393

9494
var (
95-
nv interface{}
96-
ov interface{}
95+
nv any
96+
ov any
9797
)
9898

9999
if k == "" {
@@ -147,7 +147,7 @@ func (c *C) ReloadConfig() {
147147
c.reloadLock.Lock()
148148
defer c.reloadLock.Unlock()
149149

150-
c.oldSettings = make(map[interface{}]interface{})
150+
c.oldSettings = make(map[string]any)
151151
for k, v := range c.Settings {
152152
c.oldSettings[k] = v
153153
}
@@ -167,7 +167,7 @@ func (c *C) ReloadConfigString(raw string) error {
167167
c.reloadLock.Lock()
168168
defer c.reloadLock.Unlock()
169169

170-
c.oldSettings = make(map[interface{}]interface{})
170+
c.oldSettings = make(map[string]any)
171171
for k, v := range c.Settings {
172172
c.oldSettings[k] = v
173173
}
@@ -201,7 +201,7 @@ func (c *C) GetStringSlice(k string, d []string) []string {
201201
return d
202202
}
203203

204-
rv, ok := r.([]interface{})
204+
rv, ok := r.([]any)
205205
if !ok {
206206
return d
207207
}
@@ -215,13 +215,13 @@ func (c *C) GetStringSlice(k string, d []string) []string {
215215
}
216216

217217
// GetMap will get the map for k or return the default d if not found or invalid
218-
func (c *C) GetMap(k string, d map[interface{}]interface{}) map[interface{}]interface{} {
218+
func (c *C) GetMap(k string, d map[string]any) map[string]any {
219219
r := c.Get(k)
220220
if r == nil {
221221
return d
222222
}
223223

224-
v, ok := r.(map[interface{}]interface{})
224+
v, ok := r.(map[string]any)
225225
if !ok {
226226
return d
227227
}
@@ -266,6 +266,22 @@ func (c *C) GetBool(k string, d bool) bool {
266266
return v
267267
}
268268

269+
func AsBool(v any) (value bool, ok bool) {
270+
switch x := v.(type) {
271+
case bool:
272+
return x, true
273+
case string:
274+
switch x {
275+
case "y", "yes":
276+
return true, true
277+
case "n", "no":
278+
return false, true
279+
}
280+
}
281+
282+
return false, false
283+
}
284+
269285
// GetDuration will get the duration for k or return the default d if not found or invalid
270286
func (c *C) GetDuration(k string, d time.Duration) time.Duration {
271287
r := c.GetString(k, "")
@@ -276,18 +292,18 @@ func (c *C) GetDuration(k string, d time.Duration) time.Duration {
276292
return v
277293
}
278294

279-
func (c *C) Get(k string) interface{} {
295+
func (c *C) Get(k string) any {
280296
return c.get(k, c.Settings)
281297
}
282298

283299
func (c *C) IsSet(k string) bool {
284300
return c.get(k, c.Settings) != nil
285301
}
286302

287-
func (c *C) get(k string, v interface{}) interface{} {
303+
func (c *C) get(k string, v any) any {
288304
parts := strings.Split(k, ".")
289305
for _, p := range parts {
290-
m, ok := v.(map[interface{}]interface{})
306+
m, ok := v.(map[string]any)
291307
if !ok {
292308
return nil
293309
}
@@ -346,7 +362,7 @@ func (c *C) addFile(path string, direct bool) error {
346362
}
347363

348364
func (c *C) parseRaw(b []byte) error {
349-
var m map[interface{}]interface{}
365+
var m map[string]any
350366

351367
err := yaml.Unmarshal(b, &m)
352368
if err != nil {
@@ -358,15 +374,15 @@ func (c *C) parseRaw(b []byte) error {
358374
}
359375

360376
func (c *C) parse() error {
361-
var m map[interface{}]interface{}
377+
var m map[string]any
362378

363379
for _, path := range c.files {
364380
b, err := os.ReadFile(path)
365381
if err != nil {
366382
return err
367383
}
368384

369-
var nm map[interface{}]interface{}
385+
var nm map[string]any
370386
err = yaml.Unmarshal(b, &nm)
371387
if err != nil {
372388
return err

0 commit comments

Comments
 (0)