Skip to content

Commit

Permalink
✨ Quality of Life Changes (#73)
Browse files Browse the repository at this point in the history
* 🔥 Removed log4j2 properties as to avoid them being accidentally not overridden

* 🔒 Changed visibility of driver constructors

* 🔒 AuthKey never null and now is just blank if not set

* Documented changes

* Fixed constructor visibility incorrectly modified and now connect calls return their object instance

* Fixed scala tests

* Removed a println
  • Loading branch information
DavidBakerEffendi authored Feb 25, 2021
1 parent b609713 commit 788784f
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 69 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

### Changed
- `TigerGraphDriver::authKey` never null and now just blank if not set
- Removed `log4f2.properties` under the main artifact
- Made the visibility of driver constructors module specific so that users are forced
to use the `DriverFactory`
- `connect` methods on drivers now return the driver instead of nothing.

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ abstract class GremlinDriver : IDriver {
*
* @throws IllegalArgumentException if the graph database is already connected to.
*/
open fun connect() {
open fun connect(): GremlinDriver = apply {
require(!connected) { "Please close the graph before trying to make another connection." }
graph = TinkerGraph.open(config)
g = graph.traversal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource
/**
* The driver used to connect to a remote JanusGraph instance.
*/
class JanusGraphDriver : GremlinDriver(), ISchemaSafeDriver {
class JanusGraphDriver internal constructor() : GremlinDriver(), ISchemaSafeDriver {
private val logger = LogManager.getLogger(JanusGraphDriver::class.java)

companion object {
Expand All @@ -36,7 +36,7 @@ class JanusGraphDriver : GremlinDriver(), ISchemaSafeDriver {
* @throws IllegalArgumentException if the graph database is already connected to or if the remote config path is
* not set.
*/
override fun connect() {
override fun connect(): JanusGraphDriver = apply {
require(!connected) { "Please close the graph before trying to make another connection." }
require(config.containsKey(REMOTE_CONFIG)) { "Remote config path not set! See the config field in JanusGraphDriver with key REMOTE_CONFIG." }
// Test that the connection works and then close again
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import io.shiftleft.codepropertygraph.generated.nodes.Factories as NodeFactories
/**
* The driver used to connect to a remote Neo4j instance.
*/
class Neo4jDriver : IDriver {
class Neo4jDriver internal constructor() : IDriver {

private val logger = LogManager.getLogger(Neo4jDriver::class.java)
private lateinit var driver: Driver
Expand Down Expand Up @@ -104,7 +104,7 @@ class Neo4jDriver : IDriver {
*/
fun port(value: Int) = apply { port = value }

fun connect() {
fun connect(): Neo4jDriver = apply {
require(!connected) { "Please close the graph before trying to make another connection." }
driver = GraphDatabase.driver("bolt://$hostname:$port", AuthTokens.basic(username, password))
connected = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.apache.tinkerpop.gremlin.structure.Vertex
/**
* The driver used to connect to a remote Amazon Neptune instance.
*/
class NeptuneDriver : GremlinOverriddenIdDriver() {
class NeptuneDriver internal constructor() : GremlinOverriddenIdDriver() {
private val logger = LogManager.getLogger(NeptuneDriver::class.java)

private val builder: Cluster.Builder = Cluster.build()
Expand Down Expand Up @@ -53,7 +53,7 @@ class NeptuneDriver : GremlinOverriddenIdDriver() {
*
* @throws IllegalArgumentException if the graph database is already connected.
*/
override fun connect() {
override fun connect(): NeptuneDriver = apply {
require(!connected) { "Please close the graph before trying to make another connection." }
cluster = builder.create()
super.g = traversal().withRemote(DriverRemoteConnection.using(cluster))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import io.shiftleft.codepropertygraph.generated.nodes.Factories as NodeFactories
/**
* Driver to create an OverflowDB database file from Plume's domain classes.
*/
class OverflowDbDriver : IDriver {
class OverflowDbDriver internal constructor() : IDriver {

private val logger = LogManager.getLogger(OverflowDbDriver::class.java)

Expand All @@ -30,23 +30,55 @@ class OverflowDbDriver : IDriver {
* Where the database will be serialize/deserialize and overflow to disk.
*/
var storageLocation: String = ""
private set

/**
* Specifies if OverflowDb should write to disk when memory is constrained.
*/
var overflow: Boolean = true
private set

/**
* Percentage of the heap from when overflowing should begin to occur. Default is 80%.
*/
var heapPercentageThreshold: Int = 80
private set

/**
* If specified, OverflowDB will measure and report serialization/deserialization timing averages.
*/
var serializationStatsEnabled: Boolean = false
private set

fun connect() {
/**
* Set the storage location.
*
* @param value the storage location to overflow to e.g. /tmp/cpg.bin
*/
fun storageLocation(value: String): OverflowDbDriver = apply { storageLocation = value }

/**
* Set whether the database overflows or not.
*
* @param value true to overflow, false to remain in memory.
*/
fun overflow(value: Boolean): OverflowDbDriver = apply { overflow = value }

/**
* Set the percentage threshold before overflowing.
*
* @param value the percentage of the heap space.
*/
fun heapPercentageThreshold(value: Int): OverflowDbDriver = apply { heapPercentageThreshold = value }

/**
* To set if serialization/deserialization timing averages should be reported.
*
* @param value true to report averages, false to not.
*/
fun serializationStatsEnabled(value: Boolean): OverflowDbDriver = apply { serializationStatsEnabled = value }

fun connect(): OverflowDbDriver = apply {
require(!connected) { "Please close the graph before trying to make another connection." }
val odbConfig = Config.withDefaults()
.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import io.shiftleft.codepropertygraph.generated.nodes.Factories as NodeFactories
/**
* The driver used to connect to a remote TigerGraph instance.
*/
class TigerGraphDriver : IOverridenIdDriver, ISchemaSafeDriver {
class TigerGraphDriver internal constructor() : IOverridenIdDriver, ISchemaSafeDriver {

private val logger = LogManager.getLogger(TigerGraphDriver::class.java)
private val objectMapper = ObjectMapper()
Expand Down Expand Up @@ -83,7 +83,7 @@ class TigerGraphDriver : IOverridenIdDriver, ISchemaSafeDriver {
* The authorization key used for TigerGraph servers with token authorization turned on. This is placed under
* the Authorization header when making requests.
*/
var authKey: String? = null
var authKey: String = ""
private set

init {
Expand Down Expand Up @@ -376,7 +376,7 @@ class TigerGraphDriver : IOverridenIdDriver, ISchemaSafeDriver {
PlumeKeyProvider.clearKeyPools()
}

private fun headers(contentType: String = "application/json"): Map<String, String> = if (authKey == null) {
private fun headers(contentType: String = "application/json"): Map<String, String> = if (authKey.isBlank()) {
mapOf("Content-Type" to contentType)
} else {
mapOf("Content-Type" to contentType, "Authorization" to "Bearer $authKey")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import java.io.File
/**
* The driver used to connect to an in-memory TinkerGraph instance.
*/
class TinkerGraphDriver : GremlinDriver() {
class TinkerGraphDriver internal constructor() : GremlinDriver() {

override fun connect(): TinkerGraphDriver = super.connect() as TinkerGraphDriver

/**
* Add or update a [BaseConfiguration] key-value pair.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ object SootToPlumeUtil {
sym == "<=" -> Operators.lessEqualsThan
sym == ">=" -> Operators.greaterEqualsThan
else -> {
println("Unknown $sym")
logger.warn("Unknown binary operator $sym")
sym
}
Expand Down
30 changes: 0 additions & 30 deletions plume/src/main/resources/log4j2.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ class Neo4jDriverIntTest {
@BeforeAll
fun setUpAll() = run {
testStartTime = System.nanoTime()
driver = (DriverFactory(GraphDatabase.NEO4J) as Neo4jDriver).apply {
this.hostname("localhost")
.port(7687)
.username("neo4j")
.password("neo4j123")
.database("neo4j")
.connect()
}
driver = (DriverFactory(GraphDatabase.NEO4J) as Neo4jDriver)
.hostname("localhost")
.port(7687)
.username("neo4j")
.password("neo4j123")
.database("neo4j")
.connect()
assertEquals("localhost", driver.hostname)
assertEquals(7687, driver.port)
assertEquals("neo4j", driver.username)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ class NeptuneDriverIntTest {
@BeforeAll
fun setUpAll() {
testStartTime = System.nanoTime()
driver = (DriverFactory(GraphDatabase.NEPTUNE) as NeptuneDriver).apply {
this.addHostnames(System.getenv("NEPTUNE_HOSTNAME") ?: "localhost")
.port(8182)
.keyCertChainFile("src/test/resources/conf/SFSRootCAG2.pem")
.connect()
}
driver = (DriverFactory(GraphDatabase.NEPTUNE) as NeptuneDriver)
.addHostnames(System.getenv("NEPTUNE_HOSTNAME") ?: "localhost")
.port(8182)
.keyCertChainFile("src/test/resources/conf/SFSRootCAG2.pem")
.connect()
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ class OverflowDbDriverIntTest {
@BeforeAll
fun setUpAll() {
testStartTime = System.nanoTime()
driver = (DriverFactory(GraphDatabase.OVERFLOWDB) as OverflowDbDriver).apply {
serializationStatsEnabled = false
overflow = true
heapPercentageThreshold = 90
storageLocation = OverflowDbDriverIntTest.storageLocation
connect()
}
driver = (DriverFactory(GraphDatabase.OVERFLOWDB) as OverflowDbDriver)
.serializationStatsEnabled(false)
.overflow(true)
.heapPercentageThreshold(90)
.storageLocation(storageLocation)
.connect()
assertFalse(driver.serializationStatsEnabled)
assertTrue(driver.overflow)
assertEquals(90, driver.heapPercentageThreshold)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ import io.shiftleft.codepropertygraph.generated.EdgeTypes.*
import io.shiftleft.codepropertygraph.generated.NodeKeyNames.NAME
import io.shiftleft.codepropertygraph.generated.NodeTypes.FILE
import io.shiftleft.codepropertygraph.generated.nodes.*
import io.shiftleft.proto.cpg.Cpg
import org.apache.logging.log4j.LogManager
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.*
import overflowdb.Config
import overflowdb.Graph
import scala.Option
import java.io.File
Expand All @@ -62,7 +60,7 @@ class TinkerGraphDriverIntTest {
@BeforeAll
fun setUpAll() = run {
testStartTime = System.nanoTime()
driver = (DriverFactory(GraphDatabase.TINKER_GRAPH) as TinkerGraphDriver).apply { connect() }
driver = (DriverFactory(GraphDatabase.TINKER_GRAPH) as TinkerGraphDriver).connect()
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PlumeFrontend extends LanguageFrontend {
val cpgFile = File.createTempFile("plume", ".bin")
cpgFile.deleteOnExit()
Using(DriverFactory.invoke(GraphDatabase.OVERFLOWDB).asInstanceOf[OverflowDbDriver]) { driver =>
driver.setStorageLocation(cpgFile.getAbsolutePath)
driver.storageLocation(cpgFile.getAbsolutePath)
val extractor = new Extractor(driver)
extractor.load(sourceCodeFile).project().postProject()
driver.close()
Expand Down

0 comments on commit 788784f

Please sign in to comment.