-
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.
implement nodejs feature detector, namespace detector
Signed-off-by: liang chenye <liangchenye@huawei.com>
- Loading branch information
1 parent
fbc9cf4
commit 28c5531
Showing
21 changed files
with
434 additions
and
30 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
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
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
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
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
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
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
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,89 @@ | ||
// 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 npm | ||
|
||
import ( | ||
"encoding/json" | ||
"regexp" | ||
|
||
"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", "npm") | ||
|
||
nodejsRegexp = regexp.MustCompile("(node_modules|nodejs)/.*/package.json$") | ||
) | ||
|
||
// NpmFeaturesDetector implements FeaturesDetector and detects nodejs packages | ||
type NpmFeaturesDetector struct{} | ||
|
||
type NodejsPkg struct { | ||
Version string `json:"version"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func init() { | ||
detectors.RegisterFeaturesDetector("npm", &NpmFeaturesDetector{}) | ||
} | ||
|
||
// Detect detects packages using *package.json from the input data | ||
func (detector *NpmFeaturesDetector) Detect(data map[string][]byte) ([]database.FeatureVersion, error) { | ||
// Create a map to store packages and ensure their uniqueness | ||
packagesMap := make(map[string]database.FeatureVersion) | ||
for filename, content := range data { | ||
if !nodejsRegexp.MatchString(filename) { | ||
continue | ||
} | ||
|
||
var nodejsPkg NodejsPkg | ||
err := json.Unmarshal(content, &nodejsPkg) | ||
if err != nil { | ||
log.Warningf("could not parse nodejs package file '%s': %s. skipping", filename, err.Error()) | ||
continue | ||
} | ||
|
||
version, err := types.NewVersion(nodejsPkg.Version) | ||
if err != nil { | ||
log.Warningf("could not parse package version '%s': %s. skipping", nodejsPkg.Version, err.Error()) | ||
continue | ||
} | ||
|
||
pkg := database.FeatureVersion{ | ||
Feature: database.Feature{ | ||
Name: nodejsPkg.Name, | ||
}, | ||
Version: version, | ||
} | ||
packagesMap[pkg.Feature.Name+"#"+pkg.Version.String()] = pkg | ||
} | ||
|
||
// Convert the map to a slice | ||
packages := make([]database.FeatureVersion, 0, len(packagesMap)) | ||
for _, pkg := range packagesMap { | ||
packages = append(packages, pkg) | ||
} | ||
|
||
return packages, nil | ||
} | ||
|
||
// GetRequiredFiles returns the list of files required for Detect, without | ||
// leading / | ||
func (detector *NpmFeaturesDetector) GetRequiredFiles() []*regexp.Regexp { | ||
return []*regexp.Regexp{nodejsRegexp} | ||
} |
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,46 @@ | ||
// 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 npm | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/coreos/clair/database" | ||
"github.com/coreos/clair/utils/types" | ||
"github.com/coreos/clair/worker/detectors/feature" | ||
) | ||
|
||
var npmPackagesTests = []feature.FeatureVersionTest{ | ||
{ | ||
FeatureVersions: []database.FeatureVersion{ | ||
{ | ||
Feature: database.Feature{Name: "npm"}, | ||
Version: types.NewVersionUnsafe("1.3.10"), | ||
}, | ||
{ | ||
Feature: database.Feature{Name: "hawk"}, | ||
Version: types.NewVersionUnsafe("4.0.1"), | ||
}, | ||
}, | ||
Data: map[string][]byte{ | ||
"usr/lib/nodejs/npm/package.json": feature.LoadFileForTest("npm/testdata/npm/package.json"), | ||
"usr/local/lib/node_modules/hawk/package.json": feature.LoadFileForTest("npm/testdata/hawk/package.json"), | ||
}, | ||
}, | ||
} | ||
|
||
func TestNpmFeaturesDetector(t *testing.T) { | ||
feature.TestFeaturesDetector(t, &NpmFeaturesDetector{}, npmPackagesTests) | ||
} |
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,26 @@ | ||
{ | ||
"name": "hawk", | ||
"description": "HTTP Hawk Authentication Scheme", | ||
"version": "4.0.1", | ||
"author": { | ||
"name": "Eran Hammer", | ||
"email": "eran@hammer.io", | ||
"url": "http://hueniverse.com" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/hueniverse/hawk" | ||
}, | ||
"engines": { | ||
"node": ">=4.0.0" | ||
}, | ||
"dependencies": { | ||
"hoek": "3.x.x", | ||
"boom": "3.x.x", | ||
"cryptiles": "3.x.x", | ||
"sntp": "2.x.x" | ||
}, | ||
"license": "BSD-3-Clause", | ||
"_id": "hawk@4.1.2", | ||
"_from": "hawk@" | ||
} |
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,19 @@ | ||
{ | ||
"version": "1.3.10", | ||
"name": "npm", | ||
"homepage": "https://npmjs.org/doc/", | ||
"author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me)", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/isaacs/npm" | ||
}, | ||
"dependencies": { | ||
"semver": "~2.1.0", | ||
"github-url-from-git": "1.1.1" | ||
}, | ||
"engines": { | ||
"node": ">=0.6", | ||
"npm": "1" | ||
}, | ||
"license": "Artistic-2.0" | ||
} |
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
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
Oops, something went wrong.