-
Notifications
You must be signed in to change notification settings - Fork 51
/
enforcerproxy.go
316 lines (270 loc) · 9.06 KB
/
enforcerproxy.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
// Package enforcerproxy :: This is the implementation of the RPC client
// It implements the interface of Trireme Enforcer and forwards these
// requests to the actual remote enforcer instead of implementing locally
package enforcerproxy
import (
"context"
"errors"
"fmt"
"sync"
"time"
"go.uber.org/zap"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/controller/constants"
"go.aporeto.io/trireme-lib/controller/internal/enforcer"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/constants"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/utils/rpcwrapper"
"go.aporeto.io/trireme-lib/controller/internal/portset"
"go.aporeto.io/trireme-lib/controller/internal/processmon"
"go.aporeto.io/trireme-lib/controller/pkg/fqconfig"
"go.aporeto.io/trireme-lib/controller/pkg/packetprocessor"
"go.aporeto.io/trireme-lib/controller/pkg/remoteenforcer"
"go.aporeto.io/trireme-lib/controller/pkg/secrets"
"go.aporeto.io/trireme-lib/policy"
"go.aporeto.io/trireme-lib/utils/crypto"
)
// ProxyInfo is the struct used to hold state about active enforcers in the system
type ProxyInfo struct {
MutualAuth bool
PacketLogs bool
Secrets secrets.Secrets
serverID string
validity time.Duration
prochdl processmon.ProcessManager
rpchdl rpcwrapper.RPCClient
initDone map[string]bool
filterQueue *fqconfig.FilterQueue
commandArg string
statsServerSecret string
procMountPoint string
ExternalIPCacheTimeout time.Duration
portSetInstance portset.PortSet
collector collector.EventCollector
sync.RWMutex
}
// InitRemoteEnforcer method makes a RPC call to the remote enforcer
func (s *ProxyInfo) InitRemoteEnforcer(contextID string) error {
resp := &rpcwrapper.Response{}
request := &rpcwrapper.Request{
Payload: &rpcwrapper.InitRequestPayload{
FqConfig: s.filterQueue,
MutualAuth: s.MutualAuth,
Validity: s.validity,
ServerID: s.serverID,
ExternalIPCacheTimeout: s.ExternalIPCacheTimeout,
PacketLogs: s.PacketLogs,
Secrets: s.Secrets.PublicSecrets(),
},
}
if err := s.rpchdl.RemoteCall(contextID, remoteenforcer.InitEnforcer, request, resp); err != nil {
return fmt.Errorf("failed to initialize remote enforcer: status: %s: %s", resp.Status, err)
}
if resp.Status != "" {
zap.L().Warn("received status while initializing the remote enforcer", zap.String("contextID", resp.Status))
}
s.Lock()
s.initDone[contextID] = true
s.Unlock()
return nil
}
// UpdateSecrets updates the secrets used for signing communication between trireme instances
func (s *ProxyInfo) UpdateSecrets(token secrets.Secrets) error {
s.Lock()
s.Secrets = token
s.Unlock()
resp := &rpcwrapper.Response{}
request := &rpcwrapper.Request{
Payload: &rpcwrapper.UpdateSecretsPayload{
Secrets: s.Secrets.PublicSecrets(),
},
}
for _, contextID := range s.rpchdl.ContextList() {
if err := s.rpchdl.RemoteCall(contextID, remoteenforcer.UpdateSecrets, request, resp); err != nil {
return fmt.Errorf("Failed to update secrets. status %s: %s", resp.Status, err)
}
}
return nil
}
// Enforce method makes a RPC call for the remote enforcer enforce method
func (s *ProxyInfo) Enforce(contextID string, puInfo *policy.PUInfo) error {
err := s.prochdl.LaunchProcess(contextID, puInfo.Runtime.Pid(), puInfo.Runtime.NSPath(), s.rpchdl, s.commandArg, s.statsServerSecret, s.procMountPoint)
if err != nil {
return err
}
zap.L().Debug("Called enforce and launched process", zap.String("contextID", contextID))
s.Lock()
_, ok := s.initDone[contextID]
s.Unlock()
if !ok {
if err = s.InitRemoteEnforcer(contextID); err != nil {
return err
}
}
enforcerPayload := &rpcwrapper.EnforcePayload{
ContextID: contextID,
Policy: puInfo.Policy.ToPublicPolicy(),
}
//Only the secrets need to be under lock. They can change async to the enforce call from Updatesecrets
s.RLock()
enforcerPayload.Secrets = s.Secrets.PublicSecrets()
s.RUnlock()
request := &rpcwrapper.Request{
Payload: enforcerPayload,
}
err = s.rpchdl.RemoteCall(contextID, remoteenforcer.Enforce, request, &rpcwrapper.Response{})
if err != nil {
// We can't talk to the enforcer. Kill it and restart it
s.Lock()
delete(s.initDone, contextID)
s.Unlock()
s.prochdl.KillProcess(contextID)
return fmt.Errorf("failed to send message to remote enforcer: %s", err)
}
return nil
}
// Unenforce stops enforcing policy for the given contextID.
func (s *ProxyInfo) Unenforce(contextID string) error {
s.Lock()
delete(s.initDone, contextID)
s.Unlock()
return nil
}
// GetFilterQueue returns the current FilterQueueConfig.
func (s *ProxyInfo) GetFilterQueue() *fqconfig.FilterQueue {
return s.filterQueue
}
// GetPortSetInstance returns nil for the proxy
func (s *ProxyInfo) GetPortSetInstance() portset.PortSet {
return s.portSetInstance
}
// Run starts the the remote enforcer proxy.
func (s *ProxyInfo) Run(ctx context.Context) error {
statsServer := rpcwrapper.NewRPCWrapper()
rpcServer := &StatsServer{rpchdl: statsServer, collector: s.collector, secret: s.statsServerSecret}
// Start the server for statistics collection.
go statsServer.StartServer(ctx, "unix", rpcwrapper.StatsChannel, rpcServer) // nolint
return nil
}
// NewProxyEnforcer creates a new proxy to remote enforcers.
func NewProxyEnforcer(mutualAuth bool,
filterQueue *fqconfig.FilterQueue,
collector collector.EventCollector,
service packetprocessor.PacketProcessor,
secrets secrets.Secrets,
serverID string,
validity time.Duration,
rpchdl rpcwrapper.RPCClient,
cmdArg string,
procMountPoint string,
ExternalIPCacheTimeout time.Duration,
packetLogs bool,
) enforcer.Enforcer {
return newProxyEnforcer(
mutualAuth,
filterQueue,
collector,
service,
secrets,
serverID,
validity,
rpchdl,
cmdArg,
processmon.GetProcessManagerHdl(),
procMountPoint,
ExternalIPCacheTimeout,
nil,
packetLogs,
)
}
// newProxyEnforcer creates a new proxy to remote enforcers.
func newProxyEnforcer(mutualAuth bool,
filterQueue *fqconfig.FilterQueue,
collector collector.EventCollector,
service packetprocessor.PacketProcessor,
secrets secrets.Secrets,
serverID string,
validity time.Duration,
rpchdl rpcwrapper.RPCClient,
cmdArg string,
procHdl processmon.ProcessManager,
procMountPoint string,
ExternalIPCacheTimeout time.Duration,
portSetInstance portset.PortSet,
packetLogs bool,
) enforcer.Enforcer {
statsServersecret, err := crypto.GenerateRandomString(32)
if err != nil {
// There is a very small chance of this happening we will log an error here.
zap.L().Error("Failed to generate random secret for stats reporting.Falling back to static secret", zap.Error(err))
// We will use current time as the secret
statsServersecret = time.Now().String()
}
proxydata := &ProxyInfo{
MutualAuth: mutualAuth,
Secrets: secrets,
serverID: serverID,
validity: validity,
prochdl: procHdl,
rpchdl: rpchdl,
initDone: make(map[string]bool),
filterQueue: filterQueue,
commandArg: cmdArg,
statsServerSecret: statsServersecret,
procMountPoint: procMountPoint,
ExternalIPCacheTimeout: ExternalIPCacheTimeout,
PacketLogs: packetLogs,
portSetInstance: portSetInstance,
collector: collector,
}
return proxydata
}
// NewDefaultProxyEnforcer This is the default datapth method. THis is implemented to keep the interface consistent whether we are local or remote enforcer.
func NewDefaultProxyEnforcer(serverID string,
collector collector.EventCollector,
secrets secrets.Secrets,
rpchdl rpcwrapper.RPCClient,
procMountPoint string,
) enforcer.Enforcer {
mutualAuthorization := false
fqConfig := fqconfig.NewFilterQueueWithDefaults()
defaultExternalIPCacheTimeout, err := time.ParseDuration(enforcerconstants.DefaultExternalIPTimeout)
if err != nil {
defaultExternalIPCacheTimeout = time.Second
}
defaultPacketLogs := false
validity := time.Hour * 8760
return NewProxyEnforcer(
mutualAuthorization,
fqConfig,
collector,
nil,
secrets,
serverID,
validity,
rpchdl,
constants.DefaultRemoteArg,
procMountPoint,
defaultExternalIPCacheTimeout,
defaultPacketLogs,
)
}
// StatsServer This struct is a receiver for Statsserver and maintains a handle to the RPC StatsServer.
type StatsServer struct {
collector collector.EventCollector
rpchdl rpcwrapper.RPCServer
secret string
}
// GetStats is the function called from the remoteenforcer when it has new flow events to publish.
func (r *StatsServer) GetStats(req rpcwrapper.Request, resp *rpcwrapper.Response) error {
if !r.rpchdl.ProcessMessage(&req, r.secret) {
return errors.New("message sender cannot be verified")
}
payload := req.Payload.(rpcwrapper.StatsPayload)
for _, record := range payload.Flows {
r.collector.CollectFlowEvent(record)
}
for _, record := range payload.Users {
r.collector.CollectUserEvent(record)
}
return nil
}