-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPluginUtils.java
More file actions
273 lines (239 loc) · 7.73 KB
/
Copy pathPluginUtils.java
File metadata and controls
273 lines (239 loc) · 7.73 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package com.checkmarx.eclipse.utils;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ui.PlatformUI;
import com.checkmarx.ast.results.result.Node;
import com.checkmarx.ast.results.result.Result;
import com.checkmarx.eclipse.enums.ActionName;
import com.checkmarx.eclipse.enums.Severity;
import com.checkmarx.eclipse.properties.Preferences;
import com.checkmarx.eclipse.views.DataProvider;
import com.checkmarx.eclipse.views.DisplayModel;
import com.checkmarx.eclipse.views.filters.FilterState;
public class PluginUtils {
private static final String PARAM_TIMESTAMP_PATTERN = "yyyy/MM/dd HH:mm:ss";
private static final String PARAM_SCAN_ID_VALID_FORMAT = "[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[0-9a-f]{12}";
private static final String PARAM_LINE = "line %d";
/**
* Converts a String timestamp to a specific format
*
* @param timestamp
* @return
*/
public static String convertStringTimeStamp(String timestamp) {
String parsedDate = null;
try {
Instant instant = Instant.parse(timestamp);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(PARAM_TIMESTAMP_PATTERN).withZone(ZoneId.systemDefault());
parsedDate = dateTimeFormatter.format(instant);
} catch (Exception e) {
System.out.println(e);
return timestamp;
}
return parsedDate;
}
/**
* Validate scan id format
*
* @param scanId
* @return
*/
public static boolean validateScanIdFormat(String scanId) {
return scanId.matches(PARAM_SCAN_ID_VALID_FORMAT);
}
/**
* Enables a combo viewer
*
* @param comboviewer
* @param enable
*/
public static void enableComboViewer(ComboViewer comboviewer, boolean enable) {
comboviewer.getCombo().setEnabled(enable);
}
/**
* Set combo viewer placeholder
*
* @param comboViewer
* @param text
*/
public static void setTextForComboViewer(ComboViewer comboViewer, String text) {
comboViewer.getCombo().setText(text);
comboViewer.getCombo().update();
}
/**
* Enable/Disable filter actions
*
* @param filterActions
*/
public static void updateFiltersEnabledAndCheckedState(List<Action> filterActions) {
for (Action action : filterActions) {
// avoid to disable group by severity , group by query name and group by state actions
if (!action.getId().equals(ActionName.GROUP_BY_SEVERITY.name()) && !action.getId().equals(ActionName.GROUP_BY_QUERY_NAME.name()) && !action.getId().equals(ActionName.GROUP_BY_STATE_NAME.name()) ) {
action.setEnabled(DataProvider.getInstance().containsResults());
}
if(!action.getId().equals(ActionName.FILTER_CHANGED.name())) {
action.setChecked(FilterState.isSeverityEnabled(action.getId()));
}
}
}
/**
* Create a display model to be presented in the tree
*
* @param message
* @return
*/
public static DisplayModel message(String message) {
return new DisplayModel.DisplayModelBuilder(message).build();
}
/**
* Show message in the tree
*
* @param message
*/
public static void showMessage(DisplayModel rootModel, TreeViewer viewer, String message) {
rootModel.children.clear();
rootModel.children.add(PluginUtils.message(message));
viewer.refresh();
}
/**
* Clear message in the tree
*
*/
public static void clearMessage(DisplayModel rootModel,TreeViewer viewer) {
rootModel.children.clear();
viewer.refresh();
}
/**
* Get Event Broker
*
* @return
*/
public static IEventBroker getEventBroker() {
return (IEventBroker) PlatformUI.getWorkbench().getService(IEventBroker.class);
}
/**
* Check if checkmarx credentials are defined in the Preferences
*
* @return
*/
public static boolean areCredentialsDefined() {
return StringUtils.isNotBlank(Preferences.getApiKey());
}
/**
* Add Checkmarx vulnerabilities to Problems View
*
* @param resultsList
*/
public static void addVulnerabilitiesToProblemsView(List<Result> resultsList) {
for (Result result : resultsList) {
List<Node> nodeList = result.getData().getNodes();
if (nodeList == null) {
continue;
}
for (Node node : nodeList) {
String fileName = node.getFileName();
Path filePath = new Path(fileName);
List<IFile> filesFound = findFileInWorkspace(filePath.lastSegment());
for (IFile file : filesFound) {
try {
IMarker fileMarker = file.createMarker(IMarker.PROBLEM);
fileMarker.setAttribute(IMarker.MESSAGE, node.getName());
fileMarker.setAttribute(IMarker.LOCATION, String.format(PARAM_LINE, node.getLine()));
fileMarker.setAttribute(IMarker.LINE_NUMBER, node.getLine());
fileMarker.setAttribute(IMarker.SOURCE_ID, PluginConstants.PROBLEM_SOURCE_ID);
fileMarker.setAttribute(IMarker.SEVERITY, getIMarkerSeverity(result.getSeverity()));
} catch (CoreException e) {
CxLogger.error(String.format(PluginConstants.ERROR_OPENING_FILE, e.getMessage()), e);
}
}
}
}
}
/**
* Get IMarker severity based on each checkmarx result severity
*
* @param resultSeverity
* @return
*/
private static Integer getIMarkerSeverity(String resultSeverity) {
Severity severity = Severity.getSeverity(resultSeverity);
switch (severity) {
case CRITICAL:
return IMarker.SEVERITY_ERROR;
case HIGH:
return IMarker.SEVERITY_ERROR;
case MEDIUM:
return IMarker.SEVERITY_WARNING;
case LOW:
return IMarker.SEVERITY_INFO;
case INFO:
return IMarker.SEVERITY_INFO;
default:
break;
}
return IMarker.SEVERITY_INFO;
}
/**
* Find files in workspace
*
* @param fileName
* @return
*/
public static List<IFile> findFileInWorkspace(final String fileName) {
final List<IFile> foundFiles = new ArrayList<IFile>();
try {
// visiting only resources proxy because we obtain the resource only when matching name, thus the workspace traversal is much faster
ResourcesPlugin.getWorkspace().getRoot().accept(new IResourceProxyVisitor() {
@Override
public boolean visit(IResourceProxy resourceProxy) throws CoreException {
if (resourceProxy.getType() == IResource.FILE) {
String resourceName = resourceProxy.getName();
if (resourceName.equals(fileName)) {
IFile foundFile = (IFile) resourceProxy.requestResource();
foundFiles.add(foundFile);
}
}
return true;
}
}, IResource.NONE);
} catch (Exception e) {
CxLogger.error(String.format(PluginConstants.ERROR_FINDING_FILE, e.getMessage()), e);
}
return foundFiles;
}
/**
* Clear checkmarx vulnerabilities from Problems View
*/
public static void clearVulnerabilitiesFromProblemsView() {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IResource resource = workspace.getRoot();
IMarker[] markers;
try {
markers = resource.findMarkers(IMarker.MARKER, true, IResource.DEPTH_INFINITE);
for (IMarker m : markers) {
if(m.getAttribute(IMarker.SOURCE_ID) != null && m.getAttribute(IMarker.SOURCE_ID).equals(PluginConstants.PROBLEM_SOURCE_ID)) {
m.delete();
}
}
} catch (CoreException e) {
CxLogger.error(String.format(PluginConstants.ERROR_FINDING_OR_DELETING_MARKER, e.getMessage()), e);
}
}
}