/
audit.go
167 lines (141 loc) · 5.68 KB
/
audit.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
package cmd
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/go-openapi/strfmt"
"github.com/metal-stack/metal-go/api/client/audit"
"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-lib/pkg/genericcli"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metalctl/cmd/sorters"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type auditCmd struct {
*config
}
func newAuditCmd(c *config) *cobra.Command {
w := auditCmd{
config: c,
}
cmdsConfig := &genericcli.CmdsConfig[any, any, *models.V1AuditResponse]{
BinaryName: binaryName,
GenericCLI: genericcli.NewGenericCLI[any, any, *models.V1AuditResponse](w).WithFS(c.fs),
Singular: "audit trace",
Plural: "audit traces",
Description: "show audit traces of the api. feature must be enabled on server-side.",
Sorter: sorters.AuditSorter(),
DescribePrinter: func() printers.Printer { return c.describePrinter },
ListPrinter: func() printers.Printer { return c.listPrinter },
ListCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("query", "q", "", "filters audit trace body payloads for the given text.")
cmd.Flags().String("from", "1h", "start of range of the audit traces. e.g. 1h, 10m, 2006-01-02 15:04:05")
cmd.Flags().String("to", "", "end of range of the audit traces. e.g. 1h, 10m, 2006-01-02 15:04:05")
cmd.Flags().String("component", "", "component of the audit trace.")
cmd.Flags().String("request-id", "", "request id of the audit trace.")
cmd.Flags().String("type", "", "type of the audit trace. One of [http, grpc, event].")
cmd.Flags().String("user", "", "user of the audit trace.")
cmd.Flags().String("tenant", "", "tenant of the audit trace.")
cmd.Flags().String("detail", "", "detail of the audit trace. An HTTP method, unary or stream")
cmd.Flags().String("phase", "", "phase of the audit trace. One of [request, response, single, error, opened, closed]")
cmd.Flags().String("path", "", "api path of the audit trace.")
cmd.Flags().String("forwarded-for", "", "forwarded for of the audit trace.")
cmd.Flags().String("remote-addr", "", "remote address of the audit trace.")
cmd.Flags().String("error", "", "error of the audit trace.")
cmd.Flags().Int32("status-code", 0, "HTTP status code of the audit trace.")
cmd.Flags().Int64("limit", 100, "limit the number of audit traces.")
must(cmd.RegisterFlagCompletionFunc("type", c.comp.AuditTypeCompletion))
must(cmd.RegisterFlagCompletionFunc("phase", c.comp.AuditPhaseCompletion))
},
DescribeCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().Bool("prettify-body", false, "attempts to interpret the body as json and prettifies it")
cmd.Flags().String("phase", "response", "phase of the audit trace. One of [request, response, single, error, opened, closed]")
must(cmd.RegisterFlagCompletionFunc("phase", c.comp.AuditPhaseCompletion))
},
OnlyCmds: genericcli.OnlyCmds(
genericcli.ListCmd,
genericcli.DescribeCmd,
),
}
return genericcli.NewCmds(cmdsConfig)
}
func (c auditCmd) Get(id string) (*models.V1AuditResponse, error) {
traces, err := c.client.Audit().FindAuditTraces(audit.NewFindAuditTracesParams().WithBody(&models.V1AuditFindRequest{
Rqid: id,
Phase: viper.GetString("phase"),
}), nil)
if err != nil {
return nil, err
}
if len(traces.Payload) == 0 {
return nil, fmt.Errorf("no audit trace found with request id %s", id)
}
trace := traces.Payload[0]
if viper.GetBool("prettify-body") {
trimmed := strings.Trim(trace.Body, `"`)
body := map[string]any{}
err = json.Unmarshal([]byte(trimmed), &body)
if err == nil {
if pretty, err := json.MarshalIndent(body, "", " "); err == nil {
trace.Body = string(pretty)
}
}
}
return trace, nil
}
func (c auditCmd) List() ([]*models.V1AuditResponse, error) {
fromDateTime, err := eventuallyRelativeDateTime(viper.GetString("from"))
if err != nil {
return nil, err
}
toDateTime, err := eventuallyRelativeDateTime(viper.GetString("to"))
if err != nil {
return nil, err
}
resp, err := c.client.Audit().FindAuditTraces(audit.NewFindAuditTracesParams().WithBody(&models.V1AuditFindRequest{
Body: viper.GetString("query"),
From: fromDateTime,
To: toDateTime,
Component: viper.GetString("component"),
Rqid: viper.GetString("request-id"),
Type: viper.GetString("type"),
User: viper.GetString("user"),
Tenant: viper.GetString("tenant"),
Detail: viper.GetString("detail"),
Phase: viper.GetString("phase"),
Path: viper.GetString("path"),
ForwardedFor: viper.GetString("forwarded-for"),
RemoteAddr: viper.GetString("remote-addr"),
Error: viper.GetString("error"),
StatusCode: viper.GetInt32("status-code"),
Limit: viper.GetInt64("limit"),
}), nil)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
func (c auditCmd) Delete(id string) (*models.V1AuditResponse, error) {
return nil, fmt.Errorf("not implemented for audit traces")
}
func (c auditCmd) Create(_ any) (*models.V1AuditResponse, error) {
return nil, fmt.Errorf("not implemented for audit traces")
}
func (c auditCmd) Update(_ any) (*models.V1AuditResponse, error) {
return nil, fmt.Errorf("not implemented for audit traces")
}
func (c auditCmd) Convert(r *models.V1AuditResponse) (string, any, any, error) {
return "", nil, nil, fmt.Errorf("not implemented for audit traces")
}
func eventuallyRelativeDateTime(s string) (strfmt.DateTime, error) {
if s == "" {
return strfmt.DateTime{}, nil
}
duration, err := strfmt.ParseDuration(s)
if err == nil {
return strfmt.DateTime(time.Now().Add(-duration)), nil
}
return strfmt.ParseDateTime(s)
}