Skip to content

Commit

Permalink
✨ successfully moved cpe lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Feb 13, 2025
1 parent 7644797 commit 2892bd1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 28 deletions.
12 changes: 2 additions & 10 deletions pkg/cmd/offline/cpe/cpe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"github.com/vulncheck-oss/cli/pkg/cache"
"github.com/vulncheck-oss/cli/pkg/cmd/offline/sync"
"github.com/vulncheck-oss/cli/pkg/config"
"github.com/vulncheck-oss/cli/pkg/cpe/cpeoffline"
"github.com/vulncheck-oss/cli/pkg/cpe/cpeuri"
"github.com/vulncheck-oss/cli/pkg/cpe/cpeutils"
"github.com/vulncheck-oss/cli/pkg/search"
"github.com/vulncheck-oss/cli/pkg/db"
"github.com/vulncheck-oss/cli/pkg/ui"
)

Expand Down Expand Up @@ -47,13 +46,7 @@ func Command() *cobra.Command {
return fmt.Errorf("index cpecve is required to proceed")
}

query, err := cpeoffline.Query(cpe)

if err != nil {
return err
}

results, stats, err := search.IndexCPE("cpecve", *cpe, query)
results, stats, err := db.CPESearch("cpecve", *cpe)

if err != nil {
return err
Expand All @@ -70,7 +63,6 @@ func Command() *cobra.Command {
}

ui.Stat("Results found/filtered", fmt.Sprintf("%d/%d", len(results), len(cves)))
ui.Stat("Files/Lines processed", fmt.Sprintf("%d/%d", stats.TotalFiles, stats.TotalLines))
ui.Stat("Search duration", fmt.Sprintf("%.2f seconds", stats.Duration.Seconds()))

if !statsOnly {
Expand Down
85 changes: 85 additions & 0 deletions pkg/db/cpe.go
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
package db

import (
"encoding/json"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/vulncheck-oss/cli/pkg/cpe/cpeutils"
"strings"
"time"
)

func CPESearch(indexName string, cpe cpeutils.CPE) ([]cpeutils.CPEVulnerabilities, *Stats, error) {
startTime := time.Now()

db, err := DB()
if err != nil {
return nil, nil, err
}

// Convert table name to use underscores instead of hyphens
tableName := strings.ReplaceAll(indexName, "-", "_")

// Build query based on vendor and product like search.matchesCPE
var conditions []string
var args []interface{}

if cpe.Vendor != "" {
conditions = append(conditions, "vendor LIKE ?")
args = append(args, "%"+strings.ToLower(cpe.Vendor)+"%")
}

if cpe.Product != "" {
conditions = append(conditions, "product LIKE ?")
args = append(args, "%"+strings.ToLower(cpe.Product)+"%")
}

// Create WHERE clause if we have conditions
whereClause := ""
if len(conditions) > 0 {
whereClause = "WHERE " + strings.Join(conditions, " AND ")
}

// Execute query
query := fmt.Sprintf(`SELECT "vendor", "product", "version", "update", "edition", "language", "sw_edition", "target_sw", "target_hw", "other", "cpe23Uri", "cves" FROM "%s" %s`, tableName, whereClause)
rows, err := db.Query(query, args...)
if err != nil {
return nil, nil, fmt.Errorf("failed to execute query: %w", err)
}
defer rows.Close()

var results []cpeutils.CPEVulnerabilities
for rows.Next() {
var result cpeutils.CPEVulnerabilities
var cvesJSON []byte

err := rows.Scan(
&result.Vendor,
&result.Product,
&result.Version,
&result.Update,
&result.Edition,
&result.Language,
&result.SoftwareEdition,
&result.TargetSoftware,
&result.TargetHardware,
&result.Other,
&result.CPE23URI,
&cvesJSON,
)
if err != nil {
return nil, nil, fmt.Errorf("failed to scan row: %w", err)
}

if err := json.Unmarshal(cvesJSON, &result.Cves); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal CVEs: %w", err)
}

results = append(results, result)
}

stats := &Stats{
Duration: time.Since(startTime),
}

return results, stats, nil
}
8 changes: 3 additions & 5 deletions pkg/db/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func ImportIndex(filePath string, indexDir string, progressCallback func(int)) e
}
cols[i] = def
}
createTableSQL := fmt.Sprintf(`CREATE TABLE "%s" (%s)`,
tableName, strings.Join(cols, ", "))
createTableSQL := fmt.Sprintf(`CREATE TABLE "%s" (%s)`, tableName, strings.Join(cols, ", "))

