forked from goharbor/harbor
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store vulnerability report from scanner into a relational format
Convert vulnerability report JSON obtained from scanner into a relational format describe in:goharbor/community#145 Signed-off-by: prahaladdarkin <prahaladd@vmware.com>
- Loading branch information
1 parent
be4e6a5
commit 145cb5a
Showing
10 changed files
with
951 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// 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. | ||
|
||
package scanv2 | ||
|
||
// VulnerabilityRecord of an individual vulnerability. Identifies an individual vulnerability item in the scan. | ||
// Since multiple scanners could be registered with the projects, each scanner | ||
// would have it's own definition for the same CVE ID. Hence a CVE ID is qualified along | ||
// with the ID of the scanner that owns the CVE record definition. | ||
// The scanner ID would be the same as the RegistrationUUID field of Report. | ||
// Identified by the `cve_id` and `registration_uuid`. | ||
// Relates to the image using the `digest` and to the report using the `report UUID` field | ||
type VulnerabilityRecord struct { | ||
ID int64 `orm:"pk;auto;column(id)"` | ||
CVEID string `orm:"column(cve_id)"` | ||
RegistrationUUID string `orm:"column(registration_uuid)"` | ||
Digest string `orm:"column(digest)"` | ||
Report string `orm:"column(report_uuid)"` | ||
Package string `orm:"column(package)"` | ||
PackageVersion string `orm:"column(package_version)"` | ||
PackageType string `orm:"column(package_type)"` | ||
Severity string `orm:"column(severity)"` | ||
Fix string `orm:"column(fixed_version);null"` | ||
URL string `orm:"column(urls);null"` | ||
CVE3Score float64 `orm:"column(cve3_score);null"` | ||
CVE2Score float64 `orm:"column(cve2_score);null"` | ||
CVSS3Vector string `orm:"column(cvss3_vector);null"` //e.g. CVSS:3.0/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N | ||
CVSS2Vector string `orm:"column(cvss2_vector);null"` //e.g. AV:L/AC:M/Au:N/C:P/I:N/A:N | ||
Description string `orm:"column(description);null"` | ||
CWEIds string `orm:"column(cwe_ids);null"` //e.g. CWE-476,CWE-123,CWE-234 | ||
VendorAttributes string `orm:"column(vendorattributes);type(json);null"` | ||
} | ||
|
||
//ReportVulnerabilityRecord is relation table required to optimize data storage for both the | ||
//vulnerability records and the scan report. | ||
//identified by composite key (ID, Report) | ||
//Since each scan report has a separate UUID, the composite key | ||
//would ensure that the immutability of the historical scan reports is guaranteed. | ||
//It is sufficient to store the int64 VulnerabilityRecord Id since the vulnerability records | ||
//are uniquely identified in the table based on the ScannerID and the CVEID | ||
type ReportVulnerabilityRecord struct { | ||
ID int64 `orm:"pk;auto;column(id)"` | ||
Report string `orm:"column(report_uuid);"` | ||
VulnRecordID int64 `orm:"column(vuln_record_id);"` | ||
} | ||
|
||
//TableName for VulnerabilityRecord | ||
func (vr *VulnerabilityRecord) TableName() string { | ||
return "vulnerability_record_v2" | ||
} | ||
|
||
//TableUnique for VulnerabilityRecord | ||
func (vr *VulnerabilityRecord) TableUnique() [][]string { | ||
return [][]string{ | ||
{"cve_id", "registration_uuid", "package", "package_version", "digest"}, | ||
} | ||
} | ||
|
||
//TableName for ReportVulnerabilityRecord | ||
func (rvr *ReportVulnerabilityRecord) TableName() string { | ||
return "report_vulnerability_record_v2" | ||
} | ||
|
||
//TableUnique for ReportVulnerabilityRecord | ||
func (rvr *ReportVulnerabilityRecord) TableUnique() [][]string { | ||
return [][]string{ | ||
{"report_uuid", "vuln_record_id"}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// 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. | ||
|
||
package scanv2 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/astaxie/beego/orm" | ||
"github.com/goharbor/harbor/src/common/dao" | ||
"github.com/goharbor/harbor/src/lib/q" | ||
) | ||
|
||
func init() { | ||
orm.RegisterModel(new(VulnerabilityRecord), new(ReportVulnerabilityRecord)) | ||
} | ||
|
||
// CreateVulnerabilityRecord creates new vulnerability record. | ||
func CreateVulnerabilityRecord(vr *VulnerabilityRecord) (int64, error) { | ||
o := dao.GetOrmer() | ||
_, vrID, err := o.ReadOrCreate(vr, "CVEID", "RegistrationUUID", "Digest", "Package", "PackageVersion") | ||
|
||
return vrID, err | ||
} | ||
|
||
//DeleteVulnerabilityRecord deletes a vulnerability record | ||
func DeleteVulnerabilityRecord(vr *VulnerabilityRecord) error { | ||
o := dao.GetOrmer() | ||
_, err := o.Delete(vr, "CVEID", "RegistrationUUID") | ||
|
||
return err | ||
} | ||
|
||
// ListVulnerabilityRecords lists the vulnerability records with given query parameters. | ||
// Keywords in query here will be enforced with `exact` way. | ||
// If the registration ID (which = the scanner ID is not specified), the results | ||
// would contain duplicate records for a CVE depending upon the number of registered | ||
// scanners which individually store data about the CVE. In such cases, it is the | ||
// responsibility of the calling code to de-duplicate the CVE records or bucket them | ||
// per registered scanner | ||
func ListVulnerabilityRecords(query *q.Query) ([]*VulnerabilityRecord, error) { | ||
o := dao.GetOrmer() | ||
qt := o.QueryTable(new(VulnerabilityRecord)) | ||
|
||
if query != nil { | ||
if len(query.Keywords) > 0 { | ||
for k, v := range query.Keywords { | ||
if vv, ok := v.([]interface{}); ok { | ||
qt = qt.Filter(fmt.Sprintf("%s__in", k), vv...) | ||
continue | ||
} | ||
|
||
qt = qt.Filter(k, v) | ||
} | ||
} | ||
|
||
if query.PageNumber > 0 && query.PageSize > 0 { | ||
qt = qt.Limit(query.PageSize, (query.PageNumber-1)*query.PageSize) | ||
} | ||
} | ||
|
||
l := make([]*VulnerabilityRecord, 0) | ||
_, err := qt.All(&l) | ||
|
||
return l, err | ||
} | ||
|
||
//InsertVulnerabilityDataForReport inserts a vulnerability record in the context of scan report | ||
func InsertVulnerabilityDataForReport(reportUUID string, vr *VulnerabilityRecord) (int64, error) { | ||
|
||
vrID, err := CreateVulnerabilityRecord(vr) | ||
|
||
if err != nil { | ||
return vrID, err | ||
} | ||
|
||
rvr := new(ReportVulnerabilityRecord) | ||
rvr.Report = reportUUID | ||
rvr.VulnRecordID = vrID | ||
|
||
o := dao.GetOrmer() | ||
_, rvrID, err := o.ReadOrCreate(rvr, "report_uuid", "vuln_record_id") | ||
|
||
return rvrID, err | ||
|
||
} | ||
|
||
//DeleteAllVulnerabilityRecordsForReport deletes the vulnerability records for a single report | ||
func DeleteAllVulnerabilityRecordsForReport(reportUUID string) (int64, error) { | ||
o := dao.GetOrmer() | ||
delCount, err := o.Delete(&ReportVulnerabilityRecord{Report: reportUUID}, "report_uuid") | ||
return delCount, err | ||
} | ||
|
||
// GetAllVulnerabilityRecordsForReport gets all the vulnerability records for a report | ||
func GetAllVulnerabilityRecordsForReport(reportUUID string) ([]*VulnerabilityRecord, error) { | ||
vulnRecs := make([]*VulnerabilityRecord, 0) | ||
o := dao.GetOrmer() | ||
query := `select vulnerability_record_v2.* from vulnerability_record_v2 | ||
inner join report_vulnerability_record_v2 on | ||
vulnerability_record_v2.id = report_vulnerability_record_v2.vuln_record_id and report_vulnerability_record_v2.report_uuid=?` | ||
_, err := o.Raw(query, reportUUID).QueryRows(&vulnRecs) | ||
return vulnRecs, err | ||
} | ||
|
||
// GetVulnerabilityRecordsForScanner gets all the vulnerability records known to a scanner | ||
// identified by registrationUUID | ||
func GetVulnerabilityRecordsForScanner(registrationUUID string) ([]*VulnerabilityRecord, error) { | ||
var vulnRecs []*VulnerabilityRecord | ||
o := dao.GetOrmer() | ||
vulRec := new(VulnerabilityRecord) | ||
qs := o.QueryTable(vulRec) | ||
_, err := qs.Filter("registration_uuid", registrationUUID).All(&vulnRecs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return vulnRecs, nil | ||
} | ||
|
||
// DeleteVulnerabilityRecordsForScanner deletes all the vulnerability records for a given scanner | ||
// identified by registrationUUID | ||
func DeleteVulnerabilityRecordsForScanner(registrationUUID string) (int64, error) { | ||
o := dao.GetOrmer() | ||
vulnRec := new(VulnerabilityRecord) | ||
vulnRec.RegistrationUUID = registrationUUID | ||
return o.Delete(vulnRec, "registration_uuid") | ||
} | ||
|
||
// GetVulnerabilityRecordIdsForScanner retrieves the internal Ids of the vulnerability records for a given scanner | ||
// identified by registrationUUID | ||
func GetVulnerabilityRecordIdsForScanner(registrationUUID string) ([]int, error) { | ||
vulnRecordIds := make([]int, 0) | ||
o := dao.GetOrmer() | ||
_, err := o.Raw("select id from vulnerability_record_v2 where registration_uuid = ?", registrationUUID).QueryRows(&vulnRecordIds) | ||
if err != nil { | ||
return vulnRecordIds, err | ||
} | ||
return vulnRecordIds, err | ||
} |
Oops, something went wrong.