-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
policy.go
80 lines (66 loc) · 1.7 KB
/
policy.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Package kfilters holds kfilters related files
package kfilters
import (
"encoding/json"
"errors"
)
// PolicyMode represents the policy mode (accept or deny)
type PolicyMode uint8
// PolicyFlag is a bitmask of the active filtering policies
type PolicyFlag uint8
// Policy modes
const (
PolicyModeNoFilter PolicyMode = iota
PolicyModeAccept
PolicyModeDeny
)
// Policy flags
const (
PolicyFlagBasename PolicyFlag = 1
PolicyFlagFlags PolicyFlag = 2
PolicyFlagMode PolicyFlag = 4
// need to be aligned with the kernel size
BasenameFilterSize = 256
)
func (m PolicyMode) String() string {
switch m {
case PolicyModeAccept:
return "accept"
case PolicyModeDeny:
return "deny"
case PolicyModeNoFilter:
return "no filter"
}
return ""
}
// MarshalJSON returns the JSON encoding of the policy mode
func (m PolicyMode) MarshalJSON() ([]byte, error) {
s := m.String()
if s == "" {
return nil, errors.New("invalid policy mode")
}
return json.Marshal(s)
}
// MarshalJSON returns the JSON encoding of the policy flags
func (f PolicyFlag) MarshalJSON() ([]byte, error) {
flags := f.StringArray()
return json.Marshal(flags)
}
// StringArray returns the policy flags as a string array
func (f PolicyFlag) StringArray() []string {
var flags []string
if f&PolicyFlagBasename != 0 {
flags = append(flags, "basename")
}
if f&PolicyFlagFlags != 0 {
flags = append(flags, "flags")
}
if f&PolicyFlagMode != 0 {
flags = append(flags, "mode")
}
return flags
}