-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
firewall.go
688 lines (617 loc) · 22.2 KB
/
firewall.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
Copyright 2019 The Kubernetes 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 openstackmodel
import (
"fmt"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/openstacktasks"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/wellknownports"
"k8s.io/utils/net"
sg "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
)
const (
IPProtocolTCP = string(rules.ProtocolTCP)
IPProtocolUDP = string(rules.ProtocolUDP)
IPV4 = string(rules.EtherType4)
IPV6 = string(rules.EtherType6)
ProtocolIPEncap = "4" // IP in IPv4/IPv6
)
// FirewallModelBuilder configures firewall network objects
type FirewallModelBuilder struct {
*OpenstackModelContext
Lifecycle fi.Lifecycle
Rules map[string]*openstacktasks.SecurityGroupRule
}
var _ fi.ModelBuilder = &FirewallModelBuilder{}
func (b *FirewallModelBuilder) usesOctavia() bool {
if b.Cluster.Spec.CloudProvider.Openstack.Loadbalancer != nil {
return fi.ValueOf(b.Cluster.Spec.CloudProvider.Openstack.Loadbalancer.UseOctavia)
}
return false
}
func (b *FirewallModelBuilder) getOctaviaProvider() string {
if b.Cluster.Spec.CloudProvider.Openstack.Loadbalancer != nil {
return fi.ValueOf(b.Cluster.Spec.CloudProvider.Openstack.Loadbalancer.Provider)
}
return ""
}
// addDirectionalGroupRule - create a rule on the source group to the dest group provided a securityGroupRuleTask
//
// Example
// Create an Ingress rule on source allowing traffic from dest with the options in the SecurityGroupRule
// Create an Egress rule on source allowing traffic to dest with the options in the SecurityGroupRule
func (b *FirewallModelBuilder) addDirectionalGroupRule(c *fi.ModelBuilderContext, source, dest *openstacktasks.SecurityGroup, sgr *openstacktasks.SecurityGroupRule) {
t := &openstacktasks.SecurityGroupRule{
Direction: sgr.Direction,
EtherType: sgr.EtherType,
Lifecycle: sgr.Lifecycle,
PortRangeMin: sgr.PortRangeMin,
PortRangeMax: sgr.PortRangeMax,
Protocol: sgr.Protocol,
RemoteGroup: dest,
RemoteIPPrefix: sgr.RemoteIPPrefix,
SecGroup: source,
Delete: fi.PtrTo(false),
}
klog.V(8).Infof("Adding rule %v", fi.ValueOf(t.GetName()))
b.Rules[fi.ValueOf(t.GetName())] = t
}
// addSSHRules - sets the ssh rules based on the presence of a bastion
func (b *FirewallModelBuilder) addSSHRules(c *fi.ModelBuilderContext, sgMap map[string]*openstacktasks.SecurityGroup) error {
masterName := b.SecurityGroupName(kops.InstanceGroupRoleControlPlane)
nodeName := b.SecurityGroupName(kops.InstanceGroupRoleNode)
bastionName := b.SecurityGroupName(kops.InstanceGroupRoleBastion)
masterSG := sgMap[masterName]
nodeSG := sgMap[nodeName]
bastionSG := sgMap[bastionName]
sshIngress := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(string(rules.ProtocolTCP)),
EtherType: s(string(rules.EtherType4)),
PortRangeMin: i(22),
PortRangeMax: i(22),
}
if b.UsesSSHBastion() {
for _, sshAccess := range b.Cluster.Spec.SSHAccess {
etherType := IPV4
if !net.IsIPv4CIDRString(sshAccess) {
etherType = IPV6
}
sshRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(string(rules.ProtocolTCP)),
EtherType: s(string(etherType)),
PortRangeMin: i(22),
PortRangeMax: i(22),
RemoteIPPrefix: s(sshAccess),
}
b.addDirectionalGroupRule(c, bastionSG, nil, sshRule)
}
// Allow ingress ssh from the bastion on the masters and nodes
b.addDirectionalGroupRule(c, masterSG, bastionSG, sshIngress)
b.addDirectionalGroupRule(c, nodeSG, bastionSG, sshIngress)
} else {
for _, sshAccess := range b.Cluster.Spec.SSHAccess {
etherType := IPV4
if !net.IsIPv4CIDRString(sshAccess) {
etherType = IPV6
}
sshRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(string(rules.ProtocolTCP)),
EtherType: s(string(etherType)),
PortRangeMin: i(22),
PortRangeMax: i(22),
RemoteIPPrefix: s(sshAccess),
}
b.addDirectionalGroupRule(c, masterSG, nil, sshRule)
b.addDirectionalGroupRule(c, nodeSG, nil, sshRule)
}
}
return nil
}
// addETCDRules - Add ETCD access rules based on which CNI might need to access __ETCD_ENDPOINTS__
func (b *FirewallModelBuilder) addETCDRules(c *fi.ModelBuilderContext, sgMap map[string]*openstacktasks.SecurityGroup) error {
masterName := b.SecurityGroupName(kops.InstanceGroupRoleControlPlane)
nodeName := b.SecurityGroupName(kops.InstanceGroupRoleNode)
masterSG := sgMap[masterName]
nodeSG := sgMap[nodeName]
// ETCD Peer Discovery
etcdRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(string(rules.ProtocolTCP)),
EtherType: s(IPV4),
PortRangeMin: i(4001),
PortRangeMax: i(4002),
}
etcdPeerRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(string(rules.ProtocolTCP)),
EtherType: s(IPV4),
PortRangeMin: i(2380),
PortRangeMax: i(2381),
}
b.addDirectionalGroupRule(c, masterSG, masterSG, etcdRule)
b.addDirectionalGroupRule(c, masterSG, masterSG, etcdPeerRule)
for _, portRange := range wellknownports.ETCDPortRanges() {
etcdMgmrRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(string(rules.ProtocolTCP)),
EtherType: s(string(rules.EtherType4)),
PortRangeMin: i(portRange.Min),
PortRangeMax: i(portRange.Max),
}
b.addDirectionalGroupRule(c, masterSG, masterSG, etcdMgmrRule)
}
if b.Cluster.Spec.Networking.Calico != nil {
etcdCNIRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(string(rules.ProtocolTCP)),
EtherType: s(IPV4),
PortRangeMin: i(4001),
PortRangeMax: i(4001),
}
// Master access from other masters covered above
// Allow nodes to reach ETCD endpoints
b.addDirectionalGroupRule(c, masterSG, nodeSG, etcdCNIRule)
}
return nil
}
// addNodePortRules - Add node port rules to nodes give the NodePortRange
func (b *FirewallModelBuilder) addNodePortRules(c *fi.ModelBuilderContext, sgMap map[string]*openstacktasks.SecurityGroup) error {
nodeName := b.SecurityGroupName(kops.InstanceGroupRoleNode)
nodeSG := sgMap[nodeName]
for _, nodePortAccess := range b.Cluster.Spec.NodePortAccess {
nodePortRange, err := b.NodePortRange()
if err != nil {
return err
}
for _, protocol := range []string{IPProtocolTCP, IPProtocolUDP} {
nodePortRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(protocol),
EtherType: s(IPV4),
PortRangeMin: i(nodePortRange.Base),
PortRangeMax: i(nodePortRange.Base + nodePortRange.Size - 1),
RemoteIPPrefix: s(nodePortAccess),
}
b.addDirectionalGroupRule(c, nodeSG, nil, nodePortRule)
}
}
return nil
}
// addHTTPSRules - Add rules to 443 access given the presence of a loadbalancer or not
func (b *FirewallModelBuilder) addHTTPSRules(c *fi.ModelBuilderContext, sgMap map[string]*openstacktasks.SecurityGroup, useVIPACL bool) error {
masterName := b.SecurityGroupName(kops.InstanceGroupRoleControlPlane)
nodeName := b.SecurityGroupName(kops.InstanceGroupRoleNode)
lbSGName := b.Cluster.Spec.API.PublicName
lbSG := sgMap[lbSGName]
masterSG := sgMap[masterName]
nodeSG := sgMap[nodeName]
httpsIngress := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(IPProtocolTCP),
EtherType: s(IPV4),
PortRangeMin: i(443),
PortRangeMax: i(443),
}
// Allow all local communication for kubernetes.svc and to the api.internal lb/gossip for kubelet's
b.addDirectionalGroupRule(c, masterSG, nodeSG, httpsIngress)
b.addDirectionalGroupRule(c, masterSG, masterSG, httpsIngress)
if b.UseLoadBalancerForAPI() {
if !useVIPACL {
// Allow API Access to the lb sg
for _, apiAccess := range b.Cluster.Spec.API.Access {
etherType := IPV4
if !net.IsIPv4CIDRString(apiAccess) {
etherType = IPV6
}
b.addDirectionalGroupRule(c, lbSG, nil, &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(IPProtocolTCP),
EtherType: s(etherType),
PortRangeMin: i(443),
PortRangeMax: i(443),
RemoteIPPrefix: s(apiAccess),
})
}
// Allow masters ingress from the sg
b.addDirectionalGroupRule(c, masterSG, lbSG, httpsIngress)
}
// FIXME: Octavia port traffic appears to be denied though its port is in lbSG
if b.usesOctavia() {
if b.getOctaviaProvider() == "ovn" {
for _, apiAccess := range b.Cluster.Spec.API.Access {
etherType := IPV4
if !net.IsIPv4CIDRString(apiAccess) {
etherType = IPV6
}
b.addDirectionalGroupRule(c, masterSG, nil, &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(IPProtocolTCP),
EtherType: s(etherType),
PortRangeMin: i(443),
PortRangeMax: i(443),
RemoteIPPrefix: s(apiAccess),
})
}
} else {
b.addDirectionalGroupRule(c, masterSG, nil, &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(IPProtocolTCP),
EtherType: s(IPV4),
PortRangeMin: i(443),
PortRangeMax: i(443),
RemoteIPPrefix: s(b.Cluster.Spec.NetworkCIDR),
})
}
}
} else {
// Allow the masters to receive connections from KubernetesAPIAccess
for _, apiAccess := range b.Cluster.Spec.API.Access {
etherType := IPV4
if !net.IsIPv4CIDRString(apiAccess) {
etherType = IPV6
}
b.addDirectionalGroupRule(c, masterSG, nil, &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(IPProtocolTCP),
EtherType: s(etherType),
PortRangeMin: i(443),
PortRangeMax: i(443),
RemoteIPPrefix: s(apiAccess),
})
}
}
return nil
}
// addKubeletRules - Add rules to 10250 port
func (b *FirewallModelBuilder) addKubeletRules(c *fi.ModelBuilderContext, sgMap map[string]*openstacktasks.SecurityGroup) error {
// TODO: This is the default port for kubelet and may be overridden
masterName := b.SecurityGroupName(kops.InstanceGroupRoleControlPlane)
nodeName := b.SecurityGroupName(kops.InstanceGroupRoleNode)
masterSG := sgMap[masterName]
nodeSG := sgMap[nodeName]
kubeletRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(IPProtocolTCP),
EtherType: s(IPV4),
PortRangeMin: i(10250),
PortRangeMax: i(10250),
}
// allow node-node, node-master and master-master and master-node
for _, sgName := range []*openstacktasks.SecurityGroup{masterSG, nodeSG} {
b.addDirectionalGroupRule(c, masterSG, sgName, kubeletRule)
b.addDirectionalGroupRule(c, nodeSG, sgName, kubeletRule)
}
return nil
}
// addNodeExporterAndOccmRules - Allow 9100 TCP port from nodesg, allow 10258 from nodes to master - expose occm metrics
func (b *FirewallModelBuilder) addNodeExporterAndOccmRules(c *fi.ModelBuilderContext, sgMap map[string]*openstacktasks.SecurityGroup) error {
masterName := b.SecurityGroupName(kops.InstanceGroupRoleControlPlane)
nodeName := b.SecurityGroupName(kops.InstanceGroupRoleNode)
masterSG := sgMap[masterName]
nodeSG := sgMap[nodeName]
nodeExporterIngress := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(IPProtocolTCP),
EtherType: s(IPV4),
PortRangeMin: i(9100),
PortRangeMax: i(9100),
}
// allow 9100 port from nodeSG
b.addDirectionalGroupRule(c, masterSG, nodeSG, nodeExporterIngress)
b.addDirectionalGroupRule(c, nodeSG, nodeSG, nodeExporterIngress)
occmMetrics := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(IPProtocolTCP),
EtherType: s(IPV4),
PortRangeMin: i(10258),
PortRangeMax: i(10258),
}
b.addDirectionalGroupRule(c, masterSG, nodeSG, occmMetrics)
return nil
}
// addDNSRules - Add DNS rules for internal DNS queries
func (b *FirewallModelBuilder) addDNSRules(c *fi.ModelBuilderContext, sgMap map[string]*openstacktasks.SecurityGroup) error {
masterName := b.SecurityGroupName(kops.InstanceGroupRoleControlPlane)
nodeName := b.SecurityGroupName(kops.InstanceGroupRoleNode)
masterSG := sgMap[masterName]
nodeSG := sgMap[nodeName]
for _, protocol := range []string{IPProtocolTCP, IPProtocolUDP} {
dnsRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(protocol),
EtherType: s(IPV4),
PortRangeMin: i(53),
PortRangeMax: i(53),
}
b.addDirectionalGroupRule(c, masterSG, nodeSG, dnsRule)
b.addDirectionalGroupRule(c, nodeSG, masterSG, dnsRule)
b.addDirectionalGroupRule(c, masterSG, masterSG, dnsRule)
}
return nil
}
// addCNIRules - Add ports required for different CNI implementations
func (b *FirewallModelBuilder) addCNIRules(c *fi.ModelBuilderContext, sgMap map[string]*openstacktasks.SecurityGroup) error {
udpPorts := []int{}
tcpPorts := []int{}
protocols := []string{}
// allow cadvisor
tcpPorts = append(tcpPorts, 4194)
if b.Cluster.Spec.Networking != nil {
if b.Cluster.Spec.Networking.Kopeio != nil {
// VXLAN over UDP
// https://tools.ietf.org/html/rfc7348
udpPorts = append(udpPorts, 4789)
}
if b.Cluster.Spec.Networking.Cilium != nil {
udpPorts = append(udpPorts, 8472)
tcpPorts = append(tcpPorts, 4240)
}
if b.Cluster.Spec.Networking.Weave != nil {
udpPorts = append(udpPorts, 6783)
tcpPorts = append(tcpPorts, 6783)
udpPorts = append(udpPorts, 6784)
}
if b.Cluster.Spec.Networking.Flannel != nil {
switch b.Cluster.Spec.Networking.Flannel.Backend {
case "", "udp":
udpPorts = append(udpPorts, 8285)
case "vxlan":
udpPorts = append(udpPorts, 8472)
default:
klog.Warningf("unknown flannel networking backend %q", b.Cluster.Spec.Networking.Flannel.Backend)
}
}
if b.Cluster.Spec.Networking.Calico != nil {
tcpPorts = append(tcpPorts, 179)
protocols = append(protocols, ProtocolIPEncap)
}
if b.Cluster.Spec.Networking.Kuberouter != nil {
protocols = append(protocols, ProtocolIPEncap)
}
}
masterName := b.SecurityGroupName(kops.InstanceGroupRoleControlPlane)
nodeName := b.SecurityGroupName(kops.InstanceGroupRoleNode)
masterSG := sgMap[masterName]
nodeSG := sgMap[nodeName]
for _, udpPort := range udpPorts {
udpRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(string(rules.ProtocolUDP)),
EtherType: s(string(rules.EtherType4)),
PortRangeMin: i(udpPort),
PortRangeMax: i(udpPort),
}
b.addDirectionalGroupRule(c, masterSG, masterSG, udpRule)
b.addDirectionalGroupRule(c, nodeSG, masterSG, udpRule)
b.addDirectionalGroupRule(c, masterSG, nodeSG, udpRule)
b.addDirectionalGroupRule(c, nodeSG, nodeSG, udpRule)
}
for _, tcpPort := range tcpPorts {
tcpRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(string(rules.ProtocolTCP)),
EtherType: s(string(rules.EtherType4)),
PortRangeMin: i(tcpPort),
PortRangeMax: i(tcpPort),
}
b.addDirectionalGroupRule(c, masterSG, masterSG, tcpRule)
b.addDirectionalGroupRule(c, nodeSG, masterSG, tcpRule)
b.addDirectionalGroupRule(c, masterSG, nodeSG, tcpRule)
b.addDirectionalGroupRule(c, nodeSG, nodeSG, tcpRule)
}
for _, protocol := range protocols {
protocolRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(protocol),
EtherType: s(string(rules.EtherType4)),
}
b.addDirectionalGroupRule(c, masterSG, nil, protocolRule)
b.addDirectionalGroupRule(c, nodeSG, nil, protocolRule)
}
return nil
}
// addProtokubeRules - Add rules for protokube if gossip DNS is enabled
func (b *FirewallModelBuilder) addProtokubeRules(c *fi.ModelBuilderContext, sgMap map[string]*openstacktasks.SecurityGroup) error {
if b.Cluster.IsGossip() {
masterName := b.SecurityGroupName(kops.InstanceGroupRoleControlPlane)
nodeName := b.SecurityGroupName(kops.InstanceGroupRoleNode)
masterSG := sgMap[masterName]
nodeSG := sgMap[nodeName]
for _, portRange := range wellknownports.DNSGossipPortRanges() {
protokubeRule := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirIngress)),
Protocol: s(string(rules.ProtocolTCP)),
EtherType: s(string(rules.EtherType4)),
PortRangeMin: i(portRange.Min),
PortRangeMax: i(portRange.Max),
}
b.addDirectionalGroupRule(c, masterSG, nodeSG, protokubeRule)
b.addDirectionalGroupRule(c, nodeSG, masterSG, protokubeRule)
b.addDirectionalGroupRule(c, masterSG, masterSG, protokubeRule)
b.addDirectionalGroupRule(c, nodeSG, nodeSG, protokubeRule)
}
}
return nil
}
func (b *FirewallModelBuilder) getExistingRules(sgMap map[string]*openstacktasks.SecurityGroup) error {
osCloud, err := b.createCloud()
if err != nil {
return err
}
sgIdMap := make(map[string]*openstacktasks.SecurityGroup)
for sgName, sgt := range sgMap {
sgs, err := osCloud.ListSecurityGroups(sg.ListOpts{
Name: sgName,
})
if err != nil {
return err
}
if len(sgs) == 0 {
continue
}
if len(sgs) > 1 {
return fmt.Errorf("Found multiple security groups with the same name: %v", sgName)
}
sg := sgs[0]
sgt.Name = fi.PtrTo(sg.Name)
sgIdMap[sg.ID] = sgt
}
for sgid := range sgIdMap {
sgRules, err := osCloud.ListSecurityGroupRules(rules.ListOpts{
SecGroupID: sgid,
})
if err != nil {
return err
}
for _, rule := range sgRules {
t := &openstacktasks.SecurityGroupRule{
ID: fi.PtrTo(rule.ID),
Direction: fi.PtrTo(rule.Direction),
EtherType: fi.PtrTo(rule.EtherType),
PortRangeMax: fi.PtrTo(rule.PortRangeMax),
PortRangeMin: fi.PtrTo(rule.PortRangeMin),
Protocol: fi.PtrTo(rule.Protocol),
RemoteIPPrefix: fi.PtrTo(rule.RemoteIPPrefix),
RemoteGroup: sgIdMap[rule.RemoteGroupID],
Lifecycle: b.Lifecycle,
SecGroup: sgIdMap[rule.SecGroupID],
Delete: fi.PtrTo(true),
}
klog.V(8).Infof("Adding existing rule %v", t)
b.Rules[fi.ValueOf(t.GetName())] = t
}
}
return nil
}
func (b *FirewallModelBuilder) addDefaultEgress(c *fi.ModelBuilderContext, sgMap map[string]*openstacktasks.SecurityGroup, useVIPACL bool) {
for name, sg := range sgMap {
if useVIPACL && name == b.Cluster.Spec.API.PublicName {
continue
}
t := &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirEgress)),
EtherType: s(string(rules.EtherType4)),
PortRangeMin: i(0),
PortRangeMax: i(0),
}
b.addDirectionalGroupRule(c, sg, nil, t)
t = &openstacktasks.SecurityGroupRule{
Lifecycle: b.Lifecycle,
Direction: s(string(rules.DirEgress)),
EtherType: s(string(rules.EtherType6)),
PortRangeMin: i(0),
PortRangeMax: i(0),
}
b.addDirectionalGroupRule(c, sg, nil, t)
}
}
// Build - schedule security groups and security group rule tasks for Openstack
func (b *FirewallModelBuilder) Build(c *fi.ModelBuilderContext) error {
roles := []kops.InstanceGroupRole{kops.InstanceGroupRoleControlPlane, kops.InstanceGroupRoleNode}
if b.UsesSSHBastion() {
roles = append(roles, kops.InstanceGroupRoleBastion)
}
sgMap := make(map[string]*openstacktasks.SecurityGroup)
useVIPACL := false
if b.UseLoadBalancerForAPI() && b.UseVIPACL() {
useVIPACL = true
}
sg := &openstacktasks.SecurityGroup{
Name: s(b.Cluster.Spec.API.PublicName),
Lifecycle: b.Lifecycle,
RemoveExtraRules: []string{"port=443"},
}
if useVIPACL {
sg.RemoveGroup = true
}
c.AddTask(sg)
sgMap[b.Cluster.Spec.API.PublicName] = sg
for _, role := range roles {
// Create Security Group for Role
groupName := b.SecurityGroupName(role)
sg := &openstacktasks.SecurityGroup{
Name: s(groupName),
Lifecycle: b.Lifecycle,
RemoveGroup: false,
}
if role == kops.InstanceGroupRoleBastion {
sg.RemoveExtraRules = []string{"port=22"}
} else if role == kops.InstanceGroupRoleNode {
sg.RemoveExtraRules = []string{"port=22", "port=10250"}
} else if role == kops.InstanceGroupRoleControlPlane {
sg.RemoveExtraRules = []string{"port=22", "port=443", "port=10250"}
}
c.AddTask(sg)
sgMap[groupName] = sg
}
b.Rules = make(map[string]*openstacktasks.SecurityGroupRule)
err := b.getExistingRules(sgMap)
// if this fails, it just means that existing rules won't be cleaned up
if err != nil {
klog.Warningf("Failed to list existing security groups: %v", err)
}
b.addDefaultEgress(c, sgMap, useVIPACL)
// Add API Server Rules
b.addHTTPSRules(c, sgMap, useVIPACL)
// Add SSH
b.addSSHRules(c, sgMap)
// Allow overlay DNS
b.addDNSRules(c, sgMap)
// Add Kubelet Rules
b.addKubeletRules(c, sgMap)
// Add Node exporter and occm metrics Rules
b.addNodeExporterAndOccmRules(c, sgMap)
// Protokube Rules
b.addProtokubeRules(c, sgMap)
// Allow necessary local traffic
b.addCNIRules(c, sgMap)
// ETCD Leader Election
b.addETCDRules(c, sgMap)
// Add NodePort Rules:
err = b.addNodePortRules(c, sgMap)
if err != nil {
return fmt.Errorf("failed to add node port rules: %v", err)
}
for _, r := range b.Rules {
c.AddTask(r)
}
return nil
}