-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCliService.cs
More file actions
286 lines (234 loc) · 11.9 KB
/
CliService.cs
File metadata and controls
286 lines (234 loc) · 11.9 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
274
275
276
277
278
279
280
281
282
283
284
285
286
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Cycode.VisualStudio.Extension.Shared.Cli;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO.ScanResult;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO.ScanResult.Iac;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO.ScanResult.Sast;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO.ScanResult.Sca;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO.ScanResult.Secret;
using Cycode.VisualStudio.Extension.Shared.DTO;
using Cycode.VisualStudio.Extension.Shared.Helpers;
using Cycode.VisualStudio.Extension.Shared.Services.ErrorList;
namespace Cycode.VisualStudio.Extension.Shared.Services;
public interface ICliService {
void ResetPluginCliState();
Task SyncStatusAsync(CancellationToken cancellationToken = default);
Task<bool> DoAuthAsync(CancellationToken cancellationToken = default);
Task ScanPathsSecretsAsync(
List<string> paths, bool onDemand = true, CancellationToken cancellationToken = default
);
Task ScanPathsScaAsync(
List<string> paths, bool onDemand = true, CancellationToken cancellationToken = default
);
Task ScanPathsIacAsync(
List<string> paths, bool onDemand = true, CancellationToken cancellationToken = default
);
Task ScanPathsSastAsync(
List<string> paths, bool onDemand = true, CancellationToken cancellationToken = default
);
Task<bool> DoIgnoreAsync(
CliScanType scanType, CliIgnoreType ignoreType, string value, CancellationToken cancellationToken = default
);
Task<AiRemediationResultData> GetAiRemediationAsync(string detectionId, CancellationToken cancellationToken = default);
}
public class CliService(
ILoggerService logger,
IStateService stateService,
ITemporaryStateService tempState,
IScanResultsService scanResultsService,
IErrorTaskCreatorService errorTaskCreatorService
) : ICliService {
private readonly CliWrapper _cli = new(SolutionHelper.GetSolutionRootDirectory);
private readonly ExtensionState _pluginState = stateService.Load();
public async Task SyncStatusAsync(CancellationToken cancellationToken = default) {
CliResult<StatusResult> result = await _cli.ExecuteCommandAsync<StatusResult>(["status"], cancellationToken);
CliResult<StatusResult>.Success processedResult = ProcessResult(result);
if (processedResult == null) {
ResetPluginCliState();
return;
}
tempState.CliInstalled = true;
tempState.CliAuthed = processedResult.Result.IsAuthenticated;
tempState.CliStatus = processedResult.Result;
_pluginState.CliVer = processedResult.Result.Version;
stateService.Save();
if (!tempState.CliAuthed)
ShowErrorNotification("You are not authenticated in Cycode. Please authenticate");
}
public async Task<bool> DoAuthAsync(CancellationToken cancellationToken = default) {
CliResult<AuthResult> result = await _cli.ExecuteCommandAsync<AuthResult>(["auth"], cancellationToken);
CliResult<AuthResult>.Success processedResult = ProcessResult(result);
if (processedResult == null) return false;
tempState.CliAuthed = processedResult.Result.Result;
if (!tempState.CliAuthed) ShowErrorNotification("Authentication failed. Please try again");
return tempState.CliAuthed;
}
public async Task ScanPathsSecretsAsync(
List<string> paths, bool onDemand = true, CancellationToken cancellationToken = default
) {
CliResult<SecretScanResult>.Success results =
await ScanPathsAsync<SecretScanResult>(paths, CliScanType.Secret, cancellationToken);
if (results == null) {
logger.Warn("Failed to scan Secret paths: {0}", string.Join(", ", paths));
return;
}
await ProcessCliScanResultAsync(CliScanType.Secret, results.Result.Detections, onDemand);
}
public async Task ScanPathsScaAsync(
List<string> paths, bool onDemand = true, CancellationToken cancellationToken = default
) {
CliResult<ScaScanResult>.Success results =
await ScanPathsAsync<ScaScanResult>(paths, CliScanType.Sca, cancellationToken);
if (results == null) {
logger.Warn("Failed to scan SCA paths: {0}", string.Join(", ", paths));
return;
}
await ProcessCliScanResultAsync(CliScanType.Sca, results.Result.Detections, onDemand);
}
public async Task ScanPathsIacAsync(
List<string> paths, bool onDemand = true, CancellationToken cancellationToken = default
) {
CliResult<IacScanResult>.Success results =
await ScanPathsAsync<IacScanResult>(paths, CliScanType.Iac, cancellationToken);
if (results == null) {
logger.Warn("Failed to scan IaC paths: {0}", string.Join(", ", paths));
return;
}
results = new CliResult<IacScanResult>.Success(new IacScanResult {
Detections = FilterUnsupportedIacDetections(results.Result.Detections),
Errors = results.Result.Errors
});
await ProcessCliScanResultAsync(CliScanType.Iac, results.Result.Detections, onDemand);
}
public async Task ScanPathsSastAsync(
List<string> paths, bool onDemand = true, CancellationToken cancellationToken = default
) {
CliResult<SastScanResult>.Success results =
await ScanPathsAsync<SastScanResult>(paths, CliScanType.Sast, cancellationToken);
if (results == null) {
logger.Warn("Failed to scan SAST paths: {0}", string.Join(", ", paths));
return;
}
await ProcessCliScanResultAsync(CliScanType.Sast, results.Result.Detections, onDemand);
}
public void ResetPluginCliState() {
logger.Debug("Resetting plugin CLI state");
tempState.CliAuthed = false;
tempState.CliInstalled = false;
_pluginState.CliVer = null;
stateService.Save();
}
public async Task<bool> DoIgnoreAsync(
CliScanType scanType, CliIgnoreType ignoreType, string value, CancellationToken cancellationToken = default
) {
string[] args = [
"ignore", "-t", scanType.ToString().ToLower(), MapIgnoreTypeToOptionName(ignoreType), value
];
CliResult<object> result = await _cli.ExecuteCommandAsync<object>(args, cancellationToken);
CliResult<object> processedResult = ProcessResult(result);
return processedResult != null;
}
private async Task ProcessCliScanResultAsync<T>(CliScanType scanType, List<T> detections, bool onDemand) {
scanResultsService.SetDetections(scanType, detections);
await errorTaskCreatorService.RecreateAsync();
ShowScanFileResultNotification(scanType, detections.Count, onDemand);
}
private static List<IacDetection> FilterUnsupportedIacDetections(List<IacDetection> detections) {
return detections.Where(detection => {
IacDetectionDetails detectionDetails = detection.DetectionDetails;
string filePath = detectionDetails.GetFilePath();
// TF plans are virtual files that do not exist in the file system
// "file_name": "1711298252-/Users/ilyasiamionau/projects/cycode/ilya-siamionau-payloads/tfplan.tf",
// skip such detections
try {
string _ = Path.GetFullPath(filePath);
return true;
} catch (Exception) {
return false;
}
}).ToList();
}
private static void ShowErrorNotification(string message) {
VS.StatusBar.ShowMessageAsync(message).FireAndForget();
}
private static void ShowScanFileResultNotification(CliScanType scanType, int detectionsCount, bool onDemand) {
string scanTypeString = CliUtilities.GetScanTypeDisplayName(scanType);
string message = "";
if (detectionsCount > 0)
message = $"Cycode has detected {detectionsCount} {scanTypeString} issues in your file.";
else if (onDemand) message = $"No {scanTypeString} issues were found.";
VS.StatusBar.ShowMessageAsync(message).FireAndForget();
}
private CliResult<T>.Success ProcessResult<T>(CliResult<T> result) {
switch (result) {
case CliResult<T>.Error errorResult:
logger.Info("[ProcessResult] Error result. Showing error notification");
ShowErrorNotification(errorResult.Result.Message);
return null;
case CliResult<T>.Panic { ExitCode: ExitCode.Termination }:
logger.Info("[ProcessResult] User termination panic result");
return null;
case CliResult<T>.Panic panicResult:
logger.Info("[ProcessResult] Panic result. Showing error notification");
ShowErrorNotification(panicResult.ErrorMessage);
return null;
case CliResult<T>.Success { Result: ScanResultBase } successScanResult:
logger.Info("[ProcessResult] Success scan result");
List<CliError> errors = (successScanResult.Result as ScanResultBase)?.Errors;
if (errors == null || errors.Count == 0) return successScanResult;
foreach (CliError error in errors) ShowErrorNotification(error.Message);
// we trust that it is not possible to have both errors and detections
return null;
case CliResult<T>.Success successResult:
logger.Info("[ProcessResult] Success result (not scan one)");
return successResult;
default:
ShowErrorNotification("Unknown CliResult type");
logger.Error("[ProcessResult] Unknown CliResult type: {0}", result.GetType().Name);
return null;
}
}
private static string[] GetCliScanOptions(CliScanType scanType) {
List<string> options = [];
if (scanType != CliScanType.Sast) options.Add("--sync");
if (scanType == CliScanType.Sca) options.Add("--no-restore");
return options.ToArray();
}
private async Task<CliResult<T>.Success> ScanPathsAsync<T>(
List<string> paths, CliScanType scanType, CancellationToken cancellationToken = default
) {
List<string> isolatedPaths = paths.Select(path => $"\"{path}\"").ToList();
string scanTypeString = scanType.ToString().ToLower();
CliResult<T> result = await _cli.ExecuteCommandAsync<T>(
new[] { "scan", "-t", scanTypeString }.Concat(GetCliScanOptions(scanType)).Concat(new[] { "path" })
.Concat(isolatedPaths).ToArray(),
cancellationToken
);
return ProcessResult(result);
}
private static string MapIgnoreTypeToOptionName(CliIgnoreType type) {
return type switch {
CliIgnoreType.Value => "--by-value",
CliIgnoreType.Rule => "--by-rule",
CliIgnoreType.Path => "--by-path",
CliIgnoreType.Cve => "--by-cve",
_ => throw new ArgumentException("Invalid CliIgnoreType")
};
}
public async Task<AiRemediationResultData> GetAiRemediationAsync(string detectionId, CancellationToken cancellationToken = default) {
CliResult<AiRemediationResult> result = await _cli.ExecuteCommandAsync<AiRemediationResult>(["ai_remediation", detectionId], cancellationToken);
CliResult<AiRemediationResult>.Success processedResult = ProcessResult(result);
if (processedResult == null) {
logger.Warn("Failed to get AI remediation for detection: {0}", detectionId);
return null;
}
if (processedResult.Result.Result && processedResult.Result.Data != null) return processedResult.Result.Data;
logger.Warn("AI remediation is not available for detection: {0}", detectionId);
ShowErrorNotification("AI remediation is not available for this detection");
return null;
}
}