-
-
Notifications
You must be signed in to change notification settings - Fork 778
Expand file tree
/
Copy pathPolicyEngine.java
More file actions
180 lines (164 loc) · 8.15 KB
/
Copy pathPolicyEngine.java
File metadata and controls
180 lines (164 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* 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) OWASP Foundation. All Rights Reserved.
*/
package org.dependencytrack.policy;
import alpine.common.logging.Logger;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.Policy;
import org.dependencytrack.model.PolicyCondition;
import org.dependencytrack.model.PolicyViolation;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.Tag;
import org.dependencytrack.persistence.QueryManager;
import org.dependencytrack.util.NotificationUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* A lightweight policy engine that evaluates a list of components against
* all defined policies. Each policy is evaluated using individual policy
* evaluators. Additional evaluators can be easily added in the future.
*
* @author Steve Springett
* @since 4.0.0
*/
public class PolicyEngine {
private static final Logger LOGGER = Logger.getLogger(PolicyEngine.class);
private final List<PolicyEvaluator> evaluators = new ArrayList<>();
public PolicyEngine() {
evaluators.add(new SeverityPolicyEvaluator());
evaluators.add(new CoordinatesPolicyEvaluator());
evaluators.add(new LicenseGroupPolicyEvaluator());
evaluators.add(new LicensePolicyEvaluator());
evaluators.add(new PackageURLPolicyEvaluator());
evaluators.add(new CpePolicyEvaluator());
evaluators.add(new SwidTagIdPolicyEvaluator());
evaluators.add(new VersionPolicyEvaluator());
evaluators.add(new ComponentAgePolicyEvaluator());
evaluators.add(new ComponentHashPolicyEvaluator());
evaluators.add(new CwePolicyEvaluator());
evaluators.add(new VulnerabilityIdPolicyEvaluator());
evaluators.add(new VersionDistancePolicyEvaluator());
evaluators.add(new EpssPolicyEvaluator());
}
public List<PolicyViolation> evaluate(final List<Component> components) {
LOGGER.info("Evaluating " + components.size() + " component(s) against applicable policies");
List<PolicyViolation> violations = new ArrayList<>();
try (final QueryManager qm = new QueryManager()) {
final List<Policy> policies = qm.getAllPolicies();
for (final Component component : components) {
final Component componentFromDb = qm.getObjectById(Component.class, component.getId());
violations.addAll(this.evaluate(qm, policies, componentFromDb));
}
}
LOGGER.info("Policy analysis complete");
return violations;
}
private List<PolicyViolation> evaluate(final QueryManager qm, final List<Policy> policies, final Component component) {
final List<PolicyViolation> policyViolations = new ArrayList<>();
final List<PolicyViolation> existingPolicyViolations = qm.detach(qm.getAllPolicyViolations(component));
for (final Policy policy : policies) {
if(policy.isOnlyLatestProjectVersion() && Boolean.FALSE.equals(component.getProject().isLatest())) {
continue;
}
if (policy.isGlobal() || isPolicyAssignedToProject(policy, component.getProject())
|| isPolicyAssignedToProjectTag(policy, component.getProject())) {
LOGGER.debug("Evaluating component (" + component.getUuid() + ") against policy (" + policy.getUuid() + ")");
final List<PolicyConditionViolation> policyConditionViolations = new ArrayList<>();
int policyConditionsViolated = 0;
for (final PolicyEvaluator evaluator : evaluators) {
evaluator.setQueryManager(qm);
final List<PolicyConditionViolation> policyConditionViolationsFromEvaluator = evaluator.evaluate(policy, component);
if (!policyConditionViolationsFromEvaluator.isEmpty()) {
policyConditionViolations.addAll(policyConditionViolationsFromEvaluator);
policyConditionsViolated += (int) policyConditionViolationsFromEvaluator.stream()
.map(pcv -> pcv.getPolicyCondition().getId())
.sorted()
.distinct()
.count();
}
}
if (Policy.Operator.ANY == policy.getOperator()) {
if (policyConditionsViolated > 0) {
policyViolations.addAll(createPolicyViolations(policyConditionViolations));
}
} else if (Policy.Operator.ALL == policy.getOperator() && policyConditionsViolated == policy.getPolicyConditions().size()) {
policyViolations.addAll(createPolicyViolations(policyConditionViolations));
}
}
}
qm.reconcilePolicyViolations(component, policyViolations);
for (final PolicyViolation pv : qm.getAllPolicyViolations(component)) {
if (existingPolicyViolations.stream().noneMatch(existingViolation -> existingViolation.getId() == pv.getId())) {
NotificationUtil.analyzeNotificationCriteria(qm, pv);
}
}
return policyViolations;
}
private boolean isPolicyAssignedToProject(Policy policy, Project project) {
if (policy.getProjects() == null || policy.getProjects().isEmpty()) {
return false;
}
return (policy.getProjects().stream().anyMatch(p -> p.getId() == project.getId()) || (Boolean.TRUE.equals(policy.isIncludeChildren()) && isPolicyAssignedToParentProject(policy, project)));
}
private List<PolicyViolation> createPolicyViolations(final List<PolicyConditionViolation> pcvList) {
final List<PolicyViolation> policyViolations = new ArrayList<>();
for (PolicyConditionViolation pcv : pcvList) {
final PolicyViolation pv = new PolicyViolation();
pv.setComponent(pcv.getComponent());
pv.setPolicyCondition(pcv.getPolicyCondition());
pv.setType(determineViolationType(pcv.getPolicyCondition().getSubject()));
pv.setTimestamp(new Date());
policyViolations.add(pv);
}
return policyViolations;
}
public PolicyViolation.Type determineViolationType(final PolicyCondition.Subject subject) {
if (subject == null) {
return null;
}
return switch (subject) {
case CWE, SEVERITY, VULNERABILITY_ID, EPSS -> PolicyViolation.Type.SECURITY;
case AGE, COORDINATES, PACKAGE_URL, CPE, SWID_TAGID, COMPONENT_HASH, VERSION, VERSION_DISTANCE ->
PolicyViolation.Type.OPERATIONAL;
case LICENSE, LICENSE_GROUP -> PolicyViolation.Type.LICENSE;
};
}
private boolean isPolicyAssignedToProjectTag(Policy policy, Project project) {
if (policy.getTags() == null || policy.getTags().isEmpty()) {
return false;
}
boolean flag = false;
for (Tag projectTag : project.getTags()) {
flag = policy.getTags().stream().anyMatch(policyTag -> policyTag.getId() == projectTag.getId());
if (flag) {
break;
}
}
return policy.isInvertTagMatch() != flag;
}
private boolean isPolicyAssignedToParentProject(Policy policy, Project child) {
if (child.getParent() == null) {
return false;
}
if (policy.getProjects().stream().anyMatch(p -> p.getId() == child.getParent().getId())) {
return true;
}
return isPolicyAssignedToParentProject(policy, child.getParent());
}
}