Merge pull request #1243 from adrianchiris/allow_undersocre_in_ifname

Change Validation of interface name
This commit is contained in:
Doug Smith 2024-05-23 09:43:12 -04:00 committed by GitHub
commit d9f1c7c6e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 29 deletions

View File

@ -23,6 +23,7 @@ import (
"os" "os"
"regexp" "regexp"
"strings" "strings"
"syscall"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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. // 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" // "Valid attachment names must be comprised of units of the DNS-1123 label format"
// [a-z0-9]([-a-z0-9]*[a-z0-9])? // [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. // 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 { 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 { 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])) 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) logging.Debugf("parsePodNetworkObjectName: parsed: %s, %s, %s", netNsName, networkName, netIfName)
return netNsName, networkName, netIfName, nil return netNsName, networkName, netIfName, nil
} }

View File

@ -1002,36 +1002,30 @@ users:
}) })
Context("parsePodNetworkObjectName", func() { Context("parsePodNetworkObjectName", func() {
It("fails to get podnetwork given bad annotation values", func() { DescribeTable("fails to get podnetwork given bad annotation values", func(networkAnnot string) {
fakePod := testutils.NewFakePod(fakePodName, "net1", "") pod := testutils.NewFakePod(fakePodName, "net1", "")
pod.Annotations[networkAttachmentAnnot] = networkAnnot
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"
_, err = GetPodNetwork(pod) _, err = GetPodNetwork(pod)
Expect(err).To(HaveOccurred()) 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 "@" DescribeTable("gets pod network successfully from annotation values", func(networkAnnot string) {
pod.Annotations[networkAttachmentAnnot] = "root@someIP/root@someOtherIP@garbagevalue" pod := testutils.NewFakePod(fakePodName, "net1", "")
pod.Annotations[networkAttachmentAnnot] = networkAnnot
_, err = GetPodNetwork(pod) _, err = GetPodNetwork(pod)
Expect(err).To(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
},
// invalid case 3 - not matching comma-delimited format Entry("network without namespace", "net1"),
pod.Annotations[networkAttachmentAnnot] = "root@someIP/root@someOtherIP" Entry("network with namespace", "default/net1"),
_, err = GetPodNetwork(pod) Entry("network with interface name", "net1@my_interface"),
Expect(err).To(HaveOccurred()) Entry("network with interface name and namespace", "default/net1@my_interface"),
}) )
}) })
Context("setPodNetworkAnnotation", func() { Context("setPodNetworkAnnotation", func() {