Skip to content

Commit 7916334

Browse files
authored
Check if Calico canReachHost parameter is valid (#50)
The `canReachHost` represents a string representation of an IP. In the current implementation it is not guaranteed that the passed parameter is actually a valid IP address and would automatically classify it as IPv6. The function will now check if the IP is valid and return an appropiate error message if not.
1 parent 3bee7d0 commit 7916334

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pkg/snap/util/calico.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ func MaybePatchCalicoAutoDetectionMethod(ctx context.Context, s snap.Snap, canRe
3232
}
3333

3434
var re *regexp.Regexp
35-
if ip := net.ParseIP(canReachHost); ip.To4() == nil {
35+
ip := net.ParseIP(canReachHost)
36+
if ip == nil {
37+
return fmt.Errorf("could not parse IP address %q", canReachHost)
38+
}
39+
if ip.To4() == nil {
3640
// Address is in IPv6
3741
re = ip6AutodetectionMethodRe
3842
} else {

pkg/snap/util/calico_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,19 @@ func TestMaybePatchCalicoAutoDetectionMethod(t *testing.T) {
109109
})
110110
}
111111
}
112+
113+
func TestMaybePatchCalicoAutoDetectionMethodBadIP(t *testing.T) {
114+
yaml := `
115+
- name: IP_AUTODETECTION_METHOD
116+
value: "first-found"
117+
- name: IP6_AUTODETECTION_METHOD
118+
value: "first-found"`
119+
canReachHost := "badIPRepresentation"
120+
g := NewWithT(t)
121+
snap := &mock.Snap{
122+
CNIYaml: yaml,
123+
}
124+
125+
err := snaputil.MaybePatchCalicoAutoDetectionMethod(context.Background(), snap, canReachHost, true)
126+
g.Expect(err).NotTo(BeNil())
127+
}

0 commit comments

Comments
 (0)