Skip to content

Commit

Permalink
fix(log): output version to log for debugging purpose (#1163)
Browse files Browse the repository at this point in the history
  • Loading branch information
kotakanbe committed Feb 3, 2021
1 parent 64a6222 commit 290edff
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cmd/vuls/main.go
Expand Up @@ -29,7 +29,7 @@ func main() {
flag.Parse()

if *v {
fmt.Printf("vuls %s %s\n", config.Version, config.Revision)
fmt.Printf("vuls-%s-%s\n", config.Version, config.Revision)
os.Exit(int(subcommands.ExitSuccess))
}

Expand Down
11 changes: 4 additions & 7 deletions scan/alpine.go
Expand Up @@ -32,19 +32,16 @@ func newAlpine(c config.ServerInfo) *alpine {

// Alpine
// https://github.com/mizzy/specinfra/blob/master/lib/specinfra/helper/detect_os/alpine.rb
func detectAlpine(c config.ServerInfo) (itsMe bool, os osTypeInterface) {
os = newAlpine(c)

func detectAlpine(c config.ServerInfo) (bool, osTypeInterface) {
if r := exec(c, "ls /etc/alpine-release", noSudo); !r.isSuccess() {
return false, os
return false, nil
}

if r := exec(c, "cat /etc/alpine-release", noSudo); r.isSuccess() {
os := newAlpine(c)
os.setDistro(config.Alpine, strings.TrimSpace(r.Stdout))
return true, os
}

return false, os
return false, nil
}

func (o *alpine) checkScanMode() error {
Expand Down
22 changes: 11 additions & 11 deletions scan/debian.go
Expand Up @@ -40,18 +40,16 @@ func newDebian(c config.ServerInfo) *debian {

// Ubuntu, Debian, Raspbian
// https://github.com/serverspec/specinfra/blob/master/lib/specinfra/helper/detect_os/debian.rb
func detectDebian(c config.ServerInfo) (itsMe bool, deb osTypeInterface, err error) {
deb = newDebian(c)

func detectDebian(c config.ServerInfo) (bool, osTypeInterface, error) {
if r := exec(c, "ls /etc/debian_version", noSudo); !r.isSuccess() {
if r.Error != nil {
return false, deb, nil
return false, nil, nil
}
if r.ExitStatus == 255 {
return false, deb, 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)
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)
return false, deb, nil
return false, nil, nil
}

// Raspbian
Expand All @@ -62,8 +60,8 @@ func detectDebian(c config.ServerInfo) (itsMe bool, deb osTypeInterface, err err
// Raspbian GNU/Linux 7 \n \l
result := strings.Fields(r.Stdout)
if len(result) > 2 && result[0] == config.Raspbian {
distro := strings.ToLower(trim(result[0]))
deb.setDistro(distro, trim(result[2]))
deb := newDebian(c)
deb.setDistro(strings.ToLower(trim(result[0])), trim(result[2]))
return true, deb, nil
}
}
Expand All @@ -76,10 +74,10 @@ func detectDebian(c config.ServerInfo) (itsMe bool, deb osTypeInterface, err err
re := regexp.MustCompile(`(?s)^Distributor ID:\s*(.+?)\n*Release:\s*(.+?)$`)
result := re.FindStringSubmatch(trim(r.Stdout))

deb := newDebian(c)
if len(result) == 0 {
deb.setDistro("debian/ubuntu", "unknown")
util.Log.Warnf(
"Unknown Debian/Ubuntu version. lsb_release -ir: %s", r)
util.Log.Warnf("Unknown Debian/Ubuntu version. lsb_release -ir: %s", r)
} else {
distro := strings.ToLower(trim(result[1]))
deb.setDistro(distro, trim(result[2]))
Expand All @@ -95,6 +93,7 @@ func detectDebian(c config.ServerInfo) (itsMe bool, deb osTypeInterface, err err
// DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS"
re := regexp.MustCompile(`(?s)^DISTRIB_ID=(.+?)\n*DISTRIB_RELEASE=(.+?)\n.*$`)
result := re.FindStringSubmatch(trim(r.Stdout))
deb := newDebian(c)
if len(result) == 0 {
util.Log.Warnf(
"Unknown Debian/Ubuntu. cat /etc/lsb-release: %s", r)
Expand All @@ -109,12 +108,13 @@ func detectDebian(c config.ServerInfo) (itsMe bool, deb osTypeInterface, err err
// Debian
cmd := "cat /etc/debian_version"
if r := exec(c, cmd, noSudo); r.isSuccess() {
deb := newDebian(c)
deb.setDistro(config.Debian, trim(r.Stdout))
return true, deb, nil
}

util.Log.Debugf("Not Debian like Linux: %s", c.ServerName)
return false, deb, nil
return false, nil, nil
}

func trim(str string) string {
Expand Down
7 changes: 3 additions & 4 deletions scan/freebsd.go
Expand Up @@ -31,23 +31,22 @@ func newBsd(c config.ServerInfo) *bsd {
}

//https://github.com/mizzy/specinfra/blob/master/lib/specinfra/helper/detect_os/freebsd.rb
func detectFreebsd(c config.ServerInfo) (itsMe bool, bsd osTypeInterface) {
bsd = newBsd(c)

func detectFreebsd(c config.ServerInfo) (bool, osTypeInterface) {
// Prevent from adding `set -o pipefail` option
c.Distro = config.Distro{Family: config.FreeBSD}

if r := exec(c, "uname", noSudo); r.isSuccess() {
if strings.Contains(strings.ToLower(r.Stdout), config.FreeBSD) == true {
if b := exec(c, "freebsd-version", noSudo); b.isSuccess() {
bsd := newBsd(c)
rel := strings.TrimSpace(b.Stdout)
bsd.setDistro(config.FreeBSD, rel)
return true, bsd
}
}
}
util.Log.Debugf("Not FreeBSD. servernam: %s", c.ServerName)
return false, bsd
return false, nil
}

func (o *bsd) checkScanMode() error {
Expand Down
9 changes: 6 additions & 3 deletions scan/pseudo.go
Expand Up @@ -12,9 +12,12 @@ type pseudo struct {
}

func detectPseudo(c config.ServerInfo) (itsMe bool, pseudo osTypeInterface, err error) {
p := newPseudo(c)
p.setDistro(config.ServerTypePseudo, "")
return c.Type == config.ServerTypePseudo, p, nil
if c.Type == config.ServerTypePseudo {
p := newPseudo(c)
p.setDistro(config.ServerTypePseudo, "")
return true, p, nil
}
return false, nil, nil
}

func newPseudo(c config.ServerInfo) *pseudo {
Expand Down
8 changes: 5 additions & 3 deletions scan/suse.go
Expand Up @@ -36,10 +36,10 @@ func newSUSE(c config.ServerInfo) *suse {

// https://github.com/mizzy/specinfra/blob/master/lib/specinfra/helper/detect_os/suse.rb
func detectSUSE(c config.ServerInfo) (bool, osTypeInterface) {
s := newSUSE(c)
if r := exec(c, "ls /etc/os-release", noSudo); r.isSuccess() {
if r := exec(c, "zypper -V", noSudo); r.isSuccess() {
if r := exec(c, "cat /etc/os-release", noSudo); r.isSuccess() {
s := newSUSE(c)
name, ver := s.parseOSRelease(r.Stdout)
s.setDistro(name, ver)
return true, s
Expand All @@ -52,6 +52,7 @@ func detectSUSE(c config.ServerInfo) (bool, osTypeInterface) {
result := re.FindStringSubmatch(strings.TrimSpace(r.Stdout))
if len(result) == 2 {
//TODO check opensuse or opensuse.leap
s := newSUSE(c)
s.setDistro(config.OpenSUSE, result[1])
return true, s
}
Expand All @@ -63,18 +64,19 @@ func detectSUSE(c config.ServerInfo) (bool, osTypeInterface) {
re = regexp.MustCompile(`PATCHLEVEL = (\d+)`)
result = re.FindStringSubmatch(strings.TrimSpace(r.Stdout))
if len(result) == 2 {
s := newSUSE(c)
s.setDistro(config.SUSEEnterpriseServer,
fmt.Sprintf("%s.%s", version, result[1]))
return true, s
}
}
util.Log.Warnf("Failed to parse SUSE Linux version: %s", r)
return true, s
return true, newSUSE(c)
}
}
}
util.Log.Debugf("Not SUSE Linux. servername: %s", c.ServerName)
return false, s
return false, nil
}

func (o *suse) parseOSRelease(content string) (name string, ver string) {
Expand Down
5 changes: 3 additions & 2 deletions util/logutil.go
Expand Up @@ -86,8 +86,9 @@ func NewCustomLogger(server config.ServerInfo) *logrus.Entry {
}
}

fields := logrus.Fields{"prefix": whereami}
return log.WithFields(fields)
entry := log.WithFields(logrus.Fields{"prefix": whereami})
entry.Infof("vuls-%s-%s", config.Version, config.Revision)
return entry
}

// GetDefaultLogDir returns default log directory
Expand Down

0 comments on commit 290edff

Please sign in to comment.