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

+167
View File
@@ -0,0 +1,167 @@
package service
import (
"encoding/json"
"fmt"
"sort"
"ispctl/model"
"github.com/pkg/errors"
"github.com/txix-open/isp-kit/rc"
"github.com/txix-open/isp-kit/rc/schema"
)
// Audit reports the active configs that reference SECRET variables on
// UI-exposed fields. Those values were already rendered into module browsers,
// so the variables must be rotated.
func (c Config) Audit() ([]model.AuditViolation, error) {
modules, err := c.configRepo.GetAuditModules()
if err != nil {
return nil, err
}
variables, err := c.configRepo.GetAllVariables()
if err != nil {
return nil, err
}
secrets := make(map[string]struct{}, len(variables))
for _, variable := range variables {
if variable.Type == model.SecretVariableType {
secrets[variable.Name] = struct{}{}
}
}
violations := make([]model.AuditViolation, 0)
for _, module := range modules {
configuration, err := c.configRepo.GetConfigByModuleName(module.Name)
if err != nil {
if errors.Is(err, model.ErrModuleNotFound) {
continue // no active config — nothing to audit
}
return nil, err
}
moduleViolations, err := auditModule(module.Name, module.ConfigSchema, configuration.Data, secrets)
if err != nil {
return nil, errors.WithMessagef(err, "module [%s]", module.Name)
}
violations = append(violations, moduleViolations...)
}
sort.Slice(violations, func(i, j int) bool {
if violations[i].Module != violations[j].Module {
return violations[i].Module < violations[j].Module
}
return violations[i].Path < violations[j].Path
})
return violations, nil
}
// auditModule returns the violations of a single module: UI-exposed paths whose
// config references SECRET variables, and sensitive paths whose config value
// is not a single reference to a SECRET variable. The schema and the config
// are read with the shared isp-kit readers, in the same currency as the
// config-service secret policy. The reference report covers every string
// leaf, including composite JSON strings (report-only in stage 1).
func auditModule(moduleName string, schemaData json.RawMessage, configData map[string]any, secrets map[string]struct{}) ([]model.AuditViolation, error) {
var uiPaths, sensitivePaths []string
if len(schemaData) > 0 {
var err error
uiPaths, err = schema.UIExposedPaths(schemaData)
if err != nil {
return nil, err
}
sensitivePaths, err = schema.SensitivePaths(schemaData)
if err != nil {
return nil, err
}
}
uiSet := make(map[string]struct{}, len(uiPaths))
for _, path := range uiPaths {
uiSet[path] = struct{}{}
}
configBytes, err := json.Marshal(configData)
if err != nil {
return nil, errors.WithMessage(err, "marshal config data")
}
refs, err := rc.VariableRefReport(configBytes)
if err != nil {
return nil, err
}
violations := make([]model.AuditViolation, 0)
for path, names := range refs {
if _, isUI := uiSet[path]; !isUI {
continue
}
secretNames := make([]string, 0, len(names))
for _, name := range names {
if _, isSecret := secrets[name]; isSecret {
secretNames = append(secretNames, name)
}
}
if len(secretNames) == 0 {
continue
}
sort.Strings(secretNames)
violations = append(violations, model.AuditViolation{
Module: moduleName,
Path: path,
Variables: secretNames,
Reason: "secret variable in ui-exposed field",
})
}
if len(sensitivePaths) > 0 {
sensitiveSet := make(map[string]struct{}, len(sensitivePaths))
for _, path := range sensitivePaths {
sensitiveSet[path] = struct{}{}
}
values, err := leafValues(configBytes, sensitiveSet)
if err != nil {
return nil, err
}
for _, path := range sensitivePaths {
value, exists := values[path]
if !exists || value == "" {
continue // absent or empty field
}
if name, isRef := rc.SingleVariableRef(value); isRef {
if _, isSecret := secrets[name]; isSecret {
continue // a reference to a SECRET variable is compliant
}
violations = append(violations, model.AuditViolation{
Module: moduleName,
Path: path,
Variables: []string{name},
Reason: fmt.Sprintf("references non-SECRET variable %s", name),
})
continue
}
// A plain value, including composite JSON strings, is reported
// (report-only in stage 1, the write policy does not apply to
// composite strings).
violations = append(violations, model.AuditViolation{
Module: moduleName,
Path: path,
Variables: []string{},
Reason: "plain value in sensitive field",
})
}
}
return violations, nil
}
// leafValues collects the raw values of the config's string leaves located at
// the given canonical paths.
func leafValues(data []byte, paths map[string]struct{}) (map[string]string, error) {
values := make(map[string]string, len(paths))
_, err := rc.TransformStringLeaves(data, func(path, value string) string {
if _, want := paths[path]; want {
values[path] = value
}
return value
})
return values, err
}
+244
View File
@@ -0,0 +1,244 @@
package service
import (
"encoding/json"
"reflect"
"testing"
"ispctl/model"
)
func TestAuditModuleIntersection(t *testing.T) {
schema, err := json.Marshal(map[string]any{
"type": "object",
"properties": map[string]any{
"ui": map[string]any{"type": "string", "x-ui-exposed": true},
"plain": map[string]any{"type": "string"},
},
})
if err != nil {
t.Fatal(err)
}
configData := map[string]any{
"ui": "_Var(s1) _Var(t1)",
"plain": "_Var(s1)",
}
secrets := map[string]struct{}{"s1": {}}
violations, err := auditModule("m1", schema, configData, secrets)
if err != nil {
t.Fatalf("auditModule: %v", err)
}
// Only the UI leaf with the secret reference is reported; the same
// reference in the plain leaf and the TEXT reference in the UI leaf are
// not.
want := []model.AuditViolation{{
Module: "m1",
Path: "$.ui",
Variables: []string{"s1"},
Reason: "secret variable in ui-exposed field",
}}
if !reflect.DeepEqual(violations, want) {
t.Fatalf("violations = %v, want %v", violations, want)
}
}
func TestAuditModuleSecretOutsideUI(t *testing.T) {
schema, err := json.Marshal(map[string]any{
"type": "object",
"properties": map[string]any{
"plain": map[string]any{"type": "string"},
},
})
if err != nil {
t.Fatal(err)
}
violations, err := auditModule(
"m1",
schema,
map[string]any{"plain": "_Var(s1)"},
map[string]struct{}{"s1": {}},
)
if err != nil {
t.Fatalf("auditModule: %v", err)
}
if len(violations) != 0 {
t.Fatalf("violations = %v, want none: a secret outside ui-exposed fields is not a violation", violations)
}
}
func TestAuditModuleArrayPath(t *testing.T) {
schema, err := json.Marshal(map[string]any{
"type": "object",
"properties": map[string]any{
"clients": map[string]any{
"type": "array",
"items": map[string]any{"type": "string", "x-ui-exposed": true},
},
},
})
if err != nil {
t.Fatal(err)
}
configData := map[string]any{
"clients": []any{"_Var(s1)", "nope"},
}
violations, err := auditModule("m1", schema, configData, map[string]struct{}{"s1": {}})
if err != nil {
t.Fatalf("auditModule: %v", err)
}
want := []model.AuditViolation{{Module: "m1", Path: "$.clients[]", Variables: []string{"s1"}, Reason: "secret variable in ui-exposed field"}}
if !reflect.DeepEqual(violations, want) {
t.Fatalf("violations = %v, want %v", violations, want)
}
}
func TestAuditModuleCompositeReported(t *testing.T) {
// the report covers references hidden inside composite JSON strings
// (report-only in stage 1, the policy itself does not apply to them)
schema, err := json.Marshal(map[string]any{
"type": "object",
"properties": map[string]any{
"secrets": map[string]any{"type": "string", "x-ui-exposed": true},
},
})
if err != nil {
t.Fatal(err)
}
composite, err := json.Marshal(map[string]any{"a": "_Var(s1)"})
if err != nil {
t.Fatal(err)
}
configData := map[string]any{
"secrets": string(composite),
}
violations, err := auditModule("m1", schema, configData, map[string]struct{}{"s1": {}})
if err != nil {
t.Fatalf("auditModule: %v", err)
}
want := []model.AuditViolation{{Module: "m1", Path: "$.secrets", Variables: []string{"s1"}, Reason: "secret variable in ui-exposed field"}}
if !reflect.DeepEqual(violations, want) {
t.Fatalf("violations = %v, want %v", violations, want)
}
}
func TestAuditModuleEmptySchema(t *testing.T) {
violations, err := auditModule("m1", nil, map[string]any{"a": "_Var(s1)"}, map[string]struct{}{"s1": {}})
if err != nil {
t.Fatalf("auditModule: %v", err)
}
if len(violations) != 0 {
t.Fatalf("violations = %v, want none: an empty schema exposes no fields", violations)
}
}
func sensitiveTestSchema(t *testing.T) json.RawMessage {
t.Helper()
schema, err := json.Marshal(map[string]any{
"type": "object",
"properties": map[string]any{
"token": map[string]any{"type": "string", "x-sensitive": true},
},
})
if err != nil {
t.Fatal(err)
}
return schema
}
func TestAuditModuleSensitiveLiteral(t *testing.T) {
violations, err := auditModule(
"m1",
sensitiveTestSchema(t),
map[string]any{"token": "legacy-pass-123"},
map[string]struct{}{"s1": {}},
)
if err != nil {
t.Fatalf("auditModule: %v", err)
}
want := []model.AuditViolation{{
Module: "m1",
Path: "$.token",
Variables: []string{},
Reason: "plain value in sensitive field",
}}
if !reflect.DeepEqual(violations, want) {
t.Fatalf("violations = %v, want %v", violations, want)
}
}
func TestAuditModuleSensitiveNonSecretRef(t *testing.T) {
violations, err := auditModule(
"m1",
sensitiveTestSchema(t),
map[string]any{"token": "_Var(t1)"},
map[string]struct{}{"s1": {}},
)
if err != nil {
t.Fatalf("auditModule: %v", err)
}
want := []model.AuditViolation{{
Module: "m1",
Path: "$.token",
Variables: []string{"t1"},
Reason: "references non-SECRET variable t1",
}}
if !reflect.DeepEqual(violations, want) {
t.Fatalf("violations = %v, want %v", violations, want)
}
}
func TestAuditModuleSensitiveCompliant(t *testing.T) {
for name, config := range map[string]string{
"secret reference": `{"token": "_Var(s1)"}`,
"empty value": `{"token": ""}`,
"absent field": `{}`,
} {
t.Run(name, func(t *testing.T) {
var configData map[string]any
if err := json.Unmarshal([]byte(config), &configData); err != nil {
t.Fatal(err)
}
violations, err := auditModule("m1", sensitiveTestSchema(t), configData, map[string]struct{}{"s1": {}})
if err != nil {
t.Fatalf("auditModule: %v", err)
}
if len(violations) != 0 {
t.Fatalf("violations = %v, want none", violations)
}
})
}
}
func TestAuditModuleBothClasses(t *testing.T) {
schema, err := json.Marshal(map[string]any{
"type": "object",
"properties": map[string]any{
"secrets": map[string]any{"type": "string", "x-ui-exposed": true},
"token": map[string]any{"type": "string", "x-sensitive": true},
},
})
if err != nil {
t.Fatal(err)
}
violations, err := auditModule(
"m1",
schema,
map[string]any{"secrets": "_Var(s1)", "token": "plain-pass"},
map[string]struct{}{"s1": {}},
)
if err != nil {
t.Fatalf("auditModule: %v", err)
}
want := []model.AuditViolation{
{Module: "m1", Path: "$.secrets", Variables: []string{"s1"}, Reason: "secret variable in ui-exposed field"},
{Module: "m1", Path: "$.token", Variables: []string{}, Reason: "plain value in sensitive field"},
}
if !reflect.DeepEqual(violations, want) {
t.Fatalf("violations = %v, want %v", violations, want)
}
}