-
Notifications
You must be signed in to change notification settings - Fork 51
/
enforcer.go
209 lines (174 loc) · 5.39 KB
/
enforcer.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
package enforcer
import (
"context"
"fmt"
"time"
"github.com/aporeto-inc/trireme-lib/collector"
"github.com/aporeto-inc/trireme-lib/controller/constants"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/applicationproxy"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/nfqdatapath"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/nfqdatapath/tokenaccessor"
"github.com/aporeto-inc/trireme-lib/controller/internal/portset"
"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/policy"
"github.com/aporeto-inc/trireme-lib/utils/cache"
"go.uber.org/zap"
)
// A Enforcer is an implementation of the enforcer datapath. The interface
// can be implemented by one or multiple datapaths.
type Enforcer interface {
// Enforce starts enforcing policies for the given policy.PUInfo.
Enforce(contextID string, puInfo *policy.PUInfo) error
// Unenforce stops enforcing policy for the given IP.
Unenforce(contextID string) error
// GetFilterQueue returns the current FilterQueueConfig.
GetFilterQueue() *fqconfig.FilterQueue
// GetPortSetInstance returns nil for the proxy
GetPortSetInstance() portset.PortSet
// Run starts the PolicyEnforcer.
Run(ctx context.Context) error
// UpdateSecrets -- updates the secrets of running enforcers managed by trireme. Remote enforcers will get the secret updates with the next policy push
UpdateSecrets(secrets secrets.Secrets) error
}
// enforcer holds all the active implementations of the enforcer
type enforcer struct {
proxy *applicationproxy.AppProxy
transport *nfqdatapath.Datapath
}
// Run implements the run interfaces and runs the individual data paths
func (e *enforcer) Run(ctx context.Context) error {
if e.proxy != nil {
if err := e.proxy.Run(ctx); err != nil {
return err
}
}
if e.transport != nil {
if err := e.transport.Run(ctx); err != nil {
return err
}
}
return nil
}
// Enforce implements the enforce interface by sending the event to all the enforcers.
func (e *enforcer) Enforce(contextID string, puInfo *policy.PUInfo) error {
if e.transport != nil {
if err := e.transport.Enforce(contextID, puInfo); err != nil {
return fmt.Errorf("Failed to enforce in nfq %s", err.Error())
}
}
if e.proxy != nil {
if err := e.proxy.Enforce(context.Background(), contextID, puInfo); err != nil {
return fmt.Errorf("Failed to enforce in proxy %s", err.Error())
}
}
return nil
}
// Unenforce implements the Unenforce interface by sending the event to all the enforcers.
func (e *enforcer) Unenforce(contextID string) error {
var perr, nerr error
if e.proxy != nil {
if perr = e.proxy.Unenforce(context.Background(), contextID); perr != nil {
zap.L().Error("Failed to unenforce contextID in proxy",
zap.String("ContextID", contextID),
zap.Error(perr),
)
}
}
if e.transport != nil {
if nerr = e.transport.Unenforce(contextID); nerr != nil {
zap.L().Error("Failed to unenforce contextID in transport",
zap.String("ContextID", contextID),
zap.Error(nerr),
)
}
}
if perr != nil || nerr != nil {
return fmt.Errorf("Failed to unenforce %s %s", perr, nerr)
}
return nil
}
// Updatesecrets updates the secrets of the enforcers
func (e *enforcer) UpdateSecrets(secrets secrets.Secrets) error {
if e.proxy != nil {
if err := e.proxy.UpdateSecrets(secrets); err != nil {
return err
}
}
if e.transport != nil {
if err := e.transport.UpdateSecrets(secrets); err != nil {
return err
}
}
return nil
}
// GetFilterQueue returns the current FilterQueueConfig of the transport path.
func (e *enforcer) GetFilterQueue() *fqconfig.FilterQueue {
return e.transport.GetFilterQueue()
}
// GetPortSetInstance returns the port instance of the transport datapath
func (e *enforcer) GetPortSetInstance() portset.PortSet {
return e.transport.GetPortSetInstance()
}
// New returns a new policy enforcer that implements both the data paths.
func New(
mutualAuthorization bool,
fqConfig *fqconfig.FilterQueue,
collector collector.EventCollector,
service packetprocessor.PacketProcessor,
secrets secrets.Secrets,
serverID string,
validity time.Duration,
mode constants.ModeType,
procMountPoint string,
externalIPCacheTimeout time.Duration,
packetLogs bool,
) (Enforcer, error) {
tokenAccessor, err := tokenaccessor.New(serverID, validity, secrets)
if err != nil {
zap.L().Fatal("Cannot create a token engine")
}
puFromContextID := cache.NewCache("puFromContextID")
transport := nfqdatapath.New(
mutualAuthorization,
fqConfig,
collector,
serverID,
validity,
service,
secrets,
mode,
procMountPoint,
externalIPCacheTimeout,
packetLogs,
tokenAccessor,
puFromContextID,
)
tcpProxy, err := applicationproxy.NewAppProxy(tokenAccessor, collector, puFromContextID, nil, secrets)
if err != nil {
return nil, err
}
return &enforcer{
proxy: tcpProxy,
transport: transport,
}, nil
}
// NewWithDefaults create a new data path with most things used by default
func NewWithDefaults(
serverID string,
collector collector.EventCollector,
service packetprocessor.PacketProcessor,
secrets secrets.Secrets,
mode constants.ModeType,
procMountPoint string,
) Enforcer {
return nfqdatapath.NewWithDefaults(
serverID,
collector,
service,
secrets,
mode,
procMountPoint,
)
}