GeezerVison is showing github.com — fix(config): address P1 and P2 review findings · majorcontext/keep@0a33a86
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: internal/config/load.go +36-6Lines changed: 36 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package config | 2 | 2 |
| 3 | 3 | import ( | | 4 | + "errors" | 4 | 5 | "fmt" | 5 | 6 | "os" | 6 | 7 | "path/filepath" | @@ -53,7 +54,26 @@ func LoadAll(rulesDir string, profilesDir string, packsDir string) (*LoadResult, | 53 | 54 | packs = make(map[string]*StarterPack) | 54 | 55 | } | 55 | 56 |
| 56 | | - // 4. Resolve packs for each scope. | | 57 | + // 4. Validate profile cross-references for rule files and packs, | | 58 | + // but only when a profiles directory was provided. | | 59 | + if profilesDir != "" { | | 60 | + for scope, rf := range scopes { | | 61 | + if rf.Profile != "" { | | 62 | + if _, ok := profiles[rf.Profile]; !ok { | | 63 | + return nil, fmt.Errorf("loadall: scope %q references profile %q which was not found", scope, rf.Profile) | | 64 | + } | | 65 | + } | | 66 | + } | | 67 | + for _, sp := range packs { | | 68 | + if sp.Profile != "" { | | 69 | + if _, ok := profiles[sp.Profile]; !ok { | | 70 | + return nil, fmt.Errorf("loadall: scope %q references profile %q which was not found", sp.Name, sp.Profile) | | 71 | + } | | 72 | + } | | 73 | + } | | 74 | + } | | 75 | + | | 76 | + // 5. Resolve packs for each scope. | 57 | 77 | // If no packs directory was provided, skip pack resolution and use only | 58 | 78 | // the inline rules from each rule file. | 59 | 79 | resolvedRules := make(map[string][]Rule, len(scopes)) | @@ -80,6 +100,7 @@ func LoadAll(rulesDir string, profilesDir string, packsDir string) (*LoadResult, | 80 | 100 | // LoadRules reads all .yaml and .yml files from dir, parses them as | 81 | 101 | // rule files, validates each one, and checks scope uniqueness across | 82 | 102 | // all files. Returns the parsed files indexed by scope name. | | 103 | +// Errors from all files are accumulated and returned together. | 83 | 104 | func LoadRules(dir string) (map[string]*RuleFile, error) { | 84 | 105 | entries, err := os.ReadDir(dir) | 85 | 106 | if err != nil { | @@ -90,9 +111,10 @@ func LoadRules(dir string) (map[string]*RuleFile, error) { | 90 | 111 | // Track which file first defined each scope for error reporting. | 91 | 112 | seenIn := make(map[string]string) | 92 | 113 |
| | 114 | + var errs []error | 93 | 115 | found := 0 | 94 | 116 | for _, entry := range entries { | 95 | | - if entry.IsDir() { | | 117 | + if !entry.Type().IsRegular() { | 96 | 118 | continue | 97 | 119 | } | 98 | 120 | name := entry.Name() | @@ -105,25 +127,33 @@ func LoadRules(dir string) (map[string]*RuleFile, error) { | 105 | 127 | fullPath := filepath.Join(dir, name) | 106 | 128 | data, err := os.ReadFile(fullPath) | 107 | 129 | if err != nil { | 108 | | - return nil, fmt.Errorf("loadrules: %s: cannot read file: %w", name, err) | | 130 | + errs = append(errs, fmt.Errorf("loadrules: %s: cannot read file: %w", name, err)) | | 131 | + continue | 109 | 132 | } | 110 | 133 |
| 111 | 134 | var rf RuleFile | 112 | 135 | if err := yaml.Unmarshal(data, &rf); err != nil { | 113 | | - return nil, fmt.Errorf("loadrules: %s: invalid YAML: %w", name, err) | | 136 | + errs = append(errs, fmt.Errorf("loadrules: %s: invalid YAML: %w", name, err)) | | 137 | + continue | 114 | 138 | } | 115 | 139 |
| 116 | 140 | if err := Validate(&rf); err != nil { | 117 | | - return nil, fmt.Errorf("loadrules: %s: %w", name, err) | | 141 | + errs = append(errs, fmt.Errorf("loadrules: %s: %w", name, err)) | | 142 | + continue | 118 | 143 | } | 119 | 144 |
| 120 | 145 | if prev, dup := seenIn[rf.Scope]; dup { | 121 | | - return nil, fmt.Errorf("loadrules: %s: duplicate scope %q already defined in %s", name, rf.Scope, prev) | | 146 | + errs = append(errs, fmt.Errorf("loadrules: %s: duplicate scope %q already defined in %s", name, rf.Scope, prev)) | | 147 | + continue | 122 | 148 | } | 123 | 149 | seenIn[rf.Scope] = name | 124 | 150 | result[rf.Scope] = &rf | 125 | 151 | } | 126 | 152 |
| | 153 | + if len(errs) > 0 { | | 154 | + return nil, errors.Join(errs...) | | 155 | + } | | 156 | + | 127 | 157 | if found == 0 { | 128 | 158 | return nil, fmt.Errorf("loadrules: no rule files found in %q", dir) | 129 | 159 | } | |
Collapse file Copy file name to clipboardExpand all lines: internal/config/load_all_test.go +91-1Lines changed: 91 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ package config | 3 | 3 | import ( | 4 | 4 | "os" | 5 | 5 | "path/filepath" | | 6 | + "strings" | 6 | 7 | "testing" | 7 | 8 | ) | 8 | 9 |
| @@ -115,7 +116,7 @@ func TestLoadAll_PackNotFound(t *testing.T) { | 115 | 116 | if err == nil { | 116 | 117 | t.Fatal("expected error when pack not found, got nil") | 117 | 118 | } | 118 | | - if !contains(err.Error(), "linear-safe-defaults") { | | 119 | + if !strings.Contains(err.Error(), "linear-safe-defaults") { | 119 | 120 | t.Errorf("expected error to mention pack name, got: %v", err) | 120 | 121 | } | 121 | 122 | } | @@ -131,3 +132,92 @@ func TestLoadAll_InvalidRules(t *testing.T) { | 131 | 132 | t.Fatal("expected error for invalid YAML, got nil") | 132 | 133 | } | 133 | 134 | } | | 135 | + | | 136 | +func TestLoadAll_ProfileCrossRefMissing(t *testing.T) { | | 137 | + // Rule file references a profile that does not exist in the profiles dir. | | 138 | + rulesDir := t.TempDir() | | 139 | + profilesDir := t.TempDir() | | 140 | + | | 141 | + ruleContent := `scope: test-scope | | 142 | +profile: nonexistent-profile | | 143 | +rules: | | 144 | + - name: test-rule | | 145 | + action: deny | | 146 | + message: "blocked" | | 147 | +` | | 148 | + if err := os.WriteFile(filepath.Join(rulesDir, "rules.yaml"), []byte(ruleContent), 0644); err != nil { | | 149 | + t.Fatalf("failed to write rules.yaml: %v", err) | | 150 | + } | | 151 | + | | 152 | + _, err := LoadAll(rulesDir, profilesDir, "") | | 153 | + if err == nil { | | 154 | + t.Fatal("expected error for missing profile cross-ref, got nil") | | 155 | + } | | 156 | + if !strings.Contains(err.Error(), "nonexistent-profile") { | | 157 | + t.Errorf("expected error to mention missing profile name, got: %v", err) | | 158 | + } | | 159 | +} | | 160 | + | | 161 | +func TestLoadAll_ProfileCrossRefValid(t *testing.T) { | | 162 | + // Rule file references a profile that exists — should succeed. | | 163 | + rulesDir := t.TempDir() | | 164 | + profilesDir := t.TempDir() | | 165 | + | | 166 | + ruleContent := `scope: test-scope | | 167 | +profile: my-profile | | 168 | +rules: | | 169 | + - name: test-rule | | 170 | + action: deny | | 171 | + message: "blocked" | | 172 | +` | | 173 | + profileContent := `name: my-profile | | 174 | +aliases: | | 175 | + foo: "params.fooId" | | 176 | +` | | 177 | + if err := os.WriteFile(filepath.Join(rulesDir, "rules.yaml"), []byte(ruleContent), 0644); err != nil { | | 178 | + t.Fatalf("failed to write rules.yaml: %v", err) | | 179 | + } | | 180 | + if err := os.WriteFile(filepath.Join(profilesDir, "my-profile.yaml"), []byte(profileContent), 0644); err != nil { | | 181 | + t.Fatalf("failed to write my-profile.yaml: %v", err) | | 182 | + } | | 183 | + | | 184 | + _, err := LoadAll(rulesDir, profilesDir, "") | | 185 | + if err != nil { | | 186 | + t.Fatalf("unexpected error: %v", err) | | 187 | + } | | 188 | +} | | 189 | + | | 190 | +func TestLoadAll_PackProfileCrossRefMissing(t *testing.T) { | | 191 | + // A pack references a profile that does not exist in the profiles dir. | | 192 | + rulesDir := t.TempDir() | | 193 | + profilesDir := t.TempDir() | | 194 | + packsDir := t.TempDir() | | 195 | + | | 196 | + ruleContent := `scope: test-scope | | 197 | +rules: | | 198 | + - name: test-rule | | 199 | + action: deny | | 200 | + message: "blocked" | | 201 | +` | | 202 | + packContent := `name: my-pack | | 203 | +profile: ghost-profile | | 204 | +rules: | | 205 | + - name: pack-rule | | 206 | + action: deny | | 207 | + message: "blocked" | | 208 | +` | | 209 | + if err := os.WriteFile(filepath.Join(rulesDir, "rules.yaml"), []byte(ruleContent), 0644); err != nil { | | 210 | + t.Fatalf("failed to write rules.yaml: %v", err) | | 211 | + } | | 212 | + if err := os.WriteFile(filepath.Join(packsDir, "my-pack.yaml"), []byte(packContent), 0644); err != nil { | | 213 | + t.Fatalf("failed to write my-pack.yaml: %v", err) | | 214 | + } | | 215 | + | | 216 | + _, err := LoadAll(rulesDir, profilesDir, packsDir) | | 217 | + if err == nil { | | 218 | + t.Fatal("expected error for missing pack profile cross-ref, got nil") | | 219 | + } | | 220 | + if !strings.Contains(err.Error(), "ghost-profile") { | | 221 | + t.Errorf("expected error to mention missing profile name, got: %v", err) | | 222 | + } | | 223 | +} |
Collapse file Copy file name to clipboardExpand all lines: internal/config/load_test.go +35-12Lines changed: 35 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ package config | 3 | 3 | import ( | 4 | 4 | "os" | 5 | 5 | "path/filepath" | | 6 | + "strings" | 6 | 7 | "testing" | 7 | 8 | ) | 8 | 9 |
| @@ -45,7 +46,7 @@ func TestLoadRules_DuplicateScope(t *testing.T) { | 45 | 46 | } | 46 | 47 | // Error should mention the duplicate scope name | 47 | 48 | errStr := err.Error() | 48 | | - if !contains(errStr, "duplicate-scope") { | | 49 | + if !strings.Contains(errStr, "duplicate-scope") { | 49 | 50 | t.Errorf("expected error to mention scope name \"duplicate-scope\", got: %s", errStr) | 50 | 51 | } | 51 | 52 | } | @@ -90,15 +91,37 @@ rules: | 90 | 91 | } | 91 | 92 | } | 92 | 93 |
| 93 | | -// contains is a helper since strings.Contains is in strings package. | 94 | | -func contains(s, substr string) bool { | 95 | | - return len(s) >= len(substr) && (s == substr || len(substr) == 0 || | 96 | | - func() bool { | 97 | | - for i := 0; i <= len(s)-len(substr); i++ { | 98 | | - if s[i:i+len(substr)] == substr { | 99 | | - return true | 100 | | - } | 101 | | - } | 102 | | - return false | 103 | | - }()) | | 94 | +func TestLoadRules_AccumulatesErrors(t *testing.T) { | | 95 | + dir := t.TempDir() | | 96 | + | | 97 | + // Write two invalid yaml rule files | | 98 | + bad1 := `scope: bad-scope-1 | | 99 | +rules: | | 100 | + - name: "BadName" | | 101 | + action: deny | | 102 | +` | | 103 | + bad2 := `scope: bad-scope-2 | | 104 | +rules: | | 105 | + - name: "BadName2" | | 106 | + action: deny | | 107 | +` | | 108 | + if err := os.WriteFile(filepath.Join(dir, "a.yaml"), []byte(bad1), 0644); err != nil { | | 109 | + t.Fatalf("failed to write a.yaml: %v", err) | | 110 | + } | | 111 | + if err := os.WriteFile(filepath.Join(dir, "b.yaml"), []byte(bad2), 0644); err != nil { | | 112 | + t.Fatalf("failed to write b.yaml: %v", err) | | 113 | + } | | 114 | + | | 115 | + _, err := LoadRules(dir) | | 116 | + if err == nil { | | 117 | + t.Fatal("expected error for invalid files, got nil") | | 118 | + } | | 119 | + errStr := err.Error() | | 120 | + // Both files should be mentioned | | 121 | + if !strings.Contains(errStr, "a.yaml") { | | 122 | + t.Errorf("expected error to mention a.yaml, got: %s", errStr) | | 123 | + } | | 124 | + if !strings.Contains(errStr, "b.yaml") { | | 125 | + t.Errorf("expected error to mention b.yaml, got: %s", errStr) | | 126 | + } | 104 | 127 | } |
Collapse file Copy file name to clipboardExpand all lines: internal/config/pack.go +27-2Lines changed: 27 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package config | 2 | 2 |
| 3 | 3 | import ( | | 4 | + "errors" | 4 | 5 | "fmt" | 5 | 6 | "os" | 6 | 7 | "path/filepath" | @@ -14,7 +15,7 @@ import ( | 14 | 15 | func LoadPacks(dir string) (map[string]*StarterPack, error) { | 15 | 16 | entries, err := os.ReadDir(dir) | 16 | 17 | if err != nil { | 17 | | - if os.IsNotExist(err) { | | 18 | + if errors.Is(err, os.ErrNotExist) { | 18 | 19 | return make(map[string]*StarterPack), nil | 19 | 20 | } | 20 | 21 | return nil, fmt.Errorf("loadpacks: cannot read directory %q: %w", dir, err) | @@ -25,7 +26,7 @@ func LoadPacks(dir string) (map[string]*StarterPack, error) { | 25 | 26 | seenIn := make(map[string]string) | 26 | 27 |
| 27 | 28 | for _, entry := range entries { | 28 | | - if entry.IsDir() { | | 29 | + if !entry.Type().IsRegular() { | 29 | 30 | continue | 30 | 31 | } | 31 | 32 | name := entry.Name() | @@ -45,6 +46,10 @@ func LoadPacks(dir string) (map[string]*StarterPack, error) { | 45 | 46 | return nil, fmt.Errorf("loadpacks: %s: invalid YAML: %w", name, err) | 46 | 47 | } | 47 | 48 |
| | 49 | + if err := validatePack(&sp); err != nil { | | 50 | + return nil, fmt.Errorf("loadpacks: %s: %w", name, err) | | 51 | + } | | 52 | + | 48 | 53 | if prev, dup := seenIn[sp.Name]; dup { | 49 | 54 | return nil, fmt.Errorf("loadpacks: %s: duplicate pack name %q already defined in %s", name, sp.Name, prev) | 50 | 55 | } | @@ -55,6 +60,23 @@ func LoadPacks(dir string) (map[string]*StarterPack, error) { | 55 | 60 | return result, nil | 56 | 61 | } | 57 | 62 |
| | 63 | +// validatePack checks that a StarterPack is well-formed, applying the same | | 64 | +// rule-level validations as Validate does for RuleFile rules. | | 65 | +func validatePack(sp *StarterPack) error { | | 66 | + var errs []error | | 67 | + | | 68 | + if sp.Name == "" { | | 69 | + errs = append(errs, errors.New("name: required and must not be empty")) | | 70 | + } | | 71 | + | | 72 | + for i, rule := range sp.Rules { | | 73 | + ruleErrs := validateRule(i, rule) | | 74 | + errs = append(errs, ruleErrs...) | | 75 | + } | | 76 | + | | 77 | + return errors.Join(errs...) | | 78 | +} | | 79 | + | 58 | 80 | // ResolvePacks takes a rule file's pack references, looks them up in the | 59 | 81 | // loaded packs, applies overrides, and returns the merged rule list | 60 | 82 | // (pack rules first, then inline rules). | @@ -101,6 +123,9 @@ func ResolvePacks(rf *RuleFile, packs map[string]*StarterPack) ([]Rule, error) { | 101 | 123 | if !ok { | 102 | 124 | return nil, fmt.Errorf("resolvepacks: pack %q: rule %q: override field %q must be a string", ref.Name, ruleName, field) | 103 | 125 | } | | 126 | + if len(s) > maxWhenLen { | | 127 | + return nil, fmt.Errorf("resolvepacks: pack %q: rule %q: when expression exceeds maximum length of %d", ref.Name, ruleName, maxWhenLen) | | 128 | + } | 104 | 129 | rules[idx].Match.When = s | 105 | 130 | case "message": | 106 | 131 | s, ok := val.(string) | |
|