-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
detectors/feature: add apk feature detector
- Loading branch information
1 parent
e4b5930
commit fc908e6
Showing
3 changed files
with
612 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2016 clair authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package apk | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
|
||
"github.com/coreos/clair/database" | ||
"github.com/coreos/clair/utils/types" | ||
"github.com/coreos/clair/worker/detectors" | ||
"github.com/coreos/pkg/capnslog" | ||
) | ||
|
||
var log = capnslog.NewPackageLogger("github.com/coreos/clair", "worker/detectors/packages") | ||
|
||
func init() { | ||
detectors.RegisterFeaturesDetector("apk", &detector{}) | ||
} | ||
|
||
type detector struct{} | ||
|
||
func (d *detector) Detect(data map[string][]byte) ([]database.FeatureVersion, error) { | ||
file, exists := data["lib/apk/db/installed"] | ||
if !exists { | ||
return []database.FeatureVersion{}, nil | ||
} | ||
|
||
// Iterate over each line in the "installed" file attempting to parse each | ||
// package into a feature that will be stored in a set to guarantee | ||
// uniqueness. | ||
pkgSet := make(map[string]database.FeatureVersion) | ||
ipkg := database.FeatureVersion{} | ||
scanner := bufio.NewScanner(bytes.NewBuffer(file)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if len(line) < 2 { | ||
continue | ||
} | ||
|
||
// Parse the package name or version. | ||
switch { | ||
case line[:2] == "P:": | ||
ipkg.Feature.Name = line[2:] | ||
case line[:2] == "V:": | ||
var err error | ||
ipkg.Version, err = types.NewVersion(line[2:]) | ||
if err != nil { | ||
log.Warningf("could not parse package version '%s': %s. skipping", line[2:], err.Error()) | ||
} | ||
} | ||
|
||
// If we have a whole feature, store it in the set and try to parse a new | ||
// one. | ||
if ipkg.Feature.Name != "" && ipkg.Version.String() != "" { | ||
pkgSet[ipkg.Feature.Name+"#"+ipkg.Version.String()] = ipkg | ||
ipkg = database.FeatureVersion{} | ||
} | ||
} | ||
|
||
// Convert the map into a slice. | ||
pkgs := make([]database.FeatureVersion, 0, len(pkgSet)) | ||
for _, pkg := range pkgSet { | ||
pkgs = append(pkgs, pkg) | ||
} | ||
|
||
return pkgs, nil | ||
} | ||
|
||
func (d *detector) GetRequiredFiles() []string { | ||
return []string{"lib/apk/db/installed"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2016 clair authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package apk | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/coreos/clair/database" | ||
"github.com/coreos/clair/utils/types" | ||
"github.com/coreos/clair/worker/detectors/feature" | ||
) | ||
|
||
func TestAPKFeatureDetection(t *testing.T) { | ||
testData := []feature.TestData{ | ||
{ | ||
FeatureVersions: []database.FeatureVersion{ | ||
{ | ||
Feature: database.Feature{Name: "musl"}, | ||
Version: types.NewVersionUnsafe("1.1.14-r10"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "busybox"}, | ||
Version: types.NewVersionUnsafe("1.24.2-r9"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "alpine-baselayout"}, | ||
Version: types.NewVersionUnsafe("3.0.3-r0"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "alpine-keys"}, | ||
Version: types.NewVersionUnsafe("1.1-r0"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "zlib"}, | ||
Version: types.NewVersionUnsafe("1.2.8-r2"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "libcrypto1.0"}, | ||
Version: types.NewVersionUnsafe("1.0.2h-r1"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "libssl1.0"}, | ||
Version: types.NewVersionUnsafe("1.0.2h-r1"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "apk-tools"}, | ||
Version: types.NewVersionUnsafe("2.6.7-r0"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "scanelf"}, | ||
Version: types.NewVersionUnsafe("1.1.6-r0"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "musl-utils"}, | ||
Version: types.NewVersionUnsafe("1.1.14-r10"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "libc-utils"}, | ||
Version: types.NewVersionUnsafe("0.7-r0"), | ||
}, | ||
}, | ||
Data: map[string][]byte{ | ||
"lib/apk/db/installed": feature.LoadFileForTest("apk/testdata/installed"), | ||
}, | ||
}, | ||
} | ||
feature.TestDetector(t, &detector{}, testData) | ||
} |
Oops, something went wrong.