2026-05-12 18:11:18 +05:30
|
|
|
package executor
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/safedep/dry/log"
|
2026-05-24 12:46:06 +05:30
|
|
|
"github.com/safedep/pmg/config"
|
|
|
|
|
"github.com/safedep/pmg/sandbox"
|
2026-05-12 18:11:18 +05:30
|
|
|
)
|
|
|
|
|
|
2026-05-29 20:19:53 +05:30
|
|
|
// ObserveViolations collects any sandbox violation report associated with the
|
|
|
|
|
// run, persists it to the violation cache for forensic review (via
|
|
|
|
|
// `pmg sandbox violations list` / `pmg sandbox explain`), and returns the number
|
|
|
|
|
// of violations observed. Failures are logged and swallowed; observability MUST
|
2026-05-19 14:40:54 +05:30
|
|
|
// NOT affect command exit.
|
2026-05-29 20:19:53 +05:30
|
|
|
//
|
|
|
|
|
// This is the sandbox package's only stake in command-failure handling. It
|
|
|
|
|
// deliberately does not classify or shape the failure: causation cannot be
|
|
|
|
|
// inferred from EPERM/EACCES returns alone, so attribution is left to the
|
|
|
|
|
// execution layer (see internal/runner classify).
|
|
|
|
|
func ObserveViolations(result *sandbox.ExecutionResult, runErr error) int {
|
2026-05-19 14:40:54 +05:30
|
|
|
if result == nil {
|
|
|
|
|
return 0
|
2026-05-12 18:11:18 +05:30
|
|
|
}
|
|
|
|
|
|
2026-05-19 14:40:54 +05:30
|
|
|
report, diagErr := result.BestEffortViolation(runErr)
|
|
|
|
|
if diagErr != nil {
|
|
|
|
|
log.Warnf("failed to collect sandbox diagnostics: %v", diagErr)
|
|
|
|
|
return 0
|
2026-05-12 18:11:18 +05:30
|
|
|
}
|
|
|
|
|
if report == nil || len(report.Violations) == 0 {
|
2026-05-19 14:40:54 +05:30
|
|
|
return 0
|
2026-05-12 18:11:18 +05:30
|
|
|
}
|
|
|
|
|
|
2026-05-19 14:40:54 +05:30
|
|
|
cfg := config.Get()
|
|
|
|
|
if cfg == nil {
|
|
|
|
|
return len(report.Violations)
|
2026-05-12 18:11:18 +05:30
|
|
|
}
|
|
|
|
|
|
2026-05-19 14:40:54 +05:30
|
|
|
dir := cfg.SandboxViolationCacheDir()
|
|
|
|
|
if dir == "" {
|
|
|
|
|
return len(report.Violations)
|
2026-05-12 18:11:18 +05:30
|
|
|
}
|
|
|
|
|
|
2026-05-19 14:40:54 +05:30
|
|
|
if _, err := sandbox.NewViolationCache(dir).Write(report); err != nil {
|
|
|
|
|
log.Warnf("failed to persist sandbox violation report: %v", err)
|
2026-05-12 18:11:18 +05:30
|
|
|
}
|
|
|
|
|
|
2026-05-19 14:40:54 +05:30
|
|
|
return len(report.Violations)
|
2026-05-12 18:11:18 +05:30
|
|
|
}
|