Skip to content

Commit

Permalink
featurefmt: Extract PotentialNamespace
Browse files Browse the repository at this point in the history
PotentialNamespace is feature namespace extracted while detecting
features in layer. It will server for special feature detector. The
current detectors return empty namespace.
  • Loading branch information
Allda committed Mar 7, 2019
1 parent b3fe95e commit 34c2d96
Show file tree
Hide file tree
Showing 9 changed files with 496 additions and 495 deletions.
21 changes: 11 additions & 10 deletions database/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,23 @@ func NewNamespace(name string, versionFormat string) *Namespace {
// dpkg is the version format of the installer package manager, which in this
// case could be dpkg or apk.
type Feature struct {
Name string `json:"name"`
Version string `json:"version"`
VersionFormat string `json:"versionFormat"`
Type FeatureType `json:"type"`
Name string `json:"name"`
Version string `json:"version"`
VersionFormat string `json:"versionFormat"`
Type FeatureType `json:"type"`
PotentialNamespace Namespace `json:"potentialNamespace"`
}

func NewFeature(name string, version string, versionFormat string, featureType FeatureType) *Feature {
return &Feature{name, version, versionFormat, featureType}
func NewFeature(name string, version string, versionFormat string, featureType FeatureType, namespace Namespace) *Feature {
return &Feature{name, version, versionFormat, featureType, namespace}
}

func NewBinaryPackage(name string, version string, versionFormat string) *Feature {
return &Feature{name, version, versionFormat, BinaryPackage}
func NewBinaryPackage(name string, version string, versionFormat string, namespace Namespace) *Feature {
return &Feature{name, version, versionFormat, BinaryPackage, namespace}
}

func NewSourcePackage(name string, version string, versionFormat string) *Feature {
return &Feature{name, version, versionFormat, SourcePackage}
func NewSourcePackage(name string, version string, versionFormat string, namespace Namespace) *Feature {
return &Feature{name, version, versionFormat, SourcePackage, namespace}
}

// NamespacedFeature is a feature with determined namespace and can be affected
Expand Down
2 changes: 1 addition & 1 deletion database/pgsql/complex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func testGenRandomVulnerabilityAndNamespacedFeature(t *testing.T, store database
for i := 0; i < numFeatures; i++ {
version := rand.Intn(numFeatures)

features[i] = *database.NewSourcePackage(featureName, strconv.Itoa(version), featureVersionFormat)
features[i] = *database.NewSourcePackage(featureName, strconv.Itoa(version), featureVersionFormat, database.Namespace{})
nsFeatures[i] = database.NamespacedFeature{
Namespace: namespace,
Feature: features[i],
Expand Down
6 changes: 3 additions & 3 deletions database/pgsql/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestPersistFeatures(t *testing.T) {
defer cleanup()

invalid := database.Feature{}
valid := *database.NewBinaryPackage("mount", "2.31.1-0.4ubuntu3.1", "dpkg")
valid := *database.NewBinaryPackage("mount", "2.31.1-0.4ubuntu3.1", "dpkg", database.Namespace{})

// invalid
require.NotNil(t, tx.PersistFeatures([]database.Feature{invalid}))
Expand All @@ -45,9 +45,9 @@ func TestPersistNamespacedFeatures(t *testing.T) {
defer cleanup()

// existing features
f1 := database.NewSourcePackage("ourchat", "0.5", "dpkg")
f1 := database.NewSourcePackage("ourchat", "0.5", "dpkg", database.Namespace{})
// non-existing features
f2 := database.NewSourcePackage("fake!", "", "")
f2 := database.NewSourcePackage("fake!", "", "", database.Namespace{})
// exising namespace
n1 := database.NewNamespace("debian:7", "dpkg")
// non-existing namespace
Expand Down
10 changes: 5 additions & 5 deletions database/pgsql/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ import (
// int keys must be the consistent with the database ID.
var (
realFeatures = map[int]database.Feature{
1: {"ourchat", "0.5", "dpkg", "source"},
2: {"openssl", "1.0", "dpkg", "source"},
3: {"openssl", "2.0", "dpkg", "source"},
4: {"fake", "2.0", "rpm", "source"},
5: {"mount", "2.31.1-0.4ubuntu3.1", "dpkg", "binary"},
1: {"ourchat", "0.5", "dpkg", "source", database.Namespace{}},
2: {"openssl", "1.0", "dpkg", "source", database.Namespace{}},
3: {"openssl", "2.0", "dpkg", "source", database.Namespace{}},
4: {"fake", "2.0", "rpm", "source", database.Namespace{}},
5: {"mount", "2.31.1-0.4ubuntu3.1", "dpkg", "binary", database.Namespace{}},
}

realNamespaces = map[int]database.Namespace{
Expand Down
22 changes: 11 additions & 11 deletions ext/featurefmt/apk/apk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ func TestAPKFeatureDetection(t *testing.T) {
"valid case",
map[string]string{"lib/apk/db/installed": "apk/testdata/valid"},
[]database.Feature{
{"apk-tools", "2.6.7-r0", "dpkg", "binary"},
{"musl", "1.1.14-r10", "dpkg", "binary"},
{"libssl1.0", "1.0.2h-r1", "dpkg", "binary"},
{"libc-utils", "0.7-r0", "dpkg", "binary"},
{"busybox", "1.24.2-r9", "dpkg", "binary"},
{"scanelf", "1.1.6-r0", "dpkg", "binary"},
{"alpine-keys", "1.1-r0", "dpkg", "binary"},
{"libcrypto1.0", "1.0.2h-r1", "dpkg", "binary"},
{"zlib", "1.2.8-r2", "dpkg", "binary"},
{"musl-utils", "1.1.14-r10", "dpkg", "binary"},
{"alpine-baselayout", "3.0.3-r0", "dpkg", "binary"},
{"apk-tools", "2.6.7-r0", "dpkg", "binary", database.Namespace{}},
{"musl", "1.1.14-r10", "dpkg", "binary", database.Namespace{}},
{"libssl1.0", "1.0.2h-r1", "dpkg", "binary", database.Namespace{}},
{"libc-utils", "0.7-r0", "dpkg", "binary", database.Namespace{}},
{"busybox", "1.24.2-r9", "dpkg", "binary", database.Namespace{}},
{"scanelf", "1.1.6-r0", "dpkg", "binary", database.Namespace{}},
{"alpine-keys", "1.1-r0", "dpkg", "binary", database.Namespace{}},
{"libcrypto1.0", "1.0.2h-r1", "dpkg", "binary", database.Namespace{}},
{"zlib", "1.2.8-r2", "dpkg", "binary", database.Namespace{}},
{"musl-utils", "1.1.14-r10", "dpkg", "binary", database.Namespace{}},
{"alpine-baselayout", "3.0.3-r0", "dpkg", "binary", database.Namespace{}},
},
},
} {
Expand Down
4 changes: 2 additions & 2 deletions ext/featurefmt/dpkg/dpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func parseDpkgDB(scanner *bufio.Scanner) (binaryPackage *database.Feature, sourc
if err := versionfmt.Valid(dpkg.ParserName, version); err != nil {
log.WithError(err).WithFields(log.Fields{"name": name, "version": version}).Warning("skipped unparseable package")
} else {
binaryPackage = &database.Feature{name, version, dpkg.ParserName, database.BinaryPackage}
binaryPackage = &database.Feature{name, version, dpkg.ParserName, database.BinaryPackage, database.Namespace{}}
}
}

Expand All @@ -145,7 +145,7 @@ func parseDpkgDB(scanner *bufio.Scanner) (binaryPackage *database.Feature, sourc
if err := versionfmt.Valid(dpkg.ParserName, version); err != nil {
log.WithError(err).WithFields(log.Fields{"name": name, "version": version}).Warning("skipped unparseable package")
} else {
sourcePackage = &database.Feature{sourceName, sourceVersion, dpkg.ParserName, database.SourcePackage}
sourcePackage = &database.Feature{sourceName, sourceVersion, dpkg.ParserName, database.SourcePackage, database.Namespace{}}
}
}

Expand Down

0 comments on commit 34c2d96

Please sign in to comment.