Skip to content

Commit

Permalink
state, filter: Fix the interpretation of ifaces names
Browse files Browse the repository at this point in the history
In scenarios where an interface has only numeric characters, the
application panics with a failure to extract a string type from a Go
interface (which is actually a float64).

In order to fix the problem, the parsing of the interface name is performed
using a dedicated interface-state structure.

Note: The usage of the interface name in this context is solely limited
for the filter matching.
This change does not solve the problem of incorrectly representing valid
numeric values like `0xfe` and `1.0` (they are now seen as 254 and 1).

Signed-off-by: Edward Haas <[email protected]>
  • Loading branch information
EdDev committed Mar 2, 2021
1 parent d5025ef commit 74df99b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pkg/state/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ func filterOutDynamicAttributes(iface map[string]interface{}) {
delete(options, "hello-timer")
}

func filterOutInterfaces(ifaces []interface{}, interfacesFilterGlob glob.Glob) []interface{} {
func filterOutInterfaces(ifaces []interface{}, ifacesNames []interfaceState, interfacesFilterGlob glob.Glob) []interface{} {
filteredInterfaces := []interface{}{}
for _, iface := range ifaces {
name := iface.(map[string]interface{})["name"]
if !interfacesFilterGlob.Match(name.(string)) {
for i, iface := range ifaces {
name := ifacesNames[i].Name
if !interfacesFilterGlob.Match(name) {
filterOutDynamicAttributes(iface.(map[string]interface{}))
filteredInterfaces = append(filteredInterfaces, iface)
}
Expand All @@ -92,7 +92,12 @@ func filterOut(currentState shared.State, interfacesFilterGlob glob.Glob) (share
return currentState, err
}

state.Interfaces = filterOutInterfaces(state.Interfaces, interfacesFilterGlob)
var ifacesState interfacesState
if err := yaml.Unmarshal(currentState.Raw, &ifacesState); err != nil {
return currentState, err
}

state.Interfaces = filterOutInterfaces(state.Interfaces, ifacesState.Interfaces, interfacesFilterGlob)
if state.Routes != nil {
state.Routes.Running = filterOutRoutes(state.Routes.Running, interfacesFilterGlob)
state.Routes.Config = filterOutRoutes(state.Routes.Config, interfacesFilterGlob)
Expand Down
47 changes: 47 additions & 0 deletions pkg/state/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,51 @@ interfaces:
})
})

Context("when the interfaces have (only) numeric characters", func() {
BeforeEach(func() {
state = nmstate.NewState(`
interfaces:
- name: eth0
- name: 0
- name: 1101010
`)
filteredState = nmstate.NewState(`
interfaces:
- name: eth0
- name: 1101010
`)
interfacesFilterGlob = glob.MustCompile("0*")
})

It("should filter out interfaces correctly", func() {
returnedState, err := filterOut(state, interfacesFilterGlob)
Expect(err).NotTo(HaveOccurred())
Expect(returnedState).To(MatchYAML(filteredState))
})
})

Context("when the interfaces have special numeric characters", func() {
BeforeEach(func() {
state = nmstate.NewState(`
interfaces:
- name: eth0
- name: 0.0
- name: 1.0
- name: 0xfe
`)
filteredState = nmstate.NewState(`
interfaces:
- name: eth0
- name: 0
- name: 254
`)
interfacesFilterGlob = glob.MustCompile("1*")
})

It("should filter out interfaces correctly but they are not represented correctly", func() {
returnedState, err := filterOut(state, interfacesFilterGlob)
Expect(err).NotTo(HaveOccurred())
Expect(returnedState).To(MatchYAML(filteredState))
})
})
})
8 changes: 8 additions & 0 deletions pkg/state/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ type routesState struct {
Config []interface{} `json:"config"`
Running []interface{} `json:"running"`
}

type interfacesState struct {
Interfaces []interfaceState `json:"interfaces"`
}

type interfaceState struct {
Name string `json:"name"`
}

0 comments on commit 74df99b

Please sign in to comment.