addressed PR issues, added withFinalizable to remove try->finally boiler plate code

This commit is contained in:
Patrick Kuo 2016-11-04 11:43:28 +00:00
parent 549a952cc0
commit ce41d6a7a9
7 changed files with 16 additions and 19 deletions

View File

@ -31,7 +31,6 @@ import kotlin.system.exitProcess
* The Intermediate CA certificate,Intermediate CA private key and Root CA Certificate should use alias name specified in [X509Utilities]
*/
class CertificateSigningServer(val webServerAddr: HostAndPort, val certSigningService: CertificateSigningService) : Closeable {
companion object {
val log = loggerFor<CertificateSigningServer>()
fun Server.hostAndPort(): HostAndPort {

View File

@ -29,7 +29,7 @@ interface CertificationRequestStorage {
/**
* Retrieve list of request IDs waiting for approval.
* TODO : This is use for the background thread to approve request automatically without KYC checks, should be removed after testnet.
* TODO : This is used for the background thread to approve request automatically without KYC checks, should be removed after testnet.
*/
fun pendingRequestIds(): List<String>
}

View File

@ -1,16 +1,12 @@
package com.r3corda.netpermission.internal.persistence
import com.r3corda.core.crypto.SecureHash
import com.r3corda.node.utilities.databaseTransaction
import com.r3corda.node.utilities.deserializeFromBlob
import com.r3corda.node.utilities.localDateTime
import com.r3corda.node.utilities.serializeToBlob
import com.r3corda.node.utilities.*
import org.jetbrains.exposed.sql.*
import java.security.cert.Certificate
import java.time.LocalDateTime
class DBCertificateRequestStorage(private val database: Database) : CertificationRequestStorage {
private object DataTable : Table("certificate_signing_request") {
val requestId = varchar("request_id", 64).index().primaryKey()
val hostName = varchar("hostName", 100)
@ -35,8 +31,7 @@ class DBCertificateRequestStorage(private val database: Database) : Certificatio
override fun saveCertificate(requestId: String, certificateGenerator: (CertificationData) -> Certificate) {
databaseTransaction(database) {
val finalizables = mutableListOf<() -> Unit>()
try {
withFinalizables { finalizables ->
getRequest(requestId)?.let {
val clientCert = certificateGenerator(it)
DataTable.update({ DataTable.requestId eq requestId }) {
@ -44,8 +39,6 @@ class DBCertificateRequestStorage(private val database: Database) : Certificatio
it[certificate] = serializeToBlob(clientCert, finalizables)
}
}
} finally {
finalizables.forEach { it() }
}
}
}
@ -56,8 +49,7 @@ class DBCertificateRequestStorage(private val database: Database) : Certificatio
override fun saveRequest(certificationData: CertificationData): String {
return databaseTransaction(database) {
val finalizables = mutableListOf<() -> Unit>()
try {
withFinalizables { finalizables ->
val requestId = SecureHash.randomSHA256().toString()
DataTable.insert {
it[DataTable.requestId] = requestId
@ -67,8 +59,6 @@ class DBCertificateRequestStorage(private val database: Database) : Certificatio
it[requestTimestamp] = LocalDateTime.now()
}
requestId
} finally {
finalizables.forEach { it() }
}
}
}

View File

@ -7,7 +7,7 @@ approveAll = true
dataSourceProperties {
dataSourceClassName = org.h2.jdbcx.JdbcDataSource
"dataSource.url" = "jdbc:h2:file:"${basedir}"/persistence;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=10000;MVCC=true;MV_STORE=true;WRITE_DELAY=0;AUTO_SERVER_PORT="${h2port}
"dataSource.url" = "jdbc:h2:file:"${basedir}"/persistence;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=10000;WRITE_DELAY=0;AUTO_SERVER_PORT="${h2port}
"dataSource.user" = sa
"dataSource.password" = ""
}

View File

@ -110,9 +110,7 @@ class CertificateSigningServiceTest {
})
val certificates = assertNotNull(poll())
verify(storage, times(3)).getCertificate(any())
assertEquals(3, certificates.size)
(certificates.first() as X509Certificate).run {

View File

@ -11,7 +11,6 @@ import kotlin.test.assertNull
import kotlin.test.assertTrue
class DBCertificateRequestStorageTest {
val intermediateCA = X509Utilities.createSelfSignedCACert("Corda Node Intermediate CA")
@Test

View File

@ -30,6 +30,15 @@ fun <T> databaseTransaction(db: Database, statement: Transaction.() -> T): T {
return org.jetbrains.exposed.sql.transactions.transaction(Connection.TRANSACTION_REPEATABLE_READ, 1, statement)
}
fun <T> withFinalizables(statement: (MutableList<() -> Unit>) -> T): T {
val finalizables = mutableListOf<() -> Unit>()
return try {
statement(finalizables)
} finally {
finalizables.forEach { it() }
}
}
fun createDatabaseTransaction(db: Database): Transaction {
// We need to set the database for the current [Thread] or [Fiber] here as some tests share threads across databases.
StrandLocalTransactionManager.database = db
@ -138,12 +147,14 @@ class StrandLocalTransactionManager(initWithDatabase: Database) : TransactionMan
// Composite columns for use with below Exposed helpers.
data class PartyColumns(val name: Column<String>, val owningKey: Column<PublicKey>)
data class StateRefColumns(val txId: Column<SecureHash>, val index: Column<Int>)
/**
* [Table] column helpers for use with Exposed, as per [varchar] etc.
*/
fun Table.publicKey(name: String) = this.registerColumn<PublicKey>(name, PublicKeyColumnType)
fun Table.secureHash(name: String) = this.registerColumn<SecureHash>(name, SecureHashColumnType)
fun Table.party(nameColumnName: String, keyColumnName: String) = PartyColumns(this.varchar(nameColumnName, length = 255), this.publicKey(keyColumnName))
fun Table.uuidString(name: String) = this.registerColumn<UUID>(name, UUIDStringColumnType)