|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + apiv2 "github.com/metal-stack/api/go/metalstack/api/v2" |
| 7 | + "github.com/metal-stack/cli/cmd/config" |
| 8 | + "github.com/metal-stack/cli/cmd/sorters" |
| 9 | + helpersaudit "github.com/metal-stack/cli/pkg/helpers/audit" |
| 10 | + "github.com/metal-stack/metal-lib/pkg/genericcli" |
| 11 | + "github.com/metal-stack/metal-lib/pkg/genericcli/printers" |
| 12 | + "github.com/metal-stack/metal-lib/pkg/pointer" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/spf13/viper" |
| 15 | +) |
| 16 | + |
| 17 | +type audit struct { |
| 18 | + c *config.Config |
| 19 | +} |
| 20 | + |
| 21 | +func newAuditCmd(c *config.Config) *cobra.Command { |
| 22 | + w := &audit{ |
| 23 | + c: c, |
| 24 | + } |
| 25 | + |
| 26 | + cmdsConfig := &genericcli.CmdsConfig[any, any, *apiv2.AuditTrace]{ |
| 27 | + BinaryName: config.BinaryName, |
| 28 | + GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs), |
| 29 | + Singular: "audit", |
| 30 | + Plural: "audits", |
| 31 | + Description: "read api audit traces of a tenant", |
| 32 | + Sorter: sorters.AuditSorter(), |
| 33 | + OnlyCmds: genericcli.OnlyCmds(genericcli.ListCmd, genericcli.DescribeCmd), |
| 34 | + DescribePrinter: func() printers.Printer { return c.DescribePrinter }, |
| 35 | + ListPrinter: func() printers.Printer { return c.ListPrinter }, |
| 36 | + DescribeCmdMutateFn: func(cmd *cobra.Command) { |
| 37 | + cmd.Flags().String("tenant", "", "tenant of the audit trace.") |
| 38 | + |
| 39 | + cmd.Flags().String("phase", "", "the audit trace phase.") |
| 40 | + |
| 41 | + cmd.Flags().Bool("prettify-body", true, "attempts to interpret the body as json and prettifies it.") |
| 42 | + |
| 43 | + genericcli.Must(cmd.RegisterFlagCompletionFunc("phase", c.Completion.AuditPhaseListCompletion)) |
| 44 | + genericcli.Must(cmd.RegisterFlagCompletionFunc("tenant", c.Completion.TenantListCompletion)) |
| 45 | + }, |
| 46 | + ListCmdMutateFn: func(cmd *cobra.Command) { |
| 47 | + cmd.Flags().String("request-id", "", "request id of the audit trace.") |
| 48 | + |
| 49 | + cmd.Flags().String("from", "", "start of range of the audit traces. e.g. 1h, 10m, 2006-01-02 15:04:05") |
| 50 | + cmd.Flags().String("to", "", "end of range of the audit traces. e.g. 1h, 10m, 2006-01-02 15:04:05") |
| 51 | + |
| 52 | + cmd.Flags().String("user", "", "user of the audit trace.") |
| 53 | + cmd.Flags().String("tenant", "", "tenant of the audit trace.") |
| 54 | + |
| 55 | + cmd.Flags().String("project", "", "project id of the audit trace") |
| 56 | + |
| 57 | + cmd.Flags().String("phase", "", "the audit trace phase.") |
| 58 | + cmd.Flags().String("method", "", "api method of the audit trace.") |
| 59 | + cmd.Flags().Int32("result-code", 0, "gRPC result status code of the audit trace.") |
| 60 | + cmd.Flags().String("source-ip", "", "source-ip of the audit trace.") |
| 61 | + |
| 62 | + cmd.Flags().String("body", "", "filters audit trace body payloads for the given text (full-text search).") |
| 63 | + |
| 64 | + cmd.Flags().Int64("limit", 0, "limit the number of audit traces.") |
| 65 | + |
| 66 | + cmd.Flags().Bool("prettify-body", true, "attempts to interpret the body as json and prettifies it.") |
| 67 | + |
| 68 | + genericcli.Must(cmd.RegisterFlagCompletionFunc("phase", c.Completion.AuditPhaseListCompletion)) |
| 69 | + genericcli.Must(cmd.RegisterFlagCompletionFunc("project", c.Completion.ProjectListCompletion)) |
| 70 | + genericcli.Must(cmd.RegisterFlagCompletionFunc("tenant", c.Completion.TenantListCompletion)) |
| 71 | + genericcli.Must(cmd.RegisterFlagCompletionFunc("result-code", c.Completion.AuditStatusCodesCompletion)) |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + return genericcli.NewCmds(cmdsConfig) |
| 76 | +} |
| 77 | + |
| 78 | +func (c *audit) Get(id string) (*apiv2.AuditTrace, error) { |
| 79 | + ctx, cancel := c.c.NewRequestContext() |
| 80 | + defer cancel() |
| 81 | + |
| 82 | + tenant, err := c.c.GetTenant() |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + resp, err := c.c.Client.Apiv2().Audit().Get(ctx, &apiv2.AuditServiceGetRequest{ |
| 88 | + Login: tenant, |
| 89 | + Uuid: id, |
| 90 | + Phase: helpersaudit.ToPhase(viper.GetString("phase")), |
| 91 | + }, |
| 92 | + ) |
| 93 | + if err != nil { |
| 94 | + return nil, fmt.Errorf("failed to get audit trace: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + if viper.GetBool("prettify-body") { |
| 98 | + helpersaudit.TryPrettifyBody(resp.Trace) |
| 99 | + } |
| 100 | + |
| 101 | + return resp.Trace, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (c *audit) List() ([]*apiv2.AuditTrace, error) { |
| 105 | + ctx, cancel := c.c.NewRequestContext() |
| 106 | + defer cancel() |
| 107 | + |
| 108 | + fromDateTime, err := helpersaudit.RelativeDateTime(viper.GetString("from")) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + toDateTime, err := helpersaudit.RelativeDateTime(viper.GetString("to")) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + tenant, err := c.c.GetTenant() |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("tenant is required: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + var code *int32 |
| 123 | + if viper.IsSet("result-code") { |
| 124 | + code = new(viper.GetInt32("result-code")) |
| 125 | + } |
| 126 | + |
| 127 | + resp, err := c.c.Client.Apiv2().Audit().List(ctx, &apiv2.AuditServiceListRequest{ |
| 128 | + Login: tenant, |
| 129 | + Query: &apiv2.AuditQuery{ |
| 130 | + Uuid: pointer.PointerOrNil(viper.GetString("request-id")), |
| 131 | + From: fromDateTime, |
| 132 | + To: toDateTime, |
| 133 | + User: pointer.PointerOrNil(viper.GetString("user")), |
| 134 | + Project: pointer.PointerOrNil(viper.GetString("project")), |
| 135 | + Method: pointer.PointerOrNil(viper.GetString("method")), |
| 136 | + ResultCode: code, |
| 137 | + Body: pointer.PointerOrNil(viper.GetString("body")), |
| 138 | + SourceIp: pointer.PointerOrNil(viper.GetString("source-ip")), |
| 139 | + Limit: pointer.PointerOrNil(viper.GetInt32("limit")), |
| 140 | + Phase: helpersaudit.ToPhase(viper.GetString("phase")), |
| 141 | + }, |
| 142 | + }) |
| 143 | + if err != nil { |
| 144 | + return nil, fmt.Errorf("failed to list audit traces: %w", err) |
| 145 | + } |
| 146 | + |
| 147 | + if viper.GetBool("prettify-body") { |
| 148 | + for _, trace := range resp.Traces { |
| 149 | + helpersaudit.TryPrettifyBody(trace) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return resp.Traces, nil |
| 154 | +} |
| 155 | + |
| 156 | +func (c *audit) Create(rq any) (*apiv2.AuditTrace, error) { |
| 157 | + panic("unimplemented") |
| 158 | +} |
| 159 | + |
| 160 | +func (c *audit) Delete(id string) (*apiv2.AuditTrace, error) { |
| 161 | + panic("unimplemented") |
| 162 | +} |
| 163 | + |
| 164 | +func (t *audit) Convert(r *apiv2.AuditTrace) (string, any, any, error) { |
| 165 | + panic("unimplemented") |
| 166 | +} |
| 167 | + |
| 168 | +func (t *audit) Update(rq any) (*apiv2.AuditTrace, error) { |
| 169 | + panic("unimplemented") |
| 170 | +} |
0 commit comments