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

Add unit tests for plugin name formatting. #465

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@ import (
"github.com/google/osv-scalibr/plugin"
)

const (
// Name of the detector.
Name = "cis/generic-linux/etcpasswdpermissions"
)

// Detector is a SCALIBR Detector for the CIS check "Ensure permissions on /etc/passwd- are configured"
// from the CIS Distribution Independent Linux benchmarks.
type Detector struct{}

// New returns a detector.
func New() detector.Detector {
return &Detector{}
}

// Name of the detector.
func (Detector) Name() string { return "cis/generic_linux/etcpasswdpermissions" }
func (Detector) Name() string { return Name }

// Version of the detector.
func (Detector) Version() int { return 0 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,22 @@ import (
"github.com/google/osv-scalibr/plugin"
)

const (
// Name of the detector.
Name = "cis/generic-linux/etcpasswdpermissions"
)

// Detector is a SCALIBR Detector for the CIS check "Ensure permissions on /etc/passwd- are configured"
// from the CIS Distribution Independent Linux benchmarks.
type Detector struct{}

// New returns a detector.
func New() detector.Detector {
return &Detector{}
}

// Name of the detector.
func (Detector) Name() string { return "cis/generic_linux/etcpasswdpermissions" }
func (Detector) Name() string { return Name }

// Version of the detector.
func (Detector) Version() int { return 0 }
Expand Down
10 changes: 9 additions & 1 deletion detector/cve/cve202011978/cve202011978.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type airflowPackageNames struct {
}

const (
// Name of the detector.
Name = "cve/cve-2020-11978"

airflowServerIP = "127.0.0.1"
airflowServerPort = 8080
defaultTimeout = 5 * time.Second
Expand Down Expand Up @@ -121,8 +124,13 @@ var (
// Detector is a SCALIBR Detector for CVE-2020-11978.
type Detector struct{}

// New returns a detector.
func New() detector.Detector {
return &Detector{}
}

// Name of the detector.
func (Detector) Name() string { return "cve/CVE-2020-11978" }
func (Detector) Name() string { return Name }

// Version of the detector.
func (Detector) Version() int { return 0 }
Expand Down
10 changes: 9 additions & 1 deletion detector/cve/cve202016846/cve202016846.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type saltPackageNames struct {
}

const (
// Name of the detector.
Name = "cve/cve-2020-16846"

saltServerPort = 8000
defaultTimeout = 5 * time.Second
saltServerIP = "127.0.0.1"
Expand Down Expand Up @@ -95,8 +98,13 @@ var (
// Detector is a SCALIBR Detector for CVE-2020-16846.
type Detector struct{}

// New returns a detector.
func New() detector.Detector {
return &Detector{}
}

// Name of the detector.
func (Detector) Name() string { return "cve/CVE-2020-16846" }
func (Detector) Name() string { return Name }

// Version of the detector.
func (Detector) Version() int { return 0 }
Expand Down
10 changes: 9 additions & 1 deletion detector/cve/cve202233891/cve202233891.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type sparkUIPackageNames struct {
}

const (
// Name of the detector.
Name = "cve/cve-2022-33891"

defaultTimeout = 5 * time.Second
)

Expand Down Expand Up @@ -72,8 +75,13 @@ var (
// Detector is a SCALIBR Detector for CVE-2022-33891.
type Detector struct{}

// New returns a detector.
func New() detector.Detector {
return &Detector{}
}

// Name of the detector.
func (Detector) Name() string { return "cve/CVE-2022-33891" }
func (Detector) Name() string { return Name }

// Version of the detector.
func (Detector) Version() int { return 0 }
Expand Down
12 changes: 11 additions & 1 deletion detector/cve/cve202338408/cve202338408.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import (
"github.com/google/osv-scalibr/plugin"
)

const (
// Name of the detector.
Name = "cve/cve-2023-38408"
)

var (
// Regex matching the "ssh -A" command.
sshRegex = regexp.MustCompile(`ssh (.* )?-\w*A`)
Expand All @@ -45,8 +50,13 @@ var (
// Detector is a SCALIBR Detector for CVE-2023-38408.
type Detector struct{}

// New returns a detector.
func New() detector.Detector {
return &Detector{}
}

// Name of the detector.
func (Detector) Name() string { return "cve/CVE-2023-38408" }
func (Detector) Name() string { return Name }

// Version of the detector.
func (Detector) Version() int { return 0 }
Expand Down
12 changes: 11 additions & 1 deletion detector/cve/cve20236019/cve20236019.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,21 @@ import (
"github.com/google/osv-scalibr/plugin"
)

const (
// Name of the detector.
Name = "cve/cve-2023-6019"
)

// Detector is a SCALIBR Detector for CVE-2023-6019
type Detector struct{}

// New returns a detector.
func New() detector.Detector {
return &Detector{}
}

// Name of the detector
func (Detector) Name() string { return "cve/CVE-2023-6019" }
func (Detector) Name() string { return Name }

// Version of the detector
func (Detector) Version() int { return 0 }
Expand Down
10 changes: 9 additions & 1 deletion detector/cve/cve20242912/cve20242912.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type bentomlPackageNames struct {
}

const (
// Name of the detector.
Name = "cve/cve-2024-2912"

payloadPath = "/tmp/bentoml-poc-CVE-2024-2912"
bentomlServerPort = 3000
defaultTimeout = 5 * time.Second
Expand All @@ -71,8 +74,13 @@ var (
// Detector is a SCALIBR Detector for CVE-2024-2912.
type Detector struct{}

// New returns a detector.
func New() detector.Detector {
return &Detector{}
}

// Name of the detector.
func (Detector) Name() string { return "cve/CVE-2024-2912" }
func (Detector) Name() string { return Name }

// Version of the detector.
func (Detector) Version() int { return 0 }
Expand Down
5 changes: 5 additions & 0 deletions detector/govulncheck/binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ type Detector struct {
OfflineVulnDBPath string
}

// New returns a detector.
func New() detector.Detector {
return &Detector{}
}

// Name of the detector.
func (Detector) Name() string { return Name }

Expand Down
77 changes: 41 additions & 36 deletions detector/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ package list

import (
"fmt"
"os"
"slices"
"strings"

"github.com/google/osv-scalibr/detector"
"github.com/google/osv-scalibr/detector/cis/generic_linux/etcpasswdpermissions"
Expand All @@ -28,59 +26,62 @@ import (
"github.com/google/osv-scalibr/detector/weakcredentials/etcshadow"
"github.com/google/osv-scalibr/detector/weakcredentials/filebrowser"
"github.com/google/osv-scalibr/detector/weakcredentials/winlocal"
"github.com/google/osv-scalibr/log"
"github.com/google/osv-scalibr/plugin"
"golang.org/x/exp/maps"
)

// InitFn is the detector initializer function.
type InitFn func() detector.Detector

// InitMap is a map of detector names to their initers.
type InitMap map[string][]InitFn

// CIS scanning related detectors.
var CIS []detector.Detector = []detector.Detector{&etcpasswdpermissions.Detector{}}
var CIS = InitMap{etcpasswdpermissions.Name: {etcpasswdpermissions.New}}

// CVE scanning related detectors.
var CVE []detector.Detector = []detector.Detector{&cve202338408.Detector{}}
var CVE = InitMap{cve202338408.Name: {cve202338408.New}}

// Govulncheck detectors.
var Govulncheck []detector.Detector = []detector.Detector{&binary.Detector{}}
var Govulncheck = InitMap{binary.Name: {binary.New}}

// Weakcreds detectors for weak credentials.
var Weakcreds []detector.Detector = []detector.Detector{
&etcshadow.Detector{},
&filebrowser.Detector{},
&winlocal.Detector{},
var Weakcreds = InitMap{
etcshadow.Name: {etcshadow.New},
filebrowser.Name: {filebrowser.New},
winlocal.Name: {winlocal.New},
}

// Default detectors that are recommended to be enabled.
var Default []detector.Detector = []detector.Detector{}
var Default = InitMap{}

// All detectors internal to SCALIBR.
var All []detector.Detector = slices.Concat(
var All = concat(
CIS,
CVE,
Govulncheck,
Weakcreds,
)

var detectorNames = map[string][]detector.Detector{
"cis": CIS,
"cve": CVE,
"govulncheck": Govulncheck,
"weakcreds": Weakcreds,
"default": Default,
"all": All,
}
var detectorNames = concat(All, InitMap{
"cis": vals(CIS),
"cve": vals(CVE),
"govulncheck": vals(Govulncheck),
"weakcreds": vals(Weakcreds),
"default": vals(Default),
"all": vals(All),
})

//nolint:gochecknoinits
func init() {
for _, d := range All {
register(d)
func concat(InitMaps ...InitMap) InitMap {
result := InitMap{}
for _, m := range InitMaps {
maps.Copy(result, m)
}
return result
}

func register(d detector.Detector) {
if _, ok := detectorNames[strings.ToLower(d.Name())]; ok {
log.Errorf("There are 2 detectors with the name: %q", d.Name())
os.Exit(1)
}
detectorNames[strings.ToLower(d.Name())] = []detector.Detector{d}
func vals(InitMap InitMap) []InitFn {
return slices.Concat(maps.Values(InitMap)...)
}

// FromCapabilities returns all detectors that can run under the specified
Expand All @@ -93,11 +94,14 @@ func FromCapabilities(capabs *plugin.Capabilities) []detector.Detector {
// FilterByCapabilities returns all detectors from the given list that can run
// under the specified capabilities (OS, direct filesystem access, network
// access, etc.) of the scanning environment.
func FilterByCapabilities(dets []detector.Detector, capabs *plugin.Capabilities) []detector.Detector {
func FilterByCapabilities(InitMap InitMap, capabs *plugin.Capabilities) []detector.Detector {
result := []detector.Detector{}
for _, det := range dets {
if err := plugin.ValidateRequirements(det, capabs); err == nil {
result = append(result, det)
for _, initers := range InitMap {
for _, initer := range initers {
ex := initer()
if err := plugin.ValidateRequirements(ex, capabs); err == nil {
result = append(result, ex)
}
}
}
return result
Expand All @@ -107,8 +111,9 @@ func FilterByCapabilities(dets []detector.Detector, capabs *plugin.Capabilities)
func DetectorsFromNames(names []string) ([]detector.Detector, error) {
resultMap := make(map[string]detector.Detector)
for _, n := range names {
if ds, ok := detectorNames[strings.ToLower(n)]; ok {
for _, d := range ds {
if initers, ok := detectorNames[n]; ok {
for _, initer := range initers {
d := initer()
if _, ok := resultMap[d.Name()]; !ok {
resultMap[d.Name()] = d
}
Expand Down
Loading