-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_storage.go
696 lines (577 loc) · 14.5 KB
/
memory_storage.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package cpe
import (
"fmt"
"strings"
"sync"
"time"
)
// MemoryStorage 是一个基于内存的存储实现
type MemoryStorage struct {
// CPE存储,键为CPE ID
cpes map[string]*CPE
// CVE存储,键为CVE ID
cves map[string]*CVEReference
// CPE-CVE关联关系,键为CPE ID,值为CVE ID列表
cpeToCVEs map[string][]string
// CVE-CPE关联关系,键为CVE ID,值为CPE ID列表
cveToCPEs map[string][]string
// CPE字典
dictionary *CPEDictionary
// 修改时间戳
timestamps map[string]time.Time
// 互斥锁,用于线程安全操作
mutex sync.RWMutex
}
// NewMemoryStorage 创建一个新的内存存储
func NewMemoryStorage() *MemoryStorage {
return &MemoryStorage{
cpes: make(map[string]*CPE),
cves: make(map[string]*CVEReference),
cpeToCVEs: make(map[string][]string),
cveToCPEs: make(map[string][]string),
dictionary: nil,
timestamps: make(map[string]time.Time),
mutex: sync.RWMutex{},
}
}
// Initialize 初始化存储
func (ms *MemoryStorage) Initialize() error {
ms.mutex.Lock()
defer ms.mutex.Unlock()
// 清空所有存储
ms.cpes = make(map[string]*CPE)
ms.cves = make(map[string]*CVEReference)
ms.cpeToCVEs = make(map[string][]string)
ms.cveToCPEs = make(map[string][]string)
ms.dictionary = nil
ms.timestamps = make(map[string]time.Time)
// 记录初始化时间
ms.timestamps["initialization"] = time.Now()
return nil
}
// Close 关闭存储连接
func (ms *MemoryStorage) Close() error {
// 内存存储不需要关闭连接
return nil
}
// StoreCPE 存储单个CPE
func (ms *MemoryStorage) StoreCPE(cpe *CPE) error {
if cpe == nil {
return ErrInvalidData
}
// 确保CPE有ID
if cpe.GetURI() == "" {
return fmt.Errorf("CPE must have a URI: %w", ErrInvalidData)
}
ms.mutex.Lock()
defer ms.mutex.Unlock()
// 深拷贝CPE以防止外部修改
cpeCopy := *cpe
ms.cpes[cpe.GetURI()] = &cpeCopy
// 更新时间戳
ms.timestamps["last_cpe_update"] = time.Now()
return nil
}
// RetrieveCPE 根据ID检索CPE
func (ms *MemoryStorage) RetrieveCPE(id string) (*CPE, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
cpe, ok := ms.cpes[id]
if !ok {
return nil, ErrNotFound
}
// 返回深拷贝以防止外部修改
cpeCopy := *cpe
return &cpeCopy, nil
}
// UpdateCPE 更新CPE
func (ms *MemoryStorage) UpdateCPE(cpe *CPE) error {
if cpe == nil {
return ErrInvalidData
}
// 确保CPE有ID
if cpe.GetURI() == "" {
return fmt.Errorf("CPE must have a URI: %w", ErrInvalidData)
}
ms.mutex.Lock()
defer ms.mutex.Unlock()
// 检查CPE是否存在
_, ok := ms.cpes[cpe.GetURI()]
if !ok {
return ErrNotFound
}
// 更新CPE
cpeCopy := *cpe
ms.cpes[cpe.GetURI()] = &cpeCopy
// 更新时间戳
ms.timestamps["last_cpe_update"] = time.Now()
return nil
}
// DeleteCPE 删除CPE
func (ms *MemoryStorage) DeleteCPE(id string) error {
ms.mutex.Lock()
defer ms.mutex.Unlock()
// 检查CPE是否存在
_, ok := ms.cpes[id]
if !ok {
return ErrNotFound
}
// 删除CPE
delete(ms.cpes, id)
// 删除与此CPE关联的CVE关系
delete(ms.cpeToCVEs, id)
// 更新CVE-CPE关系
for cveID, cpeIDs := range ms.cveToCPEs {
var newCPEIDs []string
for _, cpeID := range cpeIDs {
if cpeID != id {
newCPEIDs = append(newCPEIDs, cpeID)
}
}
ms.cveToCPEs[cveID] = newCPEIDs
}
// 更新时间戳
ms.timestamps["last_cpe_update"] = time.Now()
return nil
}
// SearchCPE 搜索CPE
func (ms *MemoryStorage) SearchCPE(criteria *CPE, options *MatchOptions) ([]*CPE, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
var results []*CPE
// 如果没有查询条件,返回所有CPE
if criteria == nil {
for _, cpe := range ms.cpes {
cpeCopy := *cpe
results = append(results, &cpeCopy)
}
return results, nil
}
// 如果没有选项,使用默认选项
if options == nil {
options = &MatchOptions{}
}
// 搜索匹配的CPE
for _, cpe := range ms.cpes {
if MatchCPE(criteria, cpe, options) {
cpeCopy := *cpe
results = append(results, &cpeCopy)
}
}
return results, nil
}
// AdvancedSearchCPE 高级搜索CPE
func (ms *MemoryStorage) AdvancedSearchCPE(criteria *CPE, options *AdvancedMatchOptions) ([]*CPE, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
var results []*CPE
// 如果没有查询条件,返回所有CPE
if criteria == nil {
for _, cpe := range ms.cpes {
cpeCopy := *cpe
results = append(results, &cpeCopy)
}
return results, nil
}
// 如果没有选项,使用默认选项
if options == nil {
options = &AdvancedMatchOptions{}
}
// 搜索匹配的CPE
for _, cpe := range ms.cpes {
if AdvancedMatchCPE(criteria, cpe, options) {
cpeCopy := *cpe
results = append(results, &cpeCopy)
}
}
return results, nil
}
// StoreCVE 存储CVE信息
func (ms *MemoryStorage) StoreCVE(cve *CVEReference) error {
if cve == nil {
return ErrInvalidData
}
// 确保CVE有ID
if cve.CVEID == "" {
return fmt.Errorf("CVE must have an ID: %w", ErrInvalidData)
}
ms.mutex.Lock()
defer ms.mutex.Unlock()
// 深拷贝CVE以防止外部修改
cveCopy := *cve
ms.cves[cve.CVEID] = &cveCopy
// 更新CVE-CPE关系
if len(cve.AffectedCPEs) > 0 {
var cpeIDs []string
for _, cpeName := range cve.AffectedCPEs {
// Solo necesitamos verificar que el formato es válido
var err error
if strings.HasPrefix(cpeName, "cpe:2.3:") {
_, err = ParseCpe23(cpeName)
} else if strings.HasPrefix(cpeName, "cpe:/") {
_, err = ParseCpe22(cpeName)
} else {
continue
}
if err == nil {
cpeIDs = append(cpeIDs, cpeName)
// 更新CPE-CVE关系
ms.cpeToCVEs[cpeName] = append(ms.cpeToCVEs[cpeName], cve.CVEID)
}
}
ms.cveToCPEs[cve.CVEID] = cpeIDs
}
// 更新时间戳
ms.timestamps["last_cve_update"] = time.Now()
return nil
}
// RetrieveCVE 根据CVE ID检索CVE信息
func (ms *MemoryStorage) RetrieveCVE(cveID string) (*CVEReference, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
cve, ok := ms.cves[cveID]
if !ok {
return nil, ErrNotFound
}
// 返回深拷贝以防止外部修改
cveCopy := *cve
return &cveCopy, nil
}
// UpdateCVE 更新CVE信息
func (ms *MemoryStorage) UpdateCVE(cve *CVEReference) error {
if cve == nil {
return ErrInvalidData
}
// 确保CVE有ID
if cve.CVEID == "" {
return fmt.Errorf("CVE must have an ID: %w", ErrInvalidData)
}
ms.mutex.Lock()
defer ms.mutex.Unlock()
// 检查CVE是否存在
_, ok := ms.cves[cve.CVEID]
if !ok {
return ErrNotFound
}
// 清除旧的CVE-CPE关系
oldCPEIDs, ok := ms.cveToCPEs[cve.CVEID]
if ok {
for _, cpeID := range oldCPEIDs {
// 从CPE-CVE关系中删除此CVE
var newCVEIDs []string
for _, id := range ms.cpeToCVEs[cpeID] {
if id != cve.CVEID {
newCVEIDs = append(newCVEIDs, id)
}
}
ms.cpeToCVEs[cpeID] = newCVEIDs
}
}
// 更新CVE
cveCopy := *cve
ms.cves[cve.CVEID] = &cveCopy
// 更新CVE-CPE关系
if len(cve.AffectedCPEs) > 0 {
var cpeIDs []string
for _, cpeName := range cve.AffectedCPEs {
// Solo necesitamos verificar que el formato es válido
var err error
if strings.HasPrefix(cpeName, "cpe:2.3:") {
_, err = ParseCpe23(cpeName)
} else if strings.HasPrefix(cpeName, "cpe:/") {
_, err = ParseCpe22(cpeName)
} else {
continue
}
if err == nil {
cpeIDs = append(cpeIDs, cpeName)
// 更新CPE-CVE关系
ms.cpeToCVEs[cpeName] = append(ms.cpeToCVEs[cpeName], cve.CVEID)
}
}
ms.cveToCPEs[cve.CVEID] = cpeIDs
}
// 更新时间戳
ms.timestamps["last_cve_update"] = time.Now()
return nil
}
// DeleteCVE 删除CVE信息
func (ms *MemoryStorage) DeleteCVE(cveID string) error {
ms.mutex.Lock()
defer ms.mutex.Unlock()
// 检查CVE是否存在
_, ok := ms.cves[cveID]
if !ok {
return ErrNotFound
}
// 删除CVE
delete(ms.cves, cveID)
// 清除CVE-CPE关系
cpeIDs, ok := ms.cveToCPEs[cveID]
if ok {
for _, cpeID := range cpeIDs {
// 从CPE-CVE关系中删除此CVE
var newCVEIDs []string
for _, id := range ms.cpeToCVEs[cpeID] {
if id != cveID {
newCVEIDs = append(newCVEIDs, id)
}
}
ms.cpeToCVEs[cpeID] = newCVEIDs
}
delete(ms.cveToCPEs, cveID)
}
// 更新时间戳
ms.timestamps["last_cve_update"] = time.Now()
return nil
}
// SearchCVE 搜索CVE
func (ms *MemoryStorage) SearchCVE(query string, options *SearchOptions) ([]*CVEReference, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
var results []*CVEReference
// 如果没有选项,使用默认选项
if options == nil {
options = NewSearchOptions()
}
// 如果没有查询条件,返回所有CVE(但考虑分页)
if query == "" {
for _, cve := range ms.cves {
// 应用过滤条件
if ms.applyCVEFilters(cve, options) {
cveCopy := *cve
results = append(results, &cveCopy)
}
}
} else {
// 简单的文本匹配搜索
query = strings.ToLower(query)
for _, cve := range ms.cves {
// 匹配CVE ID
if strings.Contains(strings.ToLower(cve.CVEID), query) {
// 应用过滤条件
if ms.applyCVEFilters(cve, options) {
cveCopy := *cve
results = append(results, &cveCopy)
}
continue
}
// 匹配描述
if strings.Contains(strings.ToLower(cve.Description), query) {
// 应用过滤条件
if ms.applyCVEFilters(cve, options) {
cveCopy := *cve
results = append(results, &cveCopy)
}
continue
}
// 匹配参考链接
for _, ref := range cve.References {
if strings.Contains(strings.ToLower(ref), query) {
// 应用过滤条件
if ms.applyCVEFilters(cve, options) {
cveCopy := *cve
results = append(results, &cveCopy)
}
break
}
}
}
}
// 应用分页
if options.Offset >= len(results) {
return []*CVEReference{}, nil
}
end := options.Offset + options.Limit
if end > len(results) {
end = len(results)
}
return results[options.Offset:end], nil
}
// applyCVEFilters 应用CVE过滤条件
func (ms *MemoryStorage) applyCVEFilters(cve *CVEReference, options *SearchOptions) bool {
// 检查CVSS评分范围
if options.MinCVSS > 0 && cve.CVSSScore < options.MinCVSS {
return false
}
if options.MaxCVSS > 0 && cve.CVSSScore > options.MaxCVSS {
return false
}
// 检查日期范围
if options.DateStart != nil && cve.PublishedDate.Before(*options.DateStart) {
return false
}
if options.DateEnd != nil && cve.PublishedDate.After(*options.DateEnd) {
return false
}
// 检查自定义过滤条件
for key, value := range options.Filters {
switch key {
case "severity":
if severity, ok := value.(string); ok && severity != cve.Severity {
return false
}
case "vendor":
if vendor, ok := value.(string); ok {
found := false
for _, cpeName := range cve.AffectedCPEs {
var cpe *CPE
var err error
if strings.HasPrefix(cpeName, "cpe:2.3:") {
cpe, err = ParseCpe23(cpeName)
} else if strings.HasPrefix(cpeName, "cpe:/") {
cpe, err = ParseCpe22(cpeName)
} else {
continue
}
if err == nil && string(cpe.Vendor) == vendor {
found = true
break
}
}
if !found {
return false
}
}
case "product":
if product, ok := value.(string); ok {
found := false
for _, cpeName := range cve.AffectedCPEs {
var cpe *CPE
var err error
if strings.HasPrefix(cpeName, "cpe:2.3:") {
cpe, err = ParseCpe23(cpeName)
} else if strings.HasPrefix(cpeName, "cpe:/") {
cpe, err = ParseCpe22(cpeName)
} else {
continue
}
if err == nil && string(cpe.ProductName) == product {
found = true
break
}
}
if !found {
return false
}
}
}
}
return true
}
// FindCVEsByCPE 查找与CPE关联的CVE
func (ms *MemoryStorage) FindCVEsByCPE(cpe *CPE) ([]*CVEReference, error) {
if cpe == nil {
return nil, ErrInvalidData
}
ms.mutex.RLock()
defer ms.mutex.RUnlock()
var results []*CVEReference
cpeURI := cpe.GetURI()
// 获取与此CPE关联的CVE ID列表
cveIDs, ok := ms.cpeToCVEs[cpeURI]
if !ok {
// 如果找不到精确匹配,尝试按照CPE规则进行匹配
for storedCPEURI, storedCVEIDs := range ms.cpeToCVEs {
var storedCPE *CPE
var err error
if strings.HasPrefix(storedCPEURI, "cpe:2.3:") {
storedCPE, err = ParseCpe23(storedCPEURI)
} else if strings.HasPrefix(storedCPEURI, "cpe:/") {
storedCPE, err = ParseCpe22(storedCPEURI)
} else {
continue
}
if err != nil {
continue
}
if MatchCPE(cpe, storedCPE, &MatchOptions{}) {
cveIDs = append(cveIDs, storedCVEIDs...)
}
}
// 如果仍然没有结果,返回空列表
if len(cveIDs) == 0 {
return []*CVEReference{}, nil
}
}
// 检索CVE详情
for _, cveID := range cveIDs {
cve, ok := ms.cves[cveID]
if ok {
cveCopy := *cve
results = append(results, &cveCopy)
}
}
return results, nil
}
// FindCPEsByCVE 查找与CVE关联的CPE
func (ms *MemoryStorage) FindCPEsByCVE(cveID string) ([]*CPE, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
var results []*CPE
// 获取与此CVE关联的CPE ID列表
cpeIDs, ok := ms.cveToCPEs[cveID]
if !ok {
return []*CPE{}, nil
}
// 检索CPE详情
for _, cpeID := range cpeIDs {
cpe, ok := ms.cpes[cpeID]
if ok {
cpeCopy := *cpe
results = append(results, &cpeCopy)
}
}
return results, nil
}
// StoreDictionary 存储CPE字典
func (ms *MemoryStorage) StoreDictionary(dict *CPEDictionary) error {
if dict == nil {
return ErrInvalidData
}
ms.mutex.Lock()
defer ms.mutex.Unlock()
// 存储字典的深拷贝
dictCopy := *dict
ms.dictionary = &dictCopy
// 更新时间戳
ms.timestamps["last_dictionary_update"] = time.Now()
return nil
}
// RetrieveDictionary 检索CPE字典
func (ms *MemoryStorage) RetrieveDictionary() (*CPEDictionary, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
if ms.dictionary == nil {
return nil, ErrNotFound
}
// 返回字典的深拷贝
dictCopy := *ms.dictionary
return &dictCopy, nil
}
// StoreModificationTimestamp 存储最后修改时间
func (ms *MemoryStorage) StoreModificationTimestamp(key string, timestamp time.Time) error {
ms.mutex.Lock()
defer ms.mutex.Unlock()
ms.timestamps[key] = timestamp
return nil
}
// RetrieveModificationTimestamp 检索最后修改时间
func (ms *MemoryStorage) RetrieveModificationTimestamp(key string) (time.Time, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
timestamp, ok := ms.timestamps[key]
if !ok {
return time.Time{}, ErrNotFound
}
return timestamp, nil
}
// ParseURI 根据URI格式解析CPE
func ParseURI(uri string) (*CPE, error) {
if strings.HasPrefix(uri, "cpe:2.3:") {
return ParseCpe23(uri)
} else if strings.HasPrefix(uri, "cpe:/") {
return ParseCpe22(uri)
}
return nil, NewInvalidFormatError(uri)
}