-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycodeService.kt
More file actions
executable file
·169 lines (145 loc) · 6.76 KB
/
CycodeService.kt
File metadata and controls
executable file
·169 lines (145 loc) · 6.76 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
package com.cycode.plugin.services
import com.cycode.plugin.CycodeBundle
import com.cycode.plugin.cli.CliIgnoreType
import com.cycode.plugin.cli.CliScanType
import com.cycode.plugin.cli.models.AiRemediationResultData
import com.cycode.plugin.components.toolWindow.CycodeToolWindowFactory
import com.cycode.plugin.components.toolWindow.updateToolWindowState
import com.cycode.plugin.components.toolWindow.updateToolWindowStateForAllProjects
import com.cycode.plugin.utils.CycodeNotifier
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
import com.intellij.openapi.Disposable
import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
@Service(Service.Level.PROJECT)
class CycodeService(val project: Project) : Disposable {
private val cliService = cli(project)
private val cliDownloadService = cliDownload()
private val pluginLocalState = pluginLocalState(project)
fun <T> runBackgroundTask(
title: String,
canBeCancelled: Boolean = true,
task: (ProgressIndicator) -> T,
) {
thisLogger().debug("Create background task: $title")
object : Task.Backgroundable(project, title, canBeCancelled) {
override fun run(indicator: ProgressIndicator) {
thisLogger().debug("Run background task: $title")
task(indicator)
thisLogger().debug("Finish background task: $title")
}
}.queue()
thisLogger().debug("Background task queued: $title")
}
fun installCliIfNeededAndCheckAuthentication() {
runBackgroundTask(CycodeBundle.message("pluginLoading"), canBeCancelled = false) { _ ->
thisLogger().debug("Check CLI installation and authentication")
// we are using lock of download service because it shared per application
// the current service is per project so, we can't create a lock here
synchronized(cliDownloadService.initCliLock) {
cliDownloadService.initCli()
cliService.syncStatus()
updateToolWindowState(project)
}
}
}
fun startAuth() {
runBackgroundTask(CycodeBundle.message("authProcessing")) { indicator ->
if (!pluginLocalState.cliAuthed) {
cliService.startAuth { indicator.isCanceled }
cliService.syncStatus()
updateToolWindowStateForAllProjects()
}
}
}
private fun getBackgroundScanningLabel(scanType: CliScanType) = when (scanType) {
CliScanType.Secret -> CycodeBundle.message("secretScanning")
CliScanType.Sca -> CycodeBundle.message("scaScanning")
CliScanType.Iac -> CycodeBundle.message("iacScanning")
CliScanType.Sast -> CycodeBundle.message("sastScanning")
}
fun startScanForCurrentProject(scanType: CliScanType) {
val projectRoot = cliService.getProjectRootDirectory()
if (projectRoot == null) {
CycodeNotifier.notifyInfo(project, CycodeBundle.message("noProjectRootErrorNotification"))
return
}
// the only way to run the entire project scans is by pressing the button
// so this is on demand scan
startScan(scanType, listOf(projectRoot))
}
fun startScan(scanType: CliScanType, pathsToScan: List<String>, onDemand: Boolean = true) {
runBackgroundTask(getBackgroundScanningLabel(scanType)) { indicator ->
if (!pluginLocalState.cliAuthed) {
CycodeNotifier.notifyInfo(project, CycodeBundle.message("authorizationRequiredNotification"))
return@runBackgroundTask
}
thisLogger().debug("[$scanType] Start scanning paths: $pathsToScan")
val cancelledCallback = { indicator.isCanceled }
when (scanType) {
CliScanType.Secret -> cliService.scanPathsSecrets(pathsToScan, onDemand, cancelledCallback)
CliScanType.Sca -> cliService.scanPathsSca(pathsToScan, onDemand, cancelledCallback)
CliScanType.Iac -> cliService.scanPathsIac(pathsToScan, onDemand, cancelledCallback)
CliScanType.Sast -> cliService.scanPathsSast(pathsToScan, onDemand, cancelledCallback)
}
thisLogger().debug("[$scanType] Finish scanning paths: $pathsToScan")
}
}
fun getAiRemediation(
detectionId: String,
onSuccess: (AiRemediationResultData) -> Unit,
onFailure: () -> Unit = {}
) {
runBackgroundTask(CycodeBundle.message("aiRemediationGenerating")) { indicator ->
thisLogger().debug("[AI REMEDIATION] Start generating remediation for $detectionId")
val aiRemediation = cliService.getAiRemediation(detectionId)
thisLogger().debug("[AI REMEDIATION] Finish generating remediation for $detectionId")
if (aiRemediation != null) {
onSuccess(aiRemediation)
} else {
onFailure()
}
}
}
private fun mapTypeToOptionName(type: CliIgnoreType): String {
return when (type) {
CliIgnoreType.VALUE -> "--by-value"
CliIgnoreType.RULE -> "--by-rule"
CliIgnoreType.PATH -> "--by-path"
CliIgnoreType.CVE -> "--by-cve"
}
}
private fun applyIgnoreInUi(type: CliIgnoreType, value: String) {
// exclude results from the local DB and restart the code analyzer
val scanResults = scanResults(project)
when (type) {
CliIgnoreType.VALUE -> scanResults.excludeResults(byValue = value)
CliIgnoreType.RULE -> scanResults.excludeResults(byRuleId = value)
CliIgnoreType.PATH -> scanResults.excludeResults(byPath = value)
CliIgnoreType.CVE -> scanResults.excludeResults(byCve = value)
}
DaemonCodeAnalyzer.getInstance(project).restart()
updateToolWindowState(project)
}
fun applyIgnoreFromFileAnnotation(scanType: CliScanType, type: CliIgnoreType, value: String) {
// we are removing is from UI first to show how it's blazing fast and then apply it in the background
applyIgnoreInUi(type, value)
runBackgroundTask(CycodeBundle.message("ignoresApplying"), canBeCancelled = false) { indicator ->
if (!pluginLocalState.cliAuthed) {
return@runBackgroundTask
}
cliService.ignore(
scanType.name.lowercase(),
mapTypeToOptionName(type),
value,
cancelledCallback = { indicator.isCanceled }
)
}
}
override fun dispose() {
CycodeToolWindowFactory.TabManager.removeTab(project)
}
}