/
configuration.go
561 lines (465 loc) · 14.3 KB
/
configuration.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
package dataplane
import (
"context"
"encoding/base64"
"fmt"
"sort"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
v1 "sigs.k8s.io/gateway-api/apis/v1"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/resolver"
)
const (
wildcardHostname = "~^"
alpineSSLRootCAPath = "/etc/ssl/cert.pem"
)
// BuildConfiguration builds the Configuration from the Graph.
func BuildConfiguration(
ctx context.Context,
g *graph.Graph,
resolver resolver.ServiceResolver,
configVersion int,
) Configuration {
if g.GatewayClass == nil || !g.GatewayClass.Valid {
return Configuration{Version: configVersion}
}
if g.Gateway == nil {
return Configuration{Version: configVersion}
}
upstreams := buildUpstreams(ctx, g.Gateway.Listeners, resolver)
httpServers, sslServers := buildServers(g.Gateway.Listeners)
backendGroups := buildBackendGroups(append(httpServers, sslServers...))
keyPairs := buildSSLKeyPairs(g.ReferencedSecrets, g.Gateway.Listeners)
certBundles := buildCertBundles(g.ReferencedCaCertConfigMaps, backendGroups)
config := Configuration{
HTTPServers: httpServers,
SSLServers: sslServers,
Upstreams: upstreams,
BackendGroups: backendGroups,
SSLKeyPairs: keyPairs,
Version: configVersion,
CertBundles: certBundles,
}
return config
}
// buildSSLKeyPairs builds the SSLKeyPairs from the Secrets. It will only include Secrets that are referenced by
// valid listeners, so that we don't include unused Secrets in the configuration of the data plane.
func buildSSLKeyPairs(
secrets map[types.NamespacedName]*graph.Secret,
listeners []*graph.Listener,
) map[SSLKeyPairID]SSLKeyPair {
keyPairs := make(map[SSLKeyPairID]SSLKeyPair)
for _, l := range listeners {
if l.Valid && l.ResolvedSecret != nil {
id := generateSSLKeyPairID(*l.ResolvedSecret)
secret := secrets[*l.ResolvedSecret]
// The Data map keys are guaranteed to exist by the graph package.
// the Source field is guaranteed to be non-nil by the graph package.
keyPairs[id] = SSLKeyPair{
Cert: secret.Source.Data[apiv1.TLSCertKey],
Key: secret.Source.Data[apiv1.TLSPrivateKeyKey],
}
}
}
return keyPairs
}
func buildCertBundles(
caCertConfigMaps map[types.NamespacedName]*graph.CaCertConfigMap,
backendGroups []BackendGroup,
) map[CertBundleID]CertBundle {
bundles := make(map[CertBundleID]CertBundle)
refByBG := make(map[CertBundleID]struct{})
// We only need to build the cert bundles if there are valid backend groups that reference them.
if len(backendGroups) == 0 {
return bundles
}
for _, bg := range backendGroups {
if bg.Backends == nil {
continue
}
for _, b := range bg.Backends {
if !b.Valid || b.VerifyTLS == nil {
continue
}
refByBG[b.VerifyTLS.CertBundleID] = struct{}{}
}
}
for cmName, cm := range caCertConfigMaps {
id := generateCertBundleID(cmName)
if _, exists := refByBG[id]; exists {
if cm.CACert != nil || len(cm.CACert) > 0 {
// the cert could be base64 encoded or plaintext
data := make([]byte, base64.StdEncoding.DecodedLen(len(cm.CACert)))
_, err := base64.StdEncoding.Decode(data, cm.CACert)
if err != nil {
data = cm.CACert
}
bundles[id] = CertBundle(data)
}
}
}
return bundles
}
func buildBackendGroups(servers []VirtualServer) []BackendGroup {
type key struct {
nsname types.NamespacedName
ruleIdx int
}
// There can be duplicate backend groups if a route is attached to multiple listeners.
// We use a map to deduplicate them.
uniqueGroups := make(map[key]BackendGroup)
for _, s := range servers {
for _, pr := range s.PathRules {
for _, mr := range pr.MatchRules {
group := mr.BackendGroup
key := key{
nsname: group.Source,
ruleIdx: group.RuleIdx,
}
uniqueGroups[key] = group
}
}
}
numGroups := len(uniqueGroups)
if len(uniqueGroups) == 0 {
return nil
}
groups := make([]BackendGroup, 0, numGroups)
for _, group := range uniqueGroups {
groups = append(groups, group)
}
return groups
}
func newBackendGroup(refs []graph.BackendRef, sourceNsName types.NamespacedName, ruleIdx int) BackendGroup {
var backends []Backend
if len(refs) > 0 {
backends = make([]Backend, 0, len(refs))
}
for _, ref := range refs {
backends = append(backends, Backend{
UpstreamName: ref.ServicePortReference(),
Weight: ref.Weight,
Valid: ref.Valid,
VerifyTLS: convertBackendTLS(ref.BackendTLSPolicy),
})
}
return BackendGroup{
Backends: backends,
Source: sourceNsName,
RuleIdx: ruleIdx,
}
}
func convertBackendTLS(btp *graph.BackendTLSPolicy) *VerifyTLS {
if btp == nil || !btp.Valid {
return nil
}
verify := &VerifyTLS{}
if btp.CaCertRef.Name != "" {
verify.CertBundleID = generateCertBundleID(btp.CaCertRef)
} else {
verify.RootCAPath = alpineSSLRootCAPath
}
verify.Hostname = string(btp.Source.Spec.TLS.Hostname)
return verify
}
func buildServers(listeners []*graph.Listener) (http, ssl []VirtualServer) {
rulesForProtocol := map[v1.ProtocolType]portPathRules{
v1.HTTPProtocolType: make(portPathRules),
v1.HTTPSProtocolType: make(portPathRules),
}
for _, l := range listeners {
if l.Valid {
rules := rulesForProtocol[l.Source.Protocol][l.Source.Port]
if rules == nil {
rules = newHostPathRules()
rulesForProtocol[l.Source.Protocol][l.Source.Port] = rules
}
rules.upsertListener(l)
}
}
httpRules := rulesForProtocol[v1.HTTPProtocolType]
sslRules := rulesForProtocol[v1.HTTPSProtocolType]
return httpRules.buildServers(), sslRules.buildServers()
}
// portPathRules keeps track of hostPathRules per port
type portPathRules map[v1.PortNumber]*hostPathRules
func (p portPathRules) buildServers() []VirtualServer {
serverCount := 0
for _, rules := range p {
serverCount += rules.maxServerCount()
}
servers := make([]VirtualServer, 0, serverCount)
for _, rules := range p {
servers = append(servers, rules.buildServers()...)
}
return servers
}
type pathAndType struct {
path string
pathType v1.PathMatchType
}
type hostPathRules struct {
rulesPerHost map[string]map[pathAndType]PathRule
listenersForHost map[string]*graph.Listener
httpsListeners []*graph.Listener
listenersExist bool
port int32
}
func newHostPathRules() *hostPathRules {
return &hostPathRules{
rulesPerHost: make(map[string]map[pathAndType]PathRule),
listenersForHost: make(map[string]*graph.Listener),
httpsListeners: make([]*graph.Listener, 0),
}
}
func (hpr *hostPathRules) upsertListener(l *graph.Listener) {
hpr.listenersExist = true
hpr.port = int32(l.Source.Port)
if l.Source.Protocol == v1.HTTPSProtocolType {
hpr.httpsListeners = append(hpr.httpsListeners, l)
}
for _, r := range l.Routes {
if !r.Valid {
continue
}
hpr.upsertRoute(r, l)
}
}
func (hpr *hostPathRules) upsertRoute(route *graph.Route, listener *graph.Listener) {
var hostnames []string
for _, p := range route.ParentRefs {
if val, exist := p.Attachment.AcceptedHostnames[string(listener.Source.Name)]; exist {
hostnames = val
}
}
for _, h := range hostnames {
if prevListener, exists := hpr.listenersForHost[h]; exists {
// override the previous listener if the new one has a more specific hostname
if listenerHostnameMoreSpecific(listener.Source.Hostname, prevListener.Source.Hostname) {
hpr.listenersForHost[h] = listener
}
} else {
hpr.listenersForHost[h] = listener
}
if _, exist := hpr.rulesPerHost[h]; !exist {
hpr.rulesPerHost[h] = make(map[pathAndType]PathRule)
}
}
for i, rule := range route.Source.Spec.Rules {
if !route.Rules[i].ValidMatches {
continue
}
var filters HTTPFilters
if route.Rules[i].ValidFilters {
filters = createHTTPFilters(rule.Filters)
} else {
filters = HTTPFilters{
InvalidFilter: &InvalidHTTPFilter{},
}
}
for _, h := range hostnames {
for _, m := range rule.Matches {
path := getPath(m.Path)
key := pathAndType{
path: path,
pathType: *m.Path.Type,
}
rule, exist := hpr.rulesPerHost[h][key]
if !exist {
rule.Path = path
rule.PathType = convertPathType(*m.Path.Type)
}
// create iteration variable inside the loop to fix implicit memory aliasing
om := route.Source.ObjectMeta
routeNsName := client.ObjectKeyFromObject(route.Source)
rule.MatchRules = append(rule.MatchRules, MatchRule{
Source: &om,
BackendGroup: newBackendGroup(route.Rules[i].BackendRefs, routeNsName, i),
Filters: filters,
Match: convertMatch(m),
})
hpr.rulesPerHost[h][key] = rule
}
}
}
}
func (hpr *hostPathRules) buildServers() []VirtualServer {
servers := make([]VirtualServer, 0, len(hpr.rulesPerHost)+len(hpr.httpsListeners))
for h, rules := range hpr.rulesPerHost {
s := VirtualServer{
Hostname: h,
PathRules: make([]PathRule, 0, len(rules)),
Port: hpr.port,
}
l, ok := hpr.listenersForHost[h]
if !ok {
panic(fmt.Sprintf("no listener found for hostname: %s", h))
}
if l.ResolvedSecret != nil {
s.SSL = &SSL{
KeyPairID: generateSSLKeyPairID(*l.ResolvedSecret),
}
}
for _, r := range rules {
sortMatchRules(r.MatchRules)
s.PathRules = append(s.PathRules, r)
}
// We sort the path rules so the order is preserved after reconfiguration.
sort.Slice(s.PathRules, func(i, j int) bool {
if s.PathRules[i].Path != s.PathRules[j].Path {
return s.PathRules[i].Path < s.PathRules[j].Path
}
return s.PathRules[i].PathType < s.PathRules[j].PathType
})
servers = append(servers, s)
}
for _, l := range hpr.httpsListeners {
hostname := getListenerHostname(l.Source.Hostname)
// Generate a 404 ssl server block for listeners with no routes or listeners with wildcard (match-all) routes.
// This server overrides the default ssl server.
if len(l.Routes) == 0 || hostname == wildcardHostname {
s := VirtualServer{
Hostname: hostname,
Port: hpr.port,
}
if l.ResolvedSecret != nil {
s.SSL = &SSL{
KeyPairID: generateSSLKeyPairID(*l.ResolvedSecret),
}
}
servers = append(servers, s)
}
}
// if any listeners exist, we need to generate a default server block.
if hpr.listenersExist {
servers = append(servers, VirtualServer{
IsDefault: true,
Port: hpr.port,
})
}
// We sort the servers so the order is preserved after reconfiguration.
sort.Slice(servers, func(i, j int) bool {
return servers[i].Hostname < servers[j].Hostname
})
return servers
}
// maxServerCount returns the maximum number of VirtualServers that can be built from the host path rules.
func (hpr *hostPathRules) maxServerCount() int {
// to calculate max # of servers we add up:
// - # of hostnames
// - # of https listeners - this is to account for https wildcard default servers
// - default server - for every hostPathRules we generate 1 default server
return len(hpr.rulesPerHost) + len(hpr.httpsListeners) + 1
}
func buildUpstreams(
ctx context.Context,
listeners []*graph.Listener,
resolver resolver.ServiceResolver,
) []Upstream {
// There can be duplicate upstreams if multiple routes reference the same upstream.
// We use a map to deduplicate them.
uniqueUpstreams := make(map[string]Upstream)
for _, l := range listeners {
if !l.Valid {
continue
}
for _, route := range l.Routes {
if !route.Valid {
continue
}
for _, rule := range route.Rules {
if !rule.ValidMatches || !rule.ValidFilters {
// don't generate upstreams for rules that have invalid matches or filters
continue
}
for _, br := range rule.BackendRefs {
if br.Valid {
upstreamName := br.ServicePortReference()
_, exist := uniqueUpstreams[upstreamName]
if exist {
continue
}
var errMsg string
eps, err := resolver.Resolve(ctx, br.SvcNsName, br.ServicePort)
if err != nil {
errMsg = err.Error()
}
uniqueUpstreams[upstreamName] = Upstream{
Name: upstreamName,
Endpoints: eps,
ErrorMsg: errMsg,
}
}
}
}
}
}
if len(uniqueUpstreams) == 0 {
return nil
}
upstreams := make([]Upstream, 0, len(uniqueUpstreams))
for _, up := range uniqueUpstreams {
upstreams = append(upstreams, up)
}
return upstreams
}
func getListenerHostname(h *v1.Hostname) string {
if h == nil || *h == "" {
return wildcardHostname
}
return string(*h)
}
func getPath(path *v1.HTTPPathMatch) string {
if path == nil || path.Value == nil || *path.Value == "" {
return "/"
}
return *path.Value
}
func createHTTPFilters(filters []v1.HTTPRouteFilter) HTTPFilters {
var result HTTPFilters
for _, f := range filters {
switch f.Type {
case v1.HTTPRouteFilterRequestRedirect:
if result.RequestRedirect == nil {
// using the first filter
result.RequestRedirect = convertHTTPRequestRedirectFilter(f.RequestRedirect)
}
case v1.HTTPRouteFilterURLRewrite:
if result.RequestURLRewrite == nil {
// using the first filter
result.RequestURLRewrite = convertHTTPURLRewriteFilter(f.URLRewrite)
}
case v1.HTTPRouteFilterRequestHeaderModifier:
if result.RequestHeaderModifiers == nil {
// using the first filter
result.RequestHeaderModifiers = convertHTTPHeaderFilter(f.RequestHeaderModifier)
}
}
}
return result
}
// listenerHostnameMoreSpecific returns true if host1 is more specific than host2.
func listenerHostnameMoreSpecific(host1, host2 *v1.Hostname) bool {
var host1Str, host2Str string
if host1 != nil {
host1Str = string(*host1)
}
if host2 != nil {
host2Str = string(*host2)
}
return graph.GetMoreSpecificHostname(host1Str, host2Str) == host1Str
}
// generateSSLKeyPairID generates an ID for the SSL key pair based on the Secret namespaced name.
// It is guaranteed to be unique per unique namespaced name.
// The ID is safe to use as a file name.
func generateSSLKeyPairID(secret types.NamespacedName) SSLKeyPairID {
return SSLKeyPairID(fmt.Sprintf("ssl_keypair_%s_%s", secret.Namespace, secret.Name))
}
// generateCertBundleID generates an ID for the certificate bundle based on the ConfigMap namespaced name.
// It is guaranteed to be unique per unique namespaced name.
// The ID is safe to use as a file name.
func generateCertBundleID(configMap types.NamespacedName) CertBundleID {
return CertBundleID(fmt.Sprintf("cert_bundle_%s_%s", configMap.Namespace, configMap.Name))
}