Skip to content

Commit 1d6495c

Browse files
authored
Install schema extensions from plugin zip files (#1075)
* WIP install schema extensions from plugin zip files * Run schema-extender.sh * Removal of installed schema extensions
1 parent 31c9e68 commit 1d6495c

3 files changed

Lines changed: 58 additions & 4 deletions

File tree

console/src/main/scala/io/shiftleft/console/PluginManager.scala

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import better.files.Dsl._
44
import better.files.File.apply
55

66
import java.nio.file.Path
7+
import scala.sys.process.Process
78
import scala.util.{Failure, Success, Try}
89

910
/**
1011
* Plugin management component
1112
* @param installDir the Joern/Ocular installation dir
1213
* */
13-
class PluginManager(installDir: File) {
14+
class PluginManager(val installDir: File) {
1415

1516
def listPlugins(): List[String] = {
1617
val installedPluginNames = pluginDir.toList
@@ -55,6 +56,26 @@ class PluginManager(installDir: File) {
5556
}
5657
}
5758
}
59+
installSchemaExtensions(file, pluginName)
60+
}
61+
62+
private def installSchemaExtensions(file: File, pluginName: String): Unit = {
63+
schemaDir.foreach { sDir =>
64+
file.listRecursively
65+
.filter(_.path.toString.contains("schema"))
66+
.filter(_.name.endsWith(".json"))
67+
.foreach { json =>
68+
val name = json.name
69+
val dstFileName = s"joernext-$pluginName-$name"
70+
cp(json, sDir / dstFileName)
71+
}
72+
}
73+
try {
74+
Process("./schema-extender.sh", installDir.toJava).!!
75+
} catch {
76+
case e: Exception =>
77+
System.err.println(s"Error running schema-extender: ${e.getMessage}")
78+
}
5879
}
5980

6081
private def extractToTemporaryDir(file: File) = {
@@ -76,9 +97,13 @@ class PluginManager(installDir: File) {
7697
dir.list.filter { f =>
7798
f.name.startsWith(s"joernext-$name")
7899
}
100+
} ++ schemaDir.toList.flatMap { dir =>
101+
dir.list.filter { f =>
102+
f.name.startsWith(s"joernext-$name")
103+
}
79104
}
80105
filesToRemove.foreach(f => f.delete())
81-
filesToRemove.map(_.name)
106+
filesToRemove.map(_.pathAsString)
82107
}
83108
}
84109

@@ -92,4 +117,14 @@ class PluginManager(installDir: File) {
92117
}
93118
}
94119

120+
def schemaDir: Option[Path] = {
121+
val pathToSchemaDir = installDir.path.resolve("schema-extender/schemas/")
122+
if (pathToSchemaDir.toFile.exists()) {
123+
Some(pathToSchemaDir)
124+
} else {
125+
println(s"Schema directory at $pathToSchemaDir does not exist")
126+
None
127+
}
128+
}
129+
95130
}
324 Bytes
Binary file not shown.

console/src/test/scala/io/shiftleft/console/PluginManagerTests.scala

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import better.files._
55
import org.scalatest.matchers.should.Matchers
66
import org.scalatest.wordspec.AnyWordSpec
77

8+
import java.nio.file.attribute.PosixFilePermission
9+
810
class PluginManagerTests extends AnyWordSpec with Matchers {
911

1012
"PluginManager::add" should {
@@ -27,6 +29,18 @@ class PluginManagerTests extends AnyWordSpec with Matchers {
2729
case None => fail
2830
}
2931
}
32+
33+
"copy schema file in zip to schema dir and execute schema extender" in Fixture() { manager =>
34+
val testZipFileName = "console/src/test/resources/test.zip"
35+
manager.add(testZipFileName)
36+
manager.schemaDir match {
37+
case Some(dir) =>
38+
dir.toFile.list().toList shouldBe List("joernext-test-foo.json")
39+
(manager.installDir / "out.txt").exists shouldBe true
40+
case None => fail
41+
}
42+
}
43+
3044
}
3145

3246
"PluginManager::rm" should {
@@ -38,10 +52,10 @@ class PluginManagerTests extends AnyWordSpec with Matchers {
3852
"remove existing plugin" in Fixture() { manager =>
3953
val testZipFileName = "console/src/test/resources/test.zip"
4054
manager.add(testZipFileName)
41-
manager.rm("test") shouldBe List("joernext-test-foo.jar")
55+
manager.rm("test").map(x => File(x).name).toSet shouldBe Set("joernext-test-foo.jar", "joernext-test-foo.json")
4256
manager.listPlugins() shouldBe List()
4357
manager.add(testZipFileName)
44-
manager.rm("test") shouldBe List("joernext-test-foo.jar")
58+
manager.rm("test").map(x => File(x).name).toSet shouldBe Set("joernext-test-foo.jar", "joernext-test-foo.json")
4559
manager.listPlugins() shouldBe List()
4660
}
4761

@@ -67,6 +81,11 @@ object Fixture {
6781
def apply[T]()(f: PluginManager => T): T = {
6882
val dir = File.newTemporaryDirectory("pluginmantests")
6983
mkdir(dir / "lib")
84+
mkdirs(dir / "schema-extender" / "schemas")
85+
val extender = dir / "schema-extender.sh"
86+
val extenderContents = "#!/bin/sh\necho 'foo' > " + (dir / "out.txt")
87+
extender.write(extenderContents)
88+
chmod_+(PosixFilePermission.OWNER_EXECUTE, extender)
7089
val result = f(new PluginManager(dir))
7190
dir.delete()
7291
result

0 commit comments

Comments
 (0)