MSP-8299 - move test
This commit is contained in:
1 parent
a2e6e48c46
commit
d241c0356e
4 files changed
+296
-251
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
+19
-5
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user