diff --git a/go.sum b/go.sum index b4cf388..ff3a13e 100644 --- a/go.sum +++ b/go.sum @@ -161,8 +161,6 @@ github.com/txix-open/etp/v4 v4.1.3 h1:u9ZyU2yKGgoQ1/DSaElhazWm8xASdqGEdWjoJ2xQlf github.com/txix-open/etp/v4 v4.1.3/go.mod h1:9GK+Aje8BmLFq8Hk4j6Qb976LdU9WECCWAuLK1vAKgM= github.com/txix-open/isp-kit v1.31.5-0.20261127095759-c39408f3bd58 h1:huhPwzaU5BBwfhXD0vkBNBNv1xhjvAvplEZ/dOK1osQ= github.com/txix-open/isp-kit v1.31.5-0.20261127095759-c39408f3bd58/go.mod h1:hR9iAlh3x9gEg5GfALrrkN/2SbGkgZQCmwO2GZWjC1M= -github.com/txix-open/isp-kit v1.77.0 h1:gAUyLrnHUSpr06CPNIL+atiSnKFhaM05UtjfSSP8m68= -github.com/txix-open/isp-kit v1.77.0/go.mod h1:hR9iAlh3x9gEg5GfALrrkN/2SbGkgZQCmwO2GZWjC1M= github.com/txix-open/jsonschema v1.3.0 h1:XGuOTg22G7PdqfKypjajM20/i5FyGfFYsJKmheebKi4= github.com/txix-open/jsonschema v1.3.0/go.mod h1:l8YDZ1nvJrw6uxWowSVOxCV/ebiMJyapffW87ZEqH00= github.com/txix-open/validator/v10 v10.0.0-20261027072444-85f3391fc5c3 h1:1jE11HLmMHDYYAcE+UmQ5VHgz4stzz0+daJbDWS+sEs= diff --git a/service/audit_test.go b/service/audit_test.go deleted file mode 100644 index c15c7aa..0000000 --- a/service/audit_test.go +++ /dev/null @@ -1,244 +0,0 @@ -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) - } -} diff --git a/service/config.go b/service/config.go index f788c3a..d903201 100644 --- a/service/config.go +++ b/service/config.go @@ -4,18 +4,32 @@ import ( "encoding/json" "ispctl/model" - "ispctl/repository" "github.com/pkg/errors" "github.com/txix-open/isp-kit/rc/schema" ) -type Config struct { - enableUnsafe bool - configRepo repository.Config +// configRepo is the config-service API surface consumed by Config. It is an +// interface so tests can substitute a fake; production passes the concrete +// repository.Config, which satisfies it. +type configRepo interface { + GetAvailableConfigs() ([]model.ModuleInfo, error) + GetAuditModules() ([]model.AuditModule, error) + GetConfigByModuleName(name string) (*model.Config, error) + CreateUpdateConfig(request model.Config) (*model.Config, error) + GetSchemaByModuleId(moduleId string) (*model.ConfigSchema, error) + GetAllVariables() ([]model.Variable, error) + GetVariableByName(variableName string) (*model.Variable, error) + UpsertVariables(request []model.UpsertVariableRequest) error + DeleteVariable(variableName string) error } -func NewConfig(enableUnsafe bool, configRepo repository.Config) Config { +type Config struct { + enableUnsafe bool + configRepo configRepo +} + +func NewConfig(enableUnsafe bool, configRepo configRepo) Config { return Config{ enableUnsafe: enableUnsafe, configRepo: configRepo, diff --git a/tests/audit_test.go b/tests/audit_test.go new file mode 100644 index 0000000..99dac01 --- /dev/null +++ b/tests/audit_test.go @@ -0,0 +1,277 @@ +package tests_test + +import ( + "encoding/json" + "errors" + "reflect" + "testing" + + "ispctl/model" + "ispctl/service" +) + +// fakeConfigRepo implements the service.configRepo contract with static data; +// only the methods used by Audit are backed by real values. +type fakeConfigRepo struct { + modules []model.AuditModule + configs map[string]*model.Config // keyed by module name + variables []model.Variable +} + +func (r *fakeConfigRepo) GetAvailableConfigs() ([]model.ModuleInfo, error) { + return nil, errors.New("not used in tests") +} + +func (r *fakeConfigRepo) GetAuditModules() ([]model.AuditModule, error) { + return r.modules, nil +} + +func (r *fakeConfigRepo) GetConfigByModuleName(name string) (*model.Config, error) { + config, ok := r.configs[name] + if !ok { + return nil, model.ErrModuleNotFound + } + return config, nil +} + +func (r *fakeConfigRepo) CreateUpdateConfig(request model.Config) (*model.Config, error) { + return nil, errors.New("not used in tests") +} + +func (r *fakeConfigRepo) GetSchemaByModuleId(moduleId string) (*model.ConfigSchema, error) { + return nil, errors.New("not used in tests") +} + +func (r *fakeConfigRepo) GetAllVariables() ([]model.Variable, error) { + return r.variables, nil +} + +func (r *fakeConfigRepo) GetVariableByName(variableName string) (*model.Variable, error) { + return nil, model.ErrVariableNotFound +} + +func (r *fakeConfigRepo) UpsertVariables(request []model.UpsertVariableRequest) error { + return errors.New("not used in tests") +} + +func (r *fakeConfigRepo) DeleteVariable(variableName string) error { + return errors.New("not used in tests") +} + +// secretAndTextVariables is the default variable set: s1 is SECRET, t1 is TEXT. +func secretAndTextVariables() []model.Variable { + return []model.Variable{ + {Name: "s1", Type: model.SecretVariableType}, + {Name: "t1", Type: model.TextVariableType}, + } +} + +// objectSchema marshals a properties map into a minimal JSON schema. +func objectSchema(t *testing.T, properties map[string]any) json.RawMessage { + t.Helper() + schema, err := json.Marshal(map[string]any{ + "type": "object", + "properties": properties, + }) + if err != nil { + t.Fatal(err) + } + return schema +} + +// auditFixture wires a single module m1 through the public Audit API and +// returns its violations. +func auditFixture(t *testing.T, schemaData json.RawMessage, configData map[string]any) []model.AuditViolation { + t.Helper() + repo := &fakeConfigRepo{ + modules: []model.AuditModule{{Id: "id-1", Name: "m1", ConfigSchema: schemaData}}, + configs: map[string]*model.Config{"m1": {Name: "m1", Data: configData}}, + variables: secretAndTextVariables(), + } + violations, err := service.NewConfig(false, repo).Audit() + if err != nil { + t.Fatalf("Audit: %v", err) + } + return violations +} + +func requireAuditViolations(t *testing.T, got, want []model.AuditViolation) { + t.Helper() + if !reflect.DeepEqual(got, want) { + t.Fatalf("violations = %v, want %v", got, want) + } +} + +func TestAuditModuleIntersection(t *testing.T) { + t.Parallel() + schema := objectSchema(t, map[string]any{ + "ui": map[string]any{"type": "string", "x-ui-exposed": true}, + "plain": map[string]any{"type": "string"}, + }) + configData := map[string]any{ + "ui": "_Var(s1) _Var(t1)", + "plain": "_Var(s1)", + } + + violations := auditFixture(t, schema, configData) + // 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", + }} + requireAuditViolations(t, violations, want) +} + +func TestAuditModuleSecretOutsideUI(t *testing.T) { + t.Parallel() + schema := objectSchema(t, map[string]any{ + "plain": map[string]any{"type": "string"}, + }) + + violations := auditFixture(t, schema, map[string]any{"plain": "_Var(s1)"}) + 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) { + t.Parallel() + schema := objectSchema(t, map[string]any{ + "clients": map[string]any{ + "type": "array", + "items": map[string]any{"type": "string", "x-ui-exposed": true}, + }, + }) + configData := map[string]any{ + "clients": []any{"_Var(s1)", "nope"}, + } + + violations := auditFixture(t, schema, configData) + want := []model.AuditViolation{{Module: "m1", Path: "$.clients[]", Variables: []string{"s1"}, Reason: "secret variable in ui-exposed field"}} + requireAuditViolations(t, 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) + t.Parallel() + schema := objectSchema(t, map[string]any{ + "secrets": map[string]any{"type": "string", "x-ui-exposed": true}, + }) + composite, err := json.Marshal(map[string]any{"a": "_Var(s1)"}) + if err != nil { + t.Fatal(err) + } + configData := map[string]any{ + "secrets": string(composite), + } + + violations := auditFixture(t, schema, configData) + want := []model.AuditViolation{{Module: "m1", Path: "$.secrets", Variables: []string{"s1"}, Reason: "secret variable in ui-exposed field"}} + requireAuditViolations(t, violations, want) +} + +func TestAuditModuleEmptySchema(t *testing.T) { + t.Parallel() + + violations := auditFixture(t, nil, map[string]any{"a": "_Var(s1)"}) + 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) { + t.Parallel() + + violations := auditFixture(t, sensitiveTestSchema(t), map[string]any{"token": "legacy-pass-123"}) + want := []model.AuditViolation{{ + Module: "m1", + Path: "$.token", + Variables: []string{}, + Reason: "plain value in sensitive field", + }} + requireAuditViolations(t, violations, want) +} + +func TestAuditModuleSensitiveNonSecretRef(t *testing.T) { + t.Parallel() + + violations := auditFixture(t, sensitiveTestSchema(t), map[string]any{"token": "_Var(t1)"}) + want := []model.AuditViolation{{ + Module: "m1", + Path: "$.token", + Variables: []string{"t1"}, + Reason: "references non-SECRET variable t1", + }} + requireAuditViolations(t, 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) { + t.Parallel() + var configData map[string]any + if err := json.Unmarshal([]byte(config), &configData); err != nil { + t.Fatal(err) + } + violations := auditFixture(t, sensitiveTestSchema(t), configData) + if len(violations) != 0 { + t.Fatalf("violations = %v, want none", violations) + } + }) + } +} + +func TestAuditModuleBothClasses(t *testing.T) { + t.Parallel() + schema := objectSchema(t, map[string]any{ + "secrets": map[string]any{"type": "string", "x-ui-exposed": true}, + "token": map[string]any{"type": "string", "x-sensitive": true}, + }) + configData := map[string]any{"secrets": "_Var(s1)", "token": "plain-pass"} + + violations := auditFixture(t, schema, configData) + 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"}, + } + requireAuditViolations(t, violations, want) +} + +func TestAuditSkipsModuleWithoutActiveConfig(t *testing.T) { + t.Parallel() + repo := &fakeConfigRepo{ + modules: []model.AuditModule{{Id: "id-1", Name: "m1", ConfigSchema: sensitiveTestSchema(t)}}, + variables: secretAndTextVariables(), + } + // no entry in repo.configs: GetConfigByModuleName reports ErrModuleNotFound + violations, err := service.NewConfig(false, repo).Audit() + if err != nil { + t.Fatalf("Audit: %v", err) + } + if len(violations) != 0 { + t.Fatalf("violations = %v, want none: a module without an active config is skipped", violations) + } +}