This repository has been archived by the owner on Mar 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
117 lines (107 loc) · 3.39 KB
/
view.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
package database
import (
"fmt"
"time"
"gopkg.in/Masterminds/squirrel.v1"
)
//GetVulnviewList returns []Vulnview instance
func (db *DB) GetVulnviewList(days int, score float64, product, cve string) ([]Vulnview, error) {
ds := []Vulnview{}
if db == nil {
return ds, nil
}
bldr := selectVulnview
//make "where" condition
if days > 0 {
t := time.Now()
bldr = bldr.Where(squirrel.GtOrEq{"date_update": time.Date(t.Year(), t.Month(), t.Day()-days, 0, 0, 0, 0, time.UTC).Unix()})
}
if score > 0 {
bldr = bldr.Where(squirrel.GtOrEq{"cvss_score": score})
}
if len(product) > 0 {
likeProduct := "%" + product + "%"
cond := squirrel.Or{squirrel.Expr("name like ?", likeProduct), squirrel.Expr("product_name like ?", likeProduct)}
sq, args, err := squirrel.Select("id").Distinct().From("affected").Where(cond).ToSql()
if err != nil {
return ds, err
}
bldr = bldr.Join(fmt.Sprintf("(%s) affect on affect.id = vulnview.id", sq), args...)
}
if len(cve) > 0 {
sq, args, err := squirrel.Select("id").Distinct().From("related").Where(squirrel.Eq{"vulinfo_id": cve}).ToSql()
if err != nil {
return ds, err
}
bldr = bldr.Join(fmt.Sprintf("(%s) relate on relate.id = vulnview.id", sq), args...)
}
//query
if psql, args, err := bldr.ToSql(); err != nil {
return ds, err
} else if _, err = db.GetDB().Select(&ds, psql, args...); err != nil {
return ds, err
}
return ds, nil
}
//GetVulnview returns Vulnview instance
func (db *DB) GetVulnview(id string) *Vulnview {
ds := Vulnview{}
if psql, args, err := selectVulnview.Where(squirrel.Eq{"id": id}).ToSql(); err != nil {
db.GetLogger().Println(err)
return nil
} else if err := db.GetDB().SelectOne(&ds, psql, args...); err != nil {
db.GetLogger().Println(err)
return nil
}
return &ds
}
//GetAffected returns []Affected instance
func (db *DB) GetAffected(id string) []Affected {
ds := []Affected{}
if psql, args, err := selectAffected.Where(squirrel.Eq{"id": id}).ToSql(); err != nil {
db.GetLogger().Println(err)
return nil
} else if _, err := db.GetDB().Select(&ds, psql, args...); err != nil {
db.GetLogger().Println(err)
return nil
}
return ds
}
//GetCVSS returns CVSS instance
func (db *DB) GetCVSS(id string) *CVSS {
ds := CVSS{}
if psql, args, err := selectCVSS.Where(squirrel.Eq{"id": id}).ToSql(); err != nil {
db.GetLogger().Println(err)
return nil
} else if err := db.GetDB().SelectOne(&ds, psql, args...); err != nil {
db.GetLogger().Println(err)
return nil
}
return &ds
}
//GetRelated returns []Affected instance
func (db *DB) GetRelated(id string) []Related {
ds := []Related{}
if psql, args, err := selectRelated.Where(squirrel.Eq{"id": id}).ToSql(); err != nil {
db.GetLogger().Println(err)
return nil
} else if _, err := db.GetDB().Select(&ds, psql, args...); err != nil {
db.GetLogger().Println(err)
return nil
}
return ds
}
/* Copyright 2018 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/