if _, err := db.Exec(createTableSQL); err != nil {
return fmt.Errorf("failed to create table: %w", err)
Expand All @@ -52,7 +51,7 @@ func ImportIndex(filePath string, indexDir string, progressCallback func(int)) e
for _, col := range schema.Columns {
if col.Index {
indexName := fmt.Sprintf("idx_%s_%s", tableName, col.Name)
dropIndexSQL := fmt.Sprintf("DROP INDEX IF EXISTS %s", indexName)
dropIndexSQL := fmt.Sprintf(`DROP INDEX IF EXISTS "idx_%s_%s"`, tableName, col.Name)
if _, err := db.Exec(dropIndexSQL); err != nil {
return fmt.Errorf("failed to drop index %s: %w", indexName, err)
}
Expand Down Expand Up @@ -97,8 +96,7 @@ func ImportIndex(filePath string, indexDir string, progressCallback func(int)) e
// Recreate indexes after import
for _, col := range schema.Columns {
if col.Index {
indexSQL := fmt.Sprintf("CREATE INDEX idx_%s_%s ON %s(%s)",
tableName, col.Name, tableName, col.Name)
indexSQL := fmt.Sprintf(`CREATE INDEX "idx_%s_%s" ON "%s"("%s")`, tableName, col.Name, tableName, col.Name)
if _, err := db.Exec(indexSQL); err != nil {
return fmt.Errorf("failed to create index: %w", err)
}
Expand Down
23 changes: 10 additions & 13 deletions pkg/db/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,16 @@ var Schemas = []Schema{
{
Indices: []string{"cpecve"},
Columns: []Column{
/*
{Name: "part", Type: "TEXT", Index: false, NotNull: false},
{Name: "vendor", Type: "TEXT", Index: false, NotNull: false},
{Name: "product", Type: "TEXT", Index: false, NotNull: false},
{Name: "version", Type: "TEXT", Index: false, NotNull: false},
{Name: "update", Type: "TEXT", Index: false, NotNull: false},
{Name: "edition", Type: "TEXT", Index: false, NotNull: false},
{Name: "language", Type: "TEXT", Index: false, NotNull: false},
{Name: "sw_edition", Type: "TEXT", Index: false, NotNull: false},
{Name: "target_sw", Type: "TEXT", Index: false, NotNull: false},
{Name: "target_hw", Type: "TEXT", Index: false, NotNull: false},
{Name: "other", Type: "TEXT", Index: false, NotNull: false},
*/
{Name: "vendor", Type: "TEXT", Index: true, NotNull: false},
{Name: "product", Type: "TEXT", Index: true, NotNull: false},
{Name: "version", Type: "TEXT", Index: true, NotNull: false},
{Name: "update", Type: "TEXT", Index: true, NotNull: false},
{Name: "edition", Type: "TEXT", Index: true, NotNull: false},
{Name: "language", Type: "TEXT", Index: true, NotNull: false},
{Name: "sw_edition", Type: "TEXT", Index: true, NotNull: false},
{Name: "target_sw", Type: "TEXT", Index: true, NotNull: false},
{Name: "target_hw", Type: "TEXT", Index: true, NotNull: false},
{Name: "other", Type: "TEXT", Index: true, NotNull: false},
{Name: "cpe23Uri", Type: "TEXT", Index: true, NotNull: false},
{Name: "cves", Type: "TEXT", Index: false, NotNull: false, IsJSON: true},
},
Expand Down

0 comments on commit 2892bd1

Please sign in to comment.