| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.maven; |
| 19 | |
|
| 20 | |
import java.util.List; |
| 21 | |
import org.apache.maven.project.MavenProject; |
| 22 | |
import org.owasp.dependencycheck.analyzer.Analyzer; |
| 23 | |
import org.owasp.dependencycheck.analyzer.CPEAnalyzer; |
| 24 | |
import org.owasp.dependencycheck.analyzer.FileTypeAnalyzer; |
| 25 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 26 | |
import org.owasp.dependencycheck.data.update.exception.UpdateException; |
| 27 | |
import org.owasp.dependencycheck.exception.ExceptionCollection; |
| 28 | |
import org.owasp.dependencycheck.exception.InitializationException; |
| 29 | |
import org.owasp.dependencycheck.utils.Settings; |
| 30 | |
import org.slf4j.Logger; |
| 31 | |
import org.slf4j.LoggerFactory; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public class MavenEngine extends org.owasp.dependencycheck.Engine { |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 0 | private static final transient Logger LOGGER = LoggerFactory.getLogger(MavenEngine.class); |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
private static final String CPE_ANALYZER_KEY = "dependency-check-CPEAnalyzer"; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
private MavenProject currentProject; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
private List<MavenProject> reactorProjects; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public static final String UPDATE_EXECUTED_FLAG = "dependency-check-update-executed"; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 0 | public MavenEngine(MavenProject project, List<MavenProject> reactorProjects) throws DatabaseException { |
| 73 | 0 | this.currentProject = project; |
| 74 | 0 | this.reactorProjects = reactorProjects; |
| 75 | 0 | initializeEngine(); |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
@Override |
| 85 | |
public void analyzeDependencies() throws ExceptionCollection { |
| 86 | 0 | final MavenProject root = getExecutionRoot(); |
| 87 | 0 | if (root != null) { |
| 88 | 0 | LOGGER.debug("Checking root project, {}, if updates have already been completed", root.getArtifactId()); |
| 89 | |
} else { |
| 90 | 0 | LOGGER.debug("Checking root project, null, if updates have already been completed"); |
| 91 | |
} |
| 92 | 0 | if (root != null && root.getContextValue(UPDATE_EXECUTED_FLAG) != null) { |
| 93 | 0 | System.setProperty(Settings.KEYS.AUTO_UPDATE, Boolean.FALSE.toString()); |
| 94 | |
} |
| 95 | 0 | super.analyzeDependencies(); |
| 96 | 0 | if (root != null) { |
| 97 | 0 | root.setContextValue(UPDATE_EXECUTED_FLAG, Boolean.TRUE); |
| 98 | |
} |
| 99 | 0 | } |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public void update() throws UpdateException { |
| 107 | 0 | final MavenProject root = getExecutionRoot(); |
| 108 | 0 | if (root != null && root.getContextValue(UPDATE_EXECUTED_FLAG) != null) { |
| 109 | 0 | System.setProperty(Settings.KEYS.AUTO_UPDATE, Boolean.FALSE.toString()); |
| 110 | |
} |
| 111 | 0 | this.doUpdates(); |
| 112 | 0 | } |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | 0 | private MavenEngine() throws DatabaseException { |
| 121 | 0 | } |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
@Override |
| 131 | |
protected Analyzer initializeAnalyzer(Analyzer analyzer) throws InitializationException { |
| 132 | 0 | if (analyzer instanceof CPEAnalyzer) { |
| 133 | 0 | CPEAnalyzer cpe = getPreviouslyLoadedCPEAnalyzer(); |
| 134 | 0 | if (cpe != null && cpe.isOpen()) { |
| 135 | 0 | return cpe; |
| 136 | |
} |
| 137 | 0 | cpe = (CPEAnalyzer) super.initializeAnalyzer(analyzer); |
| 138 | 0 | storeCPEAnalyzer(cpe); |
| 139 | |
} |
| 140 | 0 | return super.initializeAnalyzer(analyzer); |
| 141 | |
} |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
@Override |
| 148 | |
public void cleanup() { |
| 149 | 0 | super.cleanup(); |
| 150 | 0 | if (currentProject == null || reactorProjects == null) { |
| 151 | 0 | return; |
| 152 | |
} |
| 153 | 0 | if (this.currentProject == reactorProjects.get(reactorProjects.size() - 1)) { |
| 154 | 0 | final CPEAnalyzer cpe = getPreviouslyLoadedCPEAnalyzer(); |
| 155 | 0 | if (cpe != null) { |
| 156 | 0 | cpe.close(); |
| 157 | |
} |
| 158 | |
} |
| 159 | 0 | } |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
@Override |
| 167 | |
protected void closeAnalyzer(Analyzer analyzer) { |
| 168 | 0 | if (analyzer instanceof CPEAnalyzer) { |
| 169 | 0 | if (getPreviouslyLoadedCPEAnalyzer() == null) { |
| 170 | 0 | super.closeAnalyzer(analyzer); |
| 171 | |
} |
| 172 | |
} else { |
| 173 | 0 | super.closeAnalyzer(analyzer); |
| 174 | |
} |
| 175 | 0 | } |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
private CPEAnalyzer getPreviouslyLoadedCPEAnalyzer() { |
| 183 | 0 | CPEAnalyzer cpe = null; |
| 184 | 0 | final MavenProject project = getExecutionRoot(); |
| 185 | 0 | if (project != null) { |
| 186 | 0 | final Object obj = project.getContextValue(CPE_ANALYZER_KEY); |
| 187 | 0 | if (obj != null && obj instanceof CPEAnalyzer) { |
| 188 | 0 | cpe = (CPEAnalyzer) project.getContextValue(CPE_ANALYZER_KEY); |
| 189 | |
} |
| 190 | |
} |
| 191 | 0 | return cpe; |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
private void storeCPEAnalyzer(CPEAnalyzer cpe) { |
| 200 | 0 | final MavenProject p = getExecutionRoot(); |
| 201 | 0 | if (p != null) { |
| 202 | 0 | p.setContextValue(CPE_ANALYZER_KEY, cpe); |
| 203 | |
} |
| 204 | 0 | } |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
MavenProject getExecutionRoot() { |
| 212 | 0 | if (reactorProjects == null) { |
| 213 | 0 | return null; |
| 214 | |
} |
| 215 | 0 | for (MavenProject p : reactorProjects) { |
| 216 | 0 | if (p.isExecutionRoot()) { |
| 217 | 0 | return p; |
| 218 | |
} |
| 219 | 0 | } |
| 220 | |
|
| 221 | 0 | if (this.currentProject == null) { |
| 222 | 0 | return null; |
| 223 | |
} |
| 224 | 0 | MavenProject p = this.currentProject; |
| 225 | 0 | while (p.getParent() != null) { |
| 226 | 0 | p = p.getParent(); |
| 227 | |
} |
| 228 | 0 | return p; |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
public void resetFileTypeAnalyzers() { |
| 238 | 0 | for (FileTypeAnalyzer a : getFileTypeAnalyzers()) { |
| 239 | 0 | a.reset(); |
| 240 | 0 | } |
| 241 | 0 | } |
| 242 | |
} |