Debian Codegraph @

pkg/utils/utils.go

raw
1 // Copyright 2019 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package utils
16
17 import (
18 "bytes"
19 "fmt"
20 "regexp"
21 "unicode"
22
23 "github.com/containernetworking/cni/pkg/types"
24 )
25
26 const (
27 // cniValidNameChars is the regexp used to validate valid characters in
28 // containerID and networkName
29 cniValidNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.\-]`
30
31 // maxInterfaceNameLength is the length max of a valid interface name
32 maxInterfaceNameLength = 15
33 )
34
35 var cniReg = regexp.MustCompile(`^` + cniValidNameChars + `*$`)
36
37 // ValidateContainerID will validate that the supplied containerID is not empty does not contain invalid characters
38 func ValidateContainerID(containerID string) *types.Error {
39 if containerID == "" {
40 return types.NewError(types.ErrUnknownContainer, "missing containerID", "")
41 }
42 if !cniReg.MatchString(containerID) {
43 return types.NewError(types.ErrInvalidEnvironmentVariables, "invalid characters in containerID", containerID)
44 }
45 return nil
46 }
47
48 // ValidateNetworkName will validate that the supplied networkName does not contain invalid characters
49 func ValidateNetworkName(networkName string) *types.Error {
50 if networkName == "" {
51 return types.NewError(types.ErrInvalidNetworkConfig, "missing network name:", "")
52 }
53 if !cniReg.MatchString(networkName) {
54 return types.NewError(types.ErrInvalidNetworkConfig, "invalid characters found in network name", networkName)
55 }
56 return nil
57 }
58
59 // ValidateInterfaceName will validate the interface name based on the four rules below
60 // 1. The name must not be empty
61 // 2. The name must be less than 16 characters
62 // 3. The name must not be "." or ".."
63 // 4. The name must not contain / or : or any whitespace characters
64 // ref to https://github.com/torvalds/linux/blob/master/net/core/dev.c#L1024
65 func ValidateInterfaceName(ifName string) *types.Error {
66 if len(ifName) == 0 {
67 return types.NewError(types.ErrInvalidEnvironmentVariables, "interface name is empty", "")
68 }
69 if len(ifName) > maxInterfaceNameLength {
70 return types.NewError(types.ErrInvalidEnvironmentVariables, "interface name is too long", fmt.Sprintf("interface name should be less than %d characters", maxInterfaceNameLength+1))
71 }
72 if ifName == "." || ifName == ".." {
73 return types.NewError(types.ErrInvalidEnvironmentVariables, "interface name is . or ..", "")
74 }
75 for _, r := range bytes.Runes([]byte(ifName)) {
76 if r == '/' || r == ':' || unicode.IsSpace(r) {
77 return types.NewError(types.ErrInvalidEnvironmentVariables, "interface name contains / or : or whitespace characters", "")
78 }
79 }
80
81 return nil
82 }