-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathvalidation.go
187 lines (151 loc) · 4.75 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"errors"
"fmt"
"net"
"regexp"
"strconv"
"strings"
"k8s.io/apimachinery/pkg/util/validation"
)
const (
// Regex from: https://github.com/kubernetes-sigs/gateway-api/blob/v1.2.1/apis/v1/shared_types.go#L660
controllerNameRegex = `^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/[A-Za-z0-9\/\-._~%!$&'()*+,;=:]+$` //nolint:lll
)
func validateGatewayControllerName(value string) error {
if len(value) == 0 {
return errors.New("must be set")
}
fields := strings.Split(value, "/")
l := len(fields)
if l < 2 {
return errors.New("invalid format; must be DOMAIN/PATH")
}
if fields[0] != domain {
return fmt.Errorf("invalid domain: %s; domain must be: %s", fields[0], domain)
}
re := regexp.MustCompile(controllerNameRegex)
if !re.MatchString(value) {
return fmt.Errorf("invalid gateway controller name: %s; expected format is DOMAIN/PATH", value)
}
return nil
}
func validateResourceName(value string) error {
if len(value) == 0 {
return errors.New("must be set")
}
// used by Kubernetes to validate resource names
messages := validation.IsDNS1123Subdomain(value)
if len(messages) > 0 {
msg := strings.Join(messages, "; ")
return fmt.Errorf("invalid format: %s", msg)
}
return nil
}
func validateQualifiedName(name string) error {
if len(name) == 0 {
return errors.New("must be set")
}
messages := validation.IsQualifiedName(name)
if len(messages) > 0 {
msg := strings.Join(messages, "; ")
return fmt.Errorf("invalid format: %s", msg)
}
return nil
}
func validateIP(ip string) error {
if ip == "" {
return errors.New("IP address must be set")
}
if net.ParseIP(ip) == nil {
return fmt.Errorf("%q must be a valid IP address", ip)
}
return nil
}
// validateEndpoint validates an endpoint, which is <host>:<port> where host is either a hostname or an IP address.
func validateEndpoint(endpoint string) error {
host, port, err := net.SplitHostPort(endpoint)
if err != nil {
return fmt.Errorf("%q must be in the format <host>:<port>: %w", endpoint, err)
}
portVal, err := strconv.ParseInt(port, 10, 16)
if err != nil {
return fmt.Errorf("port must be a valid number: %w", err)
}
if portVal < 1 || portVal > 65535 {
return fmt.Errorf("port outside of valid port range [1 - 65535]: %v", port)
}
if err := validateIP(host); err == nil {
return nil
}
if errs := validation.IsDNS1123Subdomain(host); len(errs) == 0 {
return nil
}
// we don't know if the user intended to use a hostname or an IP address,
// so we return a generic error message
return fmt.Errorf("%q must be in the format <host>:<port>", endpoint)
}
func validateEndpointOptionalPort(value string) error {
if len(value) == 0 {
return errors.New("must be set")
}
// This function assumes a port exists. If it doesn't, ignore those errors. Any errors with the endpoint
// will be caught by further validation.
host, port, err := net.SplitHostPort(value)
if err != nil &&
(!strings.Contains(err.Error(), "missing port") && !strings.Contains(err.Error(), "too many colons")) {
return fmt.Errorf("error splitting %q into host and port: %w", value, err)
}
if port != "" {
portVal, err := strconv.ParseInt(port, 10, 16)
if err != nil {
return fmt.Errorf("port must be a valid number: %w", err)
}
if portVal < 1 || portVal > 65535 {
return fmt.Errorf("port outside of valid port range [1 - 65535]: %v", port)
}
}
if host == "" {
host = value
}
if err := validateIP(host); err == nil {
return nil
}
if errs := validation.IsDNS1123Subdomain(host); len(errs) == 0 {
return nil
}
// we don't know if the user intended to use a hostname or an IP address,
// so we return a generic error message
return fmt.Errorf("%q must be a domain name or IP address with optional port", value)
}
// validatePort makes sure a given port is inside the valid port range for its usage.
func validatePort(port int) error {
if port < 1024 || port > 65535 {
return fmt.Errorf("port outside of valid port range [1024 - 65535]: %v", port)
}
return nil
}
// ensureNoPortCollisions checks if the same port has been defined multiple times.
func ensureNoPortCollisions(ports ...int) error {
seen := make(map[int]struct{})
for _, port := range ports {
if _, ok := seen[port]; ok {
return fmt.Errorf("port %d has been defined multiple times", port)
}
seen[port] = struct{}{}
}
return nil
}
// validateCopyArgs ensures that arguments to the initialize command are set.
func validateCopyArgs(srcFiles []string, destDirs []string) error {
if len(srcFiles) != len(destDirs) {
return errors.New("source and destination must have the same number of elements")
}
if len(srcFiles) == 0 {
return errors.New("source must not be empty")
}
if len(destDirs) == 0 {
return errors.New("destination must not be empty")
}
return nil
}