-
Notifications
You must be signed in to change notification settings - Fork 297
/
memory.go
82 lines (70 loc) · 2.4 KB
/
memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package storage
import (
"context"
"fmt"
"sync"
"github.com/Checkmarx/kics/v2/pkg/model"
"github.com/rs/zerolog/log"
)
var (
memoryMu sync.Mutex
)
// MemoryStorage is scans' results representation
type MemoryStorage struct {
vulnerabilities []model.Vulnerability
allFiles model.FileMetadatas
}
// SaveFile adds a new file metadata to files collection
func (m *MemoryStorage) SaveFile(_ context.Context, metadata *model.FileMetadata) error {
m.allFiles = append(m.allFiles, *metadata)
return nil
}
// GetFiles returns a collection of files saved on MemoryStorage
func (m *MemoryStorage) GetFiles(_ context.Context, _ string) (model.FileMetadatas, error) {
return m.allFiles, nil
}
// SaveVulnerabilities adds a list of vulnerabilities to vulnerabilities collection
func (m *MemoryStorage) SaveVulnerabilities(_ context.Context, vulnerabilities []model.Vulnerability) error {
defer memoryMu.Unlock()
memoryMu.Lock()
m.vulnerabilities = append(m.vulnerabilities, vulnerabilities...)
return nil
}
// GetVulnerabilities returns a collection of vulnerabilities saved on MemoryStorage
func (m *MemoryStorage) GetVulnerabilities(_ context.Context, _ string) ([]model.Vulnerability, error) {
return m.getUniqueVulnerabilities(), nil
}
func (m *MemoryStorage) getUniqueVulnerabilities() []model.Vulnerability {
vulnDictionary := make(map[string]model.Vulnerability)
for i := range m.vulnerabilities {
key := fmt.Sprintf("%s:%s:%d:%s:%s:%s",
m.vulnerabilities[i].QueryID,
m.vulnerabilities[i].FileName,
m.vulnerabilities[i].Line,
m.vulnerabilities[i].SimilarityID,
m.vulnerabilities[i].SearchKey,
m.vulnerabilities[i].KeyActualValue,
)
vulnDictionary[key] = m.vulnerabilities[i]
}
var uniqueVulnerabilities []model.Vulnerability
for key := range vulnDictionary {
uniqueVulnerabilities = append(uniqueVulnerabilities, vulnDictionary[key])
}
if len(uniqueVulnerabilities) == 0 {
return m.vulnerabilities
}
return uniqueVulnerabilities
}
// GetScanSummary is not supported by MemoryStorage
func (m *MemoryStorage) GetScanSummary(_ context.Context, _ []string) ([]model.SeveritySummary, error) {
return nil, nil
}
// NewMemoryStorage creates a new MemoryStorage empty and returns it
func NewMemoryStorage() *MemoryStorage {
log.Debug().Msg("storage.NewMemoryStorage()")
return &MemoryStorage{
allFiles: make(model.FileMetadatas, 0),
vulnerabilities: make([]model.Vulnerability, 0),
}
}