/
templates.go
244 lines (209 loc) · 6.12 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
package iptablesctrl
import (
"bytes"
"crypto/md5"
"encoding/base64"
"fmt"
"io"
"strconv"
"strings"
"text/template"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/controller/constants"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/nfqdatapath/afinetrawsocket"
"go.aporeto.io/trireme-lib/controller/pkg/packet"
"go.aporeto.io/trireme-lib/policy"
"go.aporeto.io/trireme-lib/utils/cgnetcls"
)
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 := strings.Fields(m)
// 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
HostInput string
HostOutput string
NetworkSvcInput string
NetworkSvcOutput string
TriremeInput string
TriremeOutput string
UIDInput string
UIDOutput string
NatProxyNetChain string
NatProxyAppChain string
MangleProxyNetChain string
MangleProxyAppChain string
PreRouting string
AppChain string
NetChain string
AppSection string
NetSection string
// common info
DefaultConnmark string
QueueBalanceAppSyn string
QueueBalanceAppSynAck string
QueueBalanceAppAck string
QueueBalanceNetSyn string
QueueBalanceNetSynAck string
QueueBalanceNetAck string
InitialMarkVal string
RawSocketMark string
TargetTCPNetSet string
TargetUDPNetSet string
ExclusionsSet string
// UDP rules
Numpackets string
InitialCount string
UDPSignature string
// Linux PUs
TCPPorts string
UDPPorts string
TCPPortSet string
// ProxyRules
DestIPSet string
SrvIPSet string
ProxyPort string
CgroupMark string
ProxyMark string
ProxySetName string
// UID PUs
Mark string
UID string
PortSet string
NFLOGPrefix string
NFLOGAcceptPrefix string
}
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 *Instance) newACLInfo(version int, contextID string, p *policy.PUInfo, puType common.PUType) (*ACLInfo, error) {
var appChain, netChain string
var err error
if contextID != "" {
appChain, netChain, err = chainName(contextID, version)
if err != nil {
return nil, err
}
}
var tcpPorts, udpPorts string
var servicePort, mark, uid string
if p != nil {
tcpPorts, udpPorts = common.ConvertServicesToProtocolPortList(p.Runtime.Options().Services)
puType = p.Runtime.PUType()
servicePort = p.Policy.ServicesListeningPort()
mark = p.Runtime.Options().CgroupMark
uid = p.Runtime.Options().UserID
}
proxySetName := puPortSetName(contextID, proxyPortSetPrefix)
destSetName, srvSetName := i.getSetNames(proxySetName)
appSection := ""
netSection := ""
switch puType {
case common.LinuxProcessPU, common.SSHSessionPU:
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.getPortSet(contextID)
cfg := &ACLInfo{
ContextID: contextID,
PUType: puType,
// Chains
MangleTable: "mangle",
NatTable: "nat",
MainAppChain: mainAppChain,
MainNetChain: mainNetChain,
HostInput: HostModeInput,
HostOutput: HostModeOutput,
NetworkSvcInput: NetworkSvcInput,
NetworkSvcOutput: NetworkSvcOutput,
TriremeInput: TriremeInput,
TriremeOutput: TriremeOutput,
UIDInput: uidInput,
UIDOutput: uidchain,
NatProxyNetChain: natProxyInputChain,
NatProxyAppChain: natProxyOutputChain,
MangleProxyNetChain: proxyInputChain,
MangleProxyAppChain: proxyOutputChain,
PreRouting: ipTableSectionPreRouting,
AppChain: appChain,
NetChain: netChain,
AppSection: appSection,
NetSection: netSection,
// common info
DefaultConnmark: strconv.Itoa(int(constants.DefaultConnMark)),
QueueBalanceAppSyn: i.fqc.GetApplicationQueueSynStr(),
QueueBalanceAppSynAck: i.fqc.GetApplicationQueueSynAckStr(),
QueueBalanceAppAck: i.fqc.GetApplicationQueueAckStr(),
QueueBalanceNetSyn: i.fqc.GetNetworkQueueSynStr(),
QueueBalanceNetSynAck: i.fqc.GetNetworkQueueSynAckStr(),
QueueBalanceNetAck: i.fqc.GetNetworkQueueAckStr(),
InitialMarkVal: strconv.Itoa(cgnetcls.Initialmarkval - 1),
RawSocketMark: strconv.Itoa(afinetrawsocket.ApplicationRawSocketMark),
TargetTCPNetSet: targetTCPNetworkSet,
TargetUDPNetSet: targetUDPNetworkSet,
ExclusionsSet: excludedNetworkSet,
// UDP rules
Numpackets: numPackets,
InitialCount: initialCount,
UDPSignature: packet.UDPAuthMarker,
// // Linux PUs
TCPPorts: tcpPorts,
UDPPorts: udpPorts,
TCPPortSet: portSetName,
// // ProxyRules
DestIPSet: destSetName,
SrvIPSet: srvSetName,
ProxyPort: servicePort,
CgroupMark: mark,
ProxyMark: proxyMark,
ProxySetName: proxySetName,
// // UID PUs
UID: uid,
Mark: mark,
PortSet: portSetName,
NFLOGPrefix: policy.DefaultLogPrefix(contextID),
NFLOGAcceptPrefix: policy.DefaultAcceptLogPrefix(contextID),
}
return cfg, nil
}