/
clienttrafficpolicy.go
667 lines (564 loc) · 19.4 KB
/
clienttrafficpolicy.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package gatewayapi
import (
"errors"
"fmt"
"sort"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/ptr"
gwv1b1 "sigs.k8s.io/gateway-api/apis/v1"
gwv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/status"
"github.com/envoyproxy/gateway/internal/utils"
)
const (
// Use an invalid string to represent all sections (listeners) within a Gateway
AllSections = "/"
)
func hasSectionName(policy *egv1a1.ClientTrafficPolicy) bool {
return policy.Spec.TargetRef.SectionName != nil
}
func (t *Translator) ProcessClientTrafficPolicies(resources *Resources,
gateways []*GatewayContext,
xdsIR XdsIRMap, infraIR InfraIRMap) []*egv1a1.ClientTrafficPolicy {
var res []*egv1a1.ClientTrafficPolicy
clientTrafficPolicies := resources.ClientTrafficPolicies
// Sort based on timestamp
sort.Slice(clientTrafficPolicies, func(i, j int) bool {
return clientTrafficPolicies[i].CreationTimestamp.Before(&(clientTrafficPolicies[j].CreationTimestamp))
})
policyMap := make(map[types.NamespacedName]sets.Set[string])
// Build a map out of gateways for faster lookup since users might have hundreds of gateway or more.
gatewayMap := map[types.NamespacedName]*policyGatewayTargetContext{}
for _, gw := range gateways {
key := utils.NamespacedName(gw)
gatewayMap[key] = &policyGatewayTargetContext{GatewayContext: gw}
}
// Translate
// 1. First translate Policies with a sectionName set
// 2. Then loop again and translate the policies without a sectionName
// TODO: Import sort order to ensure policy with same section always appear
// before policy with no section so below loops can be flattened into 1.
for _, policy := range clientTrafficPolicies {
if hasSectionName(policy) {
policy := policy.DeepCopy()
res = append(res, policy)
gateway, resolveErr := resolveCTPolicyTargetRef(policy, gatewayMap)
// Negative statuses have already been assigned so its safe to skip
if gateway == nil {
continue
}
key := utils.NamespacedName(gateway)
ancestorRefs := []gwv1a2.ParentReference{
getAncestorRefForPolicy(key, policy.Spec.TargetRef.SectionName),
}
// Set conditions for resolve error, then skip current gateway
if resolveErr != nil {
status.SetResolveErrorForPolicyAncestors(&policy.Status,
ancestorRefs,
t.GatewayControllerName,
policy.Generation,
resolveErr,
)
continue
}
// Check if another policy targeting the same section exists
section := string(*(policy.Spec.TargetRef.SectionName))
s, ok := policyMap[key]
if ok && s.Has(section) {
message := "Unable to target section, another ClientTrafficPolicy has already attached to it"
resolveErr = &status.PolicyResolveError{
Reason: gwv1a2.PolicyReasonConflicted,
Message: message,
}
status.SetResolveErrorForPolicyAncestors(&policy.Status,
ancestorRefs,
t.GatewayControllerName,
policy.Generation,
resolveErr,
)
continue
}
// Add section to policy map
if s == nil {
policyMap[key] = sets.New[string]()
}
policyMap[key].Insert(section)
// Translate for listener matching section name
var err error
for _, l := range gateway.listeners {
// Find IR
irKey := t.getIRKey(l.gateway)
// It must exist since we've already finished processing the gateways
gwXdsIR := xdsIR[irKey]
if string(l.Name) == section {
err = validatePortOverlapForClientTrafficPolicy(l, gwXdsIR, false)
if err == nil {
err = t.translateClientTrafficPolicyForListener(policy, l, xdsIR, infraIR, resources)
}
break
}
}
// Set conditions for translation error if it got any
if err != nil {
status.SetTranslationErrorForPolicyAncestors(&policy.Status,
ancestorRefs,
t.GatewayControllerName,
policy.Generation,
status.Error2ConditionMsg(err),
)
}
// Set Accepted condition if it is unset
status.SetAcceptedForPolicyAncestors(&policy.Status, ancestorRefs, t.GatewayControllerName)
}
}
// Policy with no section set (targeting all sections)
for _, policy := range clientTrafficPolicies {
if !hasSectionName(policy) {
policy := policy.DeepCopy()
res = append(res, policy)
gateway, resolveErr := resolveCTPolicyTargetRef(policy, gatewayMap)
// Negative statuses have already been assigned so its safe to skip
if gateway == nil {
continue
}
key := utils.NamespacedName(gateway)
ancestorRefs := []gwv1a2.ParentReference{
getAncestorRefForPolicy(key, nil),
}
// Set conditions for resolve error, then skip current gateway
if resolveErr != nil {
status.SetResolveErrorForPolicyAncestors(&policy.Status,
ancestorRefs,
t.GatewayControllerName,
policy.Generation,
resolveErr,
)
continue
}
// Check if another policy targeting the same Gateway exists
s, ok := policyMap[key]
if ok && s.Has(AllSections) {
message := "Unable to target Gateway, another ClientTrafficPolicy has already attached to it"
resolveErr = &status.PolicyResolveError{
Reason: gwv1a2.PolicyReasonConflicted,
Message: message,
}
status.SetResolveErrorForPolicyAncestors(&policy.Status,
ancestorRefs,
t.GatewayControllerName,
policy.Generation,
resolveErr,
)
continue
}
// Check if another policy targeting the same Gateway exists
if ok && (s.Len() > 0) {
// Maintain order here to ensure status/string does not change with same data
sections := s.UnsortedList()
sort.Strings(sections)
message := fmt.Sprintf("There are existing ClientTrafficPolicies that are overriding these sections %v", sections)
status.SetConditionForPolicyAncestors(&policy.Status,
ancestorRefs,
t.GatewayControllerName,
egv1a1.PolicyConditionOverridden,
metav1.ConditionTrue,
egv1a1.PolicyReasonOverridden,
message,
policy.Generation,
)
}
// Add section to policy map
if s == nil {
policyMap[key] = sets.New[string]()
}
policyMap[key].Insert(AllSections)
// Translate sections that have not yet been targeted
var errs error
for _, l := range gateway.listeners {
// Skip if section has already been targeted
if s != nil && s.Has(string(l.Name)) {
continue
}
// Find IR
irKey := t.getIRKey(l.gateway)
// It must exist since we've already finished processing the gateways
gwXdsIR := xdsIR[irKey]
if err := validatePortOverlapForClientTrafficPolicy(l, gwXdsIR, true); err != nil {
errs = errors.Join(errs, err)
} else if err := t.translateClientTrafficPolicyForListener(policy, l, xdsIR, infraIR, resources); err != nil {
errs = errors.Join(errs, err)
}
}
// Set conditions for translation error if it got any
if errs != nil {
status.SetTranslationErrorForPolicyAncestors(&policy.Status,
ancestorRefs,
t.GatewayControllerName,
policy.Generation,
status.Error2ConditionMsg(errs),
)
}
// Set Accepted condition if it is unset
status.SetAcceptedForPolicyAncestors(&policy.Status, ancestorRefs, t.GatewayControllerName)
}
}
return res
}
func resolveCTPolicyTargetRef(policy *egv1a1.ClientTrafficPolicy, gateways map[types.NamespacedName]*policyGatewayTargetContext) (*GatewayContext, *status.PolicyResolveError) {
targetNs := policy.Spec.TargetRef.Namespace
// If empty, default to namespace of policy
if targetNs == nil {
targetNs = ptr.To(gwv1b1.Namespace(policy.Namespace))
}
// Check if the gateway exists
key := types.NamespacedName{
Name: string(policy.Spec.TargetRef.Name),
Namespace: string(*targetNs),
}
gateway, ok := gateways[key]
// Gateway not found
if !ok {
return nil, nil
}
// Ensure Policy and target Gateway are in the same namespace
if policy.Namespace != string(*targetNs) {
message := fmt.Sprintf("Namespace:%s TargetRef.Namespace:%s, ClientTrafficPolicy can only target a Gateway in the same namespace.",
policy.Namespace, *targetNs)
return gateway.GatewayContext, &status.PolicyResolveError{
Reason: gwv1a2.PolicyReasonInvalid,
Message: message,
}
}
// If sectionName is set, make sure its valid
sectionName := policy.Spec.TargetRef.SectionName
if sectionName != nil {
found := false
for _, l := range gateway.listeners {
if l.Name == *sectionName {
found = true
break
}
}
if !found {
message := fmt.Sprintf("No section name %s found for %s", *sectionName, key.String())
return gateway.GatewayContext, &status.PolicyResolveError{
Reason: gwv1a2.PolicyReasonInvalid,
Message: message,
}
}
}
return gateway.GatewayContext, nil
}
func validatePortOverlapForClientTrafficPolicy(l *ListenerContext, xds *ir.Xds, attachedToGateway bool) error {
// Find Listener IR
// TODO: Support TLSRoute and TCPRoute once
// https://github.com/envoyproxy/gateway/issues/1635 is completed
irListenerName := irHTTPListenerName(l)
var httpIR *ir.HTTPListener
for _, http := range xds.HTTP {
if http.Name == irListenerName {
httpIR = http
break
}
}
// IR must exist since we're past validation
if httpIR != nil {
// Get a list of all other non-TLS listeners on this Gateway that share a port with
// the listener in question.
if sameListeners := listenersWithSameHTTPPort(xds, httpIR); len(sameListeners) != 0 {
if attachedToGateway {
// If this policy is attached to an entire gateway and the mergeGateways feature
// is turned on, validate that all the listeners affected by this policy originated
// from the same Gateway resource. The name of the Gateway from which this listener
// originated is part of the listener's name by construction.
gatewayName := irListenerName[0:strings.LastIndex(irListenerName, "/")]
conflictingListeners := []string{}
for _, currName := range sameListeners {
if strings.Index(currName, gatewayName) != 0 {
conflictingListeners = append(conflictingListeners, currName)
}
}
if len(conflictingListeners) != 0 {
return fmt.Errorf("ClientTrafficPolicy is being applied to multiple http (non https) listeners (%s) on the same port, which is not allowed", strings.Join(conflictingListeners, ", "))
}
} else {
// If this policy is attached to a specific listener, any other listeners in the list
// would be affected by this policy but should not be, so this policy can't be accepted.
return fmt.Errorf("ClientTrafficPolicy is being applied to multiple http (non https) listeners (%s) on the same port, which is not allowed", strings.Join(sameListeners, ", "))
}
}
}
return nil
}
func (t *Translator) translateClientTrafficPolicyForListener(policy *egv1a1.ClientTrafficPolicy, l *ListenerContext,
xdsIR XdsIRMap, infraIR InfraIRMap, resources *Resources) error {
// Find IR
irKey := t.getIRKey(l.gateway)
// It must exist since we've already finished processing the gateways
gwXdsIR := xdsIR[irKey]
// Find Listener IR
// TODO: Support TLSRoute and TCPRoute once
// https://github.com/envoyproxy/gateway/issues/1635 is completed
irListenerName := irHTTPListenerName(l)
var httpIR *ir.HTTPListener
for _, http := range gwXdsIR.HTTP {
if http.Name == irListenerName {
httpIR = http
break
}
}
// IR must exist since we're past validation
if httpIR != nil {
// Translate TCPKeepalive
translateListenerTCPKeepalive(policy.Spec.TCPKeepalive, httpIR)
// Translate Proxy Protocol
translateListenerProxyProtocol(policy.Spec.EnableProxyProtocol, httpIR)
// Translate Client IP Detection
translateClientIPDetection(policy.Spec.ClientIPDetection, httpIR)
// Translate Header Settings
translateListenerHeaderSettings(policy.Spec.Headers, httpIR)
// Translate Path Settings
translatePathSettings(policy.Spec.Path, httpIR)
// Translate Client Timeout Settings
if err := translateClientTimeout(policy.Spec.Timeout, httpIR); err != nil {
return err
}
// Translate HTTP1 Settings
if err := translateHTTP1Settings(policy.Spec.HTTP1, httpIR); err != nil {
return err
}
// enable http3 if set and TLS is enabled
if httpIR.TLS != nil && policy.Spec.HTTP3 != nil {
http3 := &ir.HTTP3Settings{
QUICPort: int32(l.Port),
}
httpIR.HTTP3 = http3
var proxyListenerIR *ir.ProxyListener
for _, proxyListener := range infraIR[irKey].Proxy.Listeners {
if proxyListener.Name == irListenerName {
proxyListenerIR = proxyListener
break
}
}
if proxyListenerIR != nil {
proxyListenerIR.HTTP3 = http3
}
}
// Translate TLS parameters
if err := t.translateListenerTLSParameters(policy, httpIR, resources); err != nil {
return err
}
}
return nil
}
func translateListenerTCPKeepalive(tcpKeepAlive *egv1a1.TCPKeepalive, httpIR *ir.HTTPListener) {
// Return early if not set
if tcpKeepAlive == nil {
return
}
irTCPKeepalive := &ir.TCPKeepalive{}
if tcpKeepAlive.Probes != nil {
irTCPKeepalive.Probes = tcpKeepAlive.Probes
}
if tcpKeepAlive.IdleTime != nil {
d, err := time.ParseDuration(string(*tcpKeepAlive.IdleTime))
if err != nil {
return
}
irTCPKeepalive.IdleTime = ptr.To(uint32(d.Seconds()))
}
if tcpKeepAlive.Interval != nil {
d, err := time.ParseDuration(string(*tcpKeepAlive.Interval))
if err != nil {
return
}
irTCPKeepalive.Interval = ptr.To(uint32(d.Seconds()))
}
httpIR.TCPKeepalive = irTCPKeepalive
}
func translatePathSettings(pathSettings *egv1a1.PathSettings, httpIR *ir.HTTPListener) {
if pathSettings == nil {
return
}
if pathSettings.DisableMergeSlashes != nil {
httpIR.Path.MergeSlashes = !*pathSettings.DisableMergeSlashes
}
if pathSettings.EscapedSlashesAction != nil {
httpIR.Path.EscapedSlashesAction = ir.PathEscapedSlashAction(*pathSettings.EscapedSlashesAction)
}
}
func translateClientTimeout(clientTimeout *egv1a1.ClientTimeout, httpIR *ir.HTTPListener) error {
if clientTimeout == nil {
return nil
}
if clientTimeout.HTTP != nil {
if clientTimeout.HTTP.RequestReceivedTimeout != nil {
d, err := time.ParseDuration(string(*clientTimeout.HTTP.RequestReceivedTimeout))
if err != nil {
return err
}
switch {
case httpIR.Timeout == nil:
httpIR.Timeout = &ir.ClientTimeout{}
fallthrough
case httpIR.Timeout.HTTP == nil:
httpIR.Timeout.HTTP = &ir.HTTPClientTimeout{}
}
httpIR.Timeout.HTTP.RequestReceivedTimeout = &metav1.Duration{
Duration: d,
}
}
}
return nil
}
func translateListenerProxyProtocol(enableProxyProtocol *bool, httpIR *ir.HTTPListener) {
// Return early if not set
if enableProxyProtocol == nil {
return
}
if *enableProxyProtocol {
httpIR.EnableProxyProtocol = true
}
}
func translateClientIPDetection(clientIPDetection *egv1a1.ClientIPDetectionSettings, httpIR *ir.HTTPListener) {
// Return early if not set
if clientIPDetection == nil {
return
}
httpIR.ClientIPDetection = (*ir.ClientIPDetectionSettings)(clientIPDetection)
}
func translateListenerHeaderSettings(headerSettings *egv1a1.HeaderSettings, httpIR *ir.HTTPListener) {
if headerSettings == nil {
return
}
httpIR.Headers = &ir.HeaderSettings{
EnableEnvoyHeaders: ptr.Deref(headerSettings.EnableEnvoyHeaders, false),
}
}
func translateHTTP1Settings(http1Settings *egv1a1.HTTP1Settings, httpIR *ir.HTTPListener) error {
if http1Settings == nil {
return nil
}
httpIR.HTTP1 = &ir.HTTP1Settings{
EnableTrailers: ptr.Deref(http1Settings.EnableTrailers, false),
PreserveHeaderCase: ptr.Deref(http1Settings.PreserveHeaderCase, false),
}
if http1Settings.HTTP10 != nil {
var defaultHost *string
if ptr.Deref(http1Settings.HTTP10.UseDefaultHost, false) {
for _, hostname := range httpIR.Hostnames {
if !strings.Contains(hostname, "*") {
// make linter happy
theHost := hostname
defaultHost = &theHost
break
}
}
if defaultHost == nil {
return fmt.Errorf("can't set http10 default host on listener with only wildcard hostnames")
}
}
// If useDefaultHost was set, then defaultHost will have the hostname to use.
// If no good hostname was found, an error would have been returned.
httpIR.HTTP1.HTTP10 = &ir.HTTP10Settings{
DefaultHost: defaultHost,
}
}
return nil
}
func (t *Translator) translateListenerTLSParameters(policy *egv1a1.ClientTrafficPolicy,
httpIR *ir.HTTPListener, resources *Resources) error {
// Return if this listener isn't a TLS listener. There has to be
// at least one certificate defined, which would cause httpIR to
// have a TLS structure.
if httpIR.TLS == nil {
return nil
}
tlsParams := policy.Spec.TLS
// Make sure that the negotiated TLS protocol version is as expected if TLS is used,
// regardless of if TLS parameters were used in the ClientTrafficPolicy or not
httpIR.TLS.MinVersion = ptr.To(ir.TLSv12)
httpIR.TLS.MaxVersion = ptr.To(ir.TLSv13)
if tlsParams != nil && len(tlsParams.ALPNProtocols) > 0 {
httpIR.TLS.ALPNProtocols = make([]string, len(tlsParams.ALPNProtocols))
for i := range tlsParams.ALPNProtocols {
httpIR.TLS.ALPNProtocols[i] = string(tlsParams.ALPNProtocols[i])
}
}
// Return early if not set
if tlsParams == nil {
return nil
}
if tlsParams.MinVersion != nil {
httpIR.TLS.MinVersion = ptr.To(ir.TLSVersion(*tlsParams.MinVersion))
}
if tlsParams.MaxVersion != nil {
httpIR.TLS.MaxVersion = ptr.To(ir.TLSVersion(*tlsParams.MaxVersion))
}
if len(tlsParams.Ciphers) > 0 {
httpIR.TLS.Ciphers = tlsParams.Ciphers
}
if len(tlsParams.ECDHCurves) > 0 {
httpIR.TLS.ECDHCurves = tlsParams.ECDHCurves
}
if len(tlsParams.SignatureAlgorithms) > 0 {
httpIR.TLS.SignatureAlgorithms = tlsParams.SignatureAlgorithms
}
if tlsParams.ClientValidation != nil {
from := crossNamespaceFrom{
group: egv1a1.GroupName,
kind: KindClientTrafficPolicy,
namespace: policy.Namespace,
}
irCACert := &ir.TLSCACertificate{
Name: irTLSCACertName(policy.Namespace, policy.Name),
}
for _, caCertRef := range tlsParams.ClientValidation.CACertificateRefs {
if caCertRef.Kind == nil || string(*caCertRef.Kind) == KindSecret { // nolint
secret, err := t.validateSecretRef(false, from, caCertRef, resources)
if err != nil {
return err
}
secretBytes, ok := secret.Data[caCertKey]
if !ok || len(secretBytes) == 0 {
return fmt.Errorf(
"caCertificateRef not found in secret %s", caCertRef.Name)
}
if err := validateCertificate(secretBytes); err != nil {
return fmt.Errorf("invalid certificate in secret %s: %w", caCertRef.Name, err)
}
irCACert.Certificate = append(irCACert.Certificate, secretBytes...)
} else if string(*caCertRef.Kind) == KindConfigMap {
configMap, err := t.validateConfigMapRef(false, from, caCertRef, resources)
if err != nil {
return err
}
configMapBytes, ok := configMap.Data[caCertKey]
if !ok || len(configMapBytes) == 0 {
return fmt.Errorf(
"caCertificateRef not found in configMap %s", caCertRef.Name)
}
if err := validateCertificate([]byte(configMapBytes)); err != nil {
return fmt.Errorf("invalid certificate in configmap %s: %w", caCertRef.Name, err)
}
irCACert.Certificate = append(irCACert.Certificate, configMapBytes...)
} else {
return fmt.Errorf("unsupported caCertificateRef kind:%s", string(*caCertRef.Kind))
}
}
if len(irCACert.Certificate) > 0 {
httpIR.TLS.CACertificate = irCACert
}
}
return nil
}