-
Notifications
You must be signed in to change notification settings - Fork 322
/
sensors.go
120 lines (106 loc) · 3.21 KB
/
sensors.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package sensors
import (
"context"
"fmt"
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/cmd/tetra/common"
"github.com/spf13/cobra"
)
// Let's deprecated and remove this sensor interface and use the tracing policy
// gRPCs. Those are accessible trough the tracingpolicy command.
func New() *cobra.Command {
sensorsCmd := &cobra.Command{
Use: "sensors",
Short: "Manage sensors",
Deprecated: "please use the tracingpolicy command instead.",
}
sensorsListCmd := &cobra.Command{
Use: "list",
Short: "List available sensors",
Run: func(_ *cobra.Command, _ []string) {
common.CliRun(listSensors)
},
}
sensorsCmd.AddCommand(sensorsListCmd)
sensorEnableCmd := &cobra.Command{
Use: "enable <sensor>",
Short: "Enable sensor",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
sensor := args[0]
common.CliRun(func(ctx context.Context, cli tetragon.FineGuidanceSensorsClient) {
enableSensor(ctx, cli, sensor)
})
},
}
sensorsCmd.AddCommand(sensorEnableCmd)
sensorDisableCmd := &cobra.Command{
Use: "disable <sensor>",
Short: "Disable sensor",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
sensor := args[0]
common.CliRun(func(ctx context.Context, cli tetragon.FineGuidanceSensorsClient) {
disableSensor(ctx, cli, sensor)
})
},
}
sensorsCmd.AddCommand(sensorDisableCmd)
sensorRmCmd := &cobra.Command{
Use: "rm <sensor_name>",
Short: "remove a sensor",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
common.CliRun(func(ctx context.Context, cli tetragon.FineGuidanceSensorsClient) {
removeSensor(ctx, cli, args[0])
})
},
}
sensorsCmd.AddCommand(sensorRmCmd)
return sensorsCmd
}
func listSensors(ctx context.Context, client tetragon.FineGuidanceSensorsClient) {
sensors, err := client.ListSensors(ctx, &tetragon.ListSensorsRequest{})
if err != nil {
fmt.Printf("error: %s\n", err)
return
} else if sensors == nil {
fmt.Printf("error: sensors is nil\n")
return
}
for _, sensor := range sensors.Sensors {
enabled := ""
if sensor.Enabled {
enabled = "(enabled)"
} else {
enabled = "(not enabled)"
}
fmt.Printf("%s %s %s\n", sensor.Name, enabled, sensor.Collection)
}
}
func removeSensor(ctx context.Context, client tetragon.FineGuidanceSensorsClient, sensor string) {
_, err := client.RemoveSensor(ctx, &tetragon.RemoveSensorRequest{
Name: sensor,
})
if err != nil {
fmt.Printf("failed to remove tracing policy: %s\n", err)
}
}
func enableSensor(ctx context.Context, client tetragon.FineGuidanceSensorsClient, sensor string) {
_, err := client.EnableSensor(ctx, &tetragon.EnableSensorRequest{Name: sensor})
if err == nil {
fmt.Printf("sensor %s enabled\n", sensor)
} else {
fmt.Printf("failed to enable sensor %s: %s\n", sensor, err)
}
}
func disableSensor(ctx context.Context, client tetragon.FineGuidanceSensorsClient, sensor string) {
_, err := client.DisableSensor(ctx, &tetragon.DisableSensorRequest{Name: sensor})
if err == nil {
fmt.Printf("sensor %s disabled\n", sensor)
} else {
fmt.Printf("failed to disable sensor %s: %s\n", sensor, err)
}
}