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

Commit

Permalink
Plugin Commons
Browse files Browse the repository at this point in the history
  • Loading branch information
ChananM committed Oct 15, 2024
1 parent 96a8e6f commit 74de367
Show file tree
Hide file tree
Showing 28 changed files with 220 additions and 121 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<!-- Keep a Changelog guide -> https://keepachangelog.com -->

# checkov-jetbrains-idea Changelog
# Prisma Cloud Changelog

## [1.1.0] - 2024-09-23

## [1.0.22] - 2024-09-15

Expand Down
14 changes: 8 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ group = properties("pluginGroup")
version = properties("pluginVersion")

kotlin {
jvmToolchain(17)
jvmToolchain(properties("javaVersion").toInt())
}

// Configure project's dependencies
Expand All @@ -39,12 +39,10 @@ dependencies {
implementation(libs.jackson)
implementation(libs.springWeb)
implementation("org.json:json:20231013") // TODO: Remove when possible
implementation(libs.commons)
implementation(libs.commonsLang)
implementation(libs.slf4j)
implementation(libs.logback)
implementation(libs.diffUtils)
compileOnly(libs.lombok)
annotationProcessor(libs.lombok)
testImplementation(libs.junit)
testImplementation(libs.jupiterApi)
testRuntimeOnly(libs.jupiter)
Expand Down Expand Up @@ -134,7 +132,7 @@ tasks {
withType<KotlinCompile> {
compilerOptions {
jvmTarget = JvmTarget.fromTarget(it)
apiVersion = KotlinVersion.KOTLIN_1_8
apiVersion = KotlinVersion.KOTLIN_2_0
}
}
}
Expand All @@ -143,6 +141,10 @@ tasks {
gradleVersion = properties("gradleVersion")
}

build {
dependsOn(buildPlugin)
}

patchPluginXml {
// Extract the <!-- Plugin description --> section from README.md and provide for the plugin's manifest
pluginDescription.set(
Expand Down Expand Up @@ -202,4 +204,4 @@ intellijPlatformTesting {
}
}
}
}
}
11 changes: 5 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

pluginGroup = com.github.bridgecrewio.prismajetbrainsidea
pluginName = Prisma Cloud
pluginVersion=1.0.22
pluginVersion=1.1.0

platformType = IC
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild=203
platformVersion=2023.3.8
pluginSinceBuild=231
platformVersion=2023.1.7
platformDownloadSources = true
# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
Expand All @@ -20,9 +20,8 @@ platformBundledPlugins=
# See https://kotlinlang.org/docs/reference/using-gradle.html#dependency-on-the-standard-library for details.
kotlin.stdlib.default.dependency = false

# Java language level used to compile sources and to generate the files for - Java 11 is required since 2020.3
# TODO: Change to 21 on next version
javaVersion=11
# Java language level used to compile sources and to generate the files for
javaVersion=17

# Gradle Releases -> https://github.com/gradle/gradle/releases
gradleVersion=8.10.1
Expand Down
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# libraries
jackson = "2.17.2"
lombok = "1.18.34"
springWeb = "5.3.39" # TODO: Change to 6.x.x when moving to Java 17+
commons-io = "2.16.1"
springWeb = "6.1.13"
commons-lang3 = "3.17.0"
slf4j = "2.0.16"
logback = "1.5.6"
diffUtils = "4.12"
Expand All @@ -14,16 +14,16 @@ junitPlatform = "1.9.0"

# plugins
changelog = "2.2.1"
intelliJPlatform = "2.0.1"
kotlin = "1.8.22" # TODO: Change to 2.0.x on next version
intelliJPlatform = "2.1.0"
kotlin = "2.0.20"
kover = "0.8.3"
qodana = "0.1.13"

[libraries]
jackson = { group = "com.fasterxml.jackson.module", name = "jackson-module-kotlin", version.ref = "jackson"}
lombok = { group = "org.projectlombok", name = "lombok", version.ref = "lombok" }
springWeb = { group = "org.springframework", name = "spring-web", version.ref = "springWeb" }
commons = { group = "commons-io", name = "commons-io", version.ref = "commons-io" }
commonsLang = { group = "org.apache.commons", name = "commons-lang3", version.ref = "commons-lang3" }
slf4j = { group = "org.slf4j", name = "slf4j-api", version.ref = "slf4j" }
logback = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logback" }
diffUtils = { group = "io.github.java-diff-utils", name = "java-diff-utils", version.ref = "diffUtils" }
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/bridgecrew/log/LoggerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public String getLogFilePath() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger rootLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
Appender<?> appender = rootLogger.getAppender("FILE");
if (appender instanceof FileAppender) {
return ((FileAppender<?>)appender).getFile();
if (appender instanceof FileAppender<?> fileAppender) {
return fileAppender.getFile();
}
return null;
}
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/com/bridgecrew/util/ApplicationServiceUtil.java

