Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.cycode.plugin.annotators.annotationAppliers
import com.cycode.plugin.CycodeBundle
import com.cycode.plugin.annotators.convertSeverity
import com.cycode.plugin.annotators.validateTextRange
import com.cycode.plugin.cli.CliIgnoreType
import com.cycode.plugin.cli.CliResult
import com.cycode.plugin.cli.CliScanType
import com.cycode.plugin.intentions.CycodeIgnoreIntentionQuickFix
import com.cycode.plugin.intentions.CycodeIgnoreType
import com.cycode.plugin.intentions.CycodeOpenViolationCardIntentionQuickFix
import com.cycode.plugin.services.ScanResultsService
import com.intellij.lang.annotation.AnnotationHolder
Expand Down Expand Up @@ -65,14 +65,14 @@ class IacApplier(private val scanResults: ScanResultsService) : AnnotationApplie
.withFix(
CycodeIgnoreIntentionQuickFix(
CliScanType.Iac,
CycodeIgnoreType.PATH,
CliIgnoreType.PATH,
detection.detectionDetails.getFilepath()
)
)
.withFix(
CycodeIgnoreIntentionQuickFix(
CliScanType.Iac,
CycodeIgnoreType.RULE,
CliIgnoreType.RULE,
detection.detectionRuleId
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.cycode.plugin.annotators.annotationAppliers
import com.cycode.plugin.CycodeBundle
import com.cycode.plugin.annotators.convertSeverity
import com.cycode.plugin.annotators.validateTextRange
import com.cycode.plugin.cli.CliIgnoreType
import com.cycode.plugin.cli.CliResult
import com.cycode.plugin.cli.CliScanType
import com.cycode.plugin.intentions.CycodeIgnoreIntentionQuickFix
import com.cycode.plugin.intentions.CycodeIgnoreType
import com.cycode.plugin.intentions.CycodeOpenViolationCardIntentionQuickFix
import com.cycode.plugin.services.ScanResultsService
import com.intellij.lang.annotation.AnnotationHolder
Expand Down Expand Up @@ -62,14 +62,14 @@ class SastApplier(private val scanResults: ScanResultsService) : AnnotationAppli
.withFix(
CycodeIgnoreIntentionQuickFix(
CliScanType.Sast,
CycodeIgnoreType.PATH,
CliIgnoreType.PATH,
detection.detectionDetails.getFilepath()
)
)
.withFix(
CycodeIgnoreIntentionQuickFix(
CliScanType.Sast,
CycodeIgnoreType.RULE,
CliIgnoreType.RULE,
detection.detectionRuleId
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ package com.cycode.plugin.annotators.annotationAppliers
import com.cycode.plugin.CycodeBundle
import com.cycode.plugin.annotators.convertSeverity
import com.cycode.plugin.annotators.validateTextRange
import com.cycode.plugin.cli.CliResult
import com.cycode.plugin.cli.CliScanType
import com.cycode.plugin.cli.getPackageFileForLockFile
import com.cycode.plugin.cli.isSupportedLockFile
import com.cycode.plugin.cli.*
import com.cycode.plugin.intentions.CycodeIgnoreIntentionQuickFix
import com.cycode.plugin.intentions.CycodeIgnoreType
import com.cycode.plugin.intentions.CycodeOpenViolationCardIntentionQuickFix
import com.cycode.plugin.services.ScanResultsService
import com.intellij.lang.annotation.AnnotationHolder
Expand Down Expand Up @@ -93,14 +89,14 @@ class ScaApplier(private val scanResults: ScanResultsService) : AnnotationApplie
.withFix(
CycodeIgnoreIntentionQuickFix(
CliScanType.Sca,
CycodeIgnoreType.PATH,
CliIgnoreType.PATH,
detection.detectionDetails.getFilepath()
)
)
.withFix(
CycodeIgnoreIntentionQuickFix(
CliScanType.Sca,
CycodeIgnoreType.RULE,
CliIgnoreType.RULE,
detection.detectionRuleId
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.cycode.plugin.annotators.annotationAppliers
import com.cycode.plugin.CycodeBundle
import com.cycode.plugin.annotators.convertSeverity
import com.cycode.plugin.annotators.validateTextRange
import com.cycode.plugin.cli.CliIgnoreType
import com.cycode.plugin.cli.CliResult
import com.cycode.plugin.cli.CliScanType
import com.cycode.plugin.intentions.CycodeIgnoreIntentionQuickFix
import com.cycode.plugin.intentions.CycodeIgnoreType
import com.cycode.plugin.intentions.CycodeOpenViolationCardIntentionQuickFix
import com.cycode.plugin.services.ScanResultsService
import com.intellij.lang.annotation.AnnotationHolder
Expand Down Expand Up @@ -76,18 +76,18 @@ class SecretApplier(private val scanResults: ScanResultsService) : AnnotationApp
.withFix(
CycodeIgnoreIntentionQuickFix(
CliScanType.Secret,
CycodeIgnoreType.PATH,
CliIgnoreType.PATH,
detection.detectionDetails.getFilepath()
)
)
.withFix(
CycodeIgnoreIntentionQuickFix(
CliScanType.Secret,
CycodeIgnoreType.RULE,
CliIgnoreType.RULE,
detection.detectionRuleId
)
)
.withFix(CycodeIgnoreIntentionQuickFix(CliScanType.Secret, CycodeIgnoreType.VALUE, detectedValue))
.withFix(CycodeIgnoreIntentionQuickFix(CliScanType.Secret, CliIgnoreType.VALUE, detectedValue))
.create()

}
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/com/cycode/plugin/cli/CliIgnoreType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.cycode.plugin.cli

enum class CliIgnoreType {
VALUE,
RULE,
PATH
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class TreeView(

fun displayViolationCard(detection: DetectionBase) {
val card = when (detection) {
is SecretDetection -> SecretViolationCardContentTab().getContent(detection)
is SecretDetection -> SecretViolationCardContentTab(project).getContent(detection)
is ScaDetection -> ScaViolationCardContentTab().getContent(detection)
is IacDetection -> IacViolationCardContentTab().getContent(detection)
is SastDetection -> SastViolationCardContentTab().getContent(detection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ open class CommonViolationCardContentTab {
}

// blank JLabel to fill the remaining space otherwise the components are centered
gbc.gridy++
gbc.weighty = 1.0
panel.add(JLabel(" "), gbc)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.cycode.plugin.components.toolWindow.components.violationCardContentTab.common.actions

import com.intellij.ui.JBColor
import com.intellij.util.ui.JBUI
import java.awt.FlowLayout
import java.awt.GridBagConstraints
import javax.swing.JButton
import javax.swing.JComponent
import javax.swing.JPanel

open class CardActions {
private val gbc = GridBagConstraints()
private val panel: JPanel = JPanel(FlowLayout(FlowLayout.RIGHT))

init {
gbc.insets = JBUI.insets(2)

panel.border = JBUI.Borders.compound(
JBUI.Borders.customLine(JBColor.GRAY, 1, 0, 0, 0),
JBUI.Borders.empty(10, 0)
)
}

fun addActionButton(text: String, onClick: () -> Unit) {
panel.add(JButton(text).apply {
addActionListener { onClick() }
}, gbc)
}

fun getContent(): JComponent {
return panel
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@ package com.cycode.plugin.components.toolWindow.components.violationCardContentT

import com.cycode.plugin.cli.models.scanResult.secret.SecretDetection
import com.cycode.plugin.components.toolWindow.components.violationCardContentTab.common.CommonViolationCardContentTab
import com.cycode.plugin.components.toolWindow.components.violationCardContentTab.secretViolationCardContentTab.components.actions.SecretActions
import com.cycode.plugin.components.toolWindow.components.violationCardContentTab.secretViolationCardContentTab.components.companyGuidelines.SecretCompanyGuidelines
import com.cycode.plugin.components.toolWindow.components.violationCardContentTab.secretViolationCardContentTab.components.header.SecretHeader
import com.cycode.plugin.components.toolWindow.components.violationCardContentTab.secretViolationCardContentTab.components.shortSummary.SecretShortSummary
import com.cycode.plugin.components.toolWindow.components.violationCardContentTab.secretViolationCardContentTab.components.summary.SecretSummary
import com.cycode.plugin.components.toolWindow.components.violationCardContentTab.secretViolationCardContentTab.components.title.SecretTitle
import com.intellij.openapi.project.Project
import javax.swing.JComponent

class SecretViolationCardContentTab : CommonViolationCardContentTab() {
class SecretViolationCardContentTab(val project: Project) : CommonViolationCardContentTab() {
fun getContent(detection: SecretDetection): JComponent {
val titlePanel = SecretTitle().getContent(detection)
val shortSummaryPanel = SecretShortSummary().getContent(detection)
val headerContentPanel = SecretHeader().addContent(detection)
val summaryPanel = SecretSummary().getContent(detection)
val companyGuidelines = SecretCompanyGuidelines().getContent(detection)
val actionsPanel = SecretActions(project).addContent(detection)

return getContent(listOf(titlePanel, shortSummaryPanel, headerContentPanel, summaryPanel, companyGuidelines))
return getContent(
listOf(
titlePanel,
shortSummaryPanel,
headerContentPanel,
summaryPanel,
companyGuidelines,
actionsPanel,
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cycode.plugin.components.toolWindow.components.violationCardContentTab.secretViolationCardContentTab.components.actions

import com.cycode.plugin.CycodeBundle
import com.cycode.plugin.cli.CliIgnoreType
import com.cycode.plugin.cli.CliScanType
import com.cycode.plugin.cli.models.scanResult.secret.SecretDetection
import com.cycode.plugin.components.toolWindow.components.violationCardContentTab.common.actions.CardActions
import com.cycode.plugin.services.cycode
import com.intellij.openapi.project.Project
import javax.swing.JComponent

class SecretActions(val project: Project) : CardActions() {
fun addContent(detection: SecretDetection): JComponent {
addActionButton(CycodeBundle.message("secretViolationCardIgnoreViolationBtn"), onClick = {
if (detection.detectionDetails.detectedValue != null) {
cycode(project).applyIgnoreFromFileAnnotation(
CliScanType.Secret,
CliIgnoreType.VALUE,
detection.detectionDetails.detectedValue!!
)
}
})

return getContent()
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.cycode.plugin.intentions

import com.cycode.plugin.CycodeBundle
import com.cycode.plugin.cli.CliIgnoreType
import com.cycode.plugin.cli.CliScanType
import com.cycode.plugin.components.toolWindow.updateToolWindowState
import com.cycode.plugin.services.cycode
import com.cycode.plugin.services.scanResults
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
import com.intellij.codeInsight.intention.PriorityAction
import com.intellij.codeInsight.intention.impl.BaseIntentionAction
import com.intellij.openapi.diagnostic.thisLogger
Expand All @@ -18,16 +16,16 @@ import javax.swing.Icon

class CycodeIgnoreIntentionQuickFix(
private val scanType: CliScanType,
private val type: CycodeIgnoreType,
private val type: CliIgnoreType,
private val value: String
) :
BaseIntentionAction(), PriorityAction, Iconable {
override fun getText(): String {
with(type) {
return when (this) {
CycodeIgnoreType.VALUE -> CycodeBundle.message("ignoreIntentionByValueText", value)
CycodeIgnoreType.RULE -> CycodeBundle.message("ignoreIntentionByRuleText", value)
CycodeIgnoreType.PATH -> CycodeBundle.message("ignoreIntentionByPathText", value)
CliIgnoreType.VALUE -> CycodeBundle.message("ignoreIntentionByValueText", value)
CliIgnoreType.RULE -> CycodeBundle.message("ignoreIntentionByRuleText", value)
CliIgnoreType.PATH -> CycodeBundle.message("ignoreIntentionByPathText", value)
}
}
}
Expand All @@ -40,28 +38,6 @@ class CycodeIgnoreIntentionQuickFix(
return true
}

private fun mapTypeToOptionName(type: CycodeIgnoreType): String {
return when (type) {
CycodeIgnoreType.VALUE -> "--by-value"
CycodeIgnoreType.RULE -> "--by-rule"
CycodeIgnoreType.PATH -> "--by-path"
}
}

private fun applyIgnoreInUi(project: Project) {
// exclude results from the local DB and restart the code analyzer

val scanResults = scanResults(project)
when (type) {
CycodeIgnoreType.VALUE -> scanResults.excludeResults(byValue = value)
CycodeIgnoreType.RULE -> scanResults.excludeResults(byRuleId = value)
CycodeIgnoreType.PATH -> scanResults.excludeResults(byPath = value)
}

DaemonCodeAnalyzer.getInstance(project).restart()
updateToolWindowState(project)
}

override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
if (file == null || file != file.originalFile) {
/**
Expand All @@ -75,11 +51,7 @@ class CycodeIgnoreIntentionQuickFix(
}

thisLogger().warn("Ignore quick fix intention has been invoked")

// we are removing is from UI first to show how it's blazing fast and then apply it in the background
applyIgnoreInUi(project)

cycode(project).applyIgnoreFromFileAnnotation(scanType.name.toLowerCase(), mapTypeToOptionName(type), value)
cycode(project).applyIgnoreFromFileAnnotation(scanType, type, value)
}

override fun getPriority(): PriorityAction.Priority {
Expand Down

This file was deleted.

36 changes: 34 additions & 2 deletions src/main/kotlin/com/cycode/plugin/services/CycodeService.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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.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
Expand Down Expand Up @@ -139,14 +142,43 @@ class CycodeService(val project: Project) : Disposable {
}.queue()
}

fun applyIgnoreFromFileAnnotation(optionScanType: String, optionName: String, optionValue: String) {
private fun mapTypeToOptionName(type: CliIgnoreType): String {
return when (type) {
CliIgnoreType.VALUE -> "--by-value"
CliIgnoreType.RULE -> "--by-rule"
CliIgnoreType.PATH -> "--by-path"
}
}

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)
}

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)

object : Task.Backgroundable(project, CycodeBundle.message("ignoresApplying"), true) {
override fun run(indicator: ProgressIndicator) {
if (!pluginState.cliAuthed) {
return
}

cliService.ignore(optionScanType, optionName, optionValue, cancelledCallback = { indicator.isCanceled })
cliService.ignore(
scanType.name.toLowerCase(),
mapTypeToOptionName(type),
value,
cancelledCallback = { indicator.isCanceled })
}
}.queue()
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages/CycodeBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ secretViolationCardHeaderFileField=In file:
secretViolationCardHeaderShaField=Secret SHA:
secretViolationCardSummaryTitle=Summary
secretViolationCardCompanyGuidelinesTitle=Company Guidelines
secretViolationCardIgnoreViolationBtn=Ignore this violation
# iac violation card
iacViolationCardHeaderRuleIdField=Rule ID:
iacViolationCardHeaderFileField=In file:
Expand Down