Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade to yaml.v3 #1148

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions allow_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func newAllowListFromConfig(c *config.C, k string, handleKey func(key string, va
// If the handleKey func returns true, the rest of the parsing is skipped
// for this key. This allows parsing of special values like `interfaces`.
func newAllowList(k string, raw interface{}, handleKey func(key string, value interface{}) (bool, error)) (*AllowList, error) {
rawMap, ok := raw.(map[interface{}]interface{})
rawMap, ok := raw.(map[string]any)
if !ok {
return nil, fmt.Errorf("config `%s` has invalid type: %T", k, raw)
}
Expand All @@ -100,12 +100,7 @@ func newAllowList(k string, raw interface{}, handleKey func(key string, value in
rules4 := allowListRules{firstValue: true, allValuesMatch: true, defaultSet: false}
rules6 := allowListRules{firstValue: true, allValuesMatch: true, defaultSet: false}

for rawKey, rawValue := range rawMap {
rawCIDR, ok := rawKey.(string)
if !ok {
return nil, fmt.Errorf("config `%s` has invalid key (type %T): %v", k, rawKey, rawKey)
}

for rawCIDR, rawValue := range rawMap {
if handleKey != nil {
handled, err := handleKey(rawCIDR, rawValue)
if err != nil {
Expand All @@ -116,7 +111,7 @@ func newAllowList(k string, raw interface{}, handleKey func(key string, value in
}
}

value, ok := rawValue.(bool)
value, ok := config.AsBool(rawValue)
if !ok {
return nil, fmt.Errorf("config `%s` has invalid value (type %T): %v", k, rawValue, rawValue)
}
Expand Down Expand Up @@ -177,19 +172,15 @@ func newAllowList(k string, raw interface{}, handleKey func(key string, value in
func getAllowListInterfaces(k string, v interface{}) ([]AllowListNameRule, error) {
var nameRules []AllowListNameRule

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

firstEntry := true
var allValues bool
for rawName, rawAllow := range rawRules {
name, ok := rawName.(string)
if !ok {
return nil, fmt.Errorf("config `%s.interfaces` has invalid key (type %T): %v", k, rawName, rawName)
}
allow, ok := rawAllow.(bool)
for name, rawAllow := range rawRules {
allow, ok := config.AsBool(rawAllow)
if !ok {
return nil, fmt.Errorf("config `%s.interfaces` has invalid value (type %T): %v", k, rawAllow, rawAllow)
}
Expand Down Expand Up @@ -225,16 +216,11 @@ func getRemoteAllowRanges(c *config.C, k string) (*bart.Table[*AllowList], error

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

rawMap, ok := value.(map[interface{}]interface{})
rawMap, ok := value.(map[string]any)
if !ok {
return nil, fmt.Errorf("config `%s` has invalid type: %T", k, value)
}
for rawKey, rawValue := range rawMap {
rawCIDR, ok := rawKey.(string)
if !ok {
return nil, fmt.Errorf("config `%s` has invalid key (type %T): %v", k, rawKey, rawKey)
}

for rawCIDR, rawValue := range rawMap {
allowList, err := newAllowList(fmt.Sprintf("%s.%s", k, rawCIDR), rawValue, nil)
if err != nil {
return nil, err
Expand Down
24 changes: 12 additions & 12 deletions allow_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ import (
func TestNewAllowListFromConfig(t *testing.T) {
l := test.NewLogger()
c := config.NewC(l)
c.Settings["allowlist"] = map[interface{}]interface{}{
c.Settings["allowlist"] = map[string]any{
"192.168.0.0": true,
}
r, err := newAllowListFromConfig(c, "allowlist", nil)
assert.EqualError(t, err, "config `allowlist` has invalid CIDR: 192.168.0.0. netip.ParsePrefix(\"192.168.0.0\"): no '/'")
assert.Nil(t, r)

c.Settings["allowlist"] = map[interface{}]interface{}{
c.Settings["allowlist"] = map[string]any{
"192.168.0.0/16": "abc",
}
r, err = newAllowListFromConfig(c, "allowlist", nil)
assert.EqualError(t, err, "config `allowlist` has invalid value (type string): abc")

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

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

c.Settings["allowlist"] = map[interface{}]interface{}{
c.Settings["allowlist"] = map[string]any{
"0.0.0.0/0": true,
"10.0.0.0/8": false,
"10.42.42.0/24": true,
Expand All @@ -54,7 +54,7 @@ func TestNewAllowListFromConfig(t *testing.T) {
assert.NotNil(t, r)
}

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

// Test interface names

c.Settings["allowlist"] = map[interface{}]interface{}{
"interfaces": map[interface{}]interface{}{
c.Settings["allowlist"] = map[string]any{
"interfaces": map[string]any{
`docker.*`: "foo",
},
}
lr, err := NewLocalAllowListFromConfig(c, "allowlist")
assert.EqualError(t, err, "config `allowlist.interfaces` has invalid value (type string): foo")

c.Settings["allowlist"] = map[interface{}]interface{}{
"interfaces": map[interface{}]interface{}{
c.Settings["allowlist"] = map[string]any{
"interfaces": map[string]any{
`docker.*`: false,
`eth.*`: true,
},
}
lr, err = NewLocalAllowListFromConfig(c, "allowlist")
assert.EqualError(t, err, "config `allowlist.interfaces` values must all be the same true/false value")

c.Settings["allowlist"] = map[interface{}]interface{}{
"interfaces": map[interface{}]interface{}{
c.Settings["allowlist"] = map[string]any{
"interfaces": map[string]any{
`docker.*`: false,
},
}
Expand Down
2 changes: 1 addition & 1 deletion cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type NebulaEncryptionMetadata struct {
Argon2Parameters Argon2Parameters
}

type m map[string]interface{}
type m = map[string]any

// Returned if we try to unmarshal an encrypted private key without a passphrase
var ErrPrivateKeyEncrypted = errors.New("private key must be decrypted")
Expand Down
50 changes: 33 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ import (

"dario.cat/mergo"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

type C struct {
path string
files []string
Settings map[interface{}]interface{}
oldSettings map[interface{}]interface{}
Settings map[string]any
oldSettings map[string]any
callbacks []func(*C)
l *logrus.Logger
reloadLock sync.Mutex
}

func NewC(l *logrus.Logger) *C {
return &C{
Settings: make(map[interface{}]interface{}),
Settings: make(map[string]any),
l: l,
}
}
Expand Down Expand Up @@ -92,8 +92,8 @@ func (c *C) HasChanged(k string) bool {
}

var (
nv interface{}
ov interface{}
nv any
ov any
)

if k == "" {
Expand Down Expand Up @@ -147,7 +147,7 @@ func (c *C) ReloadConfig() {
c.reloadLock.Lock()
defer c.reloadLock.Unlock()

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

c.oldSettings = make(map[interface{}]interface{})
c.oldSettings = make(map[string]any)
for k, v := range c.Settings {
c.oldSettings[k] = v
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func (c *C) GetStringSlice(k string, d []string) []string {
return d
}

rv, ok := r.([]interface{})
rv, ok := r.([]any)
if !ok {
return d
}
Expand All @@ -215,13 +215,13 @@ func (c *C) GetStringSlice(k string, d []string) []string {
}

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

v, ok := r.(map[interface{}]interface{})
v, ok := r.(map[string]any)
if !ok {
return d
}
Expand Down Expand Up @@ -266,6 +266,22 @@ func (c *C) GetBool(k string, d bool) bool {
return v
}

func AsBool(v any) (value bool, ok bool) {
switch x := v.(type) {
case bool:
return x, true
case string:
switch x {
case "y", "yes":
return true, true
case "n", "no":
return false, true
}
}

return false, false
}

// GetDuration will get the duration for k or return the default d if not found or invalid
func (c *C) GetDuration(k string, d time.Duration) time.Duration {
r := c.GetString(k, "")
Expand All @@ -276,18 +292,18 @@ func (c *C) GetDuration(k string, d time.Duration) time.Duration {
return v
}

func (c *C) Get(k string) interface{} {
func (c *C) Get(k string) any {
return c.get(k, c.Settings)
}

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

func (c *C) get(k string, v interface{}) interface{} {
func (c *C) get(k string, v any) any {
parts := strings.Split(k, ".")
for _, p := range parts {
m, ok := v.(map[interface{}]interface{})
m, ok := v.(map[string]any)
if !ok {
return nil
}
Expand Down Expand Up @@ -346,7 +362,7 @@ func (c *C) addFile(path string, direct bool) error {
}

func (c *C) parseRaw(b []byte) error {
var m map[interface{}]interface{}
var m map[string]any

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

func (c *C) parse() error {
var m map[interface{}]interface{}
var m map[string]any

for _, path := range c.files {
b, err := os.ReadFile(path)
if err != nil {
return err
}

var nm map[interface{}]interface{}
var nm map[string]any
err = yaml.Unmarshal(b, &nm)
if err != nil {
return err
Expand Down
Loading
Loading