/
filters.go
150 lines (127 loc) · 4.09 KB
/
filters.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
// Copyright 2019-2020 Authors of Hubble
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package filters
import (
"context"
flowpb "github.com/cilium/cilium/api/v1/flow"
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
)
// FilterFunc is the function will be used to filter the given data.
// Should return true if the filter is hit, false otherwise.
type FilterFunc func(ev *v1.Event) bool
// FilterFuncs is a combination of multiple filters, typically applied together.
type FilterFuncs []FilterFunc
// Apply filters the flow with the given white- and blacklist. Returns true
// if the flow should be included in the result.
func Apply(whitelist, blacklist FilterFuncs, ev *v1.Event) bool {
return whitelist.MatchOne(ev) && blacklist.MatchNone(ev)
}
// MatchAll returns true if all the filters match the provided data, i.e. AND.
func (fs FilterFuncs) MatchAll(ev *v1.Event) bool {
for _, f := range fs {
if !f(ev) {
return false
}
}
return true
}
// MatchOne returns true if at least one of the filters match the provided data or
// if no filters are specified, i.e. OR.
func (fs FilterFuncs) MatchOne(ev *v1.Event) bool {
if len(fs) == 0 {
return true
}
for _, f := range fs {
if f(ev) {
return true
}
}
return false
}
// MatchNone returns true if none of the filters match the provided data or
// if no filters are specified, i.e. NOR
func (fs FilterFuncs) MatchNone(ev *v1.Event) bool {
if len(fs) == 0 {
return true
}
for _, f := range fs {
if f(ev) {
return false
}
}
return true
}
// OnBuildFilter is invoked while building a flow filter
type OnBuildFilter interface {
OnBuildFilter(context.Context, *flowpb.FlowFilter) ([]FilterFunc, error)
}
// OnBuildFilterFunc implements OnBuildFilter for a single function
type OnBuildFilterFunc func(context.Context, *flowpb.FlowFilter) ([]FilterFunc, error)
// OnBuildFilter is invoked while building a flow filter
func (f OnBuildFilterFunc) OnBuildFilter(ctx context.Context, flow *flowpb.FlowFilter) ([]FilterFunc, error) {
return f(ctx, flow)
}
// BuildFilter builds a filter based on a FlowFilter. It returns:
// - the FilterFunc to be used to filter packets based on the requested
// FlowFilter;
// - an error in case something went wrong.
func BuildFilter(ctx context.Context, ff *flowpb.FlowFilter, auxFilters []OnBuildFilter) (FilterFuncs, error) {
var fs []FilterFunc
for _, f := range auxFilters {
fl, err := f.OnBuildFilter(ctx, ff)
if err != nil {
return nil, err
}
if fl != nil {
fs = append(fs, fl...)
}
}
return fs, nil
}
// BuildFilterList constructs a list of filter functions representing the list
// of FlowFilter. It returns:
// - the FilterFunc to be used to filter packets based on the requested
// FlowFilter;
// - an error in case something went wrong.
func BuildFilterList(ctx context.Context, ff []*flowpb.FlowFilter, auxFilters []OnBuildFilter) (FilterFuncs, error) {
filterList := make([]FilterFunc, 0, len(ff))
for _, flowFilter := range ff {
// Build filter matching on all requirements of the FlowFilter
tf, err := BuildFilter(ctx, flowFilter, auxFilters)
if err != nil {
return nil, err
}
// All filters representing a FlowFilter must match
filterFunc := func(ev *v1.Event) bool {
return tf.MatchAll(ev)
}
filterList = append(filterList, filterFunc)
}
return filterList, nil
}
// DefaultFilters is the list of default filters
var DefaultFilters = []OnBuildFilter{
&EventTypeFilter{},
&VerdictFilter{},
&ReplyFilter{},
&IdentityFilter{},
&ProtocolFilter{},
&IPFilter{},
&PodFilter{},
&ServiceFilter{},
&FQDNFilter{},
&LabelsFilter{},
&PortFilter{},
&HTTPFilter{},
}