-
-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate policy evaluation into its own task
Signed-off-by: Walter de Boer <[email protected]>
- Loading branch information
Walter de Boer
committed
Mar 11, 2023
1 parent
5df38f2
commit 72e121c
Showing
7 changed files
with
295 additions
and
71 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/main/java/org/dependencytrack/event/PolicyEvaluationEvent.java
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,88 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) Steve Springett. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.event; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.dependencytrack.model.Component; | ||
import org.dependencytrack.model.Project; | ||
import alpine.event.framework.AbstractChainableEvent; | ||
|
||
/** | ||
* Defines a general purpose event to analyze components for vulnerabilities. | ||
* Additional logic in the event handler performs analysis on what specific | ||
* type of analysis should take place. | ||
* | ||
* @author Steve Springett | ||
* @since 3.0.0 | ||
*/ | ||
public class PolicyEvaluationEvent extends AbstractChainableEvent { | ||
|
||
private List<Component> components = new ArrayList<>(); | ||
private Project project; | ||
|
||
/** | ||
* Default constructed used to signal that a portfolio analysis | ||
* should be performed on all components. | ||
*/ | ||
public PolicyEvaluationEvent() { | ||
|
||
} | ||
|
||
/** | ||
* Creates an event to analyze the specified components. | ||
* @param components the components to analyze | ||
*/ | ||
public PolicyEvaluationEvent(final List<Component> components) { | ||
this.components = components; | ||
} | ||
|
||
/** | ||
* Creates an event to analyze the specified component. | ||
* @param component the component to analyze | ||
*/ | ||
public PolicyEvaluationEvent(final Component component) { | ||
this.components.add(component); | ||
} | ||
|
||
/** | ||
* Returns the list of components to analyze. | ||
* @return the list of components to analyze | ||
*/ | ||
public List<Component> getComponents() { | ||
return this.components; | ||
} | ||
|
||
/** | ||
* Fluent method that sets the project these components are | ||
* optionally a part of and returns this instance. | ||
*/ | ||
public PolicyEvaluationEvent project(final Project project) { | ||
this.project = project; | ||
return this; | ||
} | ||
|
||
/** | ||
* Returns the project these components are optionally a part of. | ||
*/ | ||
public Project getProject() { | ||
return project; | ||
} | ||
|
||
} |
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
61 changes: 61 additions & 0 deletions
61
src/main/java/org/dependencytrack/tasks/PolicyEvaluationTask.java
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,61 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) Steve Springett. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.tasks; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.dependencytrack.event.PolicyEvaluationEvent; | ||
import org.dependencytrack.event.ProjectMetricsUpdateEvent; | ||
import org.dependencytrack.model.Component; | ||
import org.dependencytrack.model.Project; | ||
import org.dependencytrack.policy.PolicyEngine; | ||
import alpine.common.logging.Logger; | ||
import alpine.event.framework.Event; | ||
import alpine.event.framework.Subscriber; | ||
|
||
public class PolicyEvaluationTask implements Subscriber { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(PolicyEvaluationTask.class); | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public void inform(final Event e) { | ||
if (e instanceof PolicyEvaluationEvent) { | ||
final PolicyEvaluationEvent event = (PolicyEvaluationEvent) e; | ||
LOGGER.info("Starting policy evaluation"); | ||
if (event.getComponents() != null && !event.getComponents().isEmpty()) { | ||
performPolicyEvaluation(event.getProject(), event.getComponents()); | ||
} else if (event.getProject() != null) { | ||
performPolicyEvaluation(event.getProject(), new ArrayList<>()); | ||
} | ||
LOGGER.info("Policy evaluation complete"); | ||
} | ||
} | ||
|
||
private void performPolicyEvaluation(Project project, List<Component> components) { | ||
// Evaluate the components against applicable policies via the PolicyEngine. | ||
final PolicyEngine pe = new PolicyEngine(); | ||
pe.evaluate(components); | ||
if (project != null) { | ||
Event.dispatch(new ProjectMetricsUpdateEvent(project.getUuid())); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.