-
Notifications
You must be signed in to change notification settings - Fork 51
/
config.go
287 lines (250 loc) · 7.96 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
package controller
import (
"fmt"
"sync"
"time"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/controller/constants"
"go.aporeto.io/trireme-lib/controller/internal/enforcer"
enforcerproxy "go.aporeto.io/trireme-lib/controller/internal/enforcer/proxy"
"go.aporeto.io/trireme-lib/controller/internal/supervisor"
supervisorproxy "go.aporeto.io/trireme-lib/controller/internal/supervisor/proxy"
"go.aporeto.io/trireme-lib/controller/pkg/env"
"go.aporeto.io/trireme-lib/controller/pkg/fqconfig"
"go.aporeto.io/trireme-lib/controller/pkg/packetprocessor"
"go.aporeto.io/trireme-lib/controller/pkg/secrets"
"go.aporeto.io/trireme-lib/controller/runtime"
"go.aporeto.io/trireme-lib/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
linuxProcess bool
mutualAuth bool
packetLogs bool
validity time.Duration
procMountPoint string
externalIPcacheTimeout time.Duration
runtimeCfg *runtime.Configuration
runtimeErrorChannel chan *policy.RuntimeError
remoteParameters *env.RemoteParameters
binaryTokens bool
}
// Option is provided using functional arguments.
type Option func(*config)
// 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
}
}
// OptionBinaryTokens enables the binary token datapath
func OptionBinaryTokens(b bool) Option {
return func(cfg *config) {
cfg.binaryTokens = b
}
}
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.binaryTokens,
)
if err != nil {
return fmt.Errorf("Failed to initialize enforcer: %s ", err)
}
}
zap.L().Debug("TriremeMode", zap.Int("Status", int(t.config.mode)))
if t.config.mode == constants.RemoteContainer {
t.enforcers[constants.RemoteContainer] = 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.binaryTokens,
)
}
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.binaryTokens,
)
if err != nil {
return fmt.Errorf("Failed to initialize sidecar enforcer: %s ", err)
}
}
return nil
}
func (t *trireme) newSupervisors() error {
if t.config.linuxProcess {
sup, err := supervisor.NewSupervisor(
t.config.collector,
t.enforcers[constants.LocalServer],
constants.LocalServer,
t.config.runtimeCfg,
t.config.service,
)
if err != nil {
return fmt.Errorf("Could Not create process supervisor :: received error %v", err)
}
t.supervisors[constants.LocalServer] = sup
}
if t.config.mode == constants.RemoteContainer {
t.supervisors[constants.RemoteContainer] = supervisorproxy.NewProxySupervisor()
}
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,
)
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
}