-
Notifications
You must be signed in to change notification settings - Fork 165
/
policyreport.go
453 lines (406 loc) · 14.8 KB
/
policyreport.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// SPDX-License-Identifier: MIT OR Apache-2.0
package outputs
import (
"context"
"fmt"
"log"
"os"
"github.com/DataDog/datadog-go/statsd"
"github.com/falcosecurity/falcosidekick/types"
"github.com/google/uuid"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/retry"
wgpolicy "sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/api/wgpolicyk8s.io/v1alpha2"
crdClient "sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/generated/v1alpha2/clientset/versioned"
)
type resource struct {
apiVersion string
kind string
}
const (
clusterPolicyReportBaseName = "falco-cluster-policy-report-"
policyReportBaseName = "falco-policy-report-"
policyReportSource = "Falco"
high wgpolicy.PolicyResultSeverity = "high"
low wgpolicy.PolicyResultSeverity = "low"
medium wgpolicy.PolicyResultSeverity = "medium"
info wgpolicy.PolicyResultSeverity = "info"
critical wgpolicy.PolicyResultSeverity = "critical"
fail wgpolicy.PolicyResult = "fail"
warn wgpolicy.PolicyResult = "warn"
skip wgpolicy.PolicyResult = "skip"
targetNS = "ka.target.namespace"
targetResource = "ka.target.resource"
targetName = "ka.target.name"
responseName = "ka.resp.name"
)
var (
minimumPriority string //nolint: unused
//slice of policy reports
policyReports = make(map[string]*wgpolicy.PolicyReport)
//cluster policy report
clusterPolicyReport *wgpolicy.ClusterPolicyReport = &wgpolicy.ClusterPolicyReport{
ObjectMeta: metav1.ObjectMeta{
Name: clusterPolicyReportBaseName,
Labels: map[string]string{
"app.kubernetes.io/created-by": "falcosidekick",
},
},
Summary: wgpolicy.PolicyReportSummary{
Fail: 0,
Warn: 0,
},
}
falcosidekickNamespace string
falcosidekickNamespaceUID k8stypes.UID
// used resources in the k8saudit ruleset
resourceMapping = map[string]resource{
"pods": {"v1", "Pod"},
"services": {"v1", "Service"},
"secrets": {"v1", "Secrets"},
"configmaps": {"v1", "ConfigMap"},
"namespaces": {"v1", "Namespace"},
"serviceaccounts": {"v1", "ServiceAccount"},
"daemonsets": {"apps/v1", "DaemonSet"},
"deployments": {"apps/v1", "Deployments"},
"cronjobs": {"batch/v1", "CronJob"},
"jobs": {"batch/v1", "Job"},
"clusterroles": {"rbac.authorization.k8s.io/v1", "ClusterRole"},
"clusterrolebindings": {"rbac.authorization.k8s.io/v1", "ClusterRoleBinding"},
"roles": {"rbac.authorization.k8s.io/v1", "Role"},
"rolebindings": {"rbac.authorization.k8s.io/v1", "RoleBinding"},
}
)
func NewPolicyReportClient(config *types.Configuration, stats *types.Statistics, promStats *types.PromStatistics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) {
clusterPolicyReport.ObjectMeta.Name += uuid.NewString()[:8]
minimumPriority = config.PolicyReport.MinimumPriority
clientConfig, err := rest.InClusterConfig()
if err != nil {
clientConfig, err = clientcmd.BuildConfigFromFlags("", config.PolicyReport.Kubeconfig)
if err != nil {
log.Printf("[ERROR] : PolicyReport - Unable to load kube config file: %v\n", err)
}
}
crdclient, err := crdClient.NewForConfig(clientConfig)
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return nil, err
}
falcosidekickNamespace = os.Getenv("NAMESPACE")
if falcosidekickNamespace == "" {
log.Println("[INFO] : PolicyReport - No env var NAMESPACE detected")
} else {
n, err := clientset.CoreV1().Namespaces().Get(context.TODO(), falcosidekickNamespace, metav1.GetOptions{})
if err != nil {
log.Printf("[ERROR] : PolicyReport - Can't get UID of namespace %v: %v\n", falcosidekickNamespace, err)
} else {
falcosidekickNamespaceUID = n.ObjectMeta.UID
}
}
return &Client{
OutputType: "PolicyReport",
Config: config,
Stats: stats,
PromStats: promStats,
StatsdClient: statsdClient,
DogstatsdClient: dogstatsdClient,
Crdclient: crdclient,
}, nil
}
// UpdateOrCreatePolicyReport creates/updates PolicyReport/ClusterPolicyReport Resource in Kubernetes
func (c *Client) UpdateOrCreatePolicyReport(falcopayload types.FalcoPayload) {
c.Stats.PolicyReport.Add(Total, 1)
event, namespace := newResult(falcopayload)
var err error
if namespace != "" {
// case where the event is namespace specific
err = updatePolicyReports(c, namespace, event)
} else {
err = updateClusterPolicyReport(c, event)
}
if err == nil {
go c.CountMetric(Outputs, 1, []string{"output:policyreport", "status:ok"})
c.Stats.PolicyReport.Add(OK, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "policyreport", "status": OK}).Inc()
} else {
go c.CountMetric(Outputs, 1, []string{"output:policyreport", "status:error"})
c.Stats.PolicyReport.Add(Error, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "policyreport", "status": Error}).Inc()
}
}
// newResult creates a new entry for Reports
func newResult(falcopayload types.FalcoPayload) (_ *wgpolicy.PolicyReportResult, namespace string) {
var properties = make(map[string]string)
for property, value := range falcopayload.OutputFields {
if property == targetNS || property == "k8s.ns.name" {
namespace = toString(value) // not empty for policy reports
}
properties[property] = toString(value)
}
return &wgpolicy.PolicyReportResult{
Policy: falcopayload.Rule,
Category: "SI - System and Information Integrity",
Source: policyReportSource,
Scored: false,
Timestamp: metav1.Timestamp{Seconds: int64(falcopayload.Time.Second()), Nanos: int32(falcopayload.Time.Nanosecond())},
Severity: mapSeverity(falcopayload),
Result: mapResult(falcopayload),
Description: falcopayload.Output,
Properties: properties,
Subjects: mapResource(falcopayload, namespace),
}, namespace
}
// check for low priority events to delete first
func checklow(result []wgpolicy.PolicyReportResult) (swapint int) {
for i, j := range result {
if j.Severity == medium || j.Severity == low || j.Severity == info {
return i
}
}
return -1
}
// update summary for clusterpolicyreport 'report'
func updateClusterPolicyReportSummary(event *wgpolicy.PolicyReportResult) {
if event.Result == fail {
clusterPolicyReport.Summary.Fail++
} else {
clusterPolicyReport.Summary.Warn++
}
}
// update summary for specific policyreport in 'policyReports' at index 'n'
func updatePolicyReportSummary(rep *wgpolicy.PolicyReport, event *wgpolicy.PolicyReportResult) {
if event.Result == fail {
rep.Summary.Fail++
} else {
rep.Summary.Warn++
}
}
func updatePolicyReports(c *Client, namespace string, event *wgpolicy.PolicyReportResult) error {
//policyReport to be created
if policyReports[namespace] == nil {
policyReports[namespace] = &wgpolicy.PolicyReport{
ObjectMeta: metav1.ObjectMeta{
Name: policyReportBaseName + uuid.NewString()[:8],
Labels: map[string]string{
"app.kubernetes.io/created-by": "falcosidekick",
},
},
Summary: wgpolicy.PolicyReportSummary{
Fail: 0,
Warn: 0,
},
}
if falcosidekickNamespace != "" && falcosidekickNamespaceUID != "" {
policyReports[namespace].ObjectMeta.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: "v1",
Kind: "Namespace",
Name: falcosidekickNamespace,
UID: falcosidekickNamespaceUID,
Controller: new(bool),
},
}
}
}
policyr := c.Crdclient.Wgpolicyk8sV1alpha2().PolicyReports(namespace)
updatePolicyReportSummary(policyReports[namespace], event)
if len(policyReports[namespace].Results) == c.Config.PolicyReport.MaxEvents {
if c.Config.PolicyReport.PruneByPriority {
pruningLogicForPolicyReports(namespace)
} else {
summaryDeletion(&policyReports[namespace].Summary, policyReports[namespace].Results[0].Result)
policyReports[namespace].Results = policyReports[namespace].Results[1:]
}
}
policyReports[namespace].Results = append(policyReports[namespace].Results, *event)
_, getErr := policyr.Get(context.Background(), policyReports[namespace].Name, metav1.GetOptions{})
if errors.IsNotFound(getErr) {
result, err := policyr.Create(context.TODO(), policyReports[namespace], metav1.CreateOptions{})
if err != nil {
log.Printf("[ERROR] : PolicyReport - Can't create Policy Report %v in namespace %v\n", err, namespace)
return err
}
log.Printf("[INFO] : PolicyReport - Create policy report %v in namespace %v\n", result.GetObjectMeta().GetName(), namespace)
} else {
// Update existing Policy Report
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
result, err := policyr.Get(context.Background(), policyReports[namespace].GetName(), metav1.GetOptions{})
if errors.IsNotFound(err) {
// This doesnt ever happen even if it is already deleted or not found
log.Printf("[ERROR] : PolicyReport - Policy Report %v not found in namespace %v\n", policyReports[namespace].GetName(), namespace)
return err
}
if err != nil {
log.Printf("[ERROR] : PolicyReport - Policy Report %v in namespace %v: %v\n", policyReports[namespace].GetName(), namespace, err)
return err
}
policyReports[namespace].SetResourceVersion(result.GetResourceVersion())
_, updateErr := policyr.Update(context.Background(), policyReports[namespace], metav1.UpdateOptions{})
return updateErr
})
if retryErr != nil {
log.Printf("[ERROR] : PolicyReport - Update has failed for Policy Report %v in namespace %v: %v\n", policyReports[namespace].GetName(), namespace, retryErr)
return retryErr
}
log.Printf("[INFO] : PolicyReport - Policy Report %v in namespace %v has been updated\n", policyReports[namespace].GetName(), namespace)
}
return nil
}
func updateClusterPolicyReport(c *Client, event *wgpolicy.PolicyReportResult) error {
updateClusterPolicyReportSummary(event)
//clusterpolicyreport to be created
clusterpr := c.Crdclient.Wgpolicyk8sV1alpha2().ClusterPolicyReports()
if len(clusterPolicyReport.Results) == c.Config.PolicyReport.MaxEvents {
if c.Config.PolicyReport.PruneByPriority {
pruningLogicForClusterPolicyReport()
} else {
summaryDeletion(&clusterPolicyReport.Summary, clusterPolicyReport.Results[0].Result)
clusterPolicyReport.Results = clusterPolicyReport.Results[1:]
}
}
clusterPolicyReport.Results = append(clusterPolicyReport.Results, *event)
_, getErr := clusterpr.Get(context.Background(), clusterPolicyReport.Name, metav1.GetOptions{})
if errors.IsNotFound(getErr) {
result, err := clusterpr.Create(context.TODO(), clusterPolicyReport, metav1.CreateOptions{})
if err != nil {
log.Printf("[ERROR] : PolicyReport - %v\n", err)
return err
}
log.Printf("[INFO] : PolicyReport - Create Cluster Policy Report %v\n", result.GetObjectMeta().GetName())
} else {
// Update existing Cluster Policy Report
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
result, err := clusterpr.Get(context.Background(), clusterPolicyReport.GetName(), metav1.GetOptions{})
if errors.IsNotFound(err) {
// This doesnt ever happen even if it is already deleted or not found
log.Printf("[ERROR] : PolicyReport - Cluster Policy Report %v not found\n", clusterPolicyReport.GetName())
return err
}
if err != nil {
log.Printf("[ERROR] : PolicyReport - Cluster Policy Report %v: %v\n", clusterPolicyReport.GetName(), err)
return err
}
clusterPolicyReport.SetResourceVersion(result.GetResourceVersion())
_, updateErr := clusterpr.Update(context.Background(), clusterPolicyReport, metav1.UpdateOptions{})
return updateErr
})
if retryErr != nil {
log.Printf("[ERROR] : PolicyReport - Update has failed for Cluster Policy Report %v: %v\n", clusterPolicyReport.GetName(), retryErr)
return retryErr
}
log.Printf("[INFO] : PolicyReport - Cluster Policy Report %v has been updated\n", clusterPolicyReport.GetName())
}
return nil
}
func pruningLogicForPolicyReports(namespace string) {
policyReport, ok := policyReports[namespace]
if !ok {
return
}
result := policyReport.Results[0]
//To do for pruning for pruning one of policyreports
checklowvalue := checklow(policyReports[namespace].Results)
if checklowvalue > 0 {
result = policyReport.Results[checklowvalue]
policyReport.Results[checklowvalue] = policyReports[namespace].Results[0]
}
if checklowvalue == -1 {
summaryDeletion(&policyReports[namespace].Summary, result.Result)
} else {
summaryDeletion(&policyReports[namespace].Summary, result.Result)
}
policyReports[namespace].Results = policyReports[namespace].Results[1:]
}
func pruningLogicForClusterPolicyReport() {
result := clusterPolicyReport.Results[0]
//To do for pruning cluster report
checklowvalue := checklow(clusterPolicyReport.Results)
if checklowvalue > 0 {
result = clusterPolicyReport.Results[checklowvalue]
clusterPolicyReport.Results[checklowvalue] = clusterPolicyReport.Results[0]
}
if checklowvalue == -1 {
summaryDeletion(&clusterPolicyReport.Summary, result.Result)
} else {
summaryDeletion(&clusterPolicyReport.Summary, result.Result)
}
clusterPolicyReport.Results = clusterPolicyReport.Results[1:]
}
func summaryDeletion(summary *wgpolicy.PolicyReportSummary, result wgpolicy.PolicyResult) {
switch result {
case fail:
summary.Fail--
case warn:
summary.Warn--
case skip:
summary.Skip--
}
}
func mapResult(event types.FalcoPayload) wgpolicy.PolicyResult {
if event.Priority <= types.Notice {
return skip
} else if event.Priority == types.Warning {
return warn
} else {
return fail
}
}
func mapSeverity(event types.FalcoPayload) wgpolicy.PolicyResultSeverity {
if event.Priority <= types.Informational {
return info
} else if event.Priority <= types.Notice {
return low
} else if event.Priority <= types.Warning {
return medium
} else if event.Priority <= types.Error {
return high
} else {
return critical
}
}
func mapResource(event types.FalcoPayload, ns string) []corev1.ObjectReference {
name := determineResourceName(event.OutputFields)
if name != "" {
return nil
}
targetResource, ok := event.OutputFields[targetResource]
if !ok {
return []corev1.ObjectReference{
{
Namespace: ns,
Name: toString(name),
},
}
}
resource, ok := resourceMapping[toString(targetResource)]
if !ok {
resource.kind = toString(targetResource)
}
return []corev1.ObjectReference{
{
Namespace: ns,
Name: toString(name),
Kind: resource.kind,
APIVersion: resource.apiVersion,
},
}
}
func determineResourceName(outputFields map[string]interface{}) string {
name, ok := outputFields[targetName]
if ok {
return toString(name)
}
return toString(outputFields[responseName])
}
func toString(value interface{}) string {
return fmt.Sprintf("%v", value)
}