This file was deleted.

14 changes: 10 additions & 4 deletions src/main/kotlin/com/bridgecrew/activities/PostStartupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ import com.bridgecrew.listeners.PrismaVirtualFileListener
import com.bridgecrew.log.LoggerService
import com.bridgecrew.settings.PrismaSettingsState
import com.bridgecrew.ui.CheckovToolWindowManagerPanel
import com.bridgecrew.util.ApplicationServiceUtil
import com.bridgecrew.utils.ApplicationServiceUtil
import com.intellij.ide.plugins.IdeaPluginDescriptor
import com.intellij.ide.plugins.PluginInstaller
import com.intellij.ide.plugins.PluginManagerCore
import com.intellij.ide.plugins.PluginStateListener
import com.intellij.openapi.components.service
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.vfs.LocalFileSystem
import org.slf4j.LoggerFactory
import java.util.*

class PostStartupActivity : StartupActivity {
class PostStartupActivity : ProjectActivity {

private val logger = LoggerFactory.getLogger(javaClass)

override fun runActivity(project: Project) {
override suspend fun execute(project: Project) {
ApplicationServiceUtil.getService(LoggerService::class.java).initializeLogger()
val version = PluginManagerCore.getPlugin(PluginId.getId("com.github.bridgecrewio.prismacloud"))?.version
logger.info("Starting Prisma Cloud JetBrains plugin version $version")
Expand All @@ -38,6 +38,12 @@ class PostStartupActivity : StartupActivity {
}
})

// ApplicationServiceUtil.getService(CommonsClient::class.java).helloWorld()
// logger.info(ApplicationServiceUtil.getService(CommonsClient::class.java).add(3, 4).toString())
// val request = Request(listOf("test", "best"), "mest", 1)
// val res = ApplicationServiceUtil.getService(CommonsClient::class.java).handleRequest(request)
// logger.info(res.toString())

PluginInstaller.addStateListener(object : PluginStateListener {
override fun install(ideaPluginDescriptor: IdeaPluginDescriptor) {
//todo this event wasn't trigger, need review
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.bridgecrew.cache.CacheDataAnalytics
import com.bridgecrew.scheduler.IntervalRunner
import com.bridgecrew.services.scan.FullScanStateService
import com.bridgecrew.services.scan.ScanTaskResult
import com.bridgecrew.util.ApplicationServiceUtil
import com.bridgecrew.utils.ApplicationServiceUtil
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
Expand Down
16 changes: 5 additions & 11 deletions src/main/kotlin/com/bridgecrew/api/PrismaApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import com.bridgecrew.listeners.CheckovSettingsListener
import com.bridgecrew.settings.DEFAULT_REPORTING_INTERVAL
import com.bridgecrew.settings.PLUGIN_NAME
import com.bridgecrew.settings.PrismaSettingsState
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.bridgecrew.utils.GlobalMapper
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import kotlinx.coroutines.CoroutineScope
import org.slf4j.LoggerFactory
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
Expand All @@ -31,19 +29,15 @@ data class PrismaConnectionDetails(
val secretKey: String
)

val mapper = ObjectMapper().apply {
registerModule(KotlinModule.Builder().build())
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
setSerializationInclusion(JsonInclude.Include.NON_NULL)
}


@Service
class PrismaApiClient {
class PrismaApiClient(private val cs: CoroutineScope) {

private val logger = LoggerFactory.getLogger(javaClass)
private var connection: PrismaConnectionDetails? = null
private val client = RestTemplate(listOf(
MappingJackson2HttpMessageConverter(mapper)
MappingJackson2HttpMessageConverter(GlobalMapper.i())
))

init {
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/com/bridgecrew/cache/CacheDataAnalytics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.bridgecrew.cache


import com.bridgecrew.analytics.AnalyticsData
import com.bridgecrew.api.mapper
import com.bridgecrew.utils.GlobalMapper
import com.intellij.openapi.project.Project

class CacheDataAnalytics(private val project: Project) {
Expand All @@ -13,14 +13,14 @@ class CacheDataAnalytics(private val project: Project) {
return
}

val analyticsEventDataCached = data.split("\n").toMutableList().map { mapper.readValue(it, AnalyticsData::class.java) }
val analyticsEventDataCached = data.split("\n").toMutableList().map { GlobalMapper.i().readValue(it, AnalyticsData::class.java) }
analyticsEventData.addAll(analyticsEventDataCached)

CacheDataAnalyticsStorage(project).clear()
}

fun stash(analyticsEventData: MutableList<AnalyticsData>) {
val data = analyticsEventData.joinToString("\n") { mapper.writeValueAsString(it) }
val data = analyticsEventData.joinToString("\n") { GlobalMapper.i().writeValueAsString(it) }
CacheDataAnalyticsStorage(project).writeDataToFile(data)
}
}
62 changes: 62 additions & 0 deletions src/main/kotlin/com/bridgecrew/commons/CommonsClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.bridgecrew.commons

import com.bridgecrew.commons.models.Request
import com.bridgecrew.commons.models.Response
import com.bridgecrew.utils.GlobalMapper
import com.fasterxml.jackson.core.JsonProcessingException
import com.intellij.openapi.components.Service
import com.sun.jna.Native
import org.eclipse.sisu.Nullable
import org.slf4j.Logger
import org.slf4j.LoggerFactory

@Service
class CommonsClient {

private val logger: Logger = LoggerFactory.getLogger(javaClass)

@Nullable
private var commons: CommonsLibrary? = null

init {
try {
val file = Native.extractFromResourcePath(
"/lib/${System.mapLibraryName("commons-${getArch()}")}",
javaClass.classLoader
)
logger.info("Loading commons lib from ${file.absolutePath}")
commons = Native.load(file.absolutePath, CommonsLibrary::class.java)
logger.info("Successfully loaded commons from ${file.absolutePath}")
} catch (e: Exception) {
logger.error("Failed to load commons lib", e)
}
}

fun helloWorld() {
commons?.HelloWorld()
}

fun add(a: Int, b: Int): Int {
return commons?.Add(a, b) ?: 0
}

fun handleRequest(request: Request?): Response {
try {
val requestJson = GlobalMapper.i().writeValueAsString(request)
val response = commons?.HandleRequest(requestJson)
return GlobalMapper.i().readValue(response, Response::class.java)
} catch (e: JsonProcessingException) {
logger.error("Failed to handle request", e)
return Response(e.message)
}
}

@Throws(Exception::class)
private fun getArch(): String {
return when (val arch = System.getProperty("os.arch").lowercase()) {
"x86_64", "amd64" -> "x64"
"arm", "arm64", "aarch64" -> "arm"
else -> throw Exception("Unsupported architecture: $arch")
}
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/com/bridgecrew/commons/CommonsLibrary.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bridgecrew.commons

import com.sun.jna.Library

interface CommonsLibrary : Library {

fun HelloWorld()

fun Add(a: Int, b: Int): Int

fun HandleRequest(request: String?): String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bridgecrew.commons.models

open class PluginCommonsResponse(

var error: String?
)
7 changes: 7 additions & 0 deletions src/main/kotlin/com/bridgecrew/commons/models/Request.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bridgecrew.commons.models

data class Request(
var strings: List<String>,
var anotherString: String,
var number: Int
)
11 changes: 11 additions & 0 deletions src/main/kotlin/com/bridgecrew/commons/models/Response.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.bridgecrew.commons.models

data class Response(
var strings: List<String>?,
var result: Boolean?
) : PluginCommonsResponse(null) {

constructor(error: String?) : this(null, null) {
this.error = error
}
}
8 changes: 1 addition & 7 deletions src/main/kotlin/com/bridgecrew/scheduler/IntervalRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.bridgecrew.scheduler
import org.apache.commons.lang3.time.StopWatch
import org.slf4j.LoggerFactory
import java.util.*
import java.util.concurrent.TimeUnit

class IntervalRunner(private val name: String) {

Expand All @@ -16,13 +15,8 @@ class IntervalRunner(private val name: String) {
timer.schedule(object : TimerTask() {
override fun run() {
try {
stopWatch.time
intervalFunction()
logger.info(
"Interval function for $name executed with delay " + TimeUnit.MILLISECONDS.toSeconds(
stopWatch.time
)
)
logger.info("Interval function for $name executed with delay " + stopWatch.duration.toSeconds())
} catch (e: Throwable) {
logger.info("Catch the exception: $e")
stopWatch.stop()
Expand Down
Loading

0 comments on commit 74de367

Please sign in to comment.