-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjecter.go
302 lines (253 loc) · 6.6 KB
/
injecter.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"sync"
k8sfirewall "github.com/SunSince90/polycube/src/components/k8s/utils/k8sfirewall"
log "github.com/sirupsen/logrus"
)
func Inject(chains map[string]k8sfirewall.Chain) {
var waiter sync.WaitGroup
waiter.Add(len(chains))
for currentIP, currentChain := range chains {
go func(ip string, chain k8sfirewall.Chain) {
defer waiter.Done()
if alive(ip) {
push(ip, chain.Rule)
apply(ip, "ingress")
apply(ip, "egress")
}
}(currentIP, currentChain)
}
waiter.Wait()
fmt.Println("\t\t--- END ---")
}
func alive(ip string) bool {
endPoint := "http://" + ip + ":9000/polycube/v1/firewall/fw/"
req, err := http.NewRequest("GET", endPoint, nil)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
_, err = client.Do(req)
if err != nil {
log.Errorf("Pod with ip %s not found (is it dying?)", ip)
return false
}
return true
}
func reset(ip string) {
ingress := func() {
for i := 1; i < 50; i++ {
// Reset ingress
endPoint := "http://" + ip + ":9000/polycube/v1/firewall/fw/chain/ingress/rule/1"
req, err := http.NewRequest("DELETE", endPoint, nil)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Errorln("Error while trying to send request:", err, "(the pod might be TERMINATING.)")
return
}
if resp.StatusCode == 409 {
break
}
}
}
egress := func() {
// Reset egress
for i := 1; i < 50; i++ {
// Reset ingress
endPoint := "http://" + ip + ":9000/polycube/v1/firewall/fw/chain/egress/rule/1"
req, err := http.NewRequest("DELETE", endPoint, nil)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Errorln("Error while trying to send request:", err, "(the pod might be TERMINATING.)")
return
}
if resp.StatusCode == 409 {
break
}
}
}
ingress()
egress()
}
func buildConnectionRules(_ip string, rule k8sfirewall.ChainRule) ([]k8sfirewall.ChainRule, []k8sfirewall.ChainRule) {
ip := _ip + "/32"
forwardTemplate := func() ([]k8sfirewall.ChainRule, []k8sfirewall.ChainRule) {
in := []k8sfirewall.ChainRule{
k8sfirewall.ChainRule{
Src: rule.Src,
Dst: rule.Dst,
Sport: rule.Sport,
Dport: rule.Dport,
L4proto: rule.L4proto,
Conntrack: "new",
Action: rule.Action,
},
}
e := []k8sfirewall.ChainRule{}
return in, e
}
dropTemplate := func(rule k8sfirewall.ChainRule) []k8sfirewall.ChainRule {
return []k8sfirewall.ChainRule{
k8sfirewall.ChainRule{
Src: rule.Src,
Dst: rule.Dst,
Sport: rule.Sport,
Dport: rule.Dport,
L4proto: rule.L4proto,
Conntrack: "new",
Action: rule.Action,
},
}
}
if rule.Action == "forward" {
if ip == rule.Src {
e, in := forwardTemplate()
return in, e
}
if ip == rule.Dst {
return forwardTemplate()
}
}
if rule.Action == "drop" {
if ip == rule.Src {
return []k8sfirewall.ChainRule{}, dropTemplate(rule)
}
if ip == rule.Dst {
return dropTemplate(rule), []k8sfirewall.ChainRule{}
}
}
return []k8sfirewall.ChainRule{}, []k8sfirewall.ChainRule{}
}
func push(ip string, rules []k8sfirewall.ChainRule) {
if len(rules) > 0 {
reset(ip)
}
for _, rule := range rules {
ingressRules, egressRules := buildConnectionRules(ip, rule)
// Egress
egress := func(ers []k8sfirewall.ChainRule) {
endPoint := "http://" + ip + ":9000/polycube/v1/firewall/fw/chain/egress/append/"
for _, er := range ers {
data, err := marshal(er)
if err == nil {
req, err := http.NewRequest("POST", endPoint, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
_, err = client.Do(req)
if err != nil {
log.Errorln("Error while trying to send request:", err, "(the pod might be TERMINATING.)")
return
}
}
}
}
// Ingress
ingress := func(irs []k8sfirewall.ChainRule) {
endPoint := "http://" + ip + ":9000/polycube/v1/firewall/fw/chain/ingress/append/"
for _, ir := range irs {
data, err := marshal(ir)
if err == nil {
req, err := http.NewRequest("POST", endPoint, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
_, err = client.Do(req)
if err != nil {
log.Errorln("Error while trying to send request:", err, "(the pod might be TERMINATING.)")
return
}
}
}
}
egress(egressRules)
ingress(ingressRules)
log.Infof("Pushed rules in %s", ip)
}
}
func formatText(rule k8sfirewall.ChainRule, ip, direction string) string {
ingress := func() string {
ingressText := "\t FROM "
if len(rule.Src) > 0 {
ingressText += rule.Src
} else {
ingressText += "ANY"
}
ingressText += " "
ingressText += "TO: "
if len(rule.Dst) > 0 {
ingressText += rule.Dst
} else {
ingressText += ip
}
if rule.Sport != 0 {
ingressText += " SOURCE-PORT " + fmt.Sprint(rule.Sport)
}
if rule.Dport != 0 {
ingressText += " DESTINATION-PORT " + fmt.Sprint(rule.Dport)
}
ingressText += " ACTION: "
if rule.Action == "forward" {
ingressText += " ALLOW"
} else {
ingressText += " DENY"
}
return ingressText
}
egress := func() string {
egressText := "\t FROM "
if len(rule.Src) > 0 {
egressText += rule.Src
} else {
egressText += ip
}
egressText += " "
egressText += "TO: "
if len(rule.Dst) > 0 {
egressText += rule.Dst
} else {
egressText += "ANY"
}
if rule.Sport != 0 {
egressText += " SOURCE-PORT " + fmt.Sprint(rule.Sport)
}
if rule.Dport != 0 {
egressText += " DESTINATION-PORT " + fmt.Sprint(rule.Dport)
}
egressText += " ACTION: "
if rule.Action == "forward" {
egressText += " ALLOW"
} else {
egressText += " DENY"
}
return egressText
}
if direction == "ingress" {
return ingress()
}
return egress()
}
func marshal(rule k8sfirewall.ChainRule) ([]byte, error) {
data, err := json.MarshalIndent(&rule, "", " ")
if err != nil {
log.Errorln("Cannot marshal to json:", err)
return nil, err
}
return data, nil
}
func apply(ip, name string) (bool, error) {
endPoint := "http://" + ip + ":9000/polycube/v1/firewall/fw/chain/" + name + "/apply-rules/"
req, err := http.NewRequest("POST", endPoint, nil)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
_, err = client.Do(req)
if err != nil {
log.Errorln("Error while trying to apply rules:", err, "(the pod might be TERMINATING.)")
return false, nil
}
return true, nil
}