-
Notifications
You must be signed in to change notification settings - Fork 51
/
templates.go
323 lines (271 loc) · 8.2 KB
/
templates.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
package iptablesctrl
import (
"bytes"
"crypto/md5"
"encoding/base64"
"fmt"
"io"
"net"
"strconv"
"strings"
"text/template"
"github.com/kballard/go-shellquote"
"go.aporeto.io/enforcerd/trireme-lib/common"
cconstants "go.aporeto.io/enforcerd/trireme-lib/controller/constants"
"go.aporeto.io/enforcerd/trireme-lib/controller/internal/enforcer/nfqdatapath/afinetrawsocket"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/packet"
"go.aporeto.io/enforcerd/trireme-lib/policy"
"go.aporeto.io/enforcerd/trireme-lib/utils/constants"
)
func extractRulesFromTemplate(tmpl *template.Template, data interface{}) ([][]string, error) {
buffer := bytes.NewBuffer([]byte{})
if err := tmpl.Execute(buffer, data); err != nil {
return [][]string{}, fmt.Errorf("unable to execute template:%s", err)
}
rules := [][]string{}
for _, m := range strings.Split(buffer.String(), "\n") {
rule, err := shellquote.Split(m)
if err != nil {
return [][]string{}, err
}
// ignore empty lines in the buffer
if len(rule) <= 1 {
continue
}
rules = append(rules, rule)
}
return rules, nil
}
// ACLInfo keeps track of all information to create ACLs
type ACLInfo struct {
ContextID string
PUType common.PUType
// Tables
MangleTable string
NatTable string
// Chains
MainAppChain string
MainNetChain string
BPFPath string
HostInput string
HostOutput string
NfqueueOutput string
NfqueueInput string
NetworkSvcInput string
NetworkSvcOutput string
TriremeInput string
TriremeOutput string
NatProxyNetChain string
NatProxyAppChain string
MangleProxyNetChain string
MangleProxyAppChain string
PreRouting string
AppChain string
NetChain string
AppSection string
NetSection string
// serviceMesh chains
IstioChain string
// common info
DefaultConnmark string
DefaultDropConnmark string
DefaultExternalConnmark string
PacketMarkToSetConnmark string
DefaultInputMark string
DefaultHandShakeMark string
RawSocketMark string
TargetTCPNetSet string
TargetUDPNetSet string
ExclusionsSet string
IpsetPrefix string
// IPv4 IPv6
DefaultIP string
needICMPRules bool
// UDP rules
Numpackets string
InitialCount string
UDPSignature string
// Linux PUs
TCPPorts string
UDPPorts string
TCPPortSet string
// ProxyRules
DestIPSet string
SrvIPSet string
ProxyPort string
DNSProxyPort string
DNSServerIP string
CgroupMark string
ProxyMark string
AuthPhaseMark string
PacketMark string
Mark string
PortSet string
AppNFLOGPrefix string
AppNFLOGDropPacketLogPrefix string
AppDefaultAction string
NetNFLOGPrefix string
NetNFLOGDropPacketLogPrefix string
NetDefaultAction string
NFQueues []int
NumNFQueues int
// icmpv6 allow bytecode
ICMPv6Allow string
// Istio Iptable rules
IstioEnabled bool
}
func chainName(contextID string, version int) (app, net string, err error) {
hash := md5.New()
if _, err := io.WriteString(hash, contextID); err != nil {
return "", "", err
}
output := base64.URLEncoding.EncodeToString(hash.Sum(nil))
if len(contextID) > 4 {
contextID = contextID[:4] + output[:6]
} else {
contextID = contextID + output[:6]
}
app = appChainPrefix + contextID + "-" + strconv.Itoa(version)
net = netChainPrefix + contextID + "-" + strconv.Itoa(version)
return app, net, nil
}
func (i *iptables) newACLInfo(version int, contextID string, p *policy.PUInfo, puType common.PUType) (*ACLInfo, error) {
var appChain, netChain string
var err error
var nfqueues []int
numQueues := i.fqc.GetNumQueues()
ipFilter := i.impl.IPFilter()
if contextID != "" {
appChain, netChain, err = chainName(contextID, version)
if err != nil {
return nil, err
}
}
parseDNSServerIP := func() string {
for _, ipString := range i.fqc.GetDNSServerAddresses() {
if ip := net.ParseIP(ipString); ip != nil {
if ipFilter(ip) {
return ipString
}
continue
}
// parseCIDR
if ip, _, err := net.ParseCIDR(ipString); err == nil {
if ipFilter(ip) {
return ipString
}
}
}
return ""
}
appDefaultAction := policy.Reject | policy.Log
netDefaultAction := policy.Reject | policy.Log
var tcpPorts, udpPorts string
var servicePort, mark, dnsProxyPort, packetMark string
if p != nil {
tcpPorts, udpPorts = common.ConvertServicesToProtocolPortList(p.Runtime.Options().Services)
puType = p.Runtime.PUType()
servicePort = p.Policy.ServicesListeningPort()
dnsProxyPort = p.Policy.DNSProxyPort()
mark = p.Runtime.Options().CgroupMark
packetMark = mark
appDefaultAction = p.Policy.AppDefaultPolicyAction()
netDefaultAction = p.Policy.NetDefaultPolicyAction()
}
destSetName, srvSetName := i.ipsetmanager.GetProxySetNames(contextID)
tcpTargetSetName, udpTargetSetName, excludedNetworkSetName := i.ipsetmanager.GetIPsetNamesForTargetAndExcludedNetworks()
appSection := ""
netSection := ""
switch puType {
case common.LinuxProcessPU, common.WindowsProcessPU:
appSection = TriremeOutput
netSection = TriremeInput
case common.HostNetworkPU:
appSection = NetworkSvcOutput
netSection = NetworkSvcInput
case common.HostPU:
appSection = HostModeOutput
netSection = HostModeInput
default:
appSection = mainAppChain
netSection = mainNetChain
}
portSetName := i.ipsetmanager.GetServerPortSetName(contextID)
for i := 0; i < numQueues; i++ {
nfqueues = append(nfqueues, i)
}
cfg := &ACLInfo{
ContextID: contextID,
PUType: puType,
// Chains
MangleTable: "mangle",
NatTable: "nat",
MainAppChain: mainAppChain,
MainNetChain: mainNetChain,
HostInput: HostModeInput,
HostOutput: HostModeOutput,
NfqueueOutput: NfqueueOutput,
NfqueueInput: NfqueueInput,
NFQueues: nfqueues,
NumNFQueues: numQueues,
NetworkSvcInput: NetworkSvcInput,
NetworkSvcOutput: NetworkSvcOutput,
TriremeInput: TriremeInput,
TriremeOutput: TriremeOutput,
NatProxyNetChain: natProxyInputChain,
NatProxyAppChain: natProxyOutputChain,
MangleProxyNetChain: proxyInputChain,
MangleProxyAppChain: proxyOutputChain,
PreRouting: ipTableSectionPreRouting,
AppChain: appChain,
NetChain: netChain,
AppSection: appSection,
NetSection: netSection,
IstioChain: istioChain,
// common info
DefaultConnmark: strconv.Itoa(int(constants.DefaultConnMark)),
DefaultDropConnmark: strconv.Itoa(int(constants.DropConnmark)),
DefaultExternalConnmark: strconv.Itoa(int(constants.DefaultExternalConnMark)),
PacketMarkToSetConnmark: strconv.Itoa(int(constants.PacketMarkToSetConnmark)),
DefaultInputMark: strconv.Itoa(int(constants.DefaultInputMark)),
RawSocketMark: strconv.Itoa(afinetrawsocket.ApplicationRawSocketMark),
DefaultHandShakeMark: strconv.Itoa(int(constants.HandshakeConnmark)),
CgroupMark: mark,
TargetTCPNetSet: tcpTargetSetName,
TargetUDPNetSet: udpTargetSetName,
ExclusionsSet: excludedNetworkSetName,
// IPv4 vs IPv6
DefaultIP: i.impl.GetDefaultIP(),
needICMPRules: i.impl.NeedICMP(),
// UDP rules
Numpackets: numPackets,
InitialCount: initialCount,
UDPSignature: packet.UDPAuthMarker,
// // Linux PUs
TCPPorts: tcpPorts,
UDPPorts: udpPorts,
TCPPortSet: portSetName,
// // ProxyRules
DestIPSet: destSetName,
SrvIPSet: srvSetName,
ProxyPort: servicePort,
DNSProxyPort: dnsProxyPort,
DNSServerIP: parseDNSServerIP(),
ProxyMark: cconstants.ProxyMark,
// PUs
PacketMark: packetMark,
Mark: mark,
PortSet: portSetName,
AppNFLOGPrefix: policy.DefaultLogPrefix(contextID, appDefaultAction),
AppNFLOGDropPacketLogPrefix: policy.DefaultDropPacketLogPrefix(contextID),
AppDefaultAction: policy.DefaultAction(appDefaultAction),
NetNFLOGPrefix: policy.DefaultLogPrefix(contextID, netDefaultAction),
NetNFLOGDropPacketLogPrefix: policy.DefaultDropPacketLogPrefix(contextID),
NetDefaultAction: policy.DefaultAction(netDefaultAction),
}
allowICMPv6(cfg)
if i.bpf != nil {
cfg.BPFPath = i.bpf.GetBPFPath()
}
return cfg, nil
}