Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename to plural #207

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func match(specifiedURI string, cpeInNvd models.CpeBase) (bool, error) {
}

func matchCpe(uri string, cve *models.CveDetail) (nvdMatch, jvnMatch bool, err error) {
for _, nvd := range cve.Nvd {
for _, nvd := range cve.Nvds {
for _, cpe := range nvd.Cpes {
match, err := match(uri, cpe.CpeBase)
if err != nil {
Expand All @@ -271,12 +271,12 @@ func matchCpe(uri string, cve *models.CveDetail) (nvdMatch, jvnMatch bool, err e
return false, false, nil
}

for _, jvn := range cve.Jvn {
for _, jvn := range cve.Jvns {
for _, jvnCpe := range jvn.Cpes {
// If NVD has data of the same `part`, `vendor`, and `product`, NVD is used in priority.
// Because NVD has version information, but JVN does not.
found := false
for _, nvd := range cve.Nvd {
for _, nvd := range cve.Nvds {
for _, nvdCpe := range nvd.Cpes {
ok, err := isSamePartVendorProduct(nvdCpe.URI, jvnCpe.URI)
if err != nil {
Expand Down Expand Up @@ -354,13 +354,13 @@ func trimBSlash(s string) string {
return strings.Replace(s, `\`, "", -1)
}

func filterCVEDetailbyCPEURI(uri string, d *models.CveDetail) error {
func filterCveDetailByCpeURI(uri string, d *models.CveDetail) error {
if d == nil {
return nil
}

nvds := append([]models.Nvd{}, d.Nvd...)
d.Nvd = []models.Nvd{}
nvds := append([]models.Nvd{}, d.Nvds...)
d.Nvds = []models.Nvd{}
for _, nvd := range nvds {
for _, cpe := range nvd.Cpes {
matched, err := match(uri, cpe.CpeBase)
Expand All @@ -377,22 +377,22 @@ func filterCVEDetailbyCPEURI(uri string, d *models.CveDetail) error {
}
}
if matched {
d.Nvd = append(d.Nvd, nvd)
d.Nvds = append(d.Nvds, nvd)
break
}
}
}

jvns := append([]models.Jvn{}, d.Jvn...)
d.Jvn = []models.Jvn{}
jvns := append([]models.Jvn{}, d.Jvns...)
d.Jvns = []models.Jvn{}
for _, jvn := range jvns {
for _, cpe := range jvn.Cpes {
matched, err := isSamePartVendorProduct(uri, cpe.URI)
if err != nil {
continue
}
if matched {
d.Jvn = append(d.Jvn, jvn)
d.Jvns = append(d.Jvns, jvn)
break
}
}
Expand Down
44 changes: 22 additions & 22 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func Test_matchCpe(t *testing.T) {
args: args{
uri: "cpe:/o:qualcomm:qcs605_firmware:1.0.0",
cve: &models.CveDetail{
Nvd: []models.Nvd{{
Nvds: []models.Nvd{{
Cpes: []models.NvdCpe{nvdRangeCpe}},
},
},
Expand All @@ -436,7 +436,7 @@ func Test_matchCpe(t *testing.T) {
args: args{
uri: "cpe:/o:qualcomm:qcs605_firmware:3.0.0",
cve: &models.CveDetail{
Nvd: []models.Nvd{{
Nvds: []models.Nvd{{
Cpes: []models.NvdCpe{nvdRangeCpe}},
},
},
Expand All @@ -449,7 +449,7 @@ func Test_matchCpe(t *testing.T) {
args: args{
uri: "cpe:/o:qualcomm:qcs605_firmware:1.0.0",
cve: &models.CveDetail{
Nvd: []models.Nvd{{
Nvds: []models.Nvd{{
Cpes: []models.NvdCpe{nvdNonSemVerCpe},
Affects: []models.NvdAffect{
{
Expand All @@ -469,7 +469,7 @@ func Test_matchCpe(t *testing.T) {
args: args{
uri: "cpe:/o:qualcomm:qcs605_firmware:1.0.0",
cve: &models.CveDetail{
Nvd: []models.Nvd{{
Nvds: []models.Nvd{{
Cpes: []models.NvdCpe{nvdNonSemVerCpe},
Affects: []models.NvdAffect{
{
Expand All @@ -489,7 +489,7 @@ func Test_matchCpe(t *testing.T) {
args: args{
uri: "cpe:/o:qualcomm:qcs605_firmware:1.0.0",
cve: &models.CveDetail{
Jvn: []models.Jvn{{
Jvns: []models.Jvn{{
Cpes: []models.JvnCpe{jvnCpe}},
},
},
Expand All @@ -502,10 +502,10 @@ func Test_matchCpe(t *testing.T) {
args: args{
uri: "cpe:/o:qualcomm:qcs605_firmware:3.0.0",
cve: &models.CveDetail{
Nvd: []models.Nvd{{
Nvds: []models.Nvd{{
Cpes: []models.NvdCpe{nvdRangeCpe}},
},
Jvn: []models.Jvn{{
Jvns: []models.Jvn{{
Cpes: []models.JvnCpe{jvnCpe}},
},
},
Expand All @@ -519,10 +519,10 @@ func Test_matchCpe(t *testing.T) {
args: args{
uri: "cpe:/o:qualcomm:qcs605_firmware:1.0.0",
cve: &models.CveDetail{
Nvd: []models.Nvd{{
Nvds: []models.Nvd{{
Cpes: []models.NvdCpe{nvdRangeCpe}},
},
Jvn: []models.Jvn{{
Jvns: []models.Jvn{{
Cpes: []models.JvnCpe{jvnCpe}},
},
},
Expand Down Expand Up @@ -569,7 +569,7 @@ func Test_filterCVEDetailbyCPEURI(t *testing.T) {
args: args{
uri: "cpe:/o:vendor:product:1.0.0",
cve: &models.CveDetail{
Nvd: []models.Nvd{
Nvds: []models.Nvd{
{
Cpes: []models.NvdCpe{
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product:1.0.0"}},
Expand All @@ -582,7 +582,7 @@ func Test_filterCVEDetailbyCPEURI(t *testing.T) {
},
},
},
Jvn: []models.Jvn{
Jvns: []models.Jvn{
{
Cpes: []models.JvnCpe{
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product"}},
Expand All @@ -592,15 +592,15 @@ func Test_filterCVEDetailbyCPEURI(t *testing.T) {
},
},
expected: &models.CveDetail{
Nvd: []models.Nvd{
Nvds: []models.Nvd{
{
Cpes: []models.NvdCpe{
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product:1.0.0"}},
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product:1.0.1"}},
},
},
},
Jvn: []models.Jvn{
Jvns: []models.Jvn{
{
Cpes: []models.JvnCpe{
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product"}},
Expand All @@ -613,15 +613,15 @@ func Test_filterCVEDetailbyCPEURI(t *testing.T) {
args: args{
uri: "cpe:/o:vendor:product",
cve: &models.CveDetail{
Nvd: []models.Nvd{
Nvds: []models.Nvd{
{
Cpes: []models.NvdCpe{
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product:1.0.0"}},
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product:1.0.1"}},
},
},
},
Jvn: []models.Jvn{
Jvns: []models.Jvn{
{
Cpes: []models.JvnCpe{
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product"}},
Expand All @@ -636,15 +636,15 @@ func Test_filterCVEDetailbyCPEURI(t *testing.T) {
},
},
expected: &models.CveDetail{
Nvd: []models.Nvd{
Nvds: []models.Nvd{
{
Cpes: []models.NvdCpe{
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product:1.0.0"}},
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product:1.0.1"}},
},
},
},
Jvn: []models.Jvn{
Jvns: []models.Jvn{
{
Cpes: []models.JvnCpe{
{CpeBase: models.CpeBase{URI: "cpe:/o:vendor:product"}},
Expand All @@ -657,7 +657,7 @@ func Test_filterCVEDetailbyCPEURI(t *testing.T) {
args: args{
uri: "cpe:/o:vmware:esxi:7.0:-",
cve: &models.CveDetail{
Nvd: []models.Nvd{
Nvds: []models.Nvd{
{
Cpes: []models.NvdCpe{
{CpeBase: models.CpeBase{
Expand All @@ -668,18 +668,18 @@ func Test_filterCVEDetailbyCPEURI(t *testing.T) {
},
},
},
Jvn: []models.Jvn{},
Jvns: []models.Jvn{},
},
},
expected: &models.CveDetail{
Nvd: []models.Nvd{},
Jvn: []models.Jvn{},
Nvds: []models.Nvd{},
Jvns: []models.Jvn{},
},
},
}

for i, tt := range tests {
_ = filterCVEDetailbyCPEURI(tt.args.uri, tt.args.cve)
_ = filterCveDetailByCpeURI(tt.args.uri, tt.args.cve)
if !reflect.DeepEqual(tt.args.cve, tt.expected) {
t.Errorf("[%d] expected: %+v, actual: %+v", i, tt.expected, tt.args.cve)
}
Expand Down
10 changes: 5 additions & 5 deletions db/rdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (r *RDBDriver) Get(cveID string) (*models.CveDetail, error) {
Preload("Affects").
Preload("References").
Preload("Certs").
Find(&detail.Nvd).Error
Find(&detail.Nvds).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
Expand All @@ -224,7 +224,7 @@ func (r *RDBDriver) Get(cveID string) (*models.CveDetail, error) {
Preload("Cpes").
Preload("References").
Preload("Certs").
Find(&detail.Jvn).Error
Find(&detail.Jvns).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func (r *RDBDriver) GetCveIDsByCpeURI(uri string) (nvdCveIDs []string, jvnCveIDs
if err != nil {
return nil, nil, err
}
if err := filterCVEDetailbyCPEURI(uri, d); err != nil {
if err := filterCveDetailByCpeURI(uri, d); err != nil {
return nil, nil, err
}
nvdMatch, jvnMatch, err := matchCpe(uri, d)
Expand Down Expand Up @@ -329,10 +329,10 @@ func (r *RDBDriver) GetByCpeURI(uri string) ([]models.CveDetail, error) {
if err != nil {
return nil, err
}
if err := filterCVEDetailbyCPEURI(uri, d); err != nil {
if err := filterCveDetailByCpeURI(uri, d); err != nil {
return nil, err
}
if len(d.Nvd) > 0 || len(d.Jvn) > 0 {
if len(d.Nvds) > 0 || len(d.Jvns) > 0 {
details = append(details, *d)
}
}
Expand Down
14 changes: 7 additions & 7 deletions db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (r *RedisDriver) Get(cveID string) (*models.CveDetail, error) {
if err != nil {
return nil, err
}
detail.Nvd = c.Nvd
detail.Nvds = c.Nvds

if cveResult = r.conn.SMembers(ctx, jvnKeyPrefix+cveID); cveResult.Err() != nil {
return nil, cveResult.Err()
Expand All @@ -151,7 +151,7 @@ func (r *RedisDriver) Get(cveID string) (*models.CveDetail, error) {
if err != nil {
return nil, err
}
detail.Jvn = c.Jvn
detail.Jvns = c.Jvns

return &detail, nil
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (r *RedisDriver) unmarshal(cveID, cveType string, cveResult *redis.StringSl
}
}

cveDetail.Nvd = nvds
cveDetail.Nvds = nvds
case models.JvnType:
jvns := []models.Jvn{}
for _, cveStr := range cveResult.Val() {
Expand All @@ -201,7 +201,7 @@ func (r *RedisDriver) unmarshal(cveID, cveType string, cveResult *redis.StringSl
}
}

cveDetail.Jvn = jvns
cveDetail.Jvns = jvns
default:
return nil, xerrors.Errorf("Not Supported cveType: %s", cveType)
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func (r *RedisDriver) GetCveIDsByCpeURI(uri string) ([]string, []string, error)
if err != nil {
return nil, nil, err
}
if err := filterCVEDetailbyCPEURI(uri, d); err != nil {
if err := filterCveDetailByCpeURI(uri, d); err != nil {
return nil, nil, err
}
nvdMatch, jvnMatch, err := matchCpe(uri, d)
Expand Down Expand Up @@ -281,10 +281,10 @@ func (r *RedisDriver) GetByCpeURI(uri string) ([]models.CveDetail, error) {
if err != nil {
return nil, err
}
if err := filterCVEDetailbyCPEURI(uri, d); err != nil {
if err := filterCveDetailByCpeURI(uri, d); err != nil {
return nil, err
}
if len(d.Nvd) > 0 || len(d.Jvn) > 0 {
if len(d.Nvds) > 0 || len(d.Jvns) > 0 {
details = append(details, *d)
}
}
Expand Down
12 changes: 6 additions & 6 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,24 @@ func (f FetchMeta) OutDated() bool {
// CveDetail :
type CveDetail struct {
CveID string
Nvd []Nvd
Jvn []Jvn
Nvds []Nvd
Jvns []Jvn
}

// HasNvd returns true if NVD contents
func (c CveDetail) HasNvd() bool {
return len(c.Nvd) != 0
return len(c.Nvds) != 0
}

// HasJvn returns true if JVN contents
func (c CveDetail) HasJvn() bool {
return len(c.Jvn) != 0
return len(c.Jvns) != 0
}

// CpeDetail :
type CpeDetail struct {
Nvd []NvdCpe
Jvn []JvnCpe
Nvds []NvdCpe
Jvns []JvnCpe
}

// Components common to Nvd and Jvn
Expand Down
Loading