fix(01-foundation): address all checker blockers and warnings in phase plans

This commit is contained in:
salvacybersec
2026-04-04 23:57:01 +03:00
parent 684b67cb73
commit fb8a1f002b
4 changed files with 337 additions and 154 deletions

View File

@@ -127,16 +127,15 @@ The embed directive must reference providers relative to loader.go location.
loader.go is at pkg/providers/loader.go.
providers/ directory is at project root.
Use: //go:embed ../../providers/*.yaml
and embed.FS path will be "../../providers/openai.yaml" etc.
Actually: Go embed paths must be relative and cannot use "..".
Actually: Go embed paths must be relative and cannot use "..".
Correct approach: place the embed in a file at project root level, or adjust.
Better approach from research: put loader in providers package, embed from pkg/providers,
Better approach from research: put loader in providers package, embed from pkg/providers,
but reference the providers/ dir which sits at root.
Resolution: The go:embed directive path is relative to the SOURCE FILE, not the module root.
Since loader.go is at pkg/providers/loader.go, to embed ../../providers/*.yaml would work
syntactically but Go's embed restricts paths containing "..".
syntactically but Go's embed restricts paths containing "..".
Use this instead: place a providers_embed.go at the PROJECT ROOT (same dir as go.mod):
package main -- NO, this breaks package separation
@@ -408,6 +407,8 @@ Create **pkg/providers/registry.go**:
package providers
import (
"fmt"
ahocorasick "github.com/petar-dambovaliev/aho-corasick"
)
@@ -480,8 +481,6 @@ func (r *Registry) AC() ahocorasick.AhoCorasick {
}
```
Note: registry.go needs `import "fmt"` added.
Then copy the three YAML files into the embed location:
```bash
mkdir -p /home/salva/Documents/apikey/pkg/providers/definitions
@@ -490,76 +489,8 @@ cp /home/salva/Documents/apikey/providers/anthropic.yaml /home/salva/Documents/a
cp /home/salva/Documents/apikey/providers/huggingface.yaml /home/salva/Documents/apikey/pkg/providers/definitions/
```
Finally, fill in **pkg/providers/registry_test.go** (replacing the stubs from Plan 01):
```go
package providers_test
import (
"testing"
"github.com/salvacybersec/keyhunter/pkg/providers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRegistryLoad(t *testing.T) {
reg, err := providers.NewRegistry()
require.NoError(t, err)
assert.GreaterOrEqual(t, len(reg.List()), 3, "expected at least 3 providers loaded")
}
func TestRegistryGet(t *testing.T) {
reg, err := providers.NewRegistry()
require.NoError(t, err)
p, ok := reg.Get("openai")
assert.True(t, ok)
assert.Equal(t, "openai", p.Name)
assert.Equal(t, 1, p.Tier)
_, ok = reg.Get("nonexistent-provider")
assert.False(t, ok)
}
func TestRegistryStats(t *testing.T) {
reg, err := providers.NewRegistry()
require.NoError(t, err)
stats := reg.Stats()
assert.GreaterOrEqual(t, stats.Total, 3)
assert.GreaterOrEqual(t, stats.ByTier[1], 2, "expected at least 2 tier-1 providers")
}
func TestAhoCorasickBuild(t *testing.T) {
reg, err := providers.NewRegistry()
require.NoError(t, err)
ac := reg.AC()
// Should match OpenAI keyword
matches := ac.FindAll("OPENAI_API_KEY=sk-proj-abc")
assert.NotEmpty(t, matches, "expected AC to find keyword in string containing 'sk-proj-'")
// Should not match clean text
noMatches := ac.FindAll("hello world no secrets here")
assert.Empty(t, noMatches, "expected no AC matches in text with no provider keywords")
}
func TestProviderSchemaValidation(t *testing.T) {
import_yaml := `
format_version: 0
name: invalid
last_verified: ""
`
// Directly test UnmarshalYAML via yaml.Unmarshal
var p providers.Provider
err := yaml.Unmarshal([]byte(import_yaml), &p) // NOTE: need import "gopkg.in/yaml.v3"
assert.Error(t, err, "expected validation error for format_version=0")
}
```
Note: The TestProviderSchemaValidation test needs `import "gopkg.in/yaml.v3"` added.
Add it to the imports. Full corrected test file with proper imports:
Finally, fill in **pkg/providers/registry_test.go** (replacing the stubs from Plan 01).
Write ONLY the following content — do not include any earlier draft versions:
```go
package providers_test
@@ -632,7 +563,7 @@ func TestProviderSchemaValidation(t *testing.T) {
- TestRegistryStats passes — Total >= 3
- TestAhoCorasickBuild passes — "sk-proj-" match found, "hello world" empty
- TestProviderSchemaValidation passes — error on format_version=0
- `grep -r 'go:embed' pkg/providers/loader.go` exits 0
- `grep -q 'go:embed' pkg/providers/loader.go` exits 0
- pkg/providers/definitions/ directory exists with 3 YAML files
</acceptance_criteria>
<done>Registry loads providers from embedded YAML, builds Aho-Corasick automaton, exposes List/Get/Stats/AC. All 5 tests pass.</done>