|
| 1 | +package gobinsec |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + _ "github.com/mattn/go-sqlite3" |
| 12 | +) |
| 13 | + |
| 14 | +// SQLiteConfig is the configuration for SQLite |
| 15 | +type SQLiteConfig struct { |
| 16 | + File string `yaml:"file"` |
| 17 | + Expiration int32 `yaml:"expiration"` |
| 18 | +} |
| 19 | + |
| 20 | +// NewSQLiteConfig builds configuration for SQLite cache |
| 21 | +func NewSQLiteConfig(config *SQLiteConfig) *SQLiteConfig { |
| 22 | + if config == nil { |
| 23 | + config = &SQLiteConfig{} |
| 24 | + } |
| 25 | + if config.File == "" { |
| 26 | + config.File = "~/.gobinsec.db" |
| 27 | + } |
| 28 | + if strings.HasPrefix(config.File, "~/") { |
| 29 | + home, err := os.UserHomeDir() |
| 30 | + if err != nil { |
| 31 | + return nil |
| 32 | + } |
| 33 | + config.File = filepath.Join(home, config.File[2:]) |
| 34 | + } |
| 35 | + if config.Expiration == 0 { |
| 36 | + config.Expiration = 86400 |
| 37 | + } |
| 38 | + return config |
| 39 | +} |
| 40 | + |
| 41 | +// SQLiteCache is the cache instance |
| 42 | +type SQLiteCache struct { |
| 43 | + Expiration int32 |
| 44 | + Database *sql.DB |
| 45 | +} |
| 46 | + |
| 47 | +// NewSQLiteCache builds a cache using SQLite |
| 48 | +func NewSQLiteCache(config *SQLiteConfig) Cache { |
| 49 | + database, err := sql.Open("sqlite3", config.File) |
| 50 | + if err != nil { |
| 51 | + return nil |
| 52 | + } |
| 53 | + cache := &SQLiteCache{ |
| 54 | + Database: database, |
| 55 | + Expiration: config.Expiration, |
| 56 | + } |
| 57 | + return cache |
| 58 | +} |
| 59 | + |
| 60 | +// Get returns NVD response for given dependency |
| 61 | +func (sc *SQLiteCache) Get(d *Dependency) []byte { |
| 62 | + vulnerabilities, err := sc.RetrieveDependency(d) |
| 63 | + if err != nil { |
| 64 | + fmt.Printf("ERROR getting dependency: %v\n", err) |
| 65 | + } |
| 66 | + return vulnerabilities |
| 67 | +} |
| 68 | + |
| 69 | +// Set put NVD response for given dependency in cache |
| 70 | +func (sc *SQLiteCache) Set(d *Dependency, v []byte) { |
| 71 | + err := sc.InsertDependency(d, v) |
| 72 | + if err != nil { |
| 73 | + fmt.Printf("ERROR setting dependency: %v\n", err) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Ping does nothing |
| 78 | +func (sc *SQLiteCache) Ping() error { |
| 79 | + return sc.CreateTable() |
| 80 | +} |
| 81 | + |
| 82 | +// Clean deletes expired entries |
| 83 | +func (sc *SQLiteCache) Clean() { |
| 84 | + err := sc.CleanTable() |
| 85 | + if err != nil { |
| 86 | + fmt.Printf("ERROR cleaning dependencies: %v\n", err) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// CreateTable creates dependencies table |
| 91 | +func (sc *SQLiteCache) CreateTable() error { |
| 92 | + query := `CREATE TABLE IF NOT EXISTS dependencies ( |
| 93 | + dependency TEXT, |
| 94 | + date TEXT, |
| 95 | + vulnerabilities TEXT |
| 96 | + )` |
| 97 | + _, err := sc.Database.Exec(query) |
| 98 | + return err |
| 99 | +} |
| 100 | + |
| 101 | +// InsertDependency insert given dependency in database |
| 102 | +func (sc *SQLiteCache) InsertDependency(d *Dependency, v []byte) error { |
| 103 | + now := time.Now().UTC().Format("2006-01-02T15:04:05") |
| 104 | + query := `INSERT INTO dependencies |
| 105 | + (dependency, date, vulnerabilities) |
| 106 | + VALUES |
| 107 | + (?, ?, ?)` |
| 108 | + _, err := sc.Database.Exec(query, d.Key(), now, string(v)) |
| 109 | + return err |
| 110 | +} |
| 111 | + |
| 112 | +// RetrieveDependency insert given dependency in database |
| 113 | +func (sc *SQLiteCache) RetrieveDependency(d *Dependency) ([]byte, error) { |
| 114 | + duration := time.Duration(sc.Expiration) * time.Second |
| 115 | + limit := time.Now().UTC().Add(-duration).Format("2006-01-02T15:04:05") |
| 116 | + query := `SELECT vulnerabilities |
| 117 | + FROM dependencies |
| 118 | + WHERE dependency = ? |
| 119 | + AND date > ?` |
| 120 | + row, err := sc.Database.Query(query, d.Key(), limit) |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + if row.Err() != nil { |
| 125 | + return nil, row.Err() |
| 126 | + } |
| 127 | + defer row.Close() |
| 128 | + if row.Next() { |
| 129 | + var vulnerabilities string |
| 130 | + if err := row.Scan(&vulnerabilities); err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + return []byte(vulnerabilities), nil |
| 134 | + } |
| 135 | + return nil, nil |
| 136 | +} |
| 137 | + |
| 138 | +// CleanTable deletes old expired entries |
| 139 | +func (sc *SQLiteCache) CleanTable() error { |
| 140 | + duration := time.Duration(sc.Expiration) * time.Second |
| 141 | + limit := time.Now().UTC().Add(-duration).Format("2006-01-02T15:04:05") |
| 142 | + query := `DELETE |
| 143 | + FROM dependencies |
| 144 | + WHERE date < ?` |
| 145 | + _, err := sc.Database.Exec(query, limit) |
| 146 | + return err |
| 147 | +} |
0 commit comments