-
Notifications
You must be signed in to change notification settings - Fork 326
/
tracingpolicy.go
207 lines (178 loc) · 5.57 KB
/
tracingpolicy.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package tracingpolicy
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/cmd/tetra/common"
"github.com/cilium/tetragon/cmd/tetra/tracingpolicy/generate"
"github.com/spf13/cobra"
)
func New() *cobra.Command {
tpCmd := &cobra.Command{
Use: "tracingpolicy",
Aliases: []string{"tp"},
Short: "Manage tracing policies",
}
tpAddCmd := &cobra.Command{
Use: "add <yaml_file>",
Short: "add a new sensor based on a tracing policy",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c := common.NewConnectedClient()
defer c.Close()
yamlb, err := os.ReadFile(args[0])
if err != nil {
return fmt.Errorf("failed to read yaml file %s: %w", args[0], err)
}
_, err = c.Client.AddTracingPolicy(c.Ctx, &tetragon.AddTracingPolicyRequest{
Yaml: string(yamlb),
})
if err != nil {
return fmt.Errorf("failed to add tracing policy: %w", err)
}
cmd.Printf("tracing policy %q added\n", args[0])
return nil
},
}
tpDelCmd := &cobra.Command{
Use: "delete <name>",
Short: "delete a tracing policy",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c := common.NewConnectedClient()
defer c.Close()
_, err := c.Client.DeleteTracingPolicy(c.Ctx, &tetragon.DeleteTracingPolicyRequest{
Name: args[0],
})
if err != nil {
return fmt.Errorf("failed to delete tracing policy: %w", err)
}
cmd.Printf("tracing policy %q deleted\n", args[0])
return nil
},
}
tpEnableCmd := &cobra.Command{
Use: "enable <name>",
Short: "enable a tracing policy",
Long: "Enable a disabled tracing policy. Use disable to re-disable the tracing policy.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c := common.NewConnectedClient()
defer c.Close()
_, err := c.Client.EnableTracingPolicy(c.Ctx, &tetragon.EnableTracingPolicyRequest{
Name: args[0],
})
if err != nil {
return fmt.Errorf("failed to enable tracing policy: %w", err)
}
cmd.Printf("tracing policy %q enabled\n", args[0])
return nil
},
}
tpDisableCmd := &cobra.Command{
Use: "disable <name>",
Short: "disable a tracing policy",
Long: "Disable an enabled tracing policy. Use enable to re-enable the tracing policy.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c := common.NewConnectedClient()
defer c.Close()
_, err := c.Client.DisableTracingPolicy(c.Ctx, &tetragon.DisableTracingPolicyRequest{
Name: args[0],
})
if err != nil {
return fmt.Errorf("failed to disable tracing policy: %w", err)
}
cmd.Printf("tracing policy %q disabled\n", args[0])
return nil
},
}
var tpListOutputFlag string
tpListCmd := &cobra.Command{
Use: "list",
Short: "list loaded tracing policies",
Long: "List loaded tracing policies, use the JSON output format for full output.",
Args: cobra.ExactArgs(0),
PreRunE: func(_ *cobra.Command, _ []string) error {
if tpListOutputFlag != "json" && tpListOutputFlag != "text" {
return fmt.Errorf("invalid value for %q flag: %s", common.KeyOutput, tpListOutputFlag)
}
return nil
},
RunE: func(cmd *cobra.Command, _ []string) error {
c := common.NewConnectedClient()
defer c.Close()
res, err := c.Client.ListTracingPolicies(c.Ctx, &tetragon.ListTracingPoliciesRequest{})
if err != nil || res == nil {
return fmt.Errorf("failed to list tracing policies: %w", err)
}
switch tpListOutputFlag {
case "json":
b, err := res.MarshalJSON()
if err != nil {
return fmt.Errorf("failed to generate json: %w", err)
}
cmd.Println(string(b))
case "text":
// tabwriter config imitates kubectl default output, i.e. 3 spaces padding
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "ID\tNAME\tSTATE\tFILTERID\tNAMESPACE\tSENSORS")
for _, pol := range res.Policies {
namespace := pol.Namespace
if namespace == "" {
namespace = "(global)"
}
sensors := strings.Join(pol.Sensors, ",")
// From v0.11 and before, enabled, filterID and error were
// bundled in a string. To have a retro-compatible tetra
// command, we scan the string. If the scan fails, it means
// something else might be in Info and we print it.
//
// we can drop the following block (and comment) when we
// feel tetra should support only version after v0.11
if pol.Info != "" {
var parsedEnabled bool
var parsedFilterID uint64
var parsedError string
var parsedName string
str := strings.NewReader(pol.Info)
_, err := fmt.Fscanf(str, "%253s enabled:%t filterID:%d error:%512s", &parsedName, &parsedEnabled, &parsedFilterID, &parsedError)
if err == nil {
if parsedEnabled {
pol.State = tetragon.TracingPolicyState_TP_STATE_ENABLED
}
pol.FilterId = parsedFilterID
pol.Error = parsedError
pol.Info = ""
}
}
fmt.Fprintf(w, "%d\t%s\t%s\t%d\t%s\t%s\t\n",
pol.Id,
pol.Name,
strings.TrimPrefix(strings.ToLower(pol.State.String()), "tp_state_"),
pol.FilterId,
namespace,
sensors,
)
}
w.Flush()
}
return nil
},
}
tpListFlags := tpListCmd.Flags()
tpListFlags.StringVarP(&tpListOutputFlag, common.KeyOutput, "o", "text", "Output format. text or json")
tpCmd.AddCommand(
tpAddCmd,
tpDelCmd,
tpEnableCmd,
tpDisableCmd,
tpListCmd,
generate.New(),
)
return tpCmd
}