-
Notifications
You must be signed in to change notification settings - Fork 10
/
validation_ingress.go
178 lines (152 loc) · 5.95 KB
/
validation_ingress.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
package main
import (
"fmt"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
extv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
var ingressHostRegExp = regexp.MustCompile(`^([a-zA-Z0-9_][a-zA-Z0-9_-]{0,62})(\.[a-zA-Z0-9_][a-zA-Z0-9_-]{0,62})*[._]?$`)
var ingressPathRegExp = regexp.MustCompile(`^[A-zA-Z0-9_/.\-]*$`)
type TlsDefinition struct {
host string
secretName string
ingressName string
ingressNamespace string
}
type PathDefinition struct {
host string
path string
serviceName string
servicePort string
ingressName string
ingressNamespace string
}
func (pathDefinition *PathDefinition) toUri() string {
return pathDefinition.host + pathDefinition.path
}
func (pathDefinition *PathDefinition) toServiceTarget() string {
return pathDefinition.serviceName + "." + pathDefinition.ingressNamespace + ":" + pathDefinition.servicePort
}
func ValidateIngress(validation *objectValidation, ingress *extv1beta1.Ingress, config *config, clientSet *kubernetes.Clientset) error {
if config.RuleIngressCollision {
targetDesc := fmt.Sprintf("Ingress %s.%s: ", ingress.Name, ingress.Namespace)
existingIngresses, err := IngressClientAllNamespaces(clientSet).List(metav1.ListOptions{})
if err != nil {
return err
}
log.Debugf("There are %d ingresses in the cluster to be compared", len(existingIngresses.Items))
newIngressPathData := ingressPath(ingress)
newIngressTlsData := ingressTls(ingress)
ValidatePathDataRegex(newIngressPathData, validation, targetDesc)
ValidateTlsDataRegex(newIngressTlsData, validation, targetDesc)
var existingIngressesPathData []PathDefinition
for _, existingIngress := range existingIngresses.Items {
existingIngressesPathData = append(existingIngressesPathData, ingressPath(&existingIngress)...)
}
ValidatePathDataCollision(newIngressPathData, existingIngressesPathData, validation, targetDesc)
var existingIngressesTlsData []TlsDefinition
for _, existingIngress := range existingIngresses.Items {
existingIngressesTlsData = append(existingIngressesTlsData, ingressTls(&existingIngress)...)
}
ValidateTlsDataCollision(newIngressTlsData, existingIngressesTlsData, validation, targetDesc)
}
return nil
}
func ValidateTlsDataRegex(tlsDefinition []TlsDefinition, validation *objectValidation, targetDesc string) {
for _, tls := range tlsDefinition {
validateHost(tls.host, validation, targetDesc)
}
}
func ValidateTlsDataCollision(newIngressTlsData []TlsDefinition, existingIngressesTlsData []TlsDefinition, validation *objectValidation, targetDesc string) {
for _, newTls := range newIngressTlsData {
for _, existingTls := range existingIngressesTlsData {
if newTls.host == existingTls.host {
//if hosts are identical then also secret name and namespace has to match
if !(newTls.ingressNamespace == existingTls.ingressNamespace && newTls.secretName == existingTls.secretName) {
validation.Violations.add(
validationViolation{
targetDesc,
fmt.Sprintf("TLS collision with '%s.%s' on '%s'", existingTls.ingressName, existingTls.ingressNamespace, existingTls.host),
},
)
}
}
}
}
}
func ValidatePathDataRegex(pathData []PathDefinition, validation *objectValidation, targetDesc string) {
for _, path := range pathData {
validatePath(path.path, validation, targetDesc)
validateHost(path.host, validation, targetDesc)
}
}
func ValidatePathDataCollision(newIngressPathData []PathDefinition, existingIngressPathData []PathDefinition, validation *objectValidation, targetDesc string) {
for _, newIngressPath := range newIngressPathData {
for _, existingIngressPath := range existingIngressPathData {
// only other ingresses are considered - when updating it's not a collision
if nameWithNamespace(newIngressPath) != nameWithNamespace(existingIngressPath) {
if newIngressPath.toUri() == existingIngressPath.toUri() {
if newIngressPath.toServiceTarget() != existingIngressPath.toServiceTarget() {
violation := validationViolation{
targetDesc,
fmt.Sprintf("Path collision with '%s' -> '%s'", existingIngressPath.toUri(), existingIngressPath.toServiceTarget()),
}
validation.Violations.add(violation)
}
}
}
}
}
}
func nameWithNamespace(pathDefinition PathDefinition) string {
return pathDefinition.ingressName + "." + pathDefinition.ingressNamespace
}
func ingressPath(ingress *extv1beta1.Ingress) (result []PathDefinition) {
for _, rule := range ingress.Spec.Rules {
host := rule.Host
if rule.HTTP != nil {
for _, path := range rule.HTTP.Paths {
pathValue := path.Path
if pathValue == "" {
pathValue = "/"
}
pathDefinition := PathDefinition{
host: host,
path: pathValue,
serviceName: path.Backend.ServiceName,
servicePort: path.Backend.ServicePort.String(),
ingressName: ingress.Name,
ingressNamespace: ingress.Namespace,
}
result = append(result, pathDefinition)
}
} else {
log.Warnf("No http definition for %s.%s in rule %v", ingress.Name, ingress.Namespace, rule)
}
}
return
}
func ingressTls(ingress *extv1beta1.Ingress) (result []TlsDefinition) {
for _, tls := range ingress.Spec.TLS {
for _, host := range tls.Hosts {
tlsDefinition := TlsDefinition{host, tls.SecretName, ingress.Name, ingress.Namespace}
result = append(result, tlsDefinition)
}
}
return
}
func validateHost(host string, validation *objectValidation, targetDesc string) {
if !ingressHostRegExp.MatchString(host) {
validation.Violations.add(validationViolation{targetDesc, fmt.Sprintf("Host '%s' is not valid", host)})
}
}
func validatePath(path string, validation *objectValidation, targetDesc string) {
valid := strings.HasPrefix(path, "/")
valid = valid && ingressPathRegExp.MatchString(path)
if !valid {
validation.Violations.add(validationViolation{targetDesc, fmt.Sprintf("Path '%s' is not valid", path)})
}
}