278 lines
8.2 KiB
Go
278 lines
8.2 KiB
Go
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)
|
|
}
|
|
}
|