|
| 1 | +package gobinsec |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "gopkg.in/yaml.v3" |
| 10 | +) |
| 11 | + |
| 12 | +// FileConfig is the configuration for file cache |
| 13 | +type FileConfig struct { |
| 14 | + File string `yaml:"name"` |
| 15 | + Expiration int32 `yaml:"expiration"` |
| 16 | +} |
| 17 | + |
| 18 | +// NewFileConfig builds configuration for file cache |
| 19 | +func NewFileConfig(config *FileConfig) *FileConfig { |
| 20 | + if config == nil { |
| 21 | + config = &FileConfig{} |
| 22 | + } |
| 23 | + if config.File == "" { |
| 24 | + config.File = "~/.gobinsec-cache.yml" |
| 25 | + } |
| 26 | + if strings.HasPrefix(config.File, "~/") { |
| 27 | + home, err := os.UserHomeDir() |
| 28 | + if err != nil { |
| 29 | + return nil |
| 30 | + } |
| 31 | + config.File = filepath.Join(home, config.File[2:]) |
| 32 | + } |
| 33 | + if config.Expiration == 0 { |
| 34 | + config.Expiration = 86400 |
| 35 | + } |
| 36 | + return config |
| 37 | +} |
| 38 | + |
| 39 | +// DependencyCache is an entry of dependency cache |
| 40 | +type DependencyCache struct { |
| 41 | + Date string |
| 42 | + Vulnerabilities string |
| 43 | +} |
| 44 | + |
| 45 | +// FileCache is the cache instance |
| 46 | +type FileCache struct { |
| 47 | + File string |
| 48 | + Expiration int32 |
| 49 | + Cache map[string]DependencyCache |
| 50 | +} |
| 51 | + |
| 52 | +// NewFileCache builds a cache using file |
| 53 | +func NewFileCache(config *FileConfig) Cache { |
| 54 | + cache := make(map[string]DependencyCache) |
| 55 | + file, err := os.ReadFile(config.File) |
| 56 | + if err == nil { |
| 57 | + if err := yaml.Unmarshal(file, cache); err != nil { |
| 58 | + cache = make(map[string]DependencyCache) |
| 59 | + } |
| 60 | + } |
| 61 | + fileCache := &FileCache{ |
| 62 | + File: config.File, |
| 63 | + Expiration: config.Expiration, |
| 64 | + Cache: cache, |
| 65 | + } |
| 66 | + fileCache.CleanCache() |
| 67 | + return fileCache |
| 68 | +} |
| 69 | + |
| 70 | +// Get returns NVD response for given dependency |
| 71 | +func (fc *FileCache) Get(d *Dependency) []byte { |
| 72 | + dependency, ok := fc.Cache[d.Name] |
| 73 | + if ok { |
| 74 | + return []byte(dependency.Vulnerabilities) |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// Set put NVD response for given dependency in cache |
| 80 | +func (fc *FileCache) Set(d *Dependency, v []byte) { |
| 81 | + now := time.Now().UTC().Format("2006-01-02T15:04:05") |
| 82 | + cache := DependencyCache{ |
| 83 | + Date: now, |
| 84 | + Vulnerabilities: string(v), |
| 85 | + } |
| 86 | + fc.Cache[d.Name] = cache |
| 87 | +} |
| 88 | + |
| 89 | +// Ping does nothing |
| 90 | +func (fc *FileCache) Open() error { |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +// Clean saves file |
| 95 | +func (fc *FileCache) Close() { |
| 96 | + text, err := yaml.Marshal(fc.Cache) |
| 97 | + if err == nil { |
| 98 | + os.WriteFile(fc.File, text, 0644) // nolint:errcheck,gosec,gomnd // if error writing cache, do nothing |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// CleanCache removes obsolete cache entries |
| 103 | +func (fc *FileCache) CleanCache() { |
| 104 | + duration := time.Duration(fc.Expiration) * time.Second |
| 105 | + limit := time.Now().UTC().Add(-duration).Format("2006-01-02T15:04:05") |
| 106 | + for name, cache := range fc.Cache { |
| 107 | + if cache.Date < limit { |
| 108 | + delete(fc.Cache, name) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments