forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall_rule.go
179 lines (147 loc) · 4.15 KB
/
firewall_rule.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
package gcetasks
import (
"fmt"
"google.golang.org/api/compute/v1"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
"strings"
)
//go:generate fitask -type=FirewallRule
type FirewallRule struct {
Name *string
Network *Network
SourceTags []string
SourceRanges []string
TargetTags []string
Allowed []string
}
var _ fi.CompareWithID = &FirewallRule{}
func (e *FirewallRule) CompareWithID() *string {
return e.Name
}
func (e *FirewallRule) Find(c *fi.Context) (*FirewallRule, error) {
cloud := c.Cloud.(*gce.GCECloud)
r, err := cloud.Compute.Firewalls.Get(cloud.Project, *e.Name).Do()
if err != nil {
if gce.IsNotFound(err) {
return nil, nil
}
return nil, fmt.Errorf("error listing FirewallRules: %v", err)
}
actual := &FirewallRule{}
actual.Name = &r.Name
actual.Network = &Network{Name: fi.String(lastComponent(r.Network))}
actual.TargetTags = r.TargetTags
actual.SourceRanges = r.SourceRanges
actual.SourceTags = r.SourceTags
for _, a := range r.Allowed {
actual.Allowed = append(actual.Allowed, serializeFirewallAllowed(a))
}
return actual, nil
}
func (e *FirewallRule) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (_ *FirewallRule) CheckChanges(a, e, changes *FirewallRule) error {
return nil
}
func parseFirewallAllowed(rule string) (*compute.FirewallAllowed, error) {
o := &compute.FirewallAllowed{}
tokens := strings.Split(rule, ":")
if len(tokens) < 1 || len(tokens) > 2 {
return nil, fmt.Errorf("expected protocol[:portspec] in firewall rule %q", rule)
}
o.IPProtocol = tokens[0]
if len(tokens) == 1 {
return o, nil
}
o.Ports = []string{tokens[1]}
return o, nil
}
func serializeFirewallAllowed(r *compute.FirewallAllowed) string {
if len(r.Ports) == 0 {
return r.IPProtocol
}
var tokens []string
for _, ports := range r.Ports {
tokens = append(tokens, r.IPProtocol+":"+ports)
}
return strings.Join(tokens, ",")
}
func (e *FirewallRule) mapToGCE(project string) (*compute.Firewall, error) {
var allowed []*compute.FirewallAllowed
if e.Allowed != nil {
for _, a := range e.Allowed {
p, err := parseFirewallAllowed(a)
if err != nil {
return nil, err
}
allowed = append(allowed, p)
}
}
firewall := &compute.Firewall{
Name: *e.Name,
Network: e.Network.URL(project),
SourceTags: e.SourceTags,
SourceRanges: e.SourceRanges,
TargetTags: e.TargetTags,
Allowed: allowed,
}
return firewall, nil
}
func (_ *FirewallRule) RenderGCE(t *gce.GCEAPITarget, a, e, changes *FirewallRule) error {
firewall, err := e.mapToGCE(t.Cloud.Project)
if err != nil {
return err
}
if a == nil {
_, err := t.Cloud.Compute.Firewalls.Insert(t.Cloud.Project, firewall).Do()
if err != nil {
return fmt.Errorf("error creating FirewallRule: %v", err)
}
} else {
_, err := t.Cloud.Compute.Firewalls.Update(t.Cloud.Project, *e.Name, firewall).Do()
if err != nil {
return fmt.Errorf("error creating FirewallRule: %v", err)
}
}
return nil
}
type terraformAllow struct {
Protocol string `json:"protocol,omitempty"`
Ports []string `json:"ports,omitempty"`
}
type terraformFirewall struct {
Name string `json:"name"`
Network *terraform.Literal `json:"network"`
Allowed []*terraformAllow `json:"allow,omitempty"`
SourceTags []string `json:"source_tags,omitempty"`
SourceRanges []string `json:"source_ranges,omitempty"`
TargetTags []string `json:"target_tags,omitempty"`
}
func (_ *FirewallRule) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *FirewallRule) error {
g, err := e.mapToGCE(t.Project)
if err != nil {
return err
}
var allowed []*terraformAllow
if g.Allowed != nil {
for _, ga := range g.Allowed {
a := &terraformAllow{
Protocol: ga.IPProtocol,
Ports: ga.Ports,
}
allowed = append(allowed, a)
}
}
tf := &terraformFirewall{
Name: g.Name,
SourceRanges: g.SourceRanges,
TargetTags: g.TargetTags,
Allowed: allowed,
}
// TODO: This doesn't seem right, but it looks like a TF problem
tf.Network = e.Network.TerraformName()
return t.RenderResource("google_compute_firewall", *e.Name, tf)
}