Refactor logger (#1185)

* refactor: logger

* refactor: logging

* refactor: rename func

* refactor: logging

* refactor: logging format
This commit is contained in:
Kota Kanbe
2021-02-26 10:36:58 +09:00
committed by GitHub
parent 518f4dc039
commit 3f2ac45d71
58 changed files with 455 additions and 408 deletions

View File

@@ -6,6 +6,7 @@ import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
"golang.org/x/xerrors"
@@ -26,7 +27,7 @@ func newAlpine(c config.ServerInfo) *alpine {
},
},
}
d.log = util.NewCustomLogger(c)
d.log = logging.NewNormalLogger()
d.setServerInfo(c)
return d
}

View File

@@ -2,8 +2,8 @@ package scanner
import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
"golang.org/x/xerrors"
)
@@ -25,7 +25,7 @@ func newAmazon(c config.ServerInfo) *amazon {
sudo: rootPrivAmazon{},
},
}
r.log = util.NewCustomLogger(c)
r.log = logging.NewNormalLogger()
r.setServerInfo(c)
return r
}

View File

@@ -15,9 +15,9 @@ import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
"github.com/sirupsen/logrus"
"golang.org/x/xerrors"
// Import library scanner
@@ -38,7 +38,7 @@ type base struct {
LibraryScanners []models.LibraryScanner
WordPress models.WordPressPackages
log *logrus.Entry
log logging.Logger
errs []error
warns []error
}
@@ -484,6 +484,10 @@ func (l *base) getErrs() []error {
return l.errs
}
func (l *base) setLogger(logger logging.Logger) {
l.log = logger
}
const (
systemd = "systemd"
upstart = "upstart"
@@ -950,19 +954,19 @@ func (l *base) pkgPs(getOwnerPkgs func([]string) ([]string, error)) error {
stdout := ""
stdout, err = l.lsProcExe(pid)
if err != nil {
l.log.Debugf("Failed to exec ls -l /proc/%s/exe err: %s", pid, err)
l.log.Debugf("Failed to exec ls -l /proc/%s/exe err: %+v", pid, err)
continue
}
s, err := l.parseLsProcExe(stdout)
if err != nil {
l.log.Debugf("Failed to parse /proc/%s/exe: %s", pid, err)
l.log.Debugf("Failed to parse /proc/%s/exe: %+v", pid, err)
continue
}
pidLoadedFiles[pid] = append(pidLoadedFiles[pid], s)
stdout, err = l.grepProcMap(pid)
if err != nil {
l.log.Debugf("Failed to exec /proc/%s/maps: %s", pid, err)
l.log.Debugf("Failed to exec /proc/%s/maps: %+v", pid, err)
continue
}
ss := l.parseGrepProcMap(stdout)

View File

@@ -39,7 +39,7 @@ f570ae647edc agitated_lovelace centos:latest`,
r := newRHEL(config.ServerInfo{})
actual, err := r.parseDockerPs(test.in)
if err != nil {
t.Errorf("Error occurred. in: %s, err: %s", test.in, err)
t.Errorf("Error occurred. in: %s, err: %+v", test.in, err)
return
}
for i, e := range test.expected {
@@ -76,7 +76,7 @@ func TestParseLxdPs(t *testing.T) {
r := newRHEL(config.ServerInfo{})
actual, err := r.parseLxdPs(test.in)
if err != nil {
t.Errorf("Error occurred. in: %s, err: %s", test.in, err)
t.Errorf("Error occurred. in: %s, err: %+v", test.in, err)
return
}
for i, e := range test.expected {

View File

@@ -2,8 +2,8 @@ package scanner
import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
)
// inherit OsTypeInterface
@@ -24,7 +24,7 @@ func newCentOS(c config.ServerInfo) *centos {
sudo: rootPrivCentos{},
},
}
r.log = util.NewCustomLogger(c)
r.log = logging.NewNormalLogger()
r.setServerInfo(c)
return r
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/future-architect/vuls/cache"
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
version "github.com/knqyf263/go-deb-version"
@@ -34,7 +35,7 @@ func newDebian(c config.ServerInfo) *debian {
},
},
}
d.log = util.NewCustomLogger(c)
d.log = logging.NewNormalLogger()
d.setServerInfo(c)
return d
}
@@ -49,7 +50,7 @@ func detectDebian(c config.ServerInfo) (bool, osTypeInterface, error) {
if r.ExitStatus == 255 {
return false, nil, xerrors.Errorf("Unable to connect via SSH. Scan with -vvv option to print SSH debugging messages and check SSH settings. If you have never SSH to the host to be scanned, SSH to the host before scanning in order to add the HostKey. %s@%s port: %s\n%s", c.User, c.Host, c.Port, r)
}
util.Log.Debugf("Not Debian like Linux. %s", r)
logging.Log.Debugf("Not Debian like Linux. %s", r)
return false, nil, nil
}
@@ -78,7 +79,7 @@ func detectDebian(c config.ServerInfo) (bool, osTypeInterface, error) {
deb := newDebian(c)
if len(result) == 0 {
deb.setDistro("debian/ubuntu", "unknown")
util.Log.Warnf("Unknown Debian/Ubuntu version. lsb_release -ir: %s", r)
logging.Log.Warnf("Unknown Debian/Ubuntu version. lsb_release -ir: %s", r)
} else {
distro := strings.ToLower(trim(result[1]))
deb.setDistro(distro, trim(result[2]))
@@ -96,7 +97,7 @@ func detectDebian(c config.ServerInfo) (bool, osTypeInterface, error) {
result := re.FindStringSubmatch(trim(r.Stdout))
deb := newDebian(c)
if len(result) == 0 {
util.Log.Warnf(
logging.Log.Warnf(
"Unknown Debian/Ubuntu. cat /etc/lsb-release: %s", r)
deb.setDistro("debian/ubuntu", "unknown")
} else {
@@ -114,7 +115,7 @@ func detectDebian(c config.ServerInfo) (bool, osTypeInterface, error) {
return true, deb, nil
}
util.Log.Debugf("Not Debian like Linux: %s", c.ServerName)
logging.Log.Debugf("Not Debian like Linux: %s", c.ServerName)
return false, nil, nil
}
@@ -380,7 +381,7 @@ func (o *debian) scanInstalledPackages() (models.Packages, models.Packages, mode
// Fill the candidate versions of upgradable packages
err = o.fillCandidateVersion(updatable)
if err != nil {
return nil, nil, nil, xerrors.Errorf("Failed to fill candidate versions. err: %s", err)
return nil, nil, nil, xerrors.Errorf("Failed to fill candidate versions. err: %w", err)
}
installed.MergeNewVersion(updatable)
@@ -513,14 +514,14 @@ func (o *debian) scanUnsecurePackages(updatable models.Packages) (models.VulnInf
// Collect CVE information of upgradable packages
vulnInfos, err := o.scanChangelogs(updatable, meta, tmpClogPath)
if err != nil {
return nil, xerrors.Errorf("Failed to scan unsecure packages. err: %s", err)
return nil, xerrors.Errorf("Failed to scan unsecure packages. err: %w", err)
}
// Delete a directory for saving changelog to get changelog in Raspbian
if o.Distro.Family == constant.Raspbian {
err := o.deleteTempChangelogDir(tmpClogPath)
if err != nil {
return nil, xerrors.Errorf("Failed to delete directory to save changelog for Raspbian. err: %s", err)
return nil, xerrors.Errorf("Failed to delete directory to save changelog for Raspbian. err: %w", err)
}
}
@@ -532,14 +533,14 @@ func (o *debian) ensureChangelogCache(current cache.Meta) (*cache.Meta, error) {
cached, found, err := cache.DB.GetMeta(current.Name)
if err != nil {
return nil, xerrors.Errorf(
"Failed to get meta. Please remove cache.db and then try again. err: %s", err)
"Failed to get meta. Please remove cache.db and then try again. err: %w", err)
}
if !found {
o.log.Debugf("Not found in meta: %s", current.Name)
err = cache.DB.EnsureBuckets(current)
if err != nil {
return nil, xerrors.Errorf("Failed to ensure buckets. err: %s", err)
return nil, xerrors.Errorf("Failed to ensure buckets. err: %w", err)
}
return &current, nil
}
@@ -549,7 +550,7 @@ func (o *debian) ensureChangelogCache(current cache.Meta) (*cache.Meta, error) {
o.log.Debugf("Need to refresh meta: %s", current.Name)
err = cache.DB.EnsureBuckets(current)
if err != nil {
return nil, xerrors.Errorf("Failed to ensure buckets. err: %s", err)
return nil, xerrors.Errorf("Failed to ensure buckets. err: %w", err)
}
return &current, nil
@@ -626,7 +627,6 @@ func (o *debian) parseAptGetUpgrade(stdout string) (updatableNames []string, err
result[1], len(updatableNames))
}
stopLineFound = true
o.log.Debugf("Found the stop line. line: %s", line)
break
}
updatableNames = append(updatableNames, strings.Fields(line)...)
@@ -660,7 +660,7 @@ func (o *debian) makeTempChangelogDir() (string, error) {
func generateSuffix() (string, error) {
var n uint64
if err := binary.Read(rand.Reader, binary.LittleEndian, &n); err != nil {
return "", xerrors.Errorf("Failed to generate Suffix. err: %s", err)
return "", xerrors.Errorf("Failed to generate Suffix. err: %w", err)
}
return strconv.FormatUint(n, 36), nil
}
@@ -803,7 +803,7 @@ func (o *debian) getChangelogCache(meta *cache.Meta, pack models.Package) string
}
changelog, err := cache.DB.GetChangelog(meta.Name, pack.Name)
if err != nil {
o.log.Warnf("Failed to get changelog. bucket: %s, key:%s, err: %s",
o.log.Warnf("Failed to get changelog. bucket: %s, key:%s, err: %+v",
meta.Name, pack.Name, err)
return ""
}
@@ -829,7 +829,7 @@ func (o *debian) fetchParseChangelog(pack models.Package, tmpClogPath string) ([
changelogPath, err := o.getChangelogPath(pack.Name, tmpClogPath)
if err != nil {
// Ignore this Error.
o.log.Warnf("Failed to get Path to Changelog for Package: %s, err: %s", pack.Name, err)
o.log.Warnf("Failed to get Path to Changelog for Package: %s, err: %+v", pack.Name, err)
return nil, nil, nil
}
cmd = fmt.Sprintf(`gzip -cd %s | cat`, changelogPath)

View File

@@ -9,9 +9,9 @@ import (
"github.com/future-architect/vuls/cache"
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/k0kubun/pp"
"github.com/sirupsen/logrus"
)
func TestGetCveIDsFromChangelog(t *testing.T) {
@@ -421,7 +421,7 @@ func TestGetChangelogCache(t *testing.T) {
}
const path = "/tmp/vuls-test-cache-11111111.db"
log := logrus.NewEntry(&logrus.Logger{})
log := logging.NewNormalLogger()
if err := cache.SetupBolt(path, log); err != nil {
t.Errorf("Failed to setup bolt: %s", err)
}

View File

@@ -12,9 +12,8 @@ import (
"golang.org/x/xerrors"
conf "github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/util"
"github.com/future-architect/vuls/logging"
homedir "github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
)
type execResult struct {
@@ -72,7 +71,7 @@ func parallelExec(fn func(osTypeInterface) error, timeoutSec ...int) {
go func(s osTypeInterface) {
defer func() {
if p := recover(); p != nil {
util.Log.Debugf("Panic: %s on %s",
logging.Log.Debugf("Panic: %s on %s",
p, s.getServerInfo().GetServerName())
}
}()
@@ -100,7 +99,7 @@ func parallelExec(fn func(osTypeInterface) error, timeoutSec ...int) {
if len(s.getErrs()) == 0 {
successes = append(successes, s)
} else {
util.Log.Errorf("Error on %s, err: %+v",
logging.Log.Errorf("Error on %s, err: %+v",
s.getServerInfo().GetServerName(), s.getErrs())
errServers = append(errServers, s)
}
@@ -121,9 +120,8 @@ func parallelExec(fn func(osTypeInterface) error, timeoutSec ...int) {
}
}
if !found {
err := xerrors.Errorf("Timed out: %s",
s.getServerInfo().GetServerName())
util.Log.Errorf("%+v", err)
err := xerrors.Errorf("Timed out: %s", s.getServerInfo().GetServerName())
logging.Log.Errorf("%+v", err)
s.setErrs([]error{err})
errServers = append(errServers, s)
}
@@ -133,7 +131,7 @@ func parallelExec(fn func(osTypeInterface) error, timeoutSec ...int) {
return
}
func exec(c conf.ServerInfo, cmd string, sudo bool, log ...*logrus.Entry) (result execResult) {
func exec(c conf.ServerInfo, cmd string, sudo bool, log ...logging.Logger) (result execResult) {
logger := getSSHLogger(log...)
logger.Debugf("Executing... %s", strings.Replace(cmd, "\n", "", -1))
@@ -143,7 +141,7 @@ func exec(c conf.ServerInfo, cmd string, sudo bool, log ...*logrus.Entry) (resul
result = sshExecExternal(c, cmd, sudo)
}
logger.Debug(result)
logger.Debugf("%+v", result)
return
}
@@ -261,9 +259,9 @@ func sshExecExternal(c conf.ServerInfo, cmd string, sudo bool) (result execResul
return
}
func getSSHLogger(log ...*logrus.Entry) *logrus.Entry {
func getSSHLogger(log ...logging.Logger) logging.Logger {
if len(log) == 0 {
return util.Log
return logging.Log
}
return log[0]
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
"golang.org/x/xerrors"
@@ -26,7 +27,7 @@ func newBsd(c config.ServerInfo) *bsd {
},
},
}
d.log = util.NewCustomLogger(c)
d.log = logging.NewNormalLogger()
d.setServerInfo(c)
return d
}
@@ -46,7 +47,7 @@ func detectFreebsd(c config.ServerInfo) (bool, osTypeInterface) {
}
}
}
util.Log.Debugf("Not FreeBSD. servernam: %s", c.ServerName)
logging.Log.Debugf("Not FreeBSD. servernam: %s", c.ServerName)
return false, nil
}

View File

@@ -2,8 +2,8 @@ package scanner
import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
)
// inherit OsTypeInterface
@@ -24,7 +24,7 @@ func newOracle(c config.ServerInfo) *oracle {
sudo: rootPrivOracle{},
},
}
r.log = util.NewCustomLogger(c)
r.log = logging.NewNormalLogger()
r.setServerInfo(c)
return r
}

View File

@@ -3,8 +3,8 @@ package scanner
import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
)
// inherit OsTypeInterface
@@ -30,7 +30,7 @@ func newPseudo(c config.ServerInfo) *pseudo {
},
},
}
d.log = util.NewCustomLogger(c)
d.log = logging.NewNormalLogger()
d.setServerInfo(c)
return d
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
"golang.org/x/xerrors"
@@ -18,7 +19,7 @@ import (
// https://github.com/serverspec/specinfra/blob/master/lib/specinfra/helper/detect_os/redhat.rb
func detectRedhat(c config.ServerInfo) (bool, osTypeInterface) {
if r := exec(c, "ls /etc/fedora-release", noSudo); r.isSuccess() {
util.Log.Warnf("Fedora not tested yet: %s", r)
logging.Log.Warnf("Fedora not tested yet: %s", r)
return true, &unknown{}
}
@@ -29,7 +30,7 @@ func detectRedhat(c config.ServerInfo) (bool, osTypeInterface) {
re := regexp.MustCompile(`(.*) release (\d[\d\.]*)`)
result := re.FindStringSubmatch(strings.TrimSpace(r.Stdout))
if len(result) != 3 {
util.Log.Warnf("Failed to parse Oracle Linux version: %s", r)
logging.Log.Warnf("Failed to parse Oracle Linux version: %s", r)
return true, newOracle(c)
}
@@ -47,7 +48,7 @@ func detectRedhat(c config.ServerInfo) (bool, osTypeInterface) {
re := regexp.MustCompile(`(.*) release (\d[\d\.]*)`)
result := re.FindStringSubmatch(strings.TrimSpace(r.Stdout))
if len(result) != 3 {
util.Log.Warnf("Failed to parse CentOS version: %s", r)
logging.Log.Warnf("Failed to parse CentOS version: %s", r)
return true, newCentOS(c)
}
@@ -58,7 +59,7 @@ func detectRedhat(c config.ServerInfo) (bool, osTypeInterface) {
cent.setDistro(constant.CentOS, release)
return true, cent
default:
util.Log.Warnf("Failed to parse CentOS: %s", r)
logging.Log.Warnf("Failed to parse CentOS: %s", r)
}
}
}
@@ -72,7 +73,7 @@ func detectRedhat(c config.ServerInfo) (bool, osTypeInterface) {
re := regexp.MustCompile(`(.*) release (\d[\d\.]*)`)
result := re.FindStringSubmatch(strings.TrimSpace(r.Stdout))
if len(result) != 3 {
util.Log.Warnf("Failed to parse RedHat/CentOS version: %s", r)
logging.Log.Warnf("Failed to parse RedHat/CentOS version: %s", r)
return true, newCentOS(c)
}
@@ -113,7 +114,7 @@ func detectRedhat(c config.ServerInfo) (bool, osTypeInterface) {
return true, amazon
}
util.Log.Debugf("Not RedHat like Linux. servername: %s", c.ServerName)
logging.Log.Debugf("Not RedHat like Linux. servername: %s", c.ServerName)
return false, &unknown{}
}
@@ -451,7 +452,7 @@ func (o *redhatBase) isExecNeedsRestarting() bool {
case constant.RedHat, constant.CentOS, constant.Oracle:
majorVersion, err := o.Distro.MajorVersion()
if err != nil || majorVersion < 6 {
o.log.Errorf("Not implemented yet: %s, err: %s", o.Distro, err)
o.log.Errorf("Not implemented yet: %s, err: %+v", o.Distro, err)
return false
}

View File

@@ -217,7 +217,7 @@ func TestParseYumCheckUpdateLine(t *testing.T) {
for _, tt := range tests {
aPack, err := r.parseUpdatablePacksLine(tt.in)
if err != nil {
t.Errorf("Error has occurred, err: %s\ntt.in: %v", err, tt.in)
t.Errorf("Error has occurred, err: %+v\ntt.in: %v", err, tt.in)
return
}
if !reflect.DeepEqual(tt.out, aPack) {
@@ -296,7 +296,7 @@ pytalloc 0 2.0.7 2.el6 @CentOS 6.5/6.5`
for _, tt := range tests {
packages, err := r.parseUpdatablePacksLines(tt.in)
if err != nil {
t.Errorf("Error has occurred, err: %s\ntt.in: %v", err, tt.in)
t.Errorf("Error has occurred, err: %+v\ntt.in: %v", err, tt.in)
return
}
for name, ePack := range tt.out {
@@ -352,7 +352,7 @@ if-not-architecture 0 100 200 amzn-main`
for _, tt := range tests {
packages, err := r.parseUpdatablePacksLines(tt.in)
if err != nil {
t.Errorf("Error has occurred, err: %s\ntt.in: %v", err, tt.in)
t.Errorf("Error has occurred, err: %+v\ntt.in: %v", err, tt.in)
return
}
for name, ePack := range tt.out {

View File

@@ -2,8 +2,8 @@ package scanner
import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
"golang.org/x/xerrors"
)
@@ -25,7 +25,7 @@ func newRHEL(c config.ServerInfo) *rhel {
sudo: rootPrivRHEL{},
},
}
r.log = util.NewCustomLogger(c)
r.log = logging.NewNormalLogger()
r.setServerInfo(c)
return r
}

View File

@@ -2,6 +2,7 @@ package scanner
import (
"fmt"
"math/rand"
"net/http"
"os"
"time"
@@ -9,6 +10,7 @@ import (
"github.com/future-architect/vuls/cache"
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
"golang.org/x/xerrors"
@@ -57,6 +59,7 @@ type osTypeInterface interface {
exitedContainers() ([]config.Container, error)
allContainers() ([]config.Container, error)
setLogger(logging.Logger)
getErrs() []error
setErrs([]error)
}
@@ -66,26 +69,29 @@ type Scanner struct {
TimeoutSec int
ScanTimeoutSec int
CacheDBPath string
Debug bool
LogDir string
Quiet bool
Targets map[string]config.ServerInfo
}
// Scan execute scan
func (s Scanner) Scan() error {
util.Log.Info("Detecting Server/Container OS... ")
logging.Log.Info("Detecting Server/Container OS... ")
if err := s.initServers(); err != nil {
return xerrors.Errorf("Failed to init servers. err: %w", err)
}
util.Log.Info("Checking Scan Modes... ")
logging.Log.Info("Checking Scan Modes... ")
if err := s.checkScanModes(); err != nil {
return xerrors.Errorf("Fix config.toml. err: %w", err)
}
util.Log.Info("Detecting Platforms... ")
logging.Log.Info("Detecting Platforms... ")
s.detectPlatform()
util.Log.Info("Detecting IPS identifiers... ")
logging.Log.Info("Detecting IPS identifiers... ")
s.detectIPS()
if err := s.execScan(); err != nil {
@@ -96,29 +102,29 @@ func (s Scanner) Scan() error {
// Configtest checks if the server is scannable.
func (s Scanner) Configtest() error {
util.Log.Info("Detecting Server/Container OS... ")
logging.Log.Info("Detecting Server/Container OS... ")
if err := s.initServers(); err != nil {
return xerrors.Errorf("Failed to init servers. err: %w", err)
}
util.Log.Info("Checking Scan Modes...")
logging.Log.Info("Checking Scan Modes...")
if err := s.checkScanModes(); err != nil {
return xerrors.Errorf("Fix config.toml. err: %w", err)
}
util.Log.Info("Checking dependencies...")
logging.Log.Info("Checking dependencies...")
s.checkDependencies()
util.Log.Info("Checking sudo settings...")
logging.Log.Info("Checking sudo settings...")
s.checkIfSudoNoPasswd()
util.Log.Info("It can be scanned with fast scan mode even if warn or err messages are displayed due to lack of dependent packages or sudo settings in fast-root or deep scan mode")
logging.Log.Info("It can be scanned with fast scan mode even if warn or err messages are displayed due to lack of dependent packages or sudo settings in fast-root or deep scan mode")
if len(servers) == 0 {
return xerrors.Errorf("No scannable servers")
}
util.Log.Info("Scannable servers are below...")
logging.Log.Info("Scannable servers are below...")
for _, s := range servers {
if s.getServerInfo().IsContainer() {
fmt.Printf("%s@%s ",
@@ -147,7 +153,7 @@ func ViaHTTP(header http.Header, body string, toLocalFile bool) (models.ScanResu
kernelRelease := header.Get("X-Vuls-Kernel-Release")
if kernelRelease == "" {
util.Log.Warn("If X-Vuls-Kernel-Release is not specified, there is a possibility of false detection")
logging.Log.Warn("If X-Vuls-Kernel-Release is not specified, there is a possibility of false detection")
}
kernelVersion := header.Get("X-Vuls-Kernel-Version")
@@ -174,7 +180,7 @@ func ViaHTTP(header http.Header, body string, toLocalFile bool) (models.ScanResu
osPackages: osPackages{
Kernel: kernel,
},
log: util.Log,
log: logging.Log,
}
var osType osTypeInterface
@@ -228,7 +234,17 @@ func (s Scanner) initServers() error {
if len(hosts) == 0 {
return xerrors.New("No scannable host OS")
}
// 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()))
}
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()))
}
// set to pkg global variable
for _, host := range hosts {
@@ -246,14 +262,14 @@ func (s Scanner) initServers() error {
}
func (s Scanner) detectServerOSes() (servers, errServers []osTypeInterface) {
util.Log.Info("Detecting OS of servers... ")
logging.Log.Info("Detecting OS of servers... ")
osTypeChan := make(chan osTypeInterface, len(s.Targets))
defer close(osTypeChan)
for _, target := range s.Targets {
go func(srv config.ServerInfo) {
defer func() {
if p := recover(); p != nil {
util.Log.Debugf("Panic: %s on %s", p, srv.ServerName)
logging.Log.Debugf("Panic: %s on %s", p, srv.ServerName)
}
}()
osTypeChan <- s.detectOS(srv)
@@ -266,16 +282,16 @@ func (s Scanner) detectServerOSes() (servers, errServers []osTypeInterface) {
case res := <-osTypeChan:
if 0 < len(res.getErrs()) {
errServers = append(errServers, res)
util.Log.Errorf("(%d/%d) Failed: %s, err: %+v",
logging.Log.Errorf("(%d/%d) Failed: %s, err: %+v",
i+1, len(s.Targets), res.getServerInfo().ServerName, res.getErrs())
} else {
servers = append(servers, res)
util.Log.Infof("(%d/%d) Detected: %s: %s",
logging.Log.Infof("(%d/%d) Detected: %s: %s",
i+1, len(s.Targets), res.getServerInfo().ServerName, res.getDistro())
}
case <-timeout:
msg := "Timed out while detecting servers"
util.Log.Error(msg)
logging.Log.Error(msg)
for servername, sInfo := range s.Targets {
found := false
for _, o := range append(servers, errServers...) {
@@ -289,7 +305,7 @@ func (s Scanner) detectServerOSes() (servers, errServers []osTypeInterface) {
u.setServerInfo(sInfo)
u.setErrs([]error{xerrors.New("Timed out")})
errServers = append(errServers, u)
util.Log.Errorf("(%d/%d) Timed out: %s", i+1, len(s.Targets), servername)
logging.Log.Errorf("(%d/%d) Timed out: %s", i+1, len(s.Targets), servername)
}
}
}
@@ -298,14 +314,14 @@ func (s Scanner) detectServerOSes() (servers, errServers []osTypeInterface) {
}
func (s Scanner) detectContainerOSes(hosts []osTypeInterface) (actives, inactives []osTypeInterface) {
util.Log.Info("Detecting OS of containers... ")
logging.Log.Info("Detecting OS of containers... ")
osTypesChan := make(chan []osTypeInterface, len(hosts))
defer close(osTypesChan)
for _, host := range hosts {
go func(h osTypeInterface) {
defer func() {
if p := recover(); p != nil {
util.Log.Debugf("Panic: %s on %s",
logging.Log.Debugf("Panic: %s on %s",
p, h.getServerInfo().GetServerName())
}
}()
@@ -321,15 +337,15 @@ func (s Scanner) detectContainerOSes(hosts []osTypeInterface) (actives, inactive
sinfo := osi.getServerInfo()
if 0 < len(osi.getErrs()) {
inactives = append(inactives, osi)
util.Log.Errorf("Failed: %s err: %+v", sinfo.ServerName, osi.getErrs())
logging.Log.Errorf("Failed: %s err: %+v", sinfo.ServerName, osi.getErrs())
continue
}
actives = append(actives, osi)
util.Log.Infof("Detected: %s@%s: %s",
logging.Log.Infof("Detected: %s@%s: %s",
sinfo.Container.Name, sinfo.ServerName, osi.getDistro())
}
case <-timeout:
util.Log.Error("Some containers timed out")
logging.Log.Error("Some containers timed out")
}
}
return
@@ -434,27 +450,27 @@ func (s Scanner) detectOS(c config.ServerInfo) (osType osTypeInterface) {
}
if itsMe {
util.Log.Debugf("Debian like Linux. Host: %s:%s", c.Host, c.Port)
logging.Log.Debugf("Debian like Linux. Host: %s:%s", c.Host, c.Port)
return
}
if itsMe, osType = detectRedhat(c); itsMe {
util.Log.Debugf("Redhat like Linux. Host: %s:%s", c.Host, c.Port)
logging.Log.Debugf("Redhat like Linux. Host: %s:%s", c.Host, c.Port)
return
}
if itsMe, osType = detectSUSE(c); itsMe {
util.Log.Debugf("SUSE Linux. Host: %s:%s", c.Host, c.Port)
logging.Log.Debugf("SUSE Linux. Host: %s:%s", c.Host, c.Port)
return
}
if itsMe, osType = detectFreebsd(c); itsMe {
util.Log.Debugf("FreeBSD. Host: %s:%s", c.Host, c.Port)
logging.Log.Debugf("FreeBSD. Host: %s:%s", c.Host, c.Port)
return
}
if itsMe, osType = detectAlpine(c); itsMe {
util.Log.Debugf("Alpine. Host: %s:%s", c.Host, c.Port)
logging.Log.Debugf("Alpine. Host: %s:%s", c.Host, c.Port)
return
}
@@ -524,7 +540,7 @@ func (s Scanner) detectPlatform() {
for i, s := range servers {
if s.getServerInfo().IsContainer() {
util.Log.Infof("(%d/%d) %s on %s is running on %s",
logging.Log.Infof("(%d/%d) %s on %s is running on %s",
i+1, len(servers),
s.getServerInfo().Container.Name,
s.getServerInfo().ServerName,
@@ -532,7 +548,7 @@ func (s Scanner) detectPlatform() {
)
} else {
util.Log.Infof("(%d/%d) %s is running on %s",
logging.Log.Infof("(%d/%d) %s is running on %s",
i+1, len(servers),
s.getServerInfo().ServerName,
s.getPlatform().Name,
@@ -552,7 +568,7 @@ func (s Scanner) detectIPS() {
for i, s := range servers {
if !s.getServerInfo().IsContainer() {
util.Log.Infof("(%d/%d) %s has %d IPS integration",
logging.Log.Infof("(%d/%d) %s has %d IPS integration",
i+1, len(servers),
s.getServerInfo().ServerName,
len(s.getServerInfo().IPSIdentifiers),
@@ -612,7 +628,7 @@ func (s Scanner) setupChangelogCache() error {
}
}
if needToSetupCache {
if err := cache.SetupBolt(s.CacheDBPath, util.Log); err != nil {
if err := cache.SetupBolt(s.CacheDBPath, logging.Log); err != nil {
return err
}
}
@@ -654,7 +670,7 @@ func (s Scanner) getScanResults(scannedAt time.Time) (results models.ScanResults
hostname, _ := os.Hostname()
ipv4s, ipv6s, err := util.IP()
if err != nil {
util.Log.Errorf("Failed to fetch scannedIPs. err: %+v", err)
logging.Log.Warnf("Failed to get scannedIPs. err: %+v", err)
}
for _, s := range append(servers, errServers...) {
@@ -670,7 +686,7 @@ func (s Scanner) getScanResults(scannedAt time.Time) (results models.ScanResults
results = append(results, r)
if 0 < len(r.Warnings) {
util.Log.Warnf("Some warnings occurred during scanning on %s. Please fix the warnings to get a useful information. Execute configtest subcommand before scanning to know the cause of the warnings. warnings: %v",
logging.Log.Warnf("Some warnings occurred during scanning on %s. Please fix the warnings to get a useful information. Execute configtest subcommand before scanning to know the cause of the warnings. warnings: %v",
r.ServerName, r.Warnings)
}
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
"golang.org/x/xerrors"
@@ -30,7 +31,7 @@ func newSUSE(c config.ServerInfo) *suse {
},
},
}
r.log = util.NewCustomLogger(c)
r.log = logging.NewNormalLogger()
r.setServerInfo(c)
return r
}
@@ -71,12 +72,12 @@ func detectSUSE(c config.ServerInfo) (bool, osTypeInterface) {
return true, s
}
}
util.Log.Warnf("Failed to parse SUSE Linux version: %s", r)
logging.Log.Warnf("Failed to parse SUSE Linux version: %s", r)
return true, newSUSE(c)
}
}
}
util.Log.Debugf("Not SUSE Linux. servername: %s", c.ServerName)
logging.Log.Debugf("Not SUSE Linux. servername: %s", c.ServerName)
return false, nil
}
@@ -89,14 +90,14 @@ func (o *suse) parseOSRelease(content string) (name string, ver string) {
} else if strings.Contains(content, `NAME="SLES_SAP"`) {
name = constant.SUSEEnterpriseServer
} else {
util.Log.Warnf("Failed to parse SUSE edition: %s", content)
logging.Log.Warnf("Failed to parse SUSE edition: %s", content)
return "unknown", "unknown"
}
re := regexp.MustCompile(`VERSION_ID=\"(.+)\"`)
result := re.FindStringSubmatch(strings.TrimSpace(content))
if len(result) != 2 {
util.Log.Warnf("Failed to parse SUSE Linux version: %s", content)
logging.Log.Warnf("Failed to parse SUSE Linux version: %s", content)
return "unknown", "unknown"
}
return name, result[1]

View File

@@ -51,7 +51,7 @@ v | Clone of SLES11-SP3-Updates for x86_64 | ConsoleKit | 0.2.10-64.65.1 | 0.2.1
for _, tt := range tests {
packages, err := r.parseZypperLULines(tt.in)
if err != nil {
t.Errorf("Error has occurred, err: %s\ntt.in: %v", err, tt.in)
t.Errorf("Error has occurred, err: %+v\ntt.in: %v", err, tt.in)
return
}
for name, ePack := range tt.out {
@@ -87,7 +87,7 @@ func TestScanUpdatablePackage(t *testing.T) {
for _, tt := range tests {
pack, err := r.parseZypperLUOneLine(tt.in)
if err != nil {
t.Errorf("Error has occurred, err: %s\ntt.in: %v", err, tt.in)
t.Errorf("Error has occurred, err: %+v\ntt.in: %v", err, tt.in)
return
}
if !reflect.DeepEqual(*pack, tt.out) {

View File

@@ -9,9 +9,9 @@ import (
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/reporter"
"github.com/future-architect/vuls/util"
"golang.org/x/xerrors"
)
@@ -36,7 +36,7 @@ func isRunningKernel(pack models.Package, family string, kernel models.Kernel) (
return false, false
default:
util.Log.Warnf("Reboot required is not implemented yet: %s, %v", family, kernel)
logging.Log.Warnf("Reboot required is not implemented yet: %s, %v", family, kernel)
}
return false, false
}