GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(config): address P1 and P2 review findings · majorcontext/keep@0a33a86

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • internal/config
    • load.go
    • load_all_test.go
    • load_test.go
    • pack.go
    • pack_test.go
    • profile.go
    • profile_test.go
    • validate.go
    • validate_test.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎internal/config/load.go‎

Copy file name to clipboardExpand all lines: internal/config/load.go
+36-6Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -53,7 +54,26 @@ func LoadAll(rulesDir string, profilesDir string, packsDir string) (*LoadResult,
5354
packs = make(map[string]*StarterPack)
5455
}
5556

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.
5777
// If no packs directory was provided, skip pack resolution and use only
5878
// the inline rules from each rule file.
5979
resolvedRules := make(map[string][]Rule, len(scopes))
@@ -80,6 +100,7 @@ func LoadAll(rulesDir string, profilesDir string, packsDir string) (*LoadResult,
80100
// LoadRules reads all .yaml and .yml files from dir, parses them as
81101
// rule files, validates each one, and checks scope uniqueness across
82102
// all files. Returns the parsed files indexed by scope name.
103+
// Errors from all files are accumulated and returned together.
83104
func LoadRules(dir string) (map[string]*RuleFile, error) {
84105
entries, err := os.ReadDir(dir)
85106
if err != nil {
@@ -90,9 +111,10 @@ func LoadRules(dir string) (map[string]*RuleFile, error) {
90111
// Track which file first defined each scope for error reporting.
91112
seenIn := make(map[string]string)
92113

114+
var errs []error
93115
found := 0
94116
for _, entry := range entries {
95-
if entry.IsDir() {
117+
if !entry.Type().IsRegular() {
96118
continue
97119
}
98120
name := entry.Name()
@@ -105,25 +127,33 @@ func LoadRules(dir string) (map[string]*RuleFile, error) {
105127
fullPath := filepath.Join(dir, name)
106128
data, err := os.ReadFile(fullPath)
107129
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
109132
}
110133

111134
var rf RuleFile
112135
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
114138
}
115139

116140
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
118143
}
119144

120145
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
122148
}
123149
seenIn[rf.Scope] = name
124150
result[rf.Scope] = &rf
125151
}
126152

153+
if len(errs) > 0 {
154+
return nil, errors.Join(errs...)
155+
}
156+
127157
if found == 0 {
128158
return nil, fmt.Errorf("loadrules: no rule files found in %q", dir)
129159
}
Collapse file

‎internal/config/load_all_test.go‎

Copy file name to clipboardExpand all lines: internal/config/load_all_test.go
+91-1Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67
"testing"
78
)
89

@@ -115,7 +116,7 @@ func TestLoadAll_PackNotFound(t *testing.T) {
115116
if err == nil {
116117
t.Fatal("expected error when pack not found, got nil")
117118
}
118-
if !contains(err.Error(), "linear-safe-defaults") {
119+
if !strings.Contains(err.Error(), "linear-safe-defaults") {
119120
t.Errorf("expected error to mention pack name, got: %v", err)
120121
}
121122
}
@@ -131,3 +132,92 @@ func TestLoadAll_InvalidRules(t *testing.T) {
131132
t.Fatal("expected error for invalid YAML, got nil")
132133
}
133134
}
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

‎internal/config/load_test.go‎

Copy file name to clipboardExpand all lines: internal/config/load_test.go
+35-12Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67
"testing"
78
)
89

@@ -45,7 +46,7 @@ func TestLoadRules_DuplicateScope(t *testing.T) {
4546
}
4647
// Error should mention the duplicate scope name
4748
errStr := err.Error()
48-
if !contains(errStr, "duplicate-scope") {
49+
if !strings.Contains(errStr, "duplicate-scope") {
4950
t.Errorf("expected error to mention scope name \"duplicate-scope\", got: %s", errStr)
5051
}
5152
}
@@ -90,15 +91,37 @@ rules:
9091
}
9192
}
9293

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+
}
104127
}
Collapse file

‎internal/config/pack.go‎

Copy file name to clipboardExpand all lines: internal/config/pack.go
+27-2Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -14,7 +15,7 @@ import (
1415
func LoadPacks(dir string) (map[string]*StarterPack, error) {
1516
entries, err := os.ReadDir(dir)
1617
if err != nil {
17-
if os.IsNotExist(err) {
18+
if errors.Is(err, os.ErrNotExist) {
1819
return make(map[string]*StarterPack), nil
1920
}
2021
return nil, fmt.Errorf("loadpacks: cannot read directory %q: %w", dir, err)
@@ -25,7 +26,7 @@ func LoadPacks(dir string) (map[string]*StarterPack, error) {
2526
seenIn := make(map[string]string)
2627

2728
for _, entry := range entries {
28-
if entry.IsDir() {
29+
if !entry.Type().IsRegular() {
2930
continue
3031
}
3132
name := entry.Name()
@@ -45,6 +46,10 @@ func LoadPacks(dir string) (map[string]*StarterPack, error) {
4546
return nil, fmt.Errorf("loadpacks: %s: invalid YAML: %w", name, err)
4647
}
4748

49+
if err := validatePack(&sp); err != nil {
50+
return nil, fmt.Errorf("loadpacks: %s: %w", name, err)
51+
}
52+
4853
if prev, dup := seenIn[sp.Name]; dup {
4954
return nil, fmt.Errorf("loadpacks: %s: duplicate pack name %q already defined in %s", name, sp.Name, prev)
5055
}
@@ -55,6 +60,23 @@ func LoadPacks(dir string) (map[string]*StarterPack, error) {
5560
return result, nil
5661
}
5762

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+
5880
// ResolvePacks takes a rule file's pack references, looks them up in the
5981
// loaded packs, applies overrides, and returns the merged rule list
6082
// (pack rules first, then inline rules).
@@ -101,6 +123,9 @@ func ResolvePacks(rf *RuleFile, packs map[string]*StarterPack) ([]Rule, error) {
101123
if !ok {
102124
return nil, fmt.Errorf("resolvepacks: pack %q: rule %q: override field %q must be a string", ref.Name, ruleName, field)
103125
}
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+
}
104129
rules[idx].Match.When = s
105130
case "message":
106131
s, ok := val.(string)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.