package command import ( "fmt" "os" "strings" "ispctl/command/utils" "ispctl/model" "github.com/olekukonko/tablewriter" "github.com/urfave/cli/v2" ) const ( tableHeaderAuditModule = "MODULE" tableHeaderAuditPath = "PATH" tableHeaderAuditVariables = "VARIABLES" tableHeaderAuditReason = "REASON" auditExitCode = 1 ) var outPrintAuditUsage = fmt.Sprintf("out print for audit; %s", OutPrintJsonValue) type AuditService interface { Audit() ([]model.AuditViolation, error) } type Audit struct { service AuditService } func NewAudit(service AuditService) Audit { return Audit{service: service} } func (c Audit) Command() *cli.Command { return &cli.Command{ Name: "config", Usage: "config commands", Subcommands: []*cli.Command{ { Name: "audit", Usage: "report secret policy violations in active configs: SECRET references in ui-exposed fields and plain or non-SECRET values in sensitive fields", Flags: []cli.Flag{ &cli.StringFlag{Name: OutPrintFlagName, Usage: outPrintAuditUsage}, }, Action: c.audit, }, }, } } func (c Audit) audit(ctx *cli.Context) error { violations, err := c.service.Audit() if err != nil { return err } switch ctx.String(OutPrintFlagName) { case OutPrintJsonValue: err = utils.PrintAnswer(violations) default: table := tablewriter.NewWriter(os.Stdout) table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false}) table.SetHeader([]string{tableHeaderAuditModule, tableHeaderAuditPath, tableHeaderAuditVariables, tableHeaderAuditReason}) for _, violation := range violations { table.Append([]string{violation.Module, violation.Path, strings.Join(violation.Variables, ", "), violation.Reason}) } table.Render() fmt.Printf("found %d violation(s); rotate the affected SECRET variables and move sensitive values to SECRET variable references\n", len(violations)) } if err != nil { return err } if len(violations) > 0 { return cli.Exit("secret policy violations found in active configs", auditExitCode) } return nil }