Skip to content

Commit bfa35cc

Browse files
committed
Migrate to RocksDB
1 parent c5a7f6e commit bfa35cc

File tree

5 files changed

+79
-52
lines changed

5 files changed

+79
-52
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ dependencies {
3030
implementation 'io.quarkus:quarkus-kotlin'
3131
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
3232
implementation 'io.quarkus:quarkus-arc'
33-
implementation 'org.mapdb:mapdb:3.1.0'
33+
implementation 'org.rocksdb:rocksdbjni:10.2.1'
3434
implementation 'com.github.ben-manes.caffeine:caffeine:3.2.0'
3535

3636
implementation 'io.projectreactor:reactor-core:3.7.5'
Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,108 @@
11
package org.exploit.keeper.db
22

33
import org.exploit.keeper.service.key.KeyOps
4-
import org.mapdb.DB
5-
import org.mapdb.DBMaker
6-
import org.mapdb.HTreeMap
7-
import org.mapdb.Serializer
8-
import org.slf4j.Logger
9-
import org.slf4j.LoggerFactory
4+
import org.rocksdb.*
105
import java.io.Closeable
11-
import java.io.File
6+
import java.nio.charset.StandardCharsets
127

138
class LMKDB(dbPath: String, private var opsProvider: () -> KeyOps) : Closeable {
14-
private val db: DB = DBMaker.fileDB(File(dbPath))
15-
.fileMmapEnable()
16-
.transactionEnable()
17-
.make()
18-
19-
val private : KeyStore = KeyStore(PRIVATE_KEY_MAP)
20-
val public: KeyStore = NoOpsKeyStore(PUBLIC_KEY_MAP)
9+
private val db: TransactionDB
10+
private val cfHandles: List<ColumnFamilyHandle>
11+
private val writeOpts: WriteOptions
12+
val private: KeyStore
13+
val public: KeyStore
14+
15+
init {
16+
RocksDB.loadLibrary()
17+
18+
writeOpts = WriteOptions()
19+
20+
val options = DBOptions()
21+
.setCreateIfMissing(true)
22+
.setCreateMissingColumnFamilies(true)
23+
24+
val txnOptions = TransactionDBOptions()
25+
26+
val cfDescs = listOf(
27+
ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
28+
ColumnFamilyDescriptor(PRIVATE_KEY_MAP.toByteArray()),
29+
ColumnFamilyDescriptor(PUBLIC_KEY_MAP.toByteArray())
30+
)
31+
32+
val handles = ArrayList<ColumnFamilyHandle>()
33+
db = TransactionDB.open(options, txnOptions, dbPath, cfDescs, handles)
34+
35+
cfHandles = handles
36+
val cfMap = mapOf(
37+
PRIVATE_KEY_MAP to cfHandles[1],
38+
PUBLIC_KEY_MAP to cfHandles[2]
39+
)
40+
private = KeyStore(PRIVATE_KEY_MAP, cfMap[PRIVATE_KEY_MAP]!!)
41+
public = NoOpsKeyStore(PUBLIC_KEY_MAP, cfMap[PUBLIC_KEY_MAP]!!)
42+
}
2143

2244
fun isInitialized(): Boolean = private.hasKey(SENTINEL_KEY)
2345

2446
fun writeSentinel(ops: KeyOps) = private.saveRaw(SENTINEL_KEY, ops.encrypt(SENTINEL_VAL))
2547

2648
fun checkValidKey(ops: KeyOps): Boolean =
27-
private.getRaw(SENTINEL_KEY)?.let { ops.decrypt(it).contentEquals(SENTINEL_VAL) }
28-
?: false
49+
private.getRaw(SENTINEL_KEY)?.let { ops.decrypt(it).contentEquals(SENTINEL_VAL) } ?: false
2950

30-
override fun close() = db.close()
31-
32-
open inner class KeyStore(val mapName: String) {
33-
private val map: HTreeMap<String, ByteArray> by lazy {
34-
db.hashMap(mapName, Serializer.STRING, Serializer.BYTE_ARRAY).createOrOpen()
35-
}
51+
override fun close() {
52+
cfHandles.forEach { it.close() }
53+
db.close()
54+
}
3655

56+
open inner class KeyStore(private val mapName: String, private val cf: ColumnFamilyHandle) {
3757
open fun save(id: String, raw: ByteArray) {
3858
val enc = opsProvider().encrypt(raw)
39-
map[id] = enc
40-
db.commit()
59+
db.beginTransaction(writeOpts).use { tx ->
60+
tx.put(cf, id.toByteArray(StandardCharsets.UTF_8), enc)
61+
tx.commit()
62+
}
4163
}
4264

4365
open fun saveRaw(id: String, raw: ByteArray) {
44-
map[id] = raw
45-
db.commit()
66+
db.beginTransaction(writeOpts).use { tx ->
67+
tx.put(cf, id.toByteArray(StandardCharsets.UTF_8), raw)
68+
tx.commit()
69+
}
4670
}
4771

4872
open fun get(id: String): ByteArray? =
49-
map[id]?.let { opsProvider().decrypt(it) }
73+
db.get(cf, id.toByteArray(StandardCharsets.UTF_8))?.let { opsProvider().decrypt(it) }
5074

5175
open fun getRaw(id: String): ByteArray? =
52-
map[id]
76+
db.get(cf, id.toByteArray(StandardCharsets.UTF_8))
5377

5478
open fun hasKey(id: String): Boolean =
55-
map.containsKey(id)
79+
db.get(cf, id.toByteArray(StandardCharsets.UTF_8)) != null
5680

5781
open fun delete(id: String) {
58-
map.remove(id); db.commit()
82+
db.beginTransaction(writeOpts).use { tx ->
83+
tx.delete(cf, id.toByteArray(StandardCharsets.UTF_8))
84+
tx.commit()
85+
}
5986
}
6087
}
6188

62-
inner class NoOpsKeyStore(mapName: String): KeyStore(mapName) {
89+
inner class NoOpsKeyStore(mapName: String, private val cf: ColumnFamilyHandle) : KeyStore(mapName, cf) {
6390
override fun save(id: String, raw: ByteArray) {
64-
db.hashMap(mapName, Serializer.STRING, Serializer.BYTE_ARRAY)
65-
.createOrOpen()[id] = raw ; db.commit()
91+
db.beginTransaction(writeOpts).use { tx ->
92+
tx.put(cf, id.toByteArray(StandardCharsets.UTF_8), raw)
93+
tx.commit()
94+
}
6695
}
96+
6797
override fun get(id: String): ByteArray? =
68-
db.hashMap(mapName, Serializer.STRING, Serializer.BYTE_ARRAY)
69-
.createOrOpen()[id]
98+
db.get(cf, id.toByteArray(StandardCharsets.UTF_8))
7099
}
71100

72101
private companion object {
73-
val LOGGER: Logger = LoggerFactory.getLogger(LMKDB::class.java)
74-
const val SENTINEL_KEY = "__meta__"
75102
val SENTINEL_VAL = "BK-v1".toByteArray()
76103

104+
const val SENTINEL_KEY = "__meta__"
77105
const val PRIVATE_KEY_MAP = "privateKey"
78-
private const val PUBLIC_KEY_MAP = "publicKey"
106+
const val PUBLIC_KEY_MAP = "publicKey"
79107
}
80108
}

src/main/kotlin/org/exploit/keeper/service/signature/gg20/GG20SessionFactory.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import org.exploit.threshield.ecdsa.context.crypto.CryptoContext
2626
import org.exploit.threshield.ecdsa.context.init.InitContext
2727
import org.exploit.threshield.ecdsa.context.integrity.InMemoryIntegrityContext
2828
import org.exploit.threshield.ecdsa.context.mta.MtAContext
29-
import org.slf4j.Logger
30-
import org.slf4j.LoggerFactory
3129
import java.util.concurrent.TimeUnit
3230

3331
@Singleton
@@ -104,20 +102,11 @@ class GG20SessionFactory(
104102
sessionMap.remove(sessionId)
105103
}
106104

107-
override fun type(): SessionType {
108-
return SessionType.GG20
109-
}
105+
override fun type(): SessionType =
106+
SessionType.GG20
110107

111108
@PreDestroy
112109
private fun destroy() {
113110
sessionMap.close()
114111
}
115-
116-
private fun ByteArray.toPointOps(curveParams: ECCurveParams): ECPointOps {
117-
return ECPointOps(curveParams.x9ECParameters.curve.decodePoint(this))
118-
}
119-
120-
private companion object {
121-
val LOGGER: Logger = LoggerFactory.getLogger(GG20SessionFactory::class.java)
122-
}
123112
}

src/main/resources/application.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
host-enabled: false
66
ssl-port: -1
77

8+
quarkus:
9+
banner:
10+
enabled: true
11+
path: banner.txt
12+
813
vault:
914
url: ${VAULT_URL}
1015
token: ${VAULT_TOKEN}

src/main/resources/banner.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
________ __ ________________ __________
2+
/_ __/ //_// ____/ ____/ __ \/ ____/ __ \
3+
/ / / ,< / __/ / __/ / /_/ / __/ / /_/ /
4+
/ / / /| |/ /___/ /___/ ____/ /___/ _, _/
5+
/_/ /_/ |_/_____/_____/_/ /_____/_/ |_|

0 commit comments

Comments
 (0)