Files
ispctl/service/audit.go
T

168 lines
4.9 KiB
Go

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
}