-
Notifications
You must be signed in to change notification settings - Fork 13
Refactor common detection function to its own package #926
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,139 @@ | ||
package detection | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"github.com/stackrox/scanner/database" | ||
"github.com/stackrox/scanner/ext/featurefmt" | ||
"github.com/stackrox/scanner/ext/featurens" | ||
"github.com/stackrox/scanner/pkg/analyzer" | ||
"github.com/stackrox/scanner/pkg/component" | ||
features2 "github.com/stackrox/scanner/pkg/features" | ||
"github.com/stackrox/scanner/pkg/rhelv2/rpm" | ||
"github.com/stackrox/scanner/pkg/wellknownnamespaces" | ||
) | ||
|
||
// LogLayerName is the name of the log field holding the detection target. | ||
const LogLayerName = "layer" | ||
|
||
// DetectComponents detects the namespace and extracts the components present in | ||
// the files of a filesystem or image layer. For layers, the parent layer should | ||
// be specified. For filesystems, which don't have the concept of intermediate | ||
// layers, or the root layer, use `nil`. Notice that language components are not | ||
// extracted by DetectComponents, but if provided they are annotated with | ||
// certified RHEL dependencies, and returned. | ||
func DetectComponents(name string, files analyzer.Files, parent *database.Layer, languageComponents []*component.Component, uncertifiedRHEL bool) (*database.Namespace, []database.FeatureVersion, *database.RHELv2Components, []*component.Component, error) { | ||
namespace := DetectNamespace(name, files, parent, uncertifiedRHEL) | ||
|
||
var featureVersions []database.FeatureVersion | ||
var rhelfeatures *database.RHELv2Components | ||
|
||
if namespace != nil && wellknownnamespaces.IsRHELNamespace(namespace.Name) { | ||
// This is a RHEL-based image that must be scanned in a certified manner. | ||
// Use the RHELv2 scanner instead. | ||
packages, cpes, err := rpm.ListFeatures(files) | ||
if err != nil { | ||
return nil, nil, nil, nil, err | ||
} | ||
rhelfeatures = &database.RHELv2Components{ | ||
Dist: namespace.Name, | ||
Packages: packages, | ||
CPEs: cpes, | ||
} | ||
logrus.WithFields(logrus.Fields{LogLayerName: name, "rhel package count": len(packages), "rhel cpe count": len(cpes)}).Debug("detected rhelv2 features") | ||
if err := rpm.AnnotateComponentsWithPackageManagerInfo(files, languageComponents); err != nil { | ||
logrus.WithError(err).Errorf("Failed to analyze package manager info for language components: %s", name) | ||
} | ||
} else { | ||
var err error | ||
// Detect features. | ||
featureVersions, err = detectFeatureVersions(name, files, namespace, parent) | ||
if err != nil { | ||
return nil, nil, nil, nil, err | ||
} | ||
if len(featureVersions) > 0 { | ||
logrus.WithFields(logrus.Fields{LogLayerName: name, "feature count": len(featureVersions)}).Debug("detected features") | ||
} | ||
} | ||
return namespace, featureVersions, rhelfeatures, languageComponents, nil | ||
} | ||
|
||
// DetectNamespace detects the layer's namespace. | ||
func DetectNamespace(name string, files analyzer.Files, parent *database.Layer, uncertifiedRHEL bool) *database.Namespace { | ||
namespace := featurens.Detect(files, &featurens.DetectorOptions{ | ||
UncertifiedRHEL: uncertifiedRHEL, | ||
}) | ||
if namespace != nil { | ||
logrus.WithFields(logrus.Fields{LogLayerName: name, "detected namespace": namespace.Name}).Debug("detected namespace") | ||
return namespace | ||
} | ||
|
||
// Fallback to the parent's namespace. | ||
if parent != nil { | ||
namespace = parent.Namespace | ||
if namespace != nil { | ||
logrus.WithFields(logrus.Fields{LogLayerName: name, "detected namespace": namespace.Name}).Debug("detected namespace (from parent)") | ||
return namespace | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func detectFeatureVersions(name string, files analyzer.Files, namespace *database.Namespace, parent *database.Layer) (features []database.FeatureVersion, err error) { | ||
// TODO(Quentin-M): We need to pass the parent image to DetectFeatures because it's possible that | ||
// some detectors would need it in order to produce the entire feature list (if they can only | ||
// detect a diff). Also, we should probably pass the detected namespace so detectors could | ||
// make their own decision. | ||
features, err = featurefmt.ListFeatures(files) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// If there are no FeatureVersions, use parent's FeatureVersions if possible. | ||
// TODO(Quentin-M): We eventually want to give the choice to each detectors to use none/some of | ||
// their parent's FeatureVersions. It would be useful for detectors that can't find their entire | ||
// result using one Layer. | ||
if len(features) == 0 && parent != nil { | ||
features = parent.Features | ||
return | ||
} | ||
|
||
// Build a map of the namespaces for each FeatureVersion in our parent layer. | ||
parentFeatureNamespaces := make(map[string]database.Namespace) | ||
if parent != nil { | ||
for _, parentFeature := range parent.Features { | ||
parentFeatureNamespaces[parentFeature.Feature.Name+":"+parentFeature.Version] = parentFeature.Feature.Namespace | ||
} | ||
} | ||
|
||
// Ensure that each FeatureVersion has an associated Namespace. | ||
for i, feature := range features { | ||
if feature.Feature.Namespace.Name != "" { | ||
// There is a Namespace associated. | ||
continue | ||
} | ||
|
||
if parentFeatureNamespace, ok := parentFeatureNamespaces[feature.Feature.Name+":"+feature.Version]; ok { | ||
// The FeatureVersion is present in the parent layer; associate with their Namespace. | ||
features[i].Feature.Namespace = parentFeatureNamespace | ||
continue | ||
} | ||
|
||
if namespace != nil { | ||
// The Namespace has been detected in this layer; associate it. | ||
features[i].Feature.Namespace = *namespace | ||
continue | ||
} | ||
|
||
logrus.WithFields(logrus.Fields{"feature name": feature.Feature.Name, "feature version": feature.Version, LogLayerName: name}).Warning("Namespace unknown") | ||
if features2.ContinueUnknownOS.Enabled() { | ||
features = nil | ||
return | ||
} | ||
|
||
err = ErrUnsupported | ||
return | ||
} | ||
|
||
return | ||
} |
This file contains hidden or 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,13 @@ | ||
package detection | ||
|
||
import "github.com/stackrox/scanner/pkg/commonerr" | ||
|
||
var ( | ||
// ErrUnsupported is the error that should be raised when an OS or package | ||
// manager is not supported. | ||
ErrUnsupported = commonerr.NewBadRequestError("worker: OS and/or package manager are not supported") | ||
|
||
// ErrParentUnknown is the error that should be raised when a parent layer | ||
// has yet to be processed for the current layer. | ||
ErrParentUnknown = commonerr.NewBadRequestError("worker: parent layer is unknown, it must be processed first") | ||
) |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming this is simply copy/pasted (aside from the function's rename)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved around the
name
to be the first parameter to matchDetectNamespace
. Other than that, yes, this is a copy'n'paste.