MSP-8299 - add new command - config audit

This commit is contained in:
ziminis committed 2026-11-27 17:07:39 +07:00
1 parent 7670e2bfe8
commit af75a06f9e
8 files changed
+586 -6

No files matched your search

+81
View File
@@ -0,0 +1,81 @@
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
}
+3
View File
@@ -13,6 +13,7 @@ type AutoComplete interface {
}
type ConfigService interface {
Audit() ([]model.AuditViolation, error)
CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error)
CreateUpdateConfigV2(configuration *model.Config) (map[string]any, error)
DeleteVariable(variableName string) error
@@ -37,6 +38,7 @@ func AllCommands(configService ConfigService, autoComplete AutoComplete) []*cli.
merge := NewMerge(configService, autoComplete)
gitGet := NewGitGet()
variables := NewVariables(configService, autoComplete)
audit := NewAudit(configService)
return []*cli.Command{
status.Command(),
get.Command(),
@@ -46,6 +48,7 @@ func AllCommands(configService ConfigService, autoComplete AutoComplete) []*cli.
merge.Command(),
gitGet.Command(),
variables.Command(),
audit.Command(),
}
}