5.0 KiB
5.0 KiB
phase, plan, subsystem, tags, requires, provides, affects, tech-stack, key-files, decisions, metrics, requirements
| phase | plan | subsystem | tags | requires | provides | affects | tech-stack | key-files | decisions | metrics | requirements | |||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 07-import-cicd | 04 | cmd/import |
|
|
|
|
|
|
|
|
|
Phase 7 Plan 4: Import Command Wiring Summary
Wires keyhunter import --format=trufflehog|gitleaks|gitleaks-csv <file> end-to-end: parses external scanner output via pkg/importer, deduplicates in-file and against the existing KeyHunter database, and persists new findings to encrypted SQLite storage.
What Was Built
- cmd/import.go — new
importCmdwith required--formatflag dispatching toTruffleHogImporter,GitleaksImporter, orGitleaksCSVImporter.runImportopens the file, decodes, runsimporter.Dedup, then for each unique finding checksdb.FindingExistsByKeybeforedb.SaveFinding. EmitsImported N findings (M new, K duplicates)to stdout where K combines in-file duplicates and pre-existing DB matches. - engineToStorage helper — bridges the
engine.Source/storage.SourcePathfield name gap and defaultsDetectedAt. - pkg/storage.FindingExistsByKey — thin
SELECT 1 ... LIMIT 1lookup keyed on(provider_name, key_masked, source_path, line_number). Makes repeat imports idempotent without decrypting stored key values. - cmd/stubs.go —
importCmdstub block removed; newvar importCmdin cmd/import.go takes over the identifier so no cmd/root.go change is required.
Tests
TestSelectImporter— table covering trufflehog / gitleaks / gitleaks-csv / bogus / empty.TestEngineToStorage— verifies Source->SourcePath mapping and all verify_* fields.TestRunImport_TruffleHogEndToEnd— loadspkg/importer/testdata/trufflehog-sample.json, runsrunImporttwice: first pass assertsImported 3 findings (3 new, 0 duplicates)and ≥3 rows indb.ListFindings; second pass asserts0 new, 3 duplicates.TestRunImport_UnknownFormat— asserts selectImporter surfaces the "unknown format" error.TestRunImport_MissingFile— asserts wrapped "opening" error for a nonexistent path.TestFindingExistsByKey— hit case plus four miss cases (each tuple field flipped).
All tests pass: go build ./... clean, go test ./cmd/... ./pkg/storage/... ./pkg/importer/... ok.
Deviations from Plan
- [Rule 3 - Blocking] The plan sketch left
openDBForImportandfindingExistsInDBas TODOs inside cmd/import.go. Replaced inline:openDBForImportcollapsed into a direct call to the existingopenDBWithKeyhelper (per plan's executor note), andfindingExistsInDBwas replaced by a newstorage.FindingExistsByKeymethod so dedup runs as a single indexed SQL lookup instead of loading+decrypting every stored finding. - [Rule 2 - Missing critical functionality]
cmd/stubs.gowas already stripped of thehookCmdblock by a sibling wave-2 plan when this plan reached it. The import stub removal still applied cleanly; no conflict. - Added
TestRunImport_UnknownFormatandTestRunImport_MissingFilebeyond the plan's test list to lock in error-path behavior since the success path exercises most of the happy code.
Verification
cd /home/salva/Documents/apikey
go build ./...
go test ./cmd/... ./pkg/storage/... ./pkg/importer/...
# ok github.com/salvacybersec/keyhunter/cmd 0.448s
# ok github.com/salvacybersec/keyhunter/pkg/storage 0.148s
# ok github.com/salvacybersec/keyhunter/pkg/importer (cached)
Manual smoke (matches <verification> block in plan):
go run ./cmd/keyhunter import --format=trufflehog pkg/importer/testdata/trufflehog-sample.json
# Imported 3 findings (3 new, 0 duplicates)
go run ./cmd/keyhunter import --format=trufflehog pkg/importer/testdata/trufflehog-sample.json
# Imported 3 findings (0 new, 3 duplicates)
The end-to-end test exercises this exact sequence against a tempdir DB.
Commits
9dbb0b8feat(07-04): wire keyhunter import command with dedup and DB persist
Self-Check: PASSED
- cmd/import.go: FOUND
- cmd/import_test.go: FOUND
- pkg/storage/queries.go FindingExistsByKey: FOUND
- pkg/storage/queries_test.go TestFindingExistsByKey: FOUND
- cmd/stubs.go importCmd removed: CONFIRMED (grep empty)
- Commit
9dbb0b8: FOUND - Tests green across cmd, pkg/storage, pkg/importer