-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
envoy_listener.go
433 lines (378 loc) · 14.3 KB
/
envoy_listener.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package translation
import (
"cmp"
"fmt"
goslices "slices"
"syscall"
envoy_config_core_v3 "github.com/cilium/proxy/go/envoy/config/core/v3"
envoy_config_listener "github.com/cilium/proxy/go/envoy/config/listener/v3"
envoy_extensions_listener_proxy_protocol_v3 "github.com/cilium/proxy/go/envoy/extensions/filters/listener/proxy_protocol/v3"
envoy_extensions_listener_tls_inspector_v3 "github.com/cilium/proxy/go/envoy/extensions/filters/listener/tls_inspector/v3"
httpConnectionManagerv3 "github.com/cilium/proxy/go/envoy/extensions/filters/network/http_connection_manager/v3"
envoy_extensions_filters_network_tcp_v3 "github.com/cilium/proxy/go/envoy/extensions/filters/network/tcp_proxy/v3"
envoy_extensions_transport_sockets_tls_v3 "github.com/cilium/proxy/go/envoy/extensions/transport_sockets/tls/v3"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"github.com/cilium/cilium/operator/pkg/model"
"github.com/cilium/cilium/pkg/envoy"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/slices"
)
const (
defaultTCPKeepAlive = 1 // enabled
defaultTCPKeepAliveIdleTimeInSeconds = 10
defaultTCPKeepAliveProbeIntervalInSeconds = 5
defaultTCPKeepAliveMaxFailures = 10
)
const (
httpConnectionManagerType = "envoy.filters.network.http_connection_manager"
tcpProxyType = "envoy.filters.network.tcp_proxy"
tlsInspectorType = "envoy.filters.listener.tls_inspector"
proxyProtocolType = "envoy.filters.listener.proxy_protocol"
tlsTransportSocketType = "envoy.transport_sockets.tls"
rawBufferTransportProtocol = "raw_buffer"
tlsTransportProtocol = "tls"
)
type ListenerMutator func(*envoy_config_listener.Listener) *envoy_config_listener.Listener
func WithProxyProtocol() ListenerMutator {
return func(listener *envoy_config_listener.Listener) *envoy_config_listener.Listener {
proxyListener := &envoy_config_listener.ListenerFilter{
Name: proxyProtocolType,
ConfigType: &envoy_config_listener.ListenerFilter_TypedConfig{
TypedConfig: toAny(&envoy_extensions_listener_proxy_protocol_v3.ProxyProtocol{}),
},
}
listener.ListenerFilters = append([]*envoy_config_listener.ListenerFilter{proxyListener}, listener.ListenerFilters...)
return listener
}
}
func WithAlpn() ListenerMutator {
return func(listener *envoy_config_listener.Listener) *envoy_config_listener.Listener {
for _, filterChain := range listener.FilterChains {
transportSocket := filterChain.GetTransportSocket()
if transportSocket == nil {
continue
}
downstreamContext := &envoy_extensions_transport_sockets_tls_v3.DownstreamTlsContext{}
err := proto.Unmarshal(transportSocket.ConfigType.(*envoy_config_core_v3.TransportSocket_TypedConfig).TypedConfig.Value, downstreamContext)
if err != nil {
continue
}
// Use `h2,http/1.1` to support both HTTP/2 and HTTP/1.1
downstreamContext.CommonTlsContext.AlpnProtocols = []string{"h2,http/1.1"}
transportSocket.ConfigType = &envoy_config_core_v3.TransportSocket_TypedConfig{
TypedConfig: toAny(downstreamContext),
}
}
return listener
}
}
func WithXffNumTrustedHops(xff uint32) ListenerMutator {
return func(listener *envoy_config_listener.Listener) *envoy_config_listener.Listener {
if xff == 0 {
return listener
}
for _, filterChain := range listener.FilterChains {
for _, filter := range filterChain.Filters {
if filter.Name == httpConnectionManagerType {
tc := filter.GetTypedConfig()
switch tc.GetTypeUrl() {
case envoy.HttpConnectionManagerTypeURL:
hcm, err := tc.UnmarshalNew()
if err != nil {
continue
}
hcmConfig, ok := hcm.(*httpConnectionManagerv3.HttpConnectionManager)
if !ok {
continue
}
hcmConfig.XffNumTrustedHops = xff
filter.ConfigType = &envoy_config_listener.Filter_TypedConfig{
TypedConfig: toAny(hcmConfig),
}
}
}
}
}
return listener
}
}
func WithHostNetworkPort(m *model.Model, ipv4Enabled bool, ipv6Enabled bool) ListenerMutator {
ports := []uint32{}
for _, hl := range m.HTTP {
ports = append(ports, hl.GetPort())
}
for _, hl := range m.TLSPassthrough {
ports = append(ports, hl.GetPort())
}
return func(listener *envoy_config_listener.Listener) *envoy_config_listener.Listener {
listener.Address, listener.AdditionalAddresses = getHostNetworkListenerAddresses(slices.SortedUnique(ports), ipv4Enabled, ipv6Enabled)
return listener
}
}
func WithSocketOption(tcpKeepAlive, tcpKeepIdleInSeconds, tcpKeepAliveProbeIntervalInSeconds, tcpKeepAliveMaxFailures int64) ListenerMutator {
return func(listener *envoy_config_listener.Listener) *envoy_config_listener.Listener {
listener.SocketOptions = []*envoy_config_core_v3.SocketOption{
{
Description: "Enable TCP keep-alive (default to enabled)",
Level: syscall.SOL_SOCKET,
Name: syscall.SO_KEEPALIVE,
Value: &envoy_config_core_v3.SocketOption_IntValue{
IntValue: tcpKeepAlive,
},
State: envoy_config_core_v3.SocketOption_STATE_PREBIND,
},
{
Description: "TCP keep-alive idle time (in seconds) (defaults to 10s)",
Level: syscall.IPPROTO_TCP,
Name: syscall.TCP_KEEPIDLE,
Value: &envoy_config_core_v3.SocketOption_IntValue{
IntValue: tcpKeepIdleInSeconds,
},
State: envoy_config_core_v3.SocketOption_STATE_PREBIND,
},
{
Description: "TCP keep-alive probe intervals (in seconds) (defaults to 5s)",
Level: syscall.IPPROTO_TCP,
Name: syscall.TCP_KEEPINTVL,
Value: &envoy_config_core_v3.SocketOption_IntValue{
IntValue: tcpKeepAliveProbeIntervalInSeconds,
},
State: envoy_config_core_v3.SocketOption_STATE_PREBIND,
},
{
Description: "TCP keep-alive probe max failures.",
Level: syscall.IPPROTO_TCP,
Name: syscall.TCP_KEEPCNT,
Value: &envoy_config_core_v3.SocketOption_IntValue{
IntValue: tcpKeepAliveMaxFailures,
},
State: envoy_config_core_v3.SocketOption_STATE_PREBIND,
},
}
return listener
}
}
// newListenerWithDefaults same as newListener but with default mutators applied.
func newListenerWithDefaults(name string, ciliumSecretNamespace string, includeHTTPFilterchain bool, tlsSecretsToHostnames map[model.TLSSecret][]string, ptBackendsToHostnames map[string][]string, mutatorFunc ...ListenerMutator) (ciliumv2.XDSResource, error) {
fns := append(mutatorFunc,
WithSocketOption(
defaultTCPKeepAlive,
defaultTCPKeepAliveIdleTimeInSeconds,
defaultTCPKeepAliveProbeIntervalInSeconds,
defaultTCPKeepAliveMaxFailures),
)
return newListener(name, ciliumSecretNamespace, includeHTTPFilterchain, tlsSecretsToHostnames, ptBackendsToHostnames, fns...)
}
func httpFilterChain(name string) (*envoy_config_listener.FilterChain, error) {
insecureHttpConnectionManagerName := fmt.Sprintf("%s-insecure", name)
insecureHttpConnectionManager, err := NewHTTPConnectionManager(
insecureHttpConnectionManagerName,
insecureHttpConnectionManagerName,
)
if err != nil {
return nil, err
}
return &envoy_config_listener.FilterChain{
FilterChainMatch: &envoy_config_listener.FilterChainMatch{TransportProtocol: rawBufferTransportProtocol},
Filters: []*envoy_config_listener.Filter{
{
Name: httpConnectionManagerType,
ConfigType: &envoy_config_listener.Filter_TypedConfig{
TypedConfig: insecureHttpConnectionManager.Any,
},
},
},
}, nil
}
func httpsFilterChains(name string, ciliumSecretNamespace string, tlsSecretsToHostnames map[model.TLSSecret][]string) ([]*envoy_config_listener.FilterChain, error) {
if len(tlsSecretsToHostnames) == 0 {
return nil, nil
}
var filterChains []*envoy_config_listener.FilterChain
orderedSecrets := maps.Keys(tlsSecretsToHostnames)
goslices.SortStableFunc(orderedSecrets, func(a, b model.TLSSecret) int { return cmp.Compare(a.Namespace+"/"+a.Name, b.Namespace+"/"+b.Name) })
for _, secret := range orderedSecrets {
hostNames := tlsSecretsToHostnames[secret]
secureHttpConnectionManagerName := fmt.Sprintf("%s-secure", name)
secureHttpConnectionManager, err := NewHTTPConnectionManager(secureHttpConnectionManagerName, secureHttpConnectionManagerName)
if err != nil {
return nil, err
}
transportSocket, err := newTransportSocket(ciliumSecretNamespace, []model.TLSSecret{secret})
if err != nil {
return nil, err
}
filterChains = append(filterChains, &envoy_config_listener.FilterChain{
FilterChainMatch: toFilterChainMatch(hostNames),
Filters: []*envoy_config_listener.Filter{
{
Name: httpConnectionManagerType,
ConfigType: &envoy_config_listener.Filter_TypedConfig{
TypedConfig: secureHttpConnectionManager.Any,
},
},
},
TransportSocket: transportSocket,
})
}
return filterChains, nil
}
// newListener creates a new Envoy listener with the given name.
// The listener will have both secure and insecure filters.
//
// Secret Discovery Service (SDS) is used to fetch the TLS certificates.
func newListener(name string, ciliumSecretNamespace string, includeHTTPFilterchain bool, tlsSecretsToHostnames map[model.TLSSecret][]string, tlsPassthroughBackendsMap map[string][]string, mutatorFunc ...ListenerMutator) (ciliumv2.XDSResource, error) {
filterChains := []*envoy_config_listener.FilterChain{}
if includeHTTPFilterchain {
httpFilterChain, err := httpFilterChain(name)
if err != nil {
return ciliumv2.XDSResource{}, err
}
filterChains = append(filterChains, httpFilterChain)
}
httpsFilterChains, err := httpsFilterChains(name, ciliumSecretNamespace, tlsSecretsToHostnames)
if err != nil {
return ciliumv2.XDSResource{}, fmt.Errorf("failed to create https filterchains: %w", err)
}
filterChains = append(filterChains, httpsFilterChains...)
tlsPassthroughFilterChains, err := tlsPassthroughFilterChains(tlsPassthroughBackendsMap)
if err != nil {
return ciliumv2.XDSResource{}, fmt.Errorf("failed to create tls passthrough filterchains: %w", err)
}
filterChains = append(filterChains, tlsPassthroughFilterChains...)
listener := &envoy_config_listener.Listener{
Name: name,
FilterChains: filterChains,
ListenerFilters: []*envoy_config_listener.ListenerFilter{
{
Name: tlsInspectorType,
ConfigType: &envoy_config_listener.ListenerFilter_TypedConfig{
TypedConfig: toAny(&envoy_extensions_listener_tls_inspector_v3.TlsInspector{}),
},
},
},
}
for _, fn := range mutatorFunc {
listener = fn(listener)
}
listenerBytes, err := proto.Marshal(listener)
if err != nil {
return ciliumv2.XDSResource{}, err
}
return ciliumv2.XDSResource{
Any: &anypb.Any{
TypeUrl: envoy.ListenerTypeURL,
Value: listenerBytes,
},
}, nil
}
func getHostNetworkListenerAddresses(ports []uint32, ipv4Enabled, ipv6Enabled bool) (*envoy_config_core_v3.Address, []*envoy_config_listener.AdditionalAddress) {
if len(ports) == 0 || (!ipv4Enabled && !ipv6Enabled) {
return nil, nil
}
bindAddresses := []string{}
if ipv4Enabled {
bindAddresses = append(bindAddresses, "0.0.0.0")
}
if ipv6Enabled {
bindAddresses = append(bindAddresses, "::")
}
addresses := []*envoy_config_core_v3.Address_SocketAddress{}
for _, p := range ports {
for _, a := range bindAddresses {
addresses = append(addresses, &envoy_config_core_v3.Address_SocketAddress{
SocketAddress: &envoy_config_core_v3.SocketAddress{
Protocol: envoy_config_core_v3.SocketAddress_TCP,
Address: a,
PortSpecifier: &envoy_config_core_v3.SocketAddress_PortValue{PortValue: p},
},
})
}
}
var additionalAddress []*envoy_config_listener.AdditionalAddress
if len(addresses) > 1 {
for _, a := range addresses[1:] {
additionalAddress = append(additionalAddress, &envoy_config_listener.AdditionalAddress{
Address: &envoy_config_core_v3.Address{
Address: a,
},
})
}
}
return &envoy_config_core_v3.Address{
Address: addresses[0],
}, additionalAddress
}
func tlsPassthroughFilterChains(ptBackendsToHostnames map[string][]string) ([]*envoy_config_listener.FilterChain, error) {
if len(ptBackendsToHostnames) == 0 {
return nil, nil
}
var filterChains []*envoy_config_listener.FilterChain
orderedBackends := maps.Keys(ptBackendsToHostnames)
goslices.Sort(orderedBackends)
for _, backend := range orderedBackends {
hostNames := ptBackendsToHostnames[backend]
filterChains = append(filterChains, &envoy_config_listener.FilterChain{
FilterChainMatch: toFilterChainMatch(hostNames),
Filters: []*envoy_config_listener.Filter{
{
Name: tcpProxyType,
ConfigType: &envoy_config_listener.Filter_TypedConfig{
TypedConfig: toAny(&envoy_extensions_filters_network_tcp_v3.TcpProxy{
StatPrefix: backend,
ClusterSpecifier: &envoy_extensions_filters_network_tcp_v3.TcpProxy_Cluster{
Cluster: backend,
},
}),
},
},
},
})
}
return filterChains, nil
}
func newTransportSocket(ciliumSecretNamespace string, tls []model.TLSSecret) (*envoy_config_core_v3.TransportSocket, error) {
var tlsSdsConfig []*envoy_extensions_transport_sockets_tls_v3.SdsSecretConfig
tlsMap := map[string]struct{}{}
for _, t := range tls {
tlsMap[fmt.Sprintf("%s/%s-%s", ciliumSecretNamespace, t.Namespace, t.Name)] = struct{}{}
}
for k := range tlsMap {
tlsSdsConfig = append(tlsSdsConfig, &envoy_extensions_transport_sockets_tls_v3.SdsSecretConfig{
Name: k,
})
}
downStreamContext := envoy_extensions_transport_sockets_tls_v3.DownstreamTlsContext{
CommonTlsContext: &envoy_extensions_transport_sockets_tls_v3.CommonTlsContext{
TlsCertificateSdsSecretConfigs: tlsSdsConfig,
},
}
downstreamBytes, err := proto.Marshal(&downStreamContext)
if err != nil {
return nil, err
}
return &envoy_config_core_v3.TransportSocket{
Name: tlsTransportSocketType,
ConfigType: &envoy_config_core_v3.TransportSocket_TypedConfig{
TypedConfig: &anypb.Any{
TypeUrl: envoy.DownstreamTlsContextURL,
Value: downstreamBytes,
},
},
}, nil
}
func toFilterChainMatch(hostNames []string) *envoy_config_listener.FilterChainMatch {
res := &envoy_config_listener.FilterChainMatch{
TransportProtocol: tlsTransportProtocol,
}
// ServerNames must be sorted and unique, however, envoy don't support "*" as a server name
serverNames := slices.SortedUnique(hostNames)
if len(serverNames) > 1 || (len(serverNames) == 1 && serverNames[0] != "*") {
res.ServerNames = serverNames
}
return res
}