generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
webhookValidationConfig.go
115 lines (104 loc) · 3 KB
/
webhookValidationConfig.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
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package ancla
import (
"errors"
"fmt"
"time"
)
var (
SpecialUseIPs = []string{
"0.0.0.0/8", //local ipv4
"fe80::/10", //local ipv6
"255.255.255.255/32", //broadcast to neighbors
"2001::/32", //ipv6 TEREDO prefix
"2001:5::/32", //EID space for lisp
"2002::/16", //ipv6 6to4
"fc00::/7", //ipv6 unique local
"192.0.0.0/24", //ipv4 IANA
"2001:0000::/23", //ipv6 IANA
"224.0.0.1/32", //ipv4 multicast
}
SpecialUseHosts = []string{
".example.",
".invalid.",
".test.",
"localhost",
}
errFailedToBuildValidators = errors.New("failed to build validators")
errFailedToBuildValidURLFuncs = errors.New("failed to build ValidURLFuncs")
)
type ValidatorConfig struct {
URL URLVConfig
TTL TTLVConfig
}
type URLVConfig struct {
HTTPSOnly bool
AllowLoopback bool
AllowIP bool
AllowSpecialUseHosts bool
AllowSpecialUseIPs bool
InvalidHosts []string
InvalidSubnets []string
}
type TTLVConfig struct {
Max time.Duration
Jitter time.Duration
Now func() time.Time
}
// BuildValidURLFuncs translates the configuration into a list of ValidURLFuncs
// to be run on the webhook.
func buildValidURLFuncs(config ValidatorConfig) ([]ValidURLFunc, error) {
var v []ValidURLFunc
v = append(v, GoodURLScheme(config.URL.HTTPSOnly))
if !config.URL.AllowLoopback {
v = append(v, RejectLoopback())
}
if !config.URL.AllowIP {
v = append(v, RejectAllIPs())
}
if !config.URL.AllowSpecialUseHosts {
config.URL.InvalidHosts = append(config.URL.InvalidHosts, SpecialUseHosts...)
}
if len(config.URL.InvalidHosts) > 0 {
v = append(v, RejectHosts(config.URL.InvalidHosts))
}
if !config.URL.AllowSpecialUseIPs {
config.URL.InvalidSubnets = append(config.URL.InvalidSubnets, SpecialUseIPs...)
}
if len(config.URL.InvalidSubnets) > 0 {
fInvalidSubnets, err := InvalidSubnets(config.URL.InvalidSubnets)
if err != nil {
return nil, fmt.Errorf("%w: %v", errFailedToBuildValidURLFuncs, err)
}
v = append(v, fInvalidSubnets)
}
return v, nil
}
// BuildValidators translates the configuration into a list of validators to be run on the
// webhook.
func BuildValidators(config ValidatorConfig) (Validators, error) {
v, err := buildValidURLFuncs(config)
if err != nil {
return nil, fmt.Errorf("%w: %v", errFailedToBuildValidators, err)
}
vs := Validators{
GoodConfigURL(v),
GoodFailureURL(v),
GoodAlternativeURLs(v),
CheckEvents(),
CheckDeviceID(),
CheckUntilOrDurationExist(),
}
fCheckDuration, err := CheckDuration(config.TTL.Max)
if err != nil {
return nil, fmt.Errorf("%w: %v", errFailedToBuildValidators, err)
}
vs = append(vs, fCheckDuration)
fCheckUntil, err := CheckUntil(config.TTL.Jitter, config.TTL.Max, config.TTL.Now)
if err != nil {
return nil, fmt.Errorf("%w: %v", errFailedToBuildValidators, err)
}
vs = append(vs, fCheckUntil)
return vs, nil
}