Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invert --hubble-monitor-events logic to be an allowlist #25167

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 15 additions & 13 deletions pkg/hubble/monitor/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,38 +63,40 @@ func NewMonitorFilter(logger logrus.FieldLogger, monitorEventFilters []string) (
return &monitorFilter, nil
}

// OnMonitorEvent implements observeroption.OnMonitorEvent interface
// It returns true if an event is to be dropped, false otherwise.
func (m *monitorFilter) OnMonitorEvent(ctx context.Context, event *observerTypes.MonitorEvent) (bool, error) {
switch payload := event.Payload.(type) {
case *observerTypes.PerfEvent:
if len(payload.Data) == 0 {
return false, errors.ErrEmptyData
return true, errors.ErrEmptyData
}

switch payload.Data[0] {
case monitorAPI.MessageTypeDrop:
return m.drop, nil
return !m.drop, nil
case monitorAPI.MessageTypeDebug:
return m.debug, nil
return !m.debug, nil
case monitorAPI.MessageTypeCapture:
return m.capture, nil
return !m.capture, nil
case monitorAPI.MessageTypeTrace:
return m.trace, nil
return !m.trace, nil
case monitorAPI.MessageTypeAccessLog: // MessageTypeAccessLog maps to MessageTypeNameL7
return m.l7, nil
return !m.l7, nil
case monitorAPI.MessageTypePolicyVerdict:
return m.policyVerdict, nil
return !m.policyVerdict, nil
case monitorAPI.MessageTypeRecCapture:
return m.recCapture, nil
return !m.recCapture, nil
case monitorAPI.MessageTypeTraceSock:
return m.traceSock, nil
return !m.traceSock, nil
default:
return false, errors.ErrUnknownEventType
return true, errors.ErrUnknownEventType
}
case *observerTypes.AgentEvent:
return m.agent, nil
return !m.agent, nil
case nil:
return false, errors.ErrEmptyData
return true, errors.ErrEmptyData
default:
return false, errors.ErrUnknownEventType
return true, errors.ErrUnknownEventType
}
}
70 changes: 35 additions & 35 deletions pkg/hubble/monitor/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestNewMonitorFilter(t *testing.T) {

type testEvent struct {
event *observerTypes.MonitorEvent
allowed bool
stop bool
expectedErr error
}

Expand All @@ -102,7 +102,7 @@ func Test_OnMonitorEvent(t *testing.T) {
event: &observerTypes.MonitorEvent{
Payload: nil,
},
allowed: false,
stop: true,
expectedErr: errors.ErrEmptyData,
},
},
Expand All @@ -115,7 +115,7 @@ func Test_OnMonitorEvent(t *testing.T) {
event: &observerTypes.MonitorEvent{
Payload: &struct{}{},
},
allowed: false,
stop: true,
expectedErr: errors.ErrUnknownEventType,
},
},
Expand All @@ -130,7 +130,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{0xff},
},
},
allowed: false,
stop: true,
expectedErr: errors.ErrUnknownEventType,
},
},
Expand All @@ -145,7 +145,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Type: monitorAPI.MessageTypeAgent,
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
},
Expand All @@ -160,7 +160,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeAccessLog},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
},
Expand All @@ -175,7 +175,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{},
},
},
allowed: false,
stop: true,
expectedErr: errors.ErrEmptyData,
},
},
Expand All @@ -190,7 +190,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeDrop},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
},
Expand All @@ -205,7 +205,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeDebug},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
},
Expand All @@ -220,7 +220,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeCapture},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
},
Expand All @@ -235,7 +235,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeTrace},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
},
Expand All @@ -250,7 +250,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypePolicyVerdict},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
},
Expand All @@ -265,7 +265,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeRecCapture},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
},
Expand All @@ -280,7 +280,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeTraceSock},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
},
Expand All @@ -295,7 +295,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeTrace},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
{
Expand All @@ -304,7 +304,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeDebug},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
{
Expand All @@ -313,7 +313,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeCapture},
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
},
Expand All @@ -328,7 +328,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeDebug},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
{
Expand All @@ -337,7 +337,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypePolicyVerdict},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
{
Expand All @@ -346,7 +346,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeTrace},
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
{
Expand All @@ -355,7 +355,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeDrop},
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
},
Expand All @@ -370,7 +370,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeCapture},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
{
Expand All @@ -379,7 +379,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeTrace},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
{
Expand All @@ -388,7 +388,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeDrop},
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
{
Expand All @@ -397,7 +397,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeDebug},
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
},
Expand All @@ -412,7 +412,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeDrop},
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
{
Expand All @@ -421,7 +421,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeDebug},
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
{
Expand All @@ -430,7 +430,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeCapture},
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
{
Expand All @@ -439,7 +439,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeTrace},
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
{
Expand All @@ -448,7 +448,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypePolicyVerdict},
},
},
allowed: true,
stop: false,
expectedErr: nil,
},
{
Expand All @@ -457,7 +457,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Data: []byte{monitorAPI.MessageTypeTraceSock},
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
{
Expand All @@ -466,7 +466,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Type: monitorAPI.MessageTypeAccessLog,
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
{
Expand All @@ -475,7 +475,7 @@ func Test_OnMonitorEvent(t *testing.T) {
Type: monitorAPI.MessageTypeAgent,
},
},
allowed: false,
stop: true,
expectedErr: nil,
},
},
Expand All @@ -488,9 +488,9 @@ func Test_OnMonitorEvent(t *testing.T) {
assert.NoError(t, err)

for _, event := range tc.events {
allowed, err := mf.OnMonitorEvent(context.Background(), event.event)
stop, err := mf.OnMonitorEvent(context.Background(), event.event)
assert.Equal(t, event.expectedErr, err)
assert.Equal(t, event.allowed, allowed)
assert.Equal(t, event.stop, stop)
}
})
}
Expand Down