-
Notifications
You must be signed in to change notification settings - Fork 51
/
config.go
231 lines (200 loc) · 6.42 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
package controller
import (
"fmt"
"sync"
"time"
"github.com/aporeto-inc/trireme-lib/collector"
"github.com/aporeto-inc/trireme-lib/common"
"github.com/aporeto-inc/trireme-lib/controller/constants"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/proxy"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/utils/rpcwrapper"
"github.com/aporeto-inc/trireme-lib/controller/internal/supervisor"
"github.com/aporeto-inc/trireme-lib/controller/internal/supervisor/proxy"
"github.com/aporeto-inc/trireme-lib/controller/pkg/fqconfig"
"github.com/aporeto-inc/trireme-lib/controller/pkg/packetprocessor"
"github.com/aporeto-inc/trireme-lib/controller/pkg/secrets"
"github.com/aporeto-inc/trireme-lib/utils/allocator"
"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
targetNetworks []string
proxyPort int
}
// 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
}
}
// OptionTargetNetworks is an option to provide target network configuration.
func OptionTargetNetworks(n []string) Option {
return func(cfg *config) {
cfg.targetNetworks = n
}
}
// OptionApplicationProxyPort is an option provide starting proxy port for application proxy
func OptionApplicationProxyPort(proxyPort int) Option {
return func(cfg *config) {
cfg.proxyPort = proxyPort
}
}
// OptionProcMountPoint is an option to provide proc mount point.
func OptionProcMountPoint(p string) Option {
return func(cfg *config) {
cfg.procMountPoint = p
}
}
// OptionPacketLogs is an option to enable packet level logging.
func OptionPacketLogs() Option {
return func(cfg *config) {
cfg.packetLogs = true
}
}
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,
)
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.service,
t.config.secret,
t.config.serverID,
t.config.validity,
t.rpchdl,
"enforce",
t.config.procMountPoint,
t.config.externalIPcacheTimeout,
t.config.packetLogs,
)
}
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.targetNetworks,
)
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 {
s, err := supervisorproxy.NewProxySupervisor(
t.config.collector,
t.enforcers[constants.RemoteContainer],
t.rpchdl,
)
if err != nil {
zap.L().Error("Unable to create proxy Supervisor:: Returned Error ", zap.Error(err))
return nil
}
t.supervisors[constants.RemoteContainer] = 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,
port: allocator.New(c.proxyPort, 100),
rpchdl: rpcwrapper.NewRPCWrapper(),
enforcers: map[constants.ModeType]enforcer.Enforcer{},
supervisors: map[constants.ModeType]supervisor.Supervisor{},
puTypeToEnforcerType: map[common.PUType]constants.ModeType{},
locks: sync.Map{},
}
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
}
if t.config.mode == constants.RemoteContainer {
t.puTypeToEnforcerType[common.ContainerPU] = constants.RemoteContainer
t.puTypeToEnforcerType[common.KubernetesPU] = constants.RemoteContainer
}
return t
}