Skip to content

Commit

Permalink
Merge pull request #1243 from adrianchiris/allow_undersocre_in_ifname
Browse files Browse the repository at this point in the history
Change Validation of interface name
  • Loading branch information
dougbtv authored May 23, 2024
2 parents 75c0245 + d625d48 commit d9f1c7c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
13 changes: 10 additions & 3 deletions pkg/k8sclient/k8sclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"regexp"
"strings"
"syscall"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -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
}
Expand Down
48 changes: 21 additions & 27 deletions pkg/k8sclient/k8sclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

// invalid case 2 - can't have more than 2 items separated by "@"
pod.Annotations[networkAttachmentAnnot] = "root@someIP/root@someOtherIP@garbagevalue"
},
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"),
)

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() {
Expand Down

0 comments on commit d9f1c7c

Please sign in to comment.