-
Notifications
You must be signed in to change notification settings - Fork 51
/
config.go
361 lines (316 loc) · 10 KB
/
config.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package controller
import (
"fmt"
"sync"
"time"
"github.com/blang/semver"
"go.aporeto.io/trireme-lib/v11/controller/internal/enforcer/utils/rpcwrapper"
"go.aporeto.io/trireme-lib/v11/collector"
"go.aporeto.io/trireme-lib/v11/common"
"go.aporeto.io/trireme-lib/v11/controller/constants"
"go.aporeto.io/trireme-lib/v11/controller/internal/enforcer"
enforcerproxy "go.aporeto.io/trireme-lib/v11/controller/internal/enforcer/proxy"
"go.aporeto.io/trireme-lib/v11/controller/internal/supervisor"
supervisornoop "go.aporeto.io/trireme-lib/v11/controller/internal/supervisor/noop"
"go.aporeto.io/trireme-lib/v11/controller/pkg/env"
"go.aporeto.io/trireme-lib/v11/controller/pkg/fqconfig"
"go.aporeto.io/trireme-lib/v11/controller/pkg/ipsetmanager"
"go.aporeto.io/trireme-lib/v11/controller/pkg/packetprocessor"
"go.aporeto.io/trireme-lib/v11/controller/pkg/secrets"
"go.aporeto.io/trireme-lib/v11/controller/runtime"
"go.aporeto.io/trireme-lib/v11/policy"
"go.uber.org/zap"
)
// config specifies all configurations accepted by trireme to start.
type config struct {
// Required Parameters.
serverID string
// External Interface implementations that we allow to plugin to components.
collector collector.EventCollector
service packetprocessor.PacketProcessor
secret secrets.Secrets
// Configurations for fine tuning internal components.
mode constants.ModeType
fq *fqconfig.FilterQueue
isBPFEnabled bool
linuxProcess bool
mutualAuth bool
packetLogs bool
validity time.Duration
procMountPoint string
externalIPcacheTimeout time.Duration
runtimeCfg *runtime.Configuration
runtimeErrorChannel chan *policy.RuntimeError
remoteParameters *env.RemoteParameters
tokenIssuer common.ServiceTokenIssuer
binaryTokens bool
aclmanager ipsetmanager.ACLManager
ipv6Enabled bool
agentVersion semver.Version
}
// Option is provided using functional arguments.
type Option func(*config)
// OptionBPFEnabled is an option
func OptionBPFEnabled(bpfEnabled bool) Option {
return func(cfg *config) {
cfg.isBPFEnabled = bpfEnabled
}
}
//OptionIPv6Enable is an option to enable ipv6
func OptionIPv6Enable(ipv6Enabled bool) Option {
return func(cfg *config) {
cfg.ipv6Enabled = ipv6Enabled
}
}
//OptionIPSetManager is an option to provide ipsetmanager
func OptionIPSetManager(manager ipsetmanager.ACLManager) Option {
return func(cfg *config) {
cfg.aclmanager = manager
}
}
// OptionCollector is an option to provide an external collector implementation.
func OptionCollector(c collector.EventCollector) Option {
return func(cfg *config) {
cfg.collector = c
}
}
// OptionDatapathService is an option to provide an external datapath service implementation.
func OptionDatapathService(s packetprocessor.PacketProcessor) Option {
return func(cfg *config) {
cfg.service = s
}
}
// OptionSecret is an option to provide an external datapath service implementation.
func OptionSecret(s secrets.Secrets) Option {
return func(cfg *config) {
cfg.secret = s
}
}
// OptionEnforceLinuxProcess is an option to request support for linux process support.
func OptionEnforceLinuxProcess() Option {
return func(cfg *config) {
cfg.linuxProcess = true
}
}
// OptionEnforceFqConfig is an option to override filter queues.
func OptionEnforceFqConfig(f *fqconfig.FilterQueue) Option {
return func(cfg *config) {
cfg.fq = f
}
}
// OptionDisableMutualAuth is an option to disable MutualAuth (enabled by default)
func OptionDisableMutualAuth() Option {
return func(cfg *config) {
cfg.mutualAuth = false
}
}
// OptionRuntimeConfiguration is an option to provide target network configuration.
func OptionRuntimeConfiguration(c *runtime.Configuration) Option {
return func(cfg *config) {
cfg.runtimeCfg = c
}
}
// OptionProcMountPoint is an option to provide proc mount point.
func OptionProcMountPoint(p string) Option {
return func(cfg *config) {
cfg.procMountPoint = p
}
}
// OptionRuntimeErrorChannel configures the error channel for the policy engine.
func OptionRuntimeErrorChannel(errorChannel chan *policy.RuntimeError) Option {
return func(cfg *config) {
cfg.runtimeErrorChannel = errorChannel
}
}
// OptionPacketLogs is an option to enable packet level logging.
func OptionPacketLogs() Option {
return func(cfg *config) {
cfg.packetLogs = true
}
}
// OptionRemoteParameters is an option to set the parameters for the remote
func OptionRemoteParameters(p *env.RemoteParameters) Option {
return func(cfg *config) {
cfg.remoteParameters = p
}
}
// OptionTokenIssuer provides the token issuer.
func OptionTokenIssuer(t common.ServiceTokenIssuer) Option {
return func(cfg *config) {
cfg.tokenIssuer = t
}
}
// OptionBinaryTokens enables the binary token datapath
func OptionBinaryTokens(b bool) Option {
return func(cfg *config) {
cfg.binaryTokens = b
}
}
// OptionAgentVersion is an option to set agent version.
func OptionAgentVersion(v semver.Version) Option {
return func(cfg *config) {
cfg.agentVersion = v
}
}
func (t *trireme) newEnforcers() error {
zap.L().Debug("LinuxProcessSupport", zap.Bool("Status", t.config.linuxProcess))
var err error
if t.config.linuxProcess {
t.enforcers[constants.LocalServer], err = enforcer.New(
t.config.mutualAuth,
t.config.fq,
t.config.collector,
t.config.service,
t.config.secret,
t.config.serverID,
t.config.validity,
constants.LocalServer,
t.config.procMountPoint,
t.config.externalIPcacheTimeout,
t.config.packetLogs,
t.config.runtimeCfg,
t.config.tokenIssuer,
t.config.binaryTokens,
t.config.aclmanager,
t.config.isBPFEnabled,
t.config.agentVersion,
)
if err != nil {
return fmt.Errorf("Failed to initialize LocalServer enforcer: %s ", err)
}
err = t.setupEnvoyAuthorizer()
if err != nil {
return fmt.Errorf("Failed to initialize LocalEnvoyAuthorizer enforcer: %s ", err)
}
}
zap.L().Debug("TriremeMode", zap.Int("Status", int(t.config.mode)))
if t.config.mode == constants.RemoteContainer {
enforcerProxy := enforcerproxy.NewProxyEnforcer(
t.config.mutualAuth,
t.config.fq,
t.config.collector,
t.config.secret,
t.config.serverID,
t.config.validity,
"enforce",
t.config.procMountPoint,
t.config.externalIPcacheTimeout,
t.config.packetLogs,
t.config.runtimeCfg,
t.config.runtimeErrorChannel,
t.config.remoteParameters,
t.config.tokenIssuer,
t.config.binaryTokens,
t.config.isBPFEnabled,
t.config.ipv6Enabled,
rpcwrapper.NewRPCServer(),
)
t.enforcers[constants.RemoteContainer] = enforcerProxy
t.enforcers[constants.RemoteContainerEnvoyAuthorizer] = enforcerProxy
}
zap.L().Debug("TriremeMode", zap.Int("Status", int(t.config.mode)))
if t.config.mode == constants.Sidecar {
t.enforcers[constants.Sidecar], err = enforcer.New(
t.config.mutualAuth,
t.config.fq,
t.config.collector,
t.config.service,
t.config.secret,
t.config.serverID,
t.config.validity,
constants.Sidecar,
t.config.procMountPoint,
t.config.externalIPcacheTimeout,
t.config.packetLogs,
t.config.runtimeCfg,
t.config.tokenIssuer,
t.config.binaryTokens,
t.config.aclmanager,
false,
t.config.agentVersion,
)
if err != nil {
return fmt.Errorf("Failed to initialize sidecar enforcer: %s ", err)
}
}
return nil
}
func (t *trireme) newSupervisors() error {
noopSup := supervisornoop.NewNoopSupervisor()
if t.config.linuxProcess {
sup, err := supervisor.NewSupervisor(
t.config.collector,
t.enforcers[constants.LocalServer],
constants.LocalServer,
t.config.runtimeCfg,
t.config.service,
t.config.aclmanager,
t.config.ipv6Enabled,
)
if err != nil {
return fmt.Errorf("Could Not create process supervisor :: received error %v", err)
}
t.supervisors[constants.LocalServer] = sup
err = t.setupEnvoySupervisor(noopSup)
if err != nil {
return fmt.Errorf("Could Not create envoy supervisor :: received error %v", err)
}
}
if t.config.mode == constants.RemoteContainer {
t.supervisors[constants.RemoteContainer] = noopSup
t.supervisors[constants.RemoteContainerEnvoyAuthorizer] = noopSup
}
if t.config.mode == constants.Sidecar {
s, err := supervisor.NewSupervisor(
t.config.collector,
t.enforcers[constants.Sidecar],
constants.Sidecar,
t.config.runtimeCfg,
t.config.service,
t.config.aclmanager,
t.config.ipv6Enabled,
)
if err != nil {
return fmt.Errorf("Could Not create process sidecar supervisor :: received error %v", err)
}
t.supervisors[constants.Sidecar] = s
}
return nil
}
// newTrireme returns a reference to the trireme object based on the parameter subelements.
func newTrireme(c *config) TriremeController {
var err error
t := &trireme{
config: c,
enforcers: map[constants.ModeType]enforcer.Enforcer{},
supervisors: map[constants.ModeType]supervisor.Supervisor{},
puTypeToEnforcerType: map[common.PUType]constants.ModeType{},
locks: sync.Map{},
enablingTrace: make(chan *traceTrigger, 10),
}
zap.L().Debug("Creating Enforcers")
if err = t.newEnforcers(); err != nil {
zap.L().Error("Unable to create datapath enforcers", zap.Error(err))
return nil
}
zap.L().Debug("Creating Supervisors")
if err = t.newSupervisors(); err != nil {
zap.L().Error("Unable to start datapath supervisor", zap.Error(err))
return nil
}
if c.linuxProcess {
t.puTypeToEnforcerType[common.LinuxProcessPU] = constants.LocalServer
t.puTypeToEnforcerType[common.UIDLoginPU] = constants.LocalServer
t.puTypeToEnforcerType[common.HostPU] = constants.LocalServer
t.puTypeToEnforcerType[common.HostNetworkPU] = constants.LocalServer
t.puTypeToEnforcerType[common.SSHSessionPU] = constants.LocalServer
}
if t.config.mode == constants.RemoteContainer {
t.puTypeToEnforcerType[common.ContainerPU] = constants.RemoteContainer
t.puTypeToEnforcerType[common.KubernetesPU] = constants.RemoteContainer
}
if t.config.mode == constants.Sidecar {
t.puTypeToEnforcerType[common.ContainerPU] = constants.Sidecar
}
return t
}