From 740781af56a5e7e957e8d95e1f8e9bd850eb8f50 Mon Sep 17 00:00:00 2001 From: Kota Kanbe Date: Mon, 5 Apr 2021 17:41:07 +0900 Subject: [PATCH] feat(logging): add -log-to-file and don't output to file by default (#1209) * feat(logging): add -log-to-file and don't output to file by default * update go-cve-dict * fix lint err --- constant/constant.go | 2 +- detector/cve_client.go | 2 +- go.mod | 11 ++++--- go.sum | 26 +++++++++------- logging/logutil.go | 68 ++++++++++++++++++++++-------------------- models/scanresults.go | 1 + oval/debian.go | 2 +- oval/oval.go | 4 +-- scanner/serverapi.go | 22 +++++--------- subcmds/configtest.go | 4 ++- subcmds/discover.go | 2 +- subcmds/report.go | 4 ++- subcmds/saas.go | 4 ++- subcmds/scan.go | 5 +++- subcmds/server.go | 4 ++- subcmds/tui.go | 4 ++- 16 files changed, 91 insertions(+), 74 deletions(-) diff --git a/constant/constant.go b/constant/constant.go index d552cb47..f394aad3 100644 --- a/constant/constant.go +++ b/constant/constant.go @@ -56,6 +56,6 @@ const ( // ServerTypePseudo is used for ServerInfo.Type, r.Family ServerTypePseudo = "pseudo" - // DeepSecurity + // DeepSecurity is DeepSecurity = "deepsecurity" ) diff --git a/detector/cve_client.go b/detector/cve_client.go index d0d2e4da..f29b110b 100644 --- a/detector/cve_client.go +++ b/detector/cve_client.go @@ -26,7 +26,7 @@ type goCveDictClient struct { } func newGoCveDictClient(cnf config.VulnDictInterface, o logging.LogOpts) (*goCveDictClient, error) { - cvelog.SetLogger(o.LogDir, o.Quiet, o.Debug, false) + cvelog.SetLogger(o.Debug, o.Quiet, false, o.LogToFile, o.LogDir) driver, locked, err := newCveDB(cnf) if locked { diff --git a/go.mod b/go.mod index b27e5822..8657a78c 100644 --- a/go.mod +++ b/go.mod @@ -15,10 +15,11 @@ require ( github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 github.com/emersion/go-smtp v0.14.0 + github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/google/subcommands v1.2.0 github.com/gosuri/uitable v0.0.4 github.com/hashicorp/go-uuid v1.0.2 - github.com/hashicorp/go-version v1.2.1 + github.com/hashicorp/go-version v1.3.0 github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c github.com/jesseduffield/gocui v0.3.0 github.com/k0kubun/pp v3.0.1+incompatible @@ -27,12 +28,13 @@ require ( github.com/knqyf263/go-deb-version v0.0.0-20190517075300-09fca494f03d github.com/knqyf263/go-rpm-version v0.0.0-20170716094938-74609b86c936 github.com/knqyf263/gost v0.1.10 - github.com/kotakanbe/go-cve-dictionary v0.5.10 + github.com/kotakanbe/go-cve-dictionary v0.5.12 github.com/kotakanbe/go-pingscanner v0.1.0 github.com/kotakanbe/goval-dictionary v0.3.3 github.com/kotakanbe/logrus-prefixed-formatter v0.0.0-20180123152602-928f7356cb96 github.com/lib/pq v1.10.0 // indirect github.com/magiconair/properties v1.8.4 // indirect + github.com/mattn/go-runewidth v0.0.12 // indirect github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.4.1 // indirect @@ -51,9 +53,10 @@ require ( github.com/vulsio/go-exploitdb v0.1.7 golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect - golang.org/x/net v0.0.0-20210323141857-08027d57d8cf // indirect + golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c // indirect golang.org/x/oauth2 v0.0.0-20210125201302-af13f521f196 - golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect + golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect + golang.org/x/text v0.3.6 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 gopkg.in/ini.v1 v1.62.0 // indirect k8s.io/utils v0.0.0-20210111153108-fddb29f9d009 diff --git a/go.sum b/go.sum index 7c20c01f..e9e1cf10 100644 --- a/go.sum +++ b/go.sum @@ -274,8 +274,9 @@ github.com/go-redis/redis/v8 v8.8.0 h1:fDZP58UN/1RD3DjtTXP/fFZ04TFohSYhjZDkcDe2d github.com/go-redis/redis/v8 v8.8.0/go.mod h1:F7resOH5Kdug49Otu24RjHWwgK7u9AmtqWMnCV1iP5Y= github.com/go-restruct/restruct v0.0.0-20191227155143-5734170a48a1/go.mod h1:KqrpKpn4M8OLznErihXTGLlsXFGeLxHUrLRRI/1YjGk= github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= -github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= @@ -401,8 +402,9 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE= github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= -github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw= +github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= @@ -474,8 +476,8 @@ github.com/knqyf263/gost v0.1.10/go.mod h1:Vs6YmVm5GdkVUc9uu8VEGaOdhMKk+0lf7rZAL github.com/knqyf263/nested v0.0.1/go.mod h1:zwhsIhMkBg90DTOJQvxPkKIypEHPYkgWHs4gybdlUmk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kotakanbe/go-cve-dictionary v0.5.10 h1:20d+JF9pgbURTL8G9pRinqYarLO/Cf0cBvZYzCbFHic= -github.com/kotakanbe/go-cve-dictionary v0.5.10/go.mod h1:E/CipfNDV31W39MH9QtgkA12JFBMe/3CELxXVvPRphQ= +github.com/kotakanbe/go-cve-dictionary v0.5.12 h1:pJZsty83I/kSO5cmU24xWqeBYOrtiTSQ0c6T/6GVOhA= +github.com/kotakanbe/go-cve-dictionary v0.5.12/go.mod h1:E/CipfNDV31W39MH9QtgkA12JFBMe/3CELxXVvPRphQ= github.com/kotakanbe/go-pingscanner v0.1.0 h1:VG4/9l0i8WeToXclj7bIGoAZAu7a07Z3qmQiIfU0gT0= github.com/kotakanbe/go-pingscanner v0.1.0/go.mod h1:/761QZzuZFcfN8h/1QuawUA+pKukp3qcNj5mxJCOiAk= github.com/kotakanbe/goval-dictionary v0.3.3 h1:b9o1ZPFGmk0WZV0nnk4mWl5vTKINequ+QVW6w9m+8RI= @@ -536,8 +538,9 @@ github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg= github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= @@ -900,8 +903,8 @@ golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210323141857-08027d57d8cf h1:sewfyKLWuY3ko6EI4hbFziQ8bHkfammpzCDfLT92I1c= -golang.org/x/net v0.0.0-20210323141857-08027d57d8cf/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= +golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c h1:KHUzaHIpjWVlVVNh65G3hhuj3KB1HnjY6Cq5cTvRQT8= +golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -980,9 +983,9 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc= -golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57iKi3297KFs0QDbGN60A= +golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -992,8 +995,9 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/logging/logutil.go b/logging/logutil.go index 60e41fda..2de1a09d 100644 --- a/logging/logutil.go +++ b/logging/logutil.go @@ -3,6 +3,7 @@ package logging import ( "flag" "fmt" + "io" "io/ioutil" "os" "path/filepath" @@ -17,10 +18,11 @@ import ( //LogOpts has options for logging type LogOpts struct { - Debug bool `json:"debug,omitempty"` - DebugSQL bool `json:"debugSQL,omitempty"` - LogDir string `json:"logDir,omitempty"` - Quiet bool `json:"quiet,omitempty"` + Debug bool `json:"debug,omitempty"` + DebugSQL bool `json:"debugSQL,omitempty"` + LogToFile bool `json:"logToFile,omitempty"` + LogDir string `json:"logDir,omitempty"` + Quiet bool `json:"quiet,omitempty"` } // Log for localhost @@ -44,7 +46,7 @@ func NewNormalLogger() Logger { } // NewCustomLogger creates logrus -func NewCustomLogger(debug, quiet bool, logDir, logMsgAnsiColor, serverName string) Logger { +func NewCustomLogger(debug, quiet, logToFile bool, logDir, logMsgAnsiColor, serverName string) Logger { log := logrus.New() log.Formatter = &formatter.TextFormatter{MsgAnsiColor: logMsgAnsiColor} log.Level = logrus.InfoLevel @@ -57,14 +59,17 @@ func NewCustomLogger(debug, quiet bool, logDir, logMsgAnsiColor, serverName stri return Logger{Entry: *logrus.NewEntry(log)} } - // File output - dir := GetDefaultLogDir() - if logDir != "" { - dir = logDir + whereami := "localhost" + if serverName != "" { + whereami = serverName } - // Only log to a file if quiet mode enabled - if quiet && flag.Lookup("test.v") == nil { + if logToFile { + dir := GetDefaultLogDir() + if logDir != "" { + dir = logDir + } + if _, err := os.Stat(dir); os.IsNotExist(err) { if err := os.Mkdir(dir, 0700); err != nil { log.Errorf("Failed to create log directory. path: %s, err: %+v", dir, err) @@ -73,36 +78,33 @@ func NewCustomLogger(debug, quiet bool, logDir, logMsgAnsiColor, serverName stri logFile := dir + "/vuls.log" if file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644); err == nil { - log.Out = file + log.Out = io.MultiWriter(os.Stderr, file) } else { log.Out = os.Stderr log.Errorf("Failed to create log file. path: %s, err: %+v", logFile, err) } + + if _, err := os.Stat(dir); err == nil { + path := filepath.Join(dir, fmt.Sprintf("%s.log", whereami)) + if _, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644); err == nil { + log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{ + logrus.DebugLevel: path, + logrus.InfoLevel: path, + logrus.WarnLevel: path, + logrus.ErrorLevel: path, + logrus.FatalLevel: path, + logrus.PanicLevel: path, + }, nil)) + } else { + log.Errorf("Failed to create log file. path: %s, err: %+v", path, err) + } + } + } else if quiet { + log.Out = io.Discard } else { log.Out = os.Stderr } - whereami := "localhost" - if 0 < len(serverName) { - whereami = serverName - } - - if _, err := os.Stat(dir); err == nil { - path := filepath.Join(dir, fmt.Sprintf("%s.log", whereami)) - if _, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644); err == nil { - log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{ - logrus.DebugLevel: path, - logrus.InfoLevel: path, - logrus.WarnLevel: path, - logrus.ErrorLevel: path, - logrus.FatalLevel: path, - logrus.PanicLevel: path, - }, nil)) - } else { - log.Errorf("Failed to create log file. path: %s, err: %+v", path, err) - } - } - entry := log.WithFields(logrus.Fields{"prefix": whereami}) return Logger{Entry: *entry} } diff --git a/models/scanresults.go b/models/scanresults.go index 932a088b..23b94576 100644 --- a/models/scanresults.go +++ b/models/scanresults.go @@ -375,6 +375,7 @@ func (r *ScanResult) CheckEOL() { } } +// SortForJSONOutput sort list elements in the ScanResult to diff in integration-test func (r *ScanResult) SortForJSONOutput() { for k, v := range r.Packages { sort.SliceStable(v.AffectedProcs, func(i, j int) bool { diff --git a/oval/debian.go b/oval/debian.go index 23cd18a0..c843fb2d 100644 --- a/oval/debian.go +++ b/oval/debian.go @@ -439,7 +439,7 @@ func (o Ubuntu) fillWithOval(r *models.ScanResult, kernelNamesInOval []string) ( } defer func() { if err := driver.CloseDB(); err != nil { - logging.Log.Errorf("Failed to close DB. err: %+v") + logging.Log.Errorf("Failed to close DB. err: %+v", err) } }() diff --git a/oval/oval.go b/oval/oval.go index ae64ec4f..efe3f388 100644 --- a/oval/oval.go +++ b/oval/oval.go @@ -41,7 +41,7 @@ func (b Base) CheckIfOvalFetched(osFamily, release string) (fetched bool, err er } defer func() { if err := driver.CloseDB(); err != nil { - logging.Log.Errorf("Failed to close DB. err: %+v") + logging.Log.Errorf("Failed to close DB. err: %+v", err) } }() @@ -80,7 +80,7 @@ func (b Base) CheckIfOvalFresh(osFamily, release string) (ok bool, err error) { } defer func() { if err := driver.CloseDB(); err != nil { - logging.Log.Errorf("Failed to close DB. err: %+v") + logging.Log.Errorf("Failed to close DB. err: %+v", err) } }() lastModified = driver.GetLastModified(ovalFamily, release) diff --git a/scanner/serverapi.go b/scanner/serverapi.go index d3b9740c..b9e3c50f 100644 --- a/scanner/serverapi.go +++ b/scanner/serverapi.go @@ -71,6 +71,7 @@ type Scanner struct { ScanTimeoutSec int CacheDBPath string Debug bool + LogToFile bool LogDir string Quiet bool DetectIPS bool @@ -198,6 +199,7 @@ func ViaHTTP(header http.Header, body string, toLocalFile bool) (models.ScanResu }, nil } +// ParseInstalledPkgs parses installed pkgs line func ParseInstalledPkgs(distro config.Distro, kernel models.Kernel, pkgList string) (models.Packages, models.SrcPackages, error) { base := base{ Distro: distro, @@ -212,21 +214,13 @@ func ParseInstalledPkgs(distro config.Distro, kernel models.Kernel, pkgList stri case constant.Debian, constant.Ubuntu: osType = &debian{base: base} case constant.RedHat: - osType = &rhel{ - redhatBase: redhatBase{base: base}, - } + osType = &rhel{redhatBase: redhatBase{base: base}} case constant.CentOS: - osType = ¢os{ - redhatBase: redhatBase{base: base}, - } + osType = ¢os{redhatBase: redhatBase{base: base}} case constant.Oracle: - osType = &oracle{ - redhatBase: redhatBase{base: base}, - } + osType = &oracle{redhatBase: redhatBase{base: base}} case constant.Amazon: - osType = &amazon{ - redhatBase: redhatBase{base: base}, - } + osType = &amazon{redhatBase: redhatBase{base: base}} default: return models.Packages{}, models.SrcPackages{}, xerrors.Errorf("Server mode for %s is not implemented yet", base.Distro.Family) } @@ -244,12 +238,12 @@ func (s Scanner) initServers() error { // to generate random color for logging rand.Seed(time.Now().UnixNano()) for _, srv := range hosts { - srv.setLogger(logging.NewCustomLogger(s.Debug, s.Quiet, s.LogDir, config.Colors[rand.Intn(len(config.Colors))], srv.getServerInfo().GetServerName())) + srv.setLogger(logging.NewCustomLogger(s.Debug, s.Quiet, s.LogToFile, s.LogDir, config.Colors[rand.Intn(len(config.Colors))], srv.getServerInfo().GetServerName())) } containers, errContainers := s.detectContainerOSes(hosts) for _, srv := range containers { - srv.setLogger(logging.NewCustomLogger(s.Debug, s.Quiet, s.LogDir, config.Colors[rand.Intn(len(config.Colors))], srv.getServerInfo().GetServerName())) + srv.setLogger(logging.NewCustomLogger(s.Debug, s.Quiet, s.LogToFile, s.LogDir, config.Colors[rand.Intn(len(config.Colors))], srv.getServerInfo().GetServerName())) } // set to pkg global variable diff --git a/subcmds/configtest.go b/subcmds/configtest.go index 53f64f80..9348600e 100644 --- a/subcmds/configtest.go +++ b/subcmds/configtest.go @@ -33,6 +33,7 @@ func (*ConfigtestCmd) Usage() string { return `configtest: configtest [-config=/path/to/config.toml] + [-log-to-file] [-log-dir=/path/to/log] [-ask-key-password] [-timeout=300] @@ -53,6 +54,7 @@ func (p *ConfigtestCmd) SetFlags(f *flag.FlagSet) { defaultLogDir := logging.GetDefaultLogDir() f.StringVar(&config.Conf.LogDir, "log-dir", defaultLogDir, "/path/to/log") + f.BoolVar(&config.Conf.LogToFile, "log-to-file", false, "output log to file") f.BoolVar(&config.Conf.Debug, "debug", false, "debug mode") f.IntVar(&p.timeoutSec, "timeout", 5*60, "Timeout(Sec)") @@ -69,7 +71,7 @@ func (p *ConfigtestCmd) SetFlags(f *flag.FlagSet) { // Execute execute func (p *ConfigtestCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogDir, "", "") + logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogToFile, config.Conf.LogDir, "", "") logging.Log.Infof("vuls-%s-%s", config.Version, config.Revision) if err := mkdirDotVuls(); err != nil { diff --git a/subcmds/discover.go b/subcmds/discover.go index eead4f5f..1ae0804a 100644 --- a/subcmds/discover.go +++ b/subcmds/discover.go @@ -39,7 +39,7 @@ func (p *DiscoverCmd) SetFlags(f *flag.FlagSet) { // Execute execute func (p *DiscoverCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogDir, "", "") + logging.Log = logging.NewCustomLogger(false, false, false, config.Conf.LogDir, "", "") logging.Log.Infof("vuls-%s-%s", config.Version, config.Revision) // validate if len(f.Args()) == 0 { diff --git a/subcmds/report.go b/subcmds/report.go index 430034a5..3bcef449 100644 --- a/subcmds/report.go +++ b/subcmds/report.go @@ -54,6 +54,7 @@ func (*ReportCmd) Usage() string { [-lang=en|ja] [-config=/path/to/config.toml] [-results-dir=/path/to/results] + [-log-to-file] [-log-dir=/path/to/log] [-refresh-cve] [-cvss-over=7] @@ -107,6 +108,7 @@ func (p *ReportCmd) SetFlags(f *flag.FlagSet) { defaultLogDir := logging.GetDefaultLogDir() f.StringVar(&config.Conf.LogDir, "log-dir", defaultLogDir, "/path/to/log") + f.BoolVar(&config.Conf.LogToFile, "log-to-file", false, "Output log to file") f.BoolVar(&config.Conf.RefreshCve, "refresh-cve", false, "Refresh CVE information in JSON file under results dir") @@ -166,7 +168,7 @@ func (p *ReportCmd) SetFlags(f *flag.FlagSet) { // Execute execute func (p *ReportCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogDir, "", "") + logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogToFile, config.Conf.LogDir, "", "") logging.Log.Infof("vuls-%s-%s", config.Version, config.Revision) if err := config.Load(p.configPath, ""); err != nil { diff --git a/subcmds/saas.go b/subcmds/saas.go index bf08ae6f..fd53ab61 100644 --- a/subcmds/saas.go +++ b/subcmds/saas.go @@ -32,6 +32,7 @@ func (*SaaSCmd) Usage() string { saas [-config=/path/to/config.toml] [-results-dir=/path/to/results] + [-log-to-file] [-log-dir=/path/to/log] [-http-proxy=http://192.168.0.1:8080] [-debug] @@ -53,6 +54,7 @@ func (p *SaaSCmd) SetFlags(f *flag.FlagSet) { defaultLogDir := logging.GetDefaultLogDir() f.StringVar(&config.Conf.LogDir, "log-dir", defaultLogDir, "/path/to/log") + f.BoolVar(&config.Conf.LogToFile, "log-to-file", false, "Output log to file") f.StringVar( &config.Conf.HTTPProxy, "http-proxy", "", @@ -61,7 +63,7 @@ func (p *SaaSCmd) SetFlags(f *flag.FlagSet) { // Execute execute func (p *SaaSCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogDir, "", "") + logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogToFile, config.Conf.LogDir, "", "") logging.Log.Infof("vuls-%s-%s", config.Version, config.Revision) if err := config.Load(p.configPath, ""); err != nil { logging.Log.Errorf("Error loading %s, %+v", p.configPath, err) diff --git a/subcmds/scan.go b/subcmds/scan.go index 453a85af..0e34f485 100644 --- a/subcmds/scan.go +++ b/subcmds/scan.go @@ -39,6 +39,7 @@ func (*ScanCmd) Usage() string { scan [-config=/path/to/config.toml] [-results-dir=/path/to/results] + [-log-to-file] [-log-dir=/path/to/log] [-cachedb-path=/path/to/cache.db] [-http-proxy=http://192.168.0.1:8080] @@ -70,6 +71,7 @@ func (p *ScanCmd) SetFlags(f *flag.FlagSet) { defaultLogDir := logging.GetDefaultLogDir() f.StringVar(&config.Conf.LogDir, "log-dir", defaultLogDir, "/path/to/log") + f.BoolVar(&config.Conf.LogToFile, "log-to-file", false, "Output log to file") defaultCacheDBPath := filepath.Join(wd, "cache.db") f.StringVar(&p.cacheDBPath, "cachedb-path", defaultCacheDBPath, @@ -98,7 +100,7 @@ func (p *ScanCmd) SetFlags(f *flag.FlagSet) { // Execute execute func (p *ScanCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogDir, "", "") + logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogToFile, config.Conf.LogDir, "", "") logging.Log.Infof("vuls-%s-%s", config.Version, config.Revision) if err := mkdirDotVuls(); err != nil { @@ -190,6 +192,7 @@ func (p *ScanCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) Targets: targets, Debug: config.Conf.Debug, Quiet: config.Conf.Quiet, + LogToFile: config.Conf.LogToFile, LogDir: config.Conf.LogDir, DetectIPS: p.detectIPS, } diff --git a/subcmds/server.go b/subcmds/server.go index 767a4ad5..48664d33 100644 --- a/subcmds/server.go +++ b/subcmds/server.go @@ -35,6 +35,7 @@ func (*ServerCmd) Usage() string { Server [-lang=en|ja] [-config=/path/to/config.toml] + [-log-to-file] [-log-dir=/path/to/log] [-cvss-over=7] [-ignore-unscored-cves] @@ -64,6 +65,7 @@ func (p *ServerCmd) SetFlags(f *flag.FlagSet) { defaultLogDir := logging.GetDefaultLogDir() f.StringVar(&config.Conf.LogDir, "log-dir", defaultLogDir, "/path/to/log") + f.BoolVar(&config.Conf.LogToFile, "log-to-file", false, "Output log to file") f.Float64Var(&config.Conf.CvssScoreOver, "cvss-over", 0, "-cvss-over=6.5 means Servering CVSS Score 6.5 and over (default: 0 (means Server all))") @@ -84,7 +86,7 @@ func (p *ServerCmd) SetFlags(f *flag.FlagSet) { // Execute execute func (p *ServerCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogDir, "", "") + logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogToFile, config.Conf.LogDir, "", "") logging.Log.Infof("vuls-%s-%s", config.Version, config.Revision) if err := config.Load(p.configPath, ""); err != nil { logging.Log.Errorf("Error loading %s. err: %+v", p.configPath, err) diff --git a/subcmds/tui.go b/subcmds/tui.go index 31eed9e8..52328509 100644 --- a/subcmds/tui.go +++ b/subcmds/tui.go @@ -42,6 +42,7 @@ func (*TuiCmd) Usage() string { [-ignore-unscored-cves] [-ignore-unfixed] [-results-dir=/path/to/results] + [-log-to-file] [-log-dir=/path/to/log] [-debug] [-debug-sql] @@ -63,6 +64,7 @@ func (p *TuiCmd) SetFlags(f *flag.FlagSet) { defaultLogDir := logging.GetDefaultLogDir() f.StringVar(&config.Conf.LogDir, "log-dir", defaultLogDir, "/path/to/log") + f.BoolVar(&config.Conf.LogToFile, "log-to-file", false, "Output log to file") wd, _ := os.Getwd() defaultResultsDir := filepath.Join(wd, "results") @@ -101,7 +103,7 @@ func (p *TuiCmd) SetFlags(f *flag.FlagSet) { // Execute execute func (p *TuiCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogDir, "", "") + logging.Log = logging.NewCustomLogger(config.Conf.Debug, config.Conf.Quiet, config.Conf.LogToFile, config.Conf.LogDir, "", "") logging.Log.Infof("vuls-%s-%s", config.Version, config.Revision) if err := config.Load(p.configPath, ""); err != nil { logging.Log.Errorf("Error loading %s, err: %+v", p.configPath, err)