Skip to content

Commit

Permalink
Improvements on audit describe command. (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Aug 18, 2023
1 parent fc00bb0 commit 938108b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
30 changes: 28 additions & 2 deletions cmd/audit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/go-openapi/strfmt"
Expand Down Expand Up @@ -56,6 +58,15 @@ func newAuditCmd(c *config) *cobra.Command {
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,
Expand All @@ -68,15 +79,30 @@ func newAuditCmd(c *config) *cobra.Command {

func (c auditCmd) Get(id string) (*models.V1AuditResponse, error) {
traces, err := c.client.Audit().FindAuditTraces(audit.NewFindAuditTracesParams().WithBody(&models.V1AuditFindRequest{
Rqid: id,
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)
}
return traces.Payload[0], nil

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) {
Expand Down
13 changes: 13 additions & 0 deletions cmd/completion/audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package completion

import (
"github.com/spf13/cobra"
)

func (c *Completion) AuditTypeCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"http", "grpc", "event"}, cobra.ShellCompDirectiveNoFileComp
}

func (c *Completion) AuditPhaseCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"request", "response", "single", "error", "opened", "closed"}, cobra.ShellCompDirectiveNoFileComp
}
4 changes: 3 additions & 1 deletion docs/metalctl_audit_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ metalctl audit describe <id> [flags]
### Options

```
-h, --help help for describe
-h, --help help for describe
--phase string phase of the audit trace. One of [request, response, single, error, opened, closed] (default "response")
--prettify-body attempts to interpret the body as json and prettifies it
```

### Options inherited from parent commands
Expand Down

0 comments on commit 938108b

Please sign in to comment.