/
action_handler.go
237 lines (181 loc) · 6.4 KB
/
action_handler.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
package clusteragent
import (
"context"
"sync"
"time"
"github.com/kube-tarian/tarian/pkg/tarianpb"
"github.com/scylladb/go-set/strset"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/timestamppb"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type ActionHandler interface {
QueueEvent(*tarianpb.Event)
}
type actionHandler struct {
eventsChan chan *tarianpb.Event
actions []*tarianpb.Action
actionsLock sync.RWMutex
configClient tarianpb.ConfigClient
eventClient tarianpb.EventClient
k8sClientset *kubernetes.Clientset
cancelFunc context.CancelFunc
cancelCtx context.Context
}
func newActionHandler(tarianServerAddress string, opts []grpc.DialOption, k8sClientset *kubernetes.Clientset) *actionHandler {
logger.Infow("connecting to the tarian server", "address", tarianServerAddress)
grpcConn, err := grpc.Dial(tarianServerAddress, opts...)
configClient := tarianpb.NewConfigClient(grpcConn)
eventClient := tarianpb.NewEventClient(grpcConn)
if err != nil {
logger.Fatalw("couldn't not connect to tarian-server", "err", err)
}
ctx, cancel := context.WithCancel(context.Background())
ah := &actionHandler{eventsChan: make(chan *tarianpb.Event, 4096), configClient: configClient, eventClient: eventClient, k8sClientset: k8sClientset, cancelFunc: cancel, cancelCtx: ctx}
return ah
}
func (ah *actionHandler) QueueEvent(event *tarianpb.Event) {
logger.Debugw("event queued", "event", event)
ah.eventsChan <- event
}
func (ah *actionHandler) Run() {
go ah.LoopSyncActions()
for e := range ah.eventsChan {
ah.ProcessActions(e)
}
}
func (ah *actionHandler) ProcessActions(event *tarianpb.Event) {
logger.Debugw("event processed", "event", event)
if event.GetTargets() == nil {
return
}
for _, target := range event.GetTargets() {
if target.GetPod() == nil {
continue
}
pod := target.GetPod()
ah.actionsLock.RLock()
for _, action := range ah.actions {
actionEventFulfilled := false
if action.OnViolatedProcess && len(target.GetViolatedProcesses()) > 0 {
actionEventFulfilled = true
}
if action.OnViolatedFile && len(target.GetViolatedFiles()) > 0 {
actionEventFulfilled = true
}
if action.OnFalcoAlert && target.GetFalcoAlert() != nil {
if target.GetFalcoAlert().GetPriority() <= action.GetFalcoPriority() {
actionEventFulfilled = true
}
}
if actionEventFulfilled && actionMatchesPod(action, pod) {
// check if action timestamp is greater than pod's creation timestamp
// to prevent the pod from being terminated multiple times
if ah.isEventTimestampRecent(event.GetClientTimestamp(), pod) {
ah.runAction(action, pod)
}
}
}
ah.actionsLock.RUnlock()
}
}
func (ah *actionHandler) isEventTimestampRecent(t *timestamppb.Timestamp, podInfo *tarianpb.Pod) bool {
if ah.k8sClientset == nil {
logger.Warnw("about to determine action timestamp is recent, but kubernetes client is nil", "pod", podInfo.GetNamespace(), "namespace", podInfo.GetNamespace())
return false
}
ctx, cancel := context.WithTimeout(ah.cancelCtx, 10*time.Second)
defer cancel()
pod, err := ah.k8sClientset.CoreV1().Pods(podInfo.GetNamespace()).Get(ctx, podInfo.GetName(), metaV1.GetOptions{})
if err != nil {
logger.Errorw("error while calling get pod to check the created timestamp", "pod", pod.GetNamespace(), "namespace", pod.GetNamespace(), "err", err)
return false
}
return t.AsTime().After(pod.CreationTimestamp.Time)
}
func actionMatchesPod(action *tarianpb.Action, pod *tarianpb.Pod) bool {
if action.GetNamespace() != pod.GetNamespace() {
return false
}
if action.GetSelector() == nil || action.GetSelector().GetMatchLabels() == nil {
return true
}
actionLabels := strset.New()
for _, l := range action.GetSelector().GetMatchLabels() {
actionLabels.Add(l.GetKey() + "=" + l.GetValue())
}
podLabels := strset.New()
for _, l := range pod.GetLabels() {
podLabels.Add(l.GetKey() + "=" + l.GetValue())
}
return podLabels.IsSubset(actionLabels)
}
func (ah *actionHandler) runAction(action *tarianpb.Action, pod *tarianpb.Pod) {
// the only supported action now
if action.GetAction() != "delete-pod" {
return
}
if ah.k8sClientset == nil {
logger.Warnw("action due to run, but kubernetes client is nil", "actionName", action.GetName(), "action", action.GetAction(), "pod", pod.GetNamespace(), "namespace", pod.GetNamespace())
return
}
logger.Infow("run action", "actionName", action.GetName(), "action", action.GetAction(), "pod", pod.GetNamespace(), "namespace", pod.GetNamespace())
ctx, cancel := context.WithTimeout(ah.cancelCtx, 120*time.Second)
defer cancel()
err := ah.k8sClientset.CoreV1().Pods(pod.GetNamespace()).Delete(ctx, pod.GetName(), metaV1.DeleteOptions{})
if err == nil {
err2 := ah.ingestPodDeletedEvent(pod)
if err2 != nil {
logger.Errorw("error while logging pod-deleted event", "actionName", action.GetName(), "action", action.GetAction(), "pod", pod.GetNamespace(), "namespace", pod.GetNamespace(), "error", err)
}
} else {
logger.Infow("error while executing delete-pod action", "actionName", action.GetName(), "action", action.GetAction(), "pod", pod.GetNamespace(), "namespace", pod.GetNamespace(), "error", err)
}
}
func (ah *actionHandler) ingestPodDeletedEvent(pod *tarianpb.Pod) error {
event := &tarianpb.Event{
Type: tarianpb.EventTypePodDeleted,
ClientTimestamp: timestamppb.Now(),
Targets: []*tarianpb.Target{
{
Pod: pod,
},
},
}
req := &tarianpb.IngestEventRequest{
Event: event,
}
ctx2, cancel2 := context.WithTimeout(ah.cancelCtx, 10*time.Second)
defer cancel2()
_, err := ah.eventClient.IngestEvent(ctx2, req)
return err
}
func (ah *actionHandler) LoopSyncActions() error {
for {
ah.SyncActions()
select {
case <-time.After(3 * time.Second):
case <-ah.cancelCtx.Done():
return ah.cancelCtx.Err()
}
}
}
func (ah *actionHandler) SyncActions() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
r, err := ah.configClient.GetActions(ctx, &tarianpb.GetActionsRequest{})
if err != nil {
logger.Errorw("error while getting actions from the server", "err", err)
}
logger.Debugw("received actions from the server", "actions", r.GetActions())
cancel()
ah.SetActions(r.GetActions())
}
func (ah *actionHandler) SetActions(actions []*tarianpb.Action) {
ah.actionsLock.Lock()
defer ah.actionsLock.Unlock()
ah.actions = actions
}
func (ah *actionHandler) Close() {
ah.cancelFunc()
}