Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
windows docker fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolii-paloaltonetworks committed Feb 1, 2024
1 parent eb14e33 commit 683a9ce
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.bridgecrew.CheckovResult
import com.bridgecrew.results.*
import com.bridgecrew.settings.CheckovGlobalState
import com.bridgecrew.utils.CheckovUtils
import com.bridgecrew.utils.fromDockerFilePath
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import org.apache.commons.io.FilenameUtils
Expand All @@ -16,7 +17,8 @@ class ResultsCacheService(val project: Project) {
var checkovResults: MutableList<BaseCheckovResult> = mutableListOf()
var modifiedResults: MutableList<BaseCheckovResult> = mutableListOf()

private val baseDir: String = if (System.getProperty("os.name").lowercase().contains("win")) FilenameUtils.separatorsToWindows(project.basePath!!) else project.basePath!!
// private val baseDir: String = if (System.getProperty("os.name").lowercase().contains("win")) FilenameUtils.separatorsToWindows(project.basePath!!) else project.basePath!!
private val baseDir: String = project.basePath!!

// This function returns `checkovResults` after accounting for changes that were done between scans
// For example, after fixing or suppressing a resource, we want to clean those entries from all client facing usages.
Expand Down Expand Up @@ -80,13 +82,15 @@ class ResultsCacheService(val project: Project) {
fun setCheckovResultsFromResultsList(results: List<CheckovResult>) {
for (result in results) {
try {
result.file_abs_path = fromDockerFilePath(result.file_abs_path)

val category: Category = mapCheckovCheckTypeToScanType(result.check_type, result.check_id)
val checkType = this.getCheckType(result.check_type)
val resource: String = CheckovUtils.extractResource(result, category, checkType)
val name: String = getResourceName(result, category)
val severity = Severity.valueOf(result.severity.uppercase())
val description = if(!result.description.isNullOrEmpty()) result.description else result.short_description
val filePath = result.file_abs_path.replace(baseDir, "")
val filePath = result.file_abs_path.replace(baseDir, "").replace("//", "/")
val fileAbsPath = if (!result.file_abs_path.contains(baseDir)) Paths.get(baseDir, File.separator, result.file_abs_path).toString() else result.file_abs_path

when (category) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class CheckovScanCommandsService(val project: Project) {
fun getExecCommandForSingleFile(filePaths: List<String>, outputFilePath: String): ArrayList<String> {
val cmds = ArrayList<String>()
cmds.addAll(getCheckovRunningCommandByServiceType(outputFilePath))
cmds.addAll(getCheckovCliArgsForExecCommand(outputFilePath))
cmds.addAll(getCheckovCliArgsForExecCommand(getOutputFilePath(outputFilePath)))

filePaths.forEach{ path -> cmds.add("-f"); cmds.add(getFilePath(path)) }

Expand All @@ -33,7 +33,7 @@ abstract class CheckovScanCommandsService(val project: Project) {

val cmdByFramework = arrayListOf<String>()
cmdByFramework.addAll(baseCmds)
cmdByFramework.addAll(getCheckovCliArgsForExecCommand(outputFilePath))
cmdByFramework.addAll(getCheckovCliArgsForExecCommand(getOutputFilePath(outputFilePath)))
cmdByFramework.add("--framework")
cmdByFramework.add(framework)

Expand Down Expand Up @@ -99,4 +99,6 @@ abstract class CheckovScanCommandsService(val project: Project) {
abstract fun getDirectory(): String
abstract fun getFilePath(originalFilePath: String): String
abstract fun getCertPath(): String
abstract fun getOutputFilePath(outputFilePath: String): String

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bridgecrew.services.checkovScanCommandsService

import com.bridgecrew.utils.PLUGIN_ID
import com.bridgecrew.utils.toDockerFilePath
import com.intellij.ide.plugins.PluginManagerCore
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.project.Project
Expand All @@ -9,7 +10,7 @@ import org.apache.commons.io.FilenameUtils
class DockerCheckovScanCommandsService(project: Project) : CheckovScanCommandsService(project) {

private val image = "bridgecrew/checkov"
private val volumeDirectory = FilenameUtils.separatorsToUnix(project.basePath)
private val volumeDirectory = getDockerUnixPath(project.basePath)
private val volumeCertPath = "/usr/lib/ssl/cert.pem"
override fun getCheckovRunningCommandByServiceType(outputFilePath: String): ArrayList<String> {
val pluginVersion =
Expand All @@ -27,7 +28,7 @@ class DockerCheckovScanCommandsService(project: Project) : CheckovScanCommandsSe
dockerCommand.addAll(arrayListOf("--volume", volumeCaFile))
}

dockerCommand.addAll(arrayListOf("--volume", "$outputFilePath:$outputFilePath"))
dockerCommand.addAll(arrayListOf("--volume", "$outputFilePath:/${getDockerUnixPath(outputFilePath)}"))

val volumeDir = "${FilenameUtils.separatorsToUnix(project.basePath)}:/${volumeDirectory}"
dockerCommand.addAll(arrayListOf("--volume", volumeDir, image))
Expand All @@ -39,6 +40,15 @@ class DockerCheckovScanCommandsService(project: Project) : CheckovScanCommandsSe
return volumeDirectory
}

private fun getDockerUnixPath(path: String?): String {
return toDockerFilePath(FilenameUtils.separatorsToUnix(path));
}


override fun getOutputFilePath(outputFilePath: String): String {
return getDockerUnixPath(outputFilePath)
}

override fun getFilePath(originalFilePath: String): String {
return originalFilePath.replace(project.basePath!!, volumeDirectory)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class InstalledCheckovScanCommandsService(project: Project) : CheckovScanCommand
return FilenameUtils.separatorsToSystem(originalFilePath)
}

override fun getOutputFilePath(outputFilePath: String): String {
return outputFilePath
}

override fun getCertPath(): String {
return settings?.certificate!!
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/com/bridgecrew/utils/fileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,11 @@ fun deleteCheckovTempDir() {

fun toVirtualFilePath(project: Project, virtualFile: VirtualFile): String {
return virtualFile.path.removePrefix(project.basePath!!).removePrefix(File.separator)
}

fun toDockerFilePath(path: String): String {
return path.replace(":/", "[--colon--]")
}
fun fromDockerFilePath(path: String): String {
return path.replace( "[--colon--]",":/")
}

0 comments on commit 683a9ce

Please sign in to comment.