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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Keep source policy audits independent of runtime file churn
pulse-triage[bot]233dec38441b
diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.goindex f27a4e1988..925a3d3bbe 100644--- a/internal/unifiedresources/code_standards_test.go+++ b/internal/unifiedresources/code_standards_test.go@@ -130,17 +130,7 @@ func TestActionTruthTypesStayUnifiedResourceOwned(t *testing.T) {if _, err := os.Stat(path); os.IsNotExist(err) {continue}- err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {- if err != nil {- return err- }- if info.IsDir() || filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") {- return nil- }- source, err := os.ReadFile(path)- if err != nil {- return err- }+ err := walkProductionGoFiles(path, func(path string, source []byte) error {for _, pattern := range forbidden {if pattern.Match(source) {t.Errorf("%s declares workflow-local action truth; use unifiedresources.ActionResultV2", filepath.ToSlash(path))@@ -200,17 +190,7 @@ func TestOperationReceiptProtocolRemainsInternalToIngestAndRuntime(t *testing.T)if _, err := os.Stat(root); os.IsNotExist(err) {continue}- err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {- if err != nil {- return err- }- if info.IsDir() || filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") {- return nil- }- source, err := os.ReadFile(path)- if err != nil {- return err- }+ err := walkProductionGoFiles(root, func(path string, source []byte) error {if regexp.MustCompile(`(?m)^\s*OperationReceiptVersion\s+`).Match(source) {t.Errorf("%s declares workflow-local receipt protocol truth", filepath.ToSlash(path))}@@ -271,17 +251,7 @@ func TestActionPolicyDecisionProvenanceHasOneOwnerAndNoPublicAuthorityField(t *tdeclaration := regexp.MustCompile(`(?m)^type\s+ActionPolicyDecisionProvenance\b`)found := []string{}for _, root := range []string{"..", "../../pkg"} {- err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {- if err != nil {- return err- }- if info.IsDir() || filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") {- return nil- }- source, err := os.ReadFile(path)- if err != nil {- return err- }+ err := walkProductionGoFiles(root, func(path string, source []byte) error {if declaration.Match(source) {found = append(found, filepath.ToSlash(path))}@@ -326,17 +296,7 @@ func TestCanonicalActionPlanConstructionCannotBypassPolicyProvenancePlanner(t *t"../mock/action_fixtures.go": true,}pattern := regexp.MustCompile(`(?s)\bActionPlan\s*\{\s*[A-Za-z_][A-Za-z0-9_]*\s*:`)- err := filepath.Walk("..", func(path string, info os.FileInfo, err error) error {- if err != nil {- return err- }- if info.IsDir() || filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") {- return nil- }- source, err := os.ReadFile(path)- if err != nil {- return err- }+ err := walkProductionGoFiles("..", func(path string, source []byte) error {if pattern.Match(source) && !allowed[filepath.ToSlash(path)] {t.Errorf("%s constructs ActionPlan outside the canonical planner or named compatibility boundary", filepath.ToSlash(path))}@@ -365,20 +325,7 @@ func readConsumerGoFiles(t *testing.T, relDir string) map[string]string {internalDir := filepath.Join("..", relDir)files := make(map[string]string)- err := filepath.Walk(internalDir, func(path string, info os.FileInfo, err error) error {- if err != nil {- return err- }- if info.IsDir() {- return nil- }- if filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") {- return nil- }- data, readErr := os.ReadFile(path)- if readErr != nil {- t.Fatalf("failed to read %s: %v", path, readErr)- }+ err := walkProductionGoFiles(internalDir, func(path string, data []byte) error {files[path] = string(data)return nil})@@ -1869,21 +1816,7 @@ func TestCanonicalIdentityIsCanonicalResourceField(t *testing.T) {// CanonicalResourceType() for legacy normalization.func TestNoLegacyHostResourceTypeSymbol(t *testing.T) {internalDir := filepath.Join("..")- err := filepath.Walk(internalDir, func(path string, info os.FileInfo, err error) error {- if err != nil {- return err- }- if info.IsDir() {- return nil- }- if filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") {- return nil- }-- data, readErr := os.ReadFile(path)- if readErr != nil {- return readErr- }+ err := walkProductionGoFiles(internalDir, func(path string, data []byte) error {content := string(data)if !strings.Contains(content, "ResourceTypeHost") {return nil@@ -2032,21 +1965,7 @@ func TestNoLegacyMigrationHintsInRuntimeCode(t *testing.T) {}internalDir := filepath.Join("..")- err := filepath.Walk(internalDir, func(path string, info os.FileInfo, err error) error {- if err != nil {- return err- }- if info.IsDir() {- return nil- }- if filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") {- return nil- }-- data, readErr := os.ReadFile(path)- if readErr != nil {- return readErr- }+ err := walkProductionGoFiles(internalDir, func(path string, data []byte) error {content := string(data)normalizedPath := filepath.ToSlash(path)for _, phrase := range bannedPhrases {diff --git a/internal/unifiedresources/source_walk_test.go b/internal/unifiedresources/source_walk_test.gonew file mode 100644index 0000000000..ef1f450c70--- /dev/null+++ b/internal/unifiedresources/source_walk_test.go@@ -0,0 +1,168 @@+package unifiedresources++import (+ "errors"+ "io/fs"+ "os"+ "path/filepath"+ "reflect"+ "strings"+ "testing"+)++// walkProductionGoFiles inspects the same production sources as the policy+// audits, without statting unrelated files. Other test packages can remove+// generated runtime files after a directory has been listed. WalkDir retains+// entry names/types, so that churn cannot fail a source-only audit. Directory,+// source-read and visitor errors still fail the audit; none are suppressed.+func walkProductionGoFiles(root string, visit func(string, []byte) error) error {+ return filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {+ if err != nil {+ return err+ }+ if entry.IsDir() || filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") {+ return nil+ }+ source, err := os.ReadFile(path)+ if err != nil {+ return err+ }+ return visit(path, source)+ })+}++func writeSourceWalkFixture(t *testing.T, root string, files map[string]string) {+ t.Helper()+ for name, source := range files {+ path := filepath.Join(root, name)+ if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(path, []byte(source), 0o600); err != nil {+ t.Fatal(err)+ }+ }+}++func TestProductionGoWalkIgnoresDisappearingNonSourceEntries(t *testing.T) {+ for _, ignored := range []string{"10-runtime.sidecar", "10-fixture_test.go", "10-notes.md"} {+ t.Run(ignored, func(t *testing.T) {+ root := t.TempDir()+ want := map[string]string{+ "00-trigger.go": "package fixture\n",+ "20-nested/production.go": "package nested\n// policy evidence\n",+ "30-last.go": "package fixture\n// final evidence\n",+ }+ writeSourceWalkFixture(t, root, want)+ writeSourceWalkFixture(t, root, map[string]string{ignored: "not production source"})+ seen := map[string]string{}+ err := walkProductionGoFiles(root, func(path string, source []byte) error {+ rel, err := filepath.Rel(root, path)+ if err != nil {+ return err+ }+ seen[filepath.ToSlash(rel)] = string(source)+ if rel == "00-trigger.go" {+ // Both walkers list siblings before visiting the first file.+ // Removal here deterministically exercises that window, with+ // no concurrent writer, database or timing-dependent sleep.+ return os.Remove(filepath.Join(root, ignored))+ }+ return nil+ })+ if err != nil {+ t.Fatalf("non-source removal must not abort source inspection: %v", err)+ }+ if !reflect.DeepEqual(seen, want) {+ t.Fatalf("inspected sources = %v, want %v", seen, want)+ }+ if _, err := os.Stat(filepath.Join(root, ignored)); !errors.Is(err, fs.ErrNotExist) {+ t.Fatalf("fixture did not remove the ignored entry: %v", err)+ }+ })+ }+}++func TestProductionGoWalkFailsOnDisappearingSourceOrDirectory(t *testing.T) {+ for _, directory := range []bool{false, true} {+ name := "10-source.go"+ if directory {+ name = "10-package"+ }+ t.Run(name, func(t *testing.T) {+ root := t.TempDir()+ writeSourceWalkFixture(t, root, map[string]string{"00-trigger.go": "package fixture\n"})+ victim := filepath.Join(root, name)+ if directory {+ if err := os.Mkdir(victim, 0o700); err != nil {+ t.Fatal(err)+ }+ } else {+ writeSourceWalkFixture(t, root, map[string]string{name: "package fixture\n"})+ }+ triggered := false+ err := walkProductionGoFiles(root, func(path string, _ []byte) error {+ if filepath.Base(path) == "00-trigger.go" {+ triggered = true+ return os.Remove(victim)+ }+ return nil+ })+ if !triggered || !errors.Is(err, fs.ErrNotExist) {+ t.Fatalf("missing audit input must fail closed: triggered=%v, err=%v", triggered, err)+ }+ })+ }+}++func TestProductionGoWalkFailsOnSourceReadError(t *testing.T) {+ root := t.TempDir()+ writeSourceWalkFixture(t, root, map[string]string{+ "00-trigger.go": "package fixture\n",+ "10-source.go": "package fixture\n",+ })+ triggered := false+ err := walkProductionGoFiles(root, func(path string, _ []byte) error {+ if filepath.Base(path) != "00-trigger.go" {+ return nil+ }+ triggered = true+ victim := filepath.Join(root, "10-source.go")+ if err := os.Remove(victim); err != nil {+ return err+ }+ // The listed entry is still a source file, but reading it now fails.+ return os.Mkdir(victim, 0o700)+ })+ if !triggered || err == nil {+ t.Fatalf("source read failure must not be ignored: triggered=%v, err=%v", triggered, err)+ }+}++func TestProductionGoWalkFailsOnMissingRoot(t *testing.T) {+ called := false+ err := walkProductionGoFiles(filepath.Join(t.TempDir(), "missing"), func(string, []byte) error {+ called = true+ return nil+ })+ if called || !errors.Is(err, fs.ErrNotExist) {+ t.Fatalf("missing scan root must fail closed: called=%v, err=%v", called, err)+ }+}++func TestProductionGoWalkPropagatesVisitorError(t *testing.T) {+ root := t.TempDir()+ writeSourceWalkFixture(t, root, map[string]string{+ "00-source.go": "package fixture\n",+ "10-source.go": "package fixture\n",+ })+ wantErr := errors.New("policy audit failed")+ visits := 0+ err := walkProductionGoFiles(root, func(string, []byte) error {+ visits+++ return wantErr+ })+ if !errors.Is(err, wantErr) || visits != 1 {+ t.Fatalf("visitor failure must stop the audit: visits=%d, err=%v", visits, err)+ }+}