/
custom_validations.go
581 lines (443 loc) · 15.7 KB
/
custom_validations.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package gaia
import (
"crypto/x509"
"encoding/pem"
"fmt"
"net"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"go.aporeto.io/gaia/constants"
"go.aporeto.io/elemental"
"go.aporeto.io/gaia/portutils"
"go.aporeto.io/gaia/protocols"
)
// ValidatePortString validates a string represents a port or a range of port.
// valid: 443, 443:555
func ValidatePortString(attribute string, portExp string) error {
// TODO: Use portutils to validate a port
ports := strings.Split(portExp, ":")
if len(ports) == 0 || len(ports) > 2 {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' must be a port (xxx) or port range (xxx:yyy)", attribute))
}
p1, err := strconv.Atoi(ports[0])
if err != nil {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' must be a port (xxx) or port range (xxx:yyy)", attribute))
}
if p1 < 1 || p1 > 65535 {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' must be a between 1 and 65535", attribute))
}
if len(ports) == 1 {
return nil
}
p2, err := strconv.Atoi(ports[1])
if err != nil {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' must be a port (xxx) or port range (xxx:yyy)", attribute))
}
if p2 < 1 || p2 > 65535 {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' must be a between 1 and 65535", attribute))
}
if p1 >= p2 {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' left bound is greater or equal to left bound ", attribute))
}
return nil
}
// ValidatePortStringList validates a list of ports.
func ValidatePortStringList(attribute string, ports []string) error {
for _, port := range ports {
if err := ValidatePortString(attribute, port); err != nil {
return err
}
}
return nil
}
var rxDNSName = regexp.MustCompile(`^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?$`)
// ValidateNetwork validates a CIDR.
func ValidateNetwork(attribute string, network string) error {
if _, _, err := net.ParseCIDR(network); err == nil {
return nil
}
if rxDNSName.MatchString(network) {
return nil
}
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' must be a CIDR or hostname", attribute))
}
// ValidateNetworkList validates a list of networks.
// The list cannot be empty
func ValidateNetworkList(attribute string, networks []string) error {
if len(networks) == 0 {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' must not be empty", attribute))
}
return ValidateOptionalNetworkList(attribute, networks)
}
// ValidateOptionalNetworkList validates a list of networks.
// It can be empty/
func ValidateOptionalNetworkList(attribute string, networks []string) error {
for _, network := range networks {
if err := ValidateNetwork(attribute, network); err != nil {
return err
}
}
return nil
}
// ValidateProtocol validates a string represents netwotk a protocol.
func ValidateProtocol(attribute string, proto string) error {
upperProto := strings.ToUpper(proto)
if protocols.L4ProtocolNumberFromName(upperProto) != -1 {
return nil
}
p, err := strconv.Atoi(proto)
if err != nil {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' valid protocol name or number", attribute))
}
if p < 0 || p > 255 {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' protocol number must be between 0 and 255 included", attribute))
}
return nil
}
// ValidateProtocolList validates a list of protocols.
func ValidateProtocolList(attribute string, protocols []string) error {
if len(protocols) == 0 {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' must not be empty", attribute))
}
for _, proto := range protocols {
if err := ValidateProtocol(attribute, proto); err != nil {
return err
}
}
return nil
}
// ValidateServiceEntity validates a Service.
func ValidateServiceEntity(service *Service) error {
var errs elemental.Errors
switch service.AuthorizationType {
case ServiceAuthorizationTypeOIDC:
if service.OIDCProviderURL == "" {
errs = errs.Append(makeValidationError("OIDCProviderURL", "`OIDCProviderURL` is required when `authorizationType` is set to `OIDC`"))
}
if u, err := url.Parse(service.OIDCProviderURL); err != nil || u == nil || u.Scheme != "https" {
errs = errs.Append(makeValidationError("OIDCProviderURL", "`OIDCProviderURL` must be a valid HTTPS URL: example https://xxx.yyy"))
}
if service.OIDCClientID == "" {
errs = errs.Append(makeValidationError("OIDCClientID", "`OIDCClientID` is required when `authorizationType` is set to `OIDC`"))
}
if service.OIDCClientSecret == "" {
errs = errs.Append(makeValidationError("OIDCClientSecret", "`OIDCClientSecret` is required when `authorizationType` is set to `OIDC`"))
}
case ServiceAuthorizationTypeJWT:
if service.JWTSigningCertificate == "" {
errs = errs.Append(makeValidationError("JWTSigningCertificate", "`JWTSigningCertificate` is required when `authorizationType` is set to `JWT`"))
}
}
if service.TLSType == ServiceTLSTypeExternal {
if service.TLSCertificate == "" {
errs = errs.Append(makeValidationError("TLSCertificate", "`TLSCertificate` is required when `TLSType` is set to `External`"))
}
if service.TLSCertificateKey == "" {
errs = errs.Append(makeValidationError("TLSCertificateKey", "`TLSCertificateKey` is required when `TLSType` is set to `External`"))
}
}
allSubnets := []*net.IPNet{}
for _, ip := range service.IPs {
ipNet, err := ipNetFromString(ip)
if err != nil {
errs = errs.Append(err)
continue
}
for j := 0; j < len(allSubnets); j++ {
if allSubnets[j].Contains(ipNet.IP) || ipNet.Contains(allSubnets[j].IP) {
errs = errs.Append(makeValidationError("IPs", "subnets cannot overlap"))
}
}
allSubnets = append(allSubnets, ipNet)
}
allHosts := map[string]bool{}
for _, name := range service.Hosts {
if !isFQDN(name) {
errs = errs.Append(makeValidationError("Hosts", "`Hosts` must be a valid hostname or FQDN, compliant with RF952"))
}
if _, ok := allHosts[name]; ok {
errs = errs.Append(makeValidationError("Hosts", "`Hosts` must be unique"))
}
allHosts[name] = true
}
if len(service.Hosts) == 0 && len(service.IPs) == 0 {
errs = errs.Append(makeValidationError("", "You must set at least one value in `hosts` or `IPs`"))
}
if len(errs) > 0 {
return errs
}
return nil
}
func makeValidationError(attribute string, message string) elemental.Error {
err := elemental.NewError(
"Validation Error",
message,
"gaia",
http.StatusUnprocessableEntity,
)
if attribute != "" {
err.Data = map[string]interface{}{"attribute": attribute}
}
return err
}
var regHostServiceName = regexp.MustCompile(`^[a-zA-Z0-9_]{0,11}$`)
// ValidateEnforcerProfile validates a an enforcer profile.
func ValidateEnforcerProfile(enforcerProfile *EnforcerProfile) error {
// Validate Target Networks
for _, tn := range enforcerProfile.TargetNetworks {
_, _, err := net.ParseCIDR(tn)
if err != nil {
return makeValidationError("targetNetworks", fmt.Sprintf("%s is not a valid CIDR", tn))
}
}
// Validate trusted CAs
for i, ca := range enforcerProfile.TrustedCAs {
rest := []byte(ca)
var block *pem.Block
for {
block, rest = pem.Decode(rest)
if block == nil || block.Type != "CERTIFICATE" {
return makeValidationError("trustedCAs", fmt.Sprintf("The CA %d is not a valid CA", i))
}
if _, err := x509.ParseCertificate(block.Bytes); err != nil {
return makeValidationError("trustedCAs", err.Error())
}
if len(rest) == 0 {
break
}
}
}
return nil
}
// ValidateProcessingUnitServicesList validates a list of processing unit services.
func ValidateProcessingUnitServicesList(attribute string, svcs []*ProcessingUnitService) error {
if _, _, err := ValidateProcessingUnitServicesListWithoutOverlap(svcs, map[int]*portutils.PortsList{}, map[int]*portutils.PortsRangeList{}); err != nil {
return makeValidationError(attribute, err.Error())
}
return nil
}
// ValidateProcessingUnitServicesListWithoutOverlap validates a list of processing unit services has no overlap with any given parameter.
func ValidateProcessingUnitServicesListWithoutOverlap(svcs []*ProcessingUnitService, cachePortsList map[int]*portutils.PortsList, cacheRanges map[int]*portutils.PortsRangeList) (map[int]*portutils.PortsList, map[int]*portutils.PortsRangeList, error) {
for _, svc := range svcs {
var cpl *portutils.PortsList
var cpr *portutils.PortsRangeList
var ok bool
if cpl, ok = cachePortsList[svc.Protocol]; !ok {
cpl = &portutils.PortsList{}
cachePortsList[svc.Protocol] = cpl
}
if cpr, ok = cacheRanges[svc.Protocol]; !ok {
cpr = &portutils.PortsRangeList{}
cacheRanges[svc.Protocol] = cpr
}
targetPorts := svc.TargetPorts
for _, ports := range targetPorts {
// Range port
if strings.Contains(ports, ":") {
pr, err := portutils.ConvertToPortsRange(ports)
if err != nil {
return nil, nil, err
}
if pr.HasOverlapWithPortsRanges(cpr) {
return nil, nil, fmt.Errorf("port range overlaps with another range")
}
if pr.HasOverlapWithPortsList(cpl) {
return nil, nil, fmt.Errorf("port range overlaps with another port")
}
*cpr = append(*cpr, pr)
cacheRanges[svc.Protocol] = cpr
continue
}
// Single & Multiple ports
pl, err := portutils.ConvertToPortsList(ports)
if err != nil {
return nil, nil, err
}
if pl.HasOverlapWithPortsList(cpl) {
return nil, nil, fmt.Errorf("port overlaps with another port")
}
if pl.HasOverlapWithPortsRanges(cpr) {
return nil, nil, fmt.Errorf("port overlaps with another port range")
}
*cpl = append(*cpl, *pl...)
cachePortsList[svc.Protocol] = cpl
}
}
return cachePortsList, cacheRanges, nil
}
// ValidateTimeDuration validates that the time duration provided is compliant
// with the go format.
func ValidateTimeDuration(attribute string, duration string) error {
_, err := time.ParseDuration(duration)
if err != nil {
return makeValidationError(attribute, fmt.Sprintf("Attribute '%s' must be valid duration (examaple: 1h or 30s)", attribute))
}
return nil
}
// hostname regex from github.com/go-playground/validator
var hostnameRegexRFC1123 = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9\-\.]+[a-z-Az0-9]$`)
func isFQDN(val string) bool {
if val == "" {
return false
}
if val[len(val)-1] == '.' {
val = val[0 : len(val)-1]
}
return hostnameRegexRFC1123.MatchString(val)
}
func ipNetFromString(ip string) (*net.IPNet, error) {
_, ipNet, err := net.ParseCIDR(ip)
if err != nil {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return nil, makeValidationError("IPs", "`IPs` must be a list of valid IPv4 address or CIDR notation")
}
ipNet = &net.IPNet{IP: parsedIP, Mask: []byte{0xf, 0xf, 0xf, 0xf}}
}
return ipNet, nil
}
// ValidateHTTPMethods validates the attribute methods is a list of HTTP verbs.
func ValidateHTTPMethods(attribute string, methods []string) error {
for _, m := range methods {
mu := strings.ToUpper(m)
if mu != http.MethodPost &&
mu != http.MethodGet &&
mu != http.MethodDelete &&
mu != http.MethodPut &&
mu != http.MethodHead &&
mu != http.MethodPatch {
return fmt.Errorf("invalid HTTP method %s", m)
}
}
return nil
}
// ValidateHostServices validates a host service. Applies to the new API only.
func ValidateHostServices(hs *HostService) error {
// Constraint on regex is used because the enforcer is using the name as nativeContextID.
if !regHostServiceName.MatchString(hs.Name) {
return makeValidationError("name", "Host service name must be less than 12 characters and contains only alphanumeric or _")
}
if !hs.HostModeEnabled && len(hs.Services) == 0 {
return makeValidationError("services", "Host service must have either HostModeEnabled or must declare services")
}
if err := ValidateHostServicesNonOverlapPorts(hs.Services); err != nil {
return makeValidationError("services", err.Error())
}
return nil
}
// ValidateHostServicesNonOverlapPorts validates a list of processing unit services has no overlap with any given parameter.
func ValidateHostServicesNonOverlapPorts(svcs []string) error {
udpPorts := portutils.PortsRangeList{}
tcpPorts := portutils.PortsRangeList{}
var pr *portutils.PortsRange
var protocol string
var err error
for _, svc := range svcs {
pr, protocol, err = portutils.ExtractPortsAndProtocolFromHostService(svc)
if err != nil {
return err
}
switch protocol {
case protocols.L4ProtocolTCP:
if pr.HasOverlapWithPortsRanges(&tcpPorts) {
return fmt.Errorf("host service cannot have overlapping TCP ports")
}
tcpPorts = append(tcpPorts, pr)
case protocols.L4ProtocolUDP:
if pr.HasOverlapWithPortsRanges(&udpPorts) {
return fmt.Errorf("host service cannot have overlapping UDP ports")
}
udpPorts = append(udpPorts, pr)
default:
return fmt.Errorf("host service has invalid format: %s", protocol)
}
}
return nil
}
// ValidateAudience validates an audience string.
func ValidateAudience(attribute string, audience string) error {
// TODO: not liking the idea of importing addedeffect here
return nil
}
// ValidatePEM validates a string contains a PEM.
func ValidatePEM(attribute string, pemdata string) error {
if pemdata == "" {
return nil
}
var i int
var block *pem.Block
rest := []byte(pemdata)
for {
block, rest = pem.Decode(rest)
if block == nil {
return makeValidationError(attribute, fmt.Sprintf("Unable to decode PEM number %d", i))
}
if len(rest) == 0 {
return nil
}
i++
}
}
// Constants to validate tags.
const (
prefixDynamicTag = "$"
prefixExpandedTag = "+"
prefixMetadata = "@"
)
// validateTagStrings validates the given string and check if it can be a valid value for a Tag.
func validateTagStrings(attribute string, acceptReservedPrefix bool, strs ...string) error {
for _, s := range strs {
if !acceptReservedPrefix && (strings.HasPrefix(s, prefixMetadata) || strings.HasPrefix(s, prefixDynamicTag) || strings.HasPrefix(s, prefixExpandedTag)) {
return makeValidationError(attribute, fmt.Sprintf("%s starts with an @, a $ or a + that is reserved", s))
}
if err := ValidateTag(attribute, s); err != nil {
return err
}
}
return nil
}
// tagRegex is the regular expression to check the format of a tag.
var tagRegex = regexp.MustCompile(`^[^= ]+=.+`)
// ValidateTag validates a single tag.
func ValidateTag(attribute string, tag string) error {
if !tagRegex.MatchString(tag) {
return makeValidationError(attribute, fmt.Sprintf("`%s must contain at least one '=' symbol separating two valid words", tag))
}
return nil
}
// ValidateTags validates a list of tags are valid. Accepts those with reserved prefix.
func ValidateTags(attribute string, tags []string) error {
return validateTagStrings(attribute, true, tags...)
}
// ValidateTagsWithoutReservedPrefixes a list of tags are valid. Refuse those with reserved prefix.
func ValidateTagsWithoutReservedPrefixes(attribute string, tags []string) error {
return validateTagStrings(attribute, false, tags...)
}
// ValidateTagsExpression validates an [][]string is a valid tag expression.
func ValidateTagsExpression(attribute string, expression [][]string) error {
for _, tags := range expression {
if err := ValidateTags(attribute, tags); err != nil {
return err
}
}
return nil
}
// ValidateMetadata validates an []string is a valid list of metadata.
func ValidateMetadata(attribute string, metadata []string) error {
for _, m := range metadata {
if !strings.HasPrefix(m, prefixMetadata) {
return makeValidationError(attribute, fmt.Sprintf("Metadata %s does not starts with an @, a $ or a + that is reserved", m))
}
if strings.HasPrefix(m, constants.AuthKey) {
return makeValidationError(attribute, fmt.Sprintf("Metadata %s is using @auth: which is reserved", m))
}
if err := ValidateTag(attribute, m); err != nil {
return err
}
}
return nil
}