diff --git a/pkg/k8sclient/k8sclient.go b/pkg/k8sclient/k8sclient.go index 9c7005fda..83f306f80 100644 --- a/pkg/k8sclient/k8sclient.go +++ b/pkg/k8sclient/k8sclient.go @@ -23,6 +23,7 @@ import ( "os" "regexp" "strings" + "syscall" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -182,16 +183,22 @@ func parsePodNetworkObjectName(podnetwork string) (string, string, string, error // Check and see if each item matches the specification for valid attachment name. // "Valid attachment names must be comprised of units of the DNS-1123 label format" // [a-z0-9]([-a-z0-9]*[a-z0-9])? - // And we allow at (@), and forward slash (/) (units separated by commas) // It must start and end alphanumerically. - allItems := []string{netNsName, networkName, netIfName} + allItems := []string{netNsName, networkName} + expr := regexp.MustCompile("^[a-z0-9]([-a-z0-9]*[a-z0-9])?$") for i := range allItems { - matched, _ := regexp.MatchString("^[a-z0-9]([-a-z0-9]*[a-z0-9])?$", allItems[i]) + matched := expr.MatchString(allItems[i]) if !matched && len([]rune(allItems[i])) > 0 { return "", "", "", logging.Errorf(fmt.Sprintf("parsePodNetworkObjectName: Failed to parse: one or more items did not match comma-delimited format (must consist of lower case alphanumeric characters). Must start and end with an alphanumeric character), mismatch @ '%v'", allItems[i])) } } + if len(netIfName) > 0 { + if len(netIfName) > (syscall.IFNAMSIZ-1) || strings.ContainsAny(netIfName, " \t\n\v\f\r/") { + return "", "", "", logging.Errorf(fmt.Sprintf("parsePodNetworkObjectName: Failed to parse interface name: must be less than 15 chars and not contain '/' or spaces. interface name '%s'", netIfName)) + } + } + logging.Debugf("parsePodNetworkObjectName: parsed: %s, %s, %s", netNsName, networkName, netIfName) return netNsName, networkName, netIfName, nil } diff --git a/pkg/k8sclient/k8sclient_test.go b/pkg/k8sclient/k8sclient_test.go index 2a1684654..8bc712395 100644 --- a/pkg/k8sclient/k8sclient_test.go +++ b/pkg/k8sclient/k8sclient_test.go @@ -1002,36 +1002,30 @@ users: }) Context("parsePodNetworkObjectName", func() { - It("fails to get podnetwork given bad annotation values", func() { - fakePod := testutils.NewFakePod(fakePodName, "net1", "") - - clientInfo := NewFakeClientInfo() - _, err := clientInfo.AddPod(fakePod) - Expect(err).NotTo(HaveOccurred()) - - _, err = clientInfo.AddNetAttachDef( - testutils.NewFakeNetAttachDef(fakePod.ObjectMeta.Namespace, "net1", "{\"type\": \"mynet\"}")) - Expect(err).NotTo(HaveOccurred()) - - k8sArgs, err := GetK8sArgs(args) - Expect(err).NotTo(HaveOccurred()) - pod, err := clientInfo.GetPod(string(k8sArgs.K8S_POD_NAMESPACE), string(k8sArgs.K8S_POD_NAME)) - - // invalid case 1 - can't have more than 2 items separated by "/" - pod.Annotations[networkAttachmentAnnot] = "root@someIP/root@someOtherIP/root@thirdIP" + DescribeTable("fails to get podnetwork given bad annotation values", func(networkAnnot string) { + pod := testutils.NewFakePod(fakePodName, "net1", "") + pod.Annotations[networkAttachmentAnnot] = networkAnnot _, err = GetPodNetwork(pod) Expect(err).To(HaveOccurred()) + }, + Entry("can't have more than 2 items separated by \"/\"", "root@someIP/root@someOtherIP/root@thirdIP"), + Entry("can't have more than 2 items separated by \"@\"", "root@someIP/root@someOtherIP@garbagevalue"), + Entry("not matching comma-delimited format", "root@someIP/root@someOtherIP"), + Entry("invalid network interface name space in netdev name", "default/net1@myIfc Name"), + Entry("invalid network interface name too long", "default/net1@very_long_interface_name"), + ) - // invalid case 2 - can't have more than 2 items separated by "@" - pod.Annotations[networkAttachmentAnnot] = "root@someIP/root@someOtherIP@garbagevalue" + DescribeTable("gets pod network successfully from annotation values", func(networkAnnot string) { + pod := testutils.NewFakePod(fakePodName, "net1", "") + pod.Annotations[networkAttachmentAnnot] = networkAnnot _, err = GetPodNetwork(pod) - Expect(err).To(HaveOccurred()) - - // invalid case 3 - not matching comma-delimited format - pod.Annotations[networkAttachmentAnnot] = "root@someIP/root@someOtherIP" - _, err = GetPodNetwork(pod) - Expect(err).To(HaveOccurred()) - }) + Expect(err).ToNot(HaveOccurred()) + }, + Entry("network without namespace", "net1"), + Entry("network with namespace", "default/net1"), + Entry("network with interface name", "net1@my_interface"), + Entry("network with interface name and namespace", "default/net1@my_interface"), + ) }) Context("setPodNetworkAnnotation", func() {