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

feat(diff): support <date> <date> for input #1175

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 23 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,29 @@ type Config struct {
Saas SaasConf `json:"-"`
DetectIPS bool `json:"detectIps,omitempty"`

RefreshCve bool `json:"refreshCve,omitempty"`
ToSlack bool `json:"toSlack,omitempty"`
ToChatWork bool `json:"toChatWork,omitempty"`
ToTelegram bool `json:"ToTelegram,omitempty"`
ToEmail bool `json:"toEmail,omitempty"`
ToSyslog bool `json:"toSyslog,omitempty"`
ToLocalFile bool `json:"toLocalFile,omitempty"`
ToS3 bool `json:"toS3,omitempty"`
ToAzureBlob bool `json:"toAzureBlob,omitempty"`
ToHTTP bool `json:"toHTTP,omitempty"`
FormatJSON bool `json:"formatJSON,omitempty"`
FormatOneEMail bool `json:"formatOneEMail,omitempty"`
FormatOneLineText bool `json:"formatOneLineText,omitempty"`
FormatList bool `json:"formatList,omitempty"`
FormatFullText bool `json:"formatFullText,omitempty"`
FormatCsvList bool `json:"formatCsvList,omitempty"`
GZIP bool `json:"gzip,omitempty"`
DiffPlus bool `json:"diffPlus,omitempty"`
DiffMinus bool `json:"diffMinus,omitempty"`
Diff bool `json:"diff,omitempty"`
RefreshCve bool `json:"refreshCve,omitempty"`
ToSlack bool `json:"toSlack,omitempty"`
ToChatWork bool `json:"toChatWork,omitempty"`
ToTelegram bool `json:"ToTelegram,omitempty"`
ToEmail bool `json:"toEmail,omitempty"`
ToSyslog bool `json:"toSyslog,omitempty"`
ToLocalFile bool `json:"toLocalFile,omitempty"`
ToS3 bool `json:"toS3,omitempty"`
ToAzureBlob bool `json:"toAzureBlob,omitempty"`
ToHTTP bool `json:"toHTTP,omitempty"`
FormatJSON bool `json:"formatJSON,omitempty"`
FormatOneEMail bool `json:"formatOneEMail,omitempty"`
FormatOneLineText bool `json:"formatOneLineText,omitempty"`
FormatList bool `json:"formatList,omitempty"`
FormatFullText bool `json:"formatFullText,omitempty"`
FormatCsvList bool `json:"formatCsvList,omitempty"`
GZIP bool `json:"gzip,omitempty"`
DiffPlus bool `json:"diffPlus,omitempty"`
DiffMinus bool `json:"diffMinus,omitempty"`
Diff bool `json:"diff,omitempty"`
Date string `json:"date,omitempty"`
BeforeDate string `json:"before,omitempty"`
AfterDate string `json:"after,omitempty"`
}

// ValidateOnConfigtest validates
Expand Down
8 changes: 7 additions & 1 deletion report/localfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ func (w LocalFileWriter) Write(rs ...models.ScanResult) (err error) {
}

for _, r := range rs {
path := filepath.Join(w.CurrentDir, r.ReportFileName())
var path string
if (c.Conf.DiffPlus || c.Conf.DiffMinus) && c.Conf.Date != "" {
// path format is path/to/results/servername_date
path = filepath.Join(filepath.Join(c.Conf.ResultsDir, c.Conf.AfterDate), r.ReportFileName()+"_"+c.Conf.BeforeDate)
} else {
path = filepath.Join(w.CurrentDir, r.ReportFileName())
}

if c.Conf.FormatJSON {
p := path + ".json"
Expand Down
9 changes: 8 additions & 1 deletion report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package report

import (
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -122,7 +123,13 @@ func FillCveInfos(dbclient DBClient, rs []models.ScanResult, dir string) ([]mode
}

if c.Conf.DiffPlus || c.Conf.DiffMinus {
prevs, err := loadPrevious(rs)
var prevs []models.ScanResult
var err error
if c.Conf.Date == "" {
prevs, err = loadPrevious(rs)
} else {
prevs, err = LoadScanResults(filepath.Join(c.Conf.ResultsDir, c.Conf.AfterDate))
}
if err != nil {
return nil, err
}
Expand Down
18 changes: 15 additions & 3 deletions report/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/future-architect/vuls/config"
c "github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
"github.com/gosuri/uitable"
Expand Down Expand Up @@ -607,7 +608,7 @@ func getPlusDiffCves(previous, current models.ScanResult) models.VulnInfos {
}

if len(updated) == 0 && len(new) == 0 {
util.Log.Infof("%s: There are %d vulnerabilities, but no difference between current result and previous one.", current.FormatServerName(), len(current.ScannedCves))
util.Log.Infof("%s: There are %d vulnerabilities, but no plus difference between before result and after one.", current.FormatServerName(), len(current.ScannedCves))
}

for cveID, vuln := range new {
Expand All @@ -631,7 +632,7 @@ func getMinusDiffCves(previous, current models.ScanResult) models.VulnInfos {
}
}
if len(clear) == 0 {
util.Log.Infof("%s: There are %d vulnerabilities, but no difference between current result and previous one.", current.FormatServerName(), len(current.ScannedCves))
util.Log.Infof("%s: There are %d vulnerabilities, but no minus difference between before result and after one.", current.FormatServerName(), len(current.ScannedCves))
}

return clear
Expand Down Expand Up @@ -764,7 +765,18 @@ func JSONDir(args []string) (string, error) {
return "", xerrors.Errorf("No results under %s",
config.Conf.ResultsDir)
}
return dirs[0], nil

if c.Conf.Date == "" {
return dirs[0], nil
}

for _, s := range dirs {
if s == filepath.Join(c.Conf.ResultsDir, c.Conf.BeforeDate) {
return s, nil
}
}
return "", xerrors.Errorf("No results under %s",
c.Conf.Date)
}

// LoadScanResults read JSON data
Expand Down
11 changes: 11 additions & 0 deletions subcmds/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"flag"
"os"
"path/filepath"
"strings"

"github.com/aquasecurity/trivy/pkg/utils"
"github.com/future-architect/vuls/config"
Expand Down Expand Up @@ -40,6 +41,7 @@ func (*ReportCmd) Usage() string {
[-log-dir=/path/to/log]
[-refresh-cve]
[-cvss-over=7]
[-date]
[-diff]
[-diff-minus]
[-diff-plus]
Expand Down Expand Up @@ -97,6 +99,9 @@ func (p *ReportCmd) SetFlags(f *flag.FlagSet) {
f.Float64Var(&c.Conf.CvssScoreOver, "cvss-over", 0,
"-cvss-over=6.5 means reporting CVSS Score 6.5 and over (default: 0 (means report all))")

f.StringVar(&c.Conf.Date, "date", "",
"Date for -diff, -diff-plus and -diff-minus option")

f.BoolVar(&c.Conf.DiffMinus, "diff-minus", false,
"Minus Difference between previous result and current result")

Expand Down Expand Up @@ -164,6 +169,12 @@ func (p *ReportCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}
c.Conf.DiffMinus = true
}

if c.Conf.Date != "" {
dates := strings.Split(c.Conf.Date, ",")
c.Conf.BeforeDate = dates[0]
c.Conf.AfterDate = dates[1]
}

var dir string
var err error
if c.Conf.DiffPlus || c.Conf.DiffMinus {
Expand Down