-
Notifications
You must be signed in to change notification settings - Fork 665
/
contourconfig_helpers.go
221 lines (190 loc) · 6.15 KB
/
contourconfig_helpers.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
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1alpha1
import (
"fmt"
"k8s.io/apimachinery/pkg/util/sets"
)
// Validate configuration that is not already covered by CRD validation.
func (c *ContourConfigurationSpec) Validate() error {
// Validation of root configuration fields.
if err := endpointsInConfict(c.Health, c.Metrics); err != nil {
return fmt.Errorf("invalid contour configuration: %v", err)
}
// Validation of nested configuration structs.
var validateFuncs []func() error
if c.XDSServer != nil {
validateFuncs = append(validateFuncs, c.XDSServer.Type.Validate)
}
if c.Envoy != nil {
validateFuncs = append(validateFuncs, c.Envoy.Validate)
}
if c.Gateway != nil {
validateFuncs = append(validateFuncs, c.Gateway.Validate)
}
for _, validate := range validateFuncs {
if err := validate(); err != nil {
return err
}
}
return nil
}
func (x XDSServerType) Validate() error {
switch x {
case ContourServerType, EnvoyServerType:
return nil
default:
return fmt.Errorf("invalid xDS server type %q", x)
}
}
func (d ClusterDNSFamilyType) Validate() error {
switch d {
case AutoClusterDNSFamily, IPv4ClusterDNSFamily, IPv6ClusterDNSFamily:
return nil
default:
return fmt.Errorf("invalid cluster dns family type %q", d)
}
}
// Validate configuration that cannot be handled with CRD validation.
func (e *EnvoyConfig) Validate() error {
if err := endpointsInConfict(e.Health, e.Metrics); err != nil {
return fmt.Errorf("invalid envoy configuration: %v", err)
}
if err := e.Logging.Validate(); err != nil {
return err
}
// DefaultHTTPVersions
var invalidHTTPVersions []string
for _, v := range e.DefaultHTTPVersions {
switch v {
case HTTPVersion1, HTTPVersion2:
continue
default:
invalidHTTPVersions = append(invalidHTTPVersions, string(v))
}
}
if len(invalidHTTPVersions) > 0 {
return fmt.Errorf("invalid HTTP versions %q", invalidHTTPVersions)
}
// Cluster.DNSLookupFamily
if e.Cluster != nil {
if err := e.Cluster.DNSLookupFamily.Validate(); err != nil {
return err
}
}
// Envoy TLS configuration
if e.Listener != nil && e.Listener.TLS != nil {
return e.Listener.TLS.Validate()
}
return nil
}
// Validate ensures EnvoyTLS configuration is valid.
func (e *EnvoyTLS) Validate() error {
if e.MinimumProtocolVersion != "" && e.MinimumProtocolVersion != "1.2" && e.MinimumProtocolVersion != "1.3" {
return fmt.Errorf("invalid TLS minimum protocol version %q", e.MinimumProtocolVersion)
}
var invalidCipherSuites []string
for _, c := range e.CipherSuites {
if _, ok := ValidTLSCiphers[c]; !ok {
invalidCipherSuites = append(invalidCipherSuites, c)
}
}
if len(invalidCipherSuites) > 0 {
return fmt.Errorf("invalid cipher suites %q", invalidCipherSuites)
}
return nil
}
// SanitizedCipherSuites returns a deduplicated list of TLS ciphers.
// Order is maintained.
func (e *EnvoyTLS) SanitizedCipherSuites() []string {
if len(e.CipherSuites) == 0 {
return DefaultTLSCiphers
}
uniqueCiphers := sets.NewString()
// We also use a list so we can maintain the order.
validatedCiphers := []string{}
for _, c := range e.CipherSuites {
if !uniqueCiphers.Has(c) {
uniqueCiphers.Insert(c)
validatedCiphers = append(validatedCiphers, c)
}
}
return validatedCiphers
}
// Validate ensures that exactly one of ControllerName or GatewayRef are specified.
func (g *GatewayConfig) Validate() error {
if g == nil {
return nil
}
if len(g.ControllerName) > 0 && g.GatewayRef != nil {
return fmt.Errorf("invalid gateway configuration: exactly one of controller name or gateway ref must be specified")
}
if len(g.ControllerName) == 0 && g.GatewayRef == nil {
return fmt.Errorf("invalid gateway configuration: exactly one of controller name or gateway ref must be specified")
}
return nil
}
func (e *EnvoyLogging) Validate() error {
if e == nil {
return nil
}
if err := e.AccessLogFormat.Validate(); err != nil {
return err
}
if err := e.AccessLogJSONFields.Validate(); err != nil {
return err
}
return AccessLogFormatString(e.AccessLogFormatString).Validate()
}
// AccessLogFormatterExtensions returns a list of formatter extension names required by the access log format.
//
// Note: When adding support for new formatter, update the list of extensions here and
// add corresponding configuration in internal/envoy/v3/accesslog.go extensionConfig().
// Currently only one extension exist in Envoy.
func (e *EnvoyLogging) AccessLogFormatterExtensions() []string {
// Function that finds out if command operator is present in a format string.
contains := func(format, command string) bool {
tokens := commandOperatorRegexp.FindAllStringSubmatch(format, -1)
for _, t := range tokens {
if t[2] == command {
return true
}
}
return false
}
extensionsMap := make(map[string]bool)
switch e.AccessLogFormat {
case EnvoyAccessLog:
if contains(e.AccessLogFormatString, "REQ_WITHOUT_QUERY") {
extensionsMap["envoy.formatter.req_without_query"] = true
}
case JSONAccessLog:
for _, f := range e.AccessLogJSONFields.AsFieldMap() {
if contains(f, "REQ_WITHOUT_QUERY") {
extensionsMap["envoy.formatter.req_without_query"] = true
}
}
}
var extensions []string
for k := range extensionsMap {
extensions = append(extensions, k)
}
return extensions
}
// endpointsInConfict returns error if different protocol are configured to use single port.
func endpointsInConfict(health *HealthConfig, metrics *MetricsConfig) error {
if health != nil && metrics != nil && metrics.TLS != nil && health.Address == metrics.Address && health.Port == metrics.Port {
return fmt.Errorf("cannot use single port for health over HTTP and metrics over HTTPS")
}
return nil
}