Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

add verify_window to agentexec command policy

rcourtman7f38a3d8bfff
diff --git a/internal/agentexec/policy.go b/internal/agentexec/policy.go
index 58043695c9..e089f965d9 100644
--- a/internal/agentexec/policy.go
+++ b/internal/agentexec/policy.go
@@ -1,22 +1,44 @@
package agentexec
import (
+ "encoding/json"
+ "fmt"
"regexp"
"strings"
+ "time"
"github.com/rs/zerolog/log"
)
+// DefaultVerifyWindow is the default duration the verifier substrate waits
+// for a postcondition to settle after a write capability has been dispatched.
+// Two minutes is enough for restart-class operations on a healthy host while
+// staying short enough that an operator watching a run does not feel stalled.
+const DefaultVerifyWindow = 2 * time.Minute
+
+// MaxVerifyWindow is the hard upper bound for the verify window. A larger
+// value would let an agent (or a misconfigured policy) pin the audit record
+// in "executing" for too long while polling for a postcondition. Fifteen
+// minutes covers slow rollouts (kubectl rollout, big VM boot) without
+// leaving the action dangling indefinitely.
+const MaxVerifyWindow = 15 * time.Minute
+
// CommandPolicy defines what commands are allowed, blocked, or require approval
type CommandPolicy struct {
// AutoApprove patterns - commands matching these are automatically allowed
- AutoApprove []string
+ AutoApprove []string `json:"auto_approve"`
// RequireApproval patterns - commands matching these need user approval
- RequireApproval []string
+ RequireApproval []string `json:"require_approval"`
// Blocked patterns - commands matching these are never allowed
- Blocked []string
+ Blocked []string `json:"blocked"`
+
+ // VerifyWindow is the maximum wall-clock duration the verifier substrate
+ // waits for a postcondition to settle after a write capability has been
+ // dispatched. Zero / negative values are coerced to DefaultVerifyWindow
+ // by NormalizeVerifyWindow; values above MaxVerifyWindow are clamped.
+ VerifyWindow time.Duration `json:"verify_window"`
// compiled regex patterns
autoApproveRe []*regexp.Regexp
@@ -24,9 +46,85 @@ type CommandPolicy struct {
blockedRe []*regexp.Regexp
}
+// NormalizeVerifyWindow returns the bounded verify window for a policy:
+// values <= 0 fall back to DefaultVerifyWindow, values above MaxVerifyWindow
+// are clamped to MaxVerifyWindow. Returned value is always within
+// [DefaultVerifyWindow, MaxVerifyWindow] except where the operator
+// explicitly picked something in (0, DefaultVerifyWindow]; we keep
+// operator-chosen small windows rather than silently widening them.
+func NormalizeVerifyWindow(d time.Duration) time.Duration {
+ if d <= 0 {
+ return DefaultVerifyWindow
+ }
+ if d > MaxVerifyWindow {
+ return MaxVerifyWindow
+ }
+ return d
+}
+
+// Normalize applies the verifier-substrate floor to the policy in place:
+// VerifyWindow is bounded to [(0,], MaxVerifyWindow] with the documented
+// default. Other policy fields are left alone; pattern compilation lives
+// in compile().
+func (p *CommandPolicy) Normalize() {
+ if p == nil {
+ return
+ }
+ p.VerifyWindow = NormalizeVerifyWindow(p.VerifyWindow)
+}
+
+// policyJSON is the wire-format shadow of CommandPolicy used to serialize
+// VerifyWindow as a Go duration string (e.g. "2m0s") rather than the raw
+// nanosecond integer time.Duration would otherwise produce. The other
+// pattern slices are passed through unchanged.
+type policyJSON struct {
+ AutoApprove []string `json:"auto_approve"`
+ RequireApproval []string `json:"require_approval"`
+ Blocked []string `json:"blocked"`
+ VerifyWindow string `json:"verify_window"`
+}
+
+// MarshalJSON emits the policy with VerifyWindow as a Go duration string so
+// the operator-facing config remains human-readable.
+func (p *CommandPolicy) MarshalJSON() ([]byte, error) {
+ if p == nil {
+ return []byte("null"), nil
+ }
+ window := NormalizeVerifyWindow(p.VerifyWindow)
+ return json.Marshal(policyJSON{
+ AutoApprove: p.AutoApprove,
+ RequireApproval: p.RequireApproval,
+ Blocked: p.Blocked,
+ VerifyWindow: window.String(),
+ })
+}
+
+// UnmarshalJSON parses the wire-format policy and applies NormalizeVerifyWindow
+// so deserialized values always satisfy the configured bounds.
+func (p *CommandPolicy) UnmarshalJSON(data []byte) error {
+ var shadow policyJSON
+ if err := json.Unmarshal(data, &shadow); err != nil {
+ return err
+ }
+ p.AutoApprove = shadow.AutoApprove
+ p.RequireApproval = shadow.RequireApproval
+ p.Blocked = shadow.Blocked
+ if shadow.VerifyWindow == "" {
+ p.VerifyWindow = DefaultVerifyWindow
+ } else {
+ d, err := time.ParseDuration(shadow.VerifyWindow)
+ if err != nil {
+ return fmt.Errorf("parse verify_window %q: %w", shadow.VerifyWindow, err)
+ }
+ p.VerifyWindow = NormalizeVerifyWindow(d)
+ }
+ return nil
+}
+
// DefaultPolicy returns a sensible default command policy
func DefaultPolicy() *CommandPolicy {
p := &CommandPolicy{
+ VerifyWindow: DefaultVerifyWindow,
AutoApprove: []string{
// System inspection
`^ps(\s|$)`,
@@ -209,6 +307,7 @@ func DefaultPolicy() *CommandPolicy {
}
p.compile()
+ p.Normalize()
return p
}
diff --git a/internal/agentexec/policy_test.go b/internal/agentexec/policy_test.go
index 98f6775560..4e98b0c932 100644
--- a/internal/agentexec/policy_test.go
+++ b/internal/agentexec/policy_test.go
@@ -1,6 +1,11 @@
package agentexec
-import "testing"
+import (
+ "encoding/json"
+ "strings"
+ "testing"
+ "time"
+)
func TestCompilePatternsIgnoresInvalidRegex(t *testing.T) {
res := compilePatterns([]string{"^df(\\s|$)", "["})
@@ -84,3 +89,100 @@ func TestPolicyHelpers(t *testing.T) {
t.Fatalf("expected single-target ping probe to be auto approved")
}
}
+
+func TestDefaultPolicyVerifyWindow(t *testing.T) {
+ p := DefaultPolicy()
+ if p.VerifyWindow != DefaultVerifyWindow {
+ t.Fatalf("default verify window = %v, want %v", p.VerifyWindow, DefaultVerifyWindow)
+ }
+}
+
+func TestNormalizeVerifyWindowBounds(t *testing.T) {
+ cases := []struct {
+ in time.Duration
+ want time.Duration
+ }{
+ {0, DefaultVerifyWindow},
+ {-1 * time.Second, DefaultVerifyWindow},
+ {30 * time.Second, 30 * time.Second},
+ {DefaultVerifyWindow, DefaultVerifyWindow},
+ {MaxVerifyWindow, MaxVerifyWindow},
+ {MaxVerifyWindow + time.Second, MaxVerifyWindow},
+ {1 * time.Hour, MaxVerifyWindow},
+ }
+ for _, tc := range cases {
+ if got := NormalizeVerifyWindow(tc.in); got != tc.want {
+ t.Fatalf("NormalizeVerifyWindow(%v) = %v, want %v", tc.in, got, tc.want)
+ }
+ }
+}
+
+func TestCommandPolicyNormalizeBoundsInPlace(t *testing.T) {
+ p := &CommandPolicy{VerifyWindow: time.Hour}
+ p.Normalize()
+ if p.VerifyWindow != MaxVerifyWindow {
+ t.Fatalf("Normalize did not clamp verify_window: got %v, want %v", p.VerifyWindow, MaxVerifyWindow)
+ }
+
+ q := &CommandPolicy{VerifyWindow: 0}
+ q.Normalize()
+ if q.VerifyWindow != DefaultVerifyWindow {
+ t.Fatalf("Normalize did not default verify_window: got %v, want %v", q.VerifyWindow, DefaultVerifyWindow)
+ }
+}
+
+func TestCommandPolicyJSONRoundtripVerifyWindow(t *testing.T) {
+ original := &CommandPolicy{
+ AutoApprove: []string{"^df$"},
+ RequireApproval: []string{"^systemctl\\s+restart"},
+ Blocked: []string{"^reboot$"},
+ VerifyWindow: 90 * time.Second,
+ }
+
+ raw, err := json.Marshal(original)
+ if err != nil {
+ t.Fatalf("marshal: %v", err)
+ }
+ if !strings.Contains(string(raw), `"verify_window":"1m30s"`) {
+ t.Fatalf("verify_window should serialize as a duration string; got %s", raw)
+ }
+
+ var decoded CommandPolicy
+ if err := json.Unmarshal(raw, &decoded); err != nil {
+ t.Fatalf("unmarshal: %v", err)
+ }
+ if decoded.VerifyWindow != 90*time.Second {
+ t.Fatalf("roundtripped verify_window = %v, want %v", decoded.VerifyWindow, 90*time.Second)
+ }
+}
+
+func TestCommandPolicyJSONUnmarshalAppliesBounds(t *testing.T) {
+ cases := []struct {
+ name string
+ raw string
+ want time.Duration
+ }{
+ {"empty defaults to default window", `{}`, DefaultVerifyWindow},
+ {"zero string defaults to default", `{"verify_window":"0s"}`, DefaultVerifyWindow},
+ {"over-cap clamps to max", `{"verify_window":"1h"}`, MaxVerifyWindow},
+ {"reasonable value passes through", `{"verify_window":"5m"}`, 5 * time.Minute},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ var p CommandPolicy
+ if err := json.Unmarshal([]byte(tc.raw), &p); err != nil {
+ t.Fatalf("unmarshal: %v", err)
+ }
+ if p.VerifyWindow != tc.want {
+ t.Fatalf("verify_window = %v, want %v", p.VerifyWindow, tc.want)
+ }
+ })
+ }
+}
+
+func TestCommandPolicyJSONUnmarshalRejectsInvalidDuration(t *testing.T) {
+ var p CommandPolicy
+ if err := json.Unmarshal([]byte(`{"verify_window":"not-a-duration"}`), &p); err == nil {
+ t.Fatalf("expected error for invalid verify_window")
+ }
+}