-
Notifications
You must be signed in to change notification settings - Fork 525
Expand file tree
/
Copy pathtracingpolicy.go
More file actions
187 lines (162 loc) · 5.28 KB
/
tracingpolicy.go
File metadata and controls
187 lines (162 loc) · 5.28 KB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package tracingpolicy
import (
"fmt"
"os"
"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, err := common.NewClientWithDefaultContextAndAddress()
if err != nil {
return fmt.Errorf("failed create gRPC client: %w", err)
}
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
},
}
var tpDelNamespaceFlag string
tpDelCmd := &cobra.Command{
Use: "delete <name>",
Short: "delete a tracing policy",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := common.NewClientWithDefaultContextAndAddress()
if err != nil {
return fmt.Errorf("failed create gRPC client: %w", err)
}
defer c.Close()
_, err = c.Client.DeleteTracingPolicy(c.Ctx, &tetragon.DeleteTracingPolicyRequest{
Name: args[0],
Namespace: tpDelNamespaceFlag,
})
if err != nil {
return fmt.Errorf("failed to delete tracing policy: %w", err)
}
cmd.Printf("tracing policy %q deleted\n", args[0])
return nil
},
}
var tpEnableNamespaceFlag string
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, err := common.NewClientWithDefaultContextAndAddress()
if err != nil {
return fmt.Errorf("failed create gRPC client: %w", err)
}
defer c.Close()
_, err = c.Client.EnableTracingPolicy(c.Ctx, &tetragon.EnableTracingPolicyRequest{
Name: args[0],
Namespace: tpEnableNamespaceFlag,
})
if err != nil {
return fmt.Errorf("failed to enable tracing policy: %w", err)
}
cmd.Printf("tracing policy %q enabled\n", args[0])
return nil
},
}
var tpDisableNamespaceFlag string
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, err := common.NewClientWithDefaultContextAndAddress()
if err != nil {
return fmt.Errorf("failed create gRPC client: %w", err)
}
defer c.Close()
_, err = c.Client.DisableTracingPolicy(c.Ctx, &tetragon.DisableTracingPolicyRequest{
Name: args[0],
Namespace: tpDisableNamespaceFlag,
})
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, err := common.NewClientWithDefaultContextAndAddress()
if err != nil {
return fmt.Errorf("failed create gRPC client: %w", err)
}
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":
common.PrintTracingPolicies(cmd.OutOrStdout(), res.Policies, nil)
}
return nil
},
}
tpDelFlags := tpDelCmd.Flags()
tpDelFlags.StringVarP(&tpDelNamespaceFlag, common.KeyNamespace, "n", "", "Namespace of the tracing policy.")
tpEnableFlags := tpEnableCmd.Flags()
tpEnableFlags.StringVarP(&tpEnableNamespaceFlag, common.KeyNamespace, "n", "", "Namespace of the tracing policy.")
tpDisableFlags := tpDisableCmd.Flags()
tpDisableFlags.StringVarP(&tpDisableNamespaceFlag, common.KeyNamespace, "n", "", "Namespace of the tracing policy.")
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
}