Skip to content

Commit

Permalink
Add suse linux enterprise server support (aquasecurity#55)
Browse files Browse the repository at this point in the history
* Add suse linux enterprise server

* Fix comment

* Add openSUSE Tumbleweed support
  • Loading branch information
knqyf263 authored Dec 15, 2019
2 parents 99e4876 + 897cc6c commit 0473ad8
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
_ "github.com/aquasecurity/fanal/analyzer/os/alpine"
_ "github.com/aquasecurity/fanal/analyzer/os/amazonlinux"
_ "github.com/aquasecurity/fanal/analyzer/os/debianbase"
_ "github.com/aquasecurity/fanal/analyzer/os/opensuse"
_ "github.com/aquasecurity/fanal/analyzer/os/suse"
_ "github.com/aquasecurity/fanal/analyzer/os/redhatbase"
_ "github.com/aquasecurity/fanal/analyzer/pkg/apk"
_ "github.com/aquasecurity/fanal/analyzer/pkg/dpkg"
Expand Down
5 changes: 4 additions & 1 deletion analyzer/os/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ const (
// OpenSUSE is done
OpenSUSE = "opensuse"

// OpenSUSELeap is
// OpenSUSELeap is done
OpenSUSELeap = "opensuse.leap"

// OpenSUSETumbleweed is done
OpenSUSETumbleweed = "opensuse.tumbleweed"

// SUSE Linux Enterplise Server is done
SLES = "suse linux enterprise server"

// Alpine is done
Alpine = "alpine"
)
Expand Down
17 changes: 10 additions & 7 deletions analyzer/os/opensuse/opensuse.go → analyzer/os/suse/suse.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package opensuse
package suse

import (
"bufio"
Expand All @@ -14,13 +14,12 @@ import (
)

func init() {
analyzer.RegisterOSAnalyzer(&opensuseOSAnalyzer{})
analyzer.RegisterOSAnalyzer(&suseOSAnalyzer{})
}

type opensuseOSAnalyzer struct{}
type suseOSAnalyzer struct{}

// TODO : need investigation
func (a opensuseOSAnalyzer) Analyze(fileMap extractor.FileMap) (analyzer.OS, error) {
func (a suseOSAnalyzer) Analyze(fileMap extractor.FileMap) (analyzer.OS, error) {
for _, filename := range a.RequiredFiles() {
file, ok := fileMap[filename]
if !ok {
Expand All @@ -40,6 +39,10 @@ func (a opensuseOSAnalyzer) Analyze(fileMap extractor.FileMap) (analyzer.OS, err
}
continue
}
if strings.HasPrefix(line, "NAME=\"SLES") {
suseName = os.SLES
continue
}

if suseName != "" && strings.HasPrefix(line, "VERSION_ID=") {
return analyzer.OS{
Expand All @@ -49,10 +52,10 @@ func (a opensuseOSAnalyzer) Analyze(fileMap extractor.FileMap) (analyzer.OS, err
}
}
}
return analyzer.OS{}, xerrors.Errorf("opensuse: %w", os.AnalyzeOSError)
return analyzer.OS{}, xerrors.Errorf("suse: %w", os.AnalyzeOSError)
}

func (a opensuseOSAnalyzer) RequiredFiles() []string {
func (a suseOSAnalyzer) RequiredFiles() []string {
return []string{
"usr/lib/os-release",
"etc/os-release",
Expand Down
71 changes: 71 additions & 0 deletions analyzer/os/suse/suse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package suse

import (
"reflect"
"testing"

"golang.org/x/xerrors"

"github.com/aquasecurity/fanal/analyzer"
"github.com/aquasecurity/fanal/analyzer/os"
)

func TestAnalyze(t *testing.T) {
var tests = map[string]struct {
path string
os analyzer.OS
wantErr error
}{
"OpenSUSELeap15.0": {
path: "./testdata/opensuse_leap_150",
os: analyzer.OS{Family: os.OpenSUSELeap, Name: "15.0"},
},
"OpenSUSELeap15.1": {
path: "./testdata/opensuse_leap_151",
os: analyzer.OS{Family: os.OpenSUSELeap, Name: "15.1"},
},
"OpenSUSELeap42.3": {
path: "./testdata/opensuse_leap_423",
os: analyzer.OS{Family: os.OpenSUSELeap, Name: "42.3"},
},
"SLES12": {
path: "./testdata/sles_12",
os: analyzer.OS{Family: os.SLES, Name: "12"},
},
"SLES15": {
path: "./testdata/sles_15",
os: analyzer.OS{Family: os.SLES, Name: "15"},
},
"SLES15.1": {
path: "./testdata/sles_151",
os: analyzer.OS{Family: os.SLES, Name: "15.1"},
},
"openSUSE Tumbleweed": {
path: "./testdata/opensuse_leap_tumbleweed",
os: analyzer.OS{Family: os.OpenSUSETumbleweed, Name: "20191204"},
},
"Invalid": {
path: "./testdata/not_suse",
wantErr: os.AnalyzeOSError,
},
}
a := suseOSAnalyzer{}
for testname, v := range tests {
fileMap, err := os.GetFileMap(v.path)
if err != nil {
t.Errorf("%s : catch the error : %v", testname, err)
}
osInfo, err := a.Analyze(fileMap)
if v.wantErr != nil {
if err == nil {
t.Errorf("%s : expected error but no error", testname)
}
if !xerrors.Is(err, v.wantErr) {
t.Errorf("[%s]\nexpected : %v\nactual : %v", testname, v.wantErr, err)
}
}
if !reflect.DeepEqual(v.os, osInfo) {
t.Errorf("[%s]\nexpected : %v\nactual : %v", testname, v.os, osInfo)
}
}
}
1 change: 1 addition & 0 deletions analyzer/os/suse/testdata/not_suse/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Red Hat Linux release 6.2 (Zoot)
10 changes: 10 additions & 0 deletions analyzer/os/suse/testdata/opensuse_leap_150/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
NAME="openSUSE Leap"
VERSION="15.0"
ID="opensuse-leap"
ID_LIKE="suse opensuse"
VERSION_ID="15.0"
PRETTY_NAME="openSUSE Leap 15.0"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:15.0"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
10 changes: 10 additions & 0 deletions analyzer/os/suse/testdata/opensuse_leap_151/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
NAME="openSUSE Leap"
VERSION="15.1"
ID="opensuse-leap"
ID_LIKE="suse opensuse"
VERSION_ID="15.1"
PRETTY_NAME="openSUSE Leap 15.1"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:15.1"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
10 changes: 10 additions & 0 deletions analyzer/os/suse/testdata/opensuse_leap_423/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
NAME="openSUSE Leap"
VERSION="42.3"
ID="opensuse-leap"
ID_LIKE="suse opensuse"
VERSION_ID="42.3"
PRETTY_NAME="openSUSE Leap 42.3"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:42.3"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
11 changes: 11 additions & 0 deletions analyzer/os/suse/testdata/opensuse_leap_tumbleweed/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NAME="openSUSE Tumbleweed"
# VERSION="20191204"
ID="opensuse-tumbleweed"
ID_LIKE="opensuse suse"
VERSION_ID="20191204"
PRETTY_NAME="openSUSE Tumbleweed"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:tumbleweed:20191204"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
LOGO="distributor-logo"
8 changes: 8 additions & 0 deletions analyzer/os/suse/testdata/sles_12/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME="SLES"
VERSION="12"
VERSION_ID="12"
PRETTY_NAME="SUSE Linux Enterprise Server 12"
ID="sles"
ID_LIKE="suse"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:12"
8 changes: 8 additions & 0 deletions analyzer/os/suse/testdata/sles_15/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME="SLES"
VERSION="15"
VERSION_ID="15"
PRETTY_NAME="SUSE Linux Enterprise Server 15"
ID="sles"
ID_LIKE="suse"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:15"
8 changes: 8 additions & 0 deletions analyzer/os/suse/testdata/sles_151/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME="SLES"
VERSION="15-SP1"
VERSION_ID="15.1"
PRETTY_NAME="SUSE Linux Enterprise Server 15 SP1"
ID="sles"
ID_LIKE="suse"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:15:sp1"
2 changes: 1 addition & 1 deletion cmd/fanal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
_ "github.com/aquasecurity/fanal/analyzer/os/alpine"
_ "github.com/aquasecurity/fanal/analyzer/os/amazonlinux"
_ "github.com/aquasecurity/fanal/analyzer/os/debianbase"
_ "github.com/aquasecurity/fanal/analyzer/os/opensuse"
_ "github.com/aquasecurity/fanal/analyzer/os/redhatbase"
_ "github.com/aquasecurity/fanal/analyzer/os/suse"
_ "github.com/aquasecurity/fanal/analyzer/pkg/apk"
_ "github.com/aquasecurity/fanal/analyzer/pkg/dpkg"
_ "github.com/aquasecurity/fanal/analyzer/pkg/rpm"
Expand Down

0 comments on commit 0473ad8

Please sign in to comment.