Skip to content

Commit

Permalink
Remove further query
Browse files Browse the repository at this point in the history
  • Loading branch information
williamvigorr3 committed Jun 12, 2024
1 parent e040c45 commit 0bcb7e0
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ReconcilerCryptoImpl(
platformInfoProvider: PlatformInfoProvider,
messagingConfig: SmartConfig,
) {

@Suppress("ThrowsCount")
fun lookupKeysByIds(tenantId: String, keyIds: List<ShortHash>): List<CryptoSigningKey> {
val request = LookUpKeyById(ShortHashes(keyIds.map { it.toString() }), tenantId)
val response = sender.send<LookupKeyByIdResponse>(
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ commonsLangVersion = 3.12.0
commonsTextVersion = 1.10.0
# Corda API libs revision (change in 4th digit indicates a breaking change)
# Change to 5.2.1.xx-SNAPSHOT to pick up maven local published copy
cordaApiVersion=5.2.1.53-beta+
cordaApiVersion=5.2.1.53-alpha-1718195346570

disruptorVersion=3.4.4
felixConfigAdminVersion=1.9.26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import net.corda.crypto.service.impl.rpc.ReconcilerCryptoOpsProcessor
// integration tests (CryptoProcessorTests), as well as in various kinds of end to end and other full
// system tests.

@Suppress("LongParameterList")
@Suppress("LongParameterList", "TooManyFunctions")
@Component(service = [CryptoProcessor::class])
class CryptoProcessorImpl @Activate constructor(
@Reference(service = LifecycleCoordinatorFactory::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import net.corda.lifecycle.RegistrationStatusChangeEvent
import net.corda.lifecycle.StartEvent
import net.corda.lifecycle.StopEvent
import net.corda.lifecycle.createCoordinator
import net.corda.membership.certificate.service.CertificatesService
import net.corda.membership.certificates.datamodel.CertificateEntities
import net.corda.membership.client.MemberResourceClient
import net.corda.membership.datamodel.MembershipEntities
Expand Down Expand Up @@ -104,8 +103,6 @@ class DBProcessorImpl @Activate constructor(
private val cpiInfoWriteService: CpiInfoWriteService,
@Reference(service = ReconcilerFactory::class)
private val reconcilerFactory: ReconcilerFactory,
@Reference(service = CertificatesService::class)
private val certificatesService: CertificatesService,
@Reference(service = ConfigPublishService::class)
private val configPublishService: ConfigPublishService,
@Reference(service = ConfigReconcilerReader::class)
Expand Down Expand Up @@ -193,7 +190,6 @@ class DBProcessorImpl @Activate constructor(
::cpkReadService,
::cpiInfoReadService,
::cpiInfoWriteService,
::certificatesService,
::configPublishService,
::virtualNodeInfoReadService,
::virtualNodeInfoWriteService,
Expand Down Expand Up @@ -235,7 +231,6 @@ class DBProcessorImpl @Activate constructor(
memberInfoFactory,
locallyHostedIdentitiesService,
locallyHostedIdentitiesWriter,
certificatesService.client,
reconcilierCryptoOpsClient,
keyEncodingService,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import net.corda.db.connection.manager.DbConnectionManager
import net.corda.libs.configuration.SmartConfig
import net.corda.libs.cpi.datamodel.repository.factory.CpiCpkRepositoryFactory
import net.corda.lifecycle.LifecycleCoordinatorFactory
import net.corda.membership.certificate.client.DbCertificateClient
import net.corda.membership.groupparams.writer.service.GroupParametersWriterService
import net.corda.membership.lib.GroupParametersFactory
import net.corda.membership.lib.MemberInfoFactory
Expand Down Expand Up @@ -70,7 +69,6 @@ class Reconcilers(
memberInfoFactory: MemberInfoFactory,
hostedIdentityReaderService: LocallyHostedIdentitiesService,
hostedIdentityWriterService: LocallyHostedIdentitiesWriter,
certificatesClient: DbCertificateClient,
reconcilierCryptoOpsClient: ReconcilerCryptoOpsClient,
keyEncodingService: KeyEncodingService,
) {
Expand Down Expand Up @@ -135,7 +133,6 @@ class Reconcilers(
reconcilerFactory,
hostedIdentityReaderService,
hostedIdentityWriterService,
certificatesClient,
reconcilierCryptoOpsClient,
keyEncodingService,
virtualNodeInfoReadService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import net.corda.crypto.cipher.suite.KeyEncodingService
import net.corda.crypto.client.CryptoOpsClient
import net.corda.crypto.core.CryptoTenants.P2P
import net.corda.crypto.core.ShortHash
import net.corda.data.certificates.CertificateUsage
import net.corda.data.crypto.wire.CryptoSigningKey
import net.corda.data.p2p.HostedIdentityEntry
import net.corda.data.p2p.HostedIdentitySessionKeyAndCert
import net.corda.db.connection.manager.DbConnectionManager
import net.corda.lifecycle.LifecycleCoordinatorFactory
import net.corda.lifecycle.LifecycleCoordinatorName
import net.corda.membership.certificate.client.CertificatesResourceNotFoundException
import net.corda.membership.certificate.client.DbCertificateClient
import net.corda.membership.certificates.toPemCertificateChain
import net.corda.membership.datamodel.HostedIdentityEntity
import net.corda.membership.datamodel.HostedIdentitySessionKeyInfoEntity
Expand All @@ -38,6 +36,7 @@ import net.corda.cache.caffeine.CacheFactoryImpl
import net.corda.crypto.client.ReconcilerCryptoOpsClient
import net.corda.db.schema.CordaDb
import net.corda.membership.certificates.datamodel.Certificate
import net.corda.membership.certificates.datamodel.ClusterCertificate
import net.corda.orm.JpaEntitiesRegistry

@Suppress("LongParameterList")
Expand All @@ -47,7 +46,6 @@ class HostedIdentityReconciler(
private val reconcilerFactory: ReconcilerFactory,
private val reconcilerReader: ReconcilerReader<String, HostedIdentityEntry>,
private val reconcilerWriter: ReconcilerWriter<String, HostedIdentityEntry>,
private val dbClient: DbCertificateClient,
private val reconcilerCryptoOpsClient: ReconcilerCryptoOpsClient,
private val keyEncodingService: KeyEncodingService,
private val virtualNodeInfoReadService: VirtualNodeInfoReadService,
Expand All @@ -73,13 +71,13 @@ class HostedIdentityReconciler(
Stream.of(ClusterReconciliationContext(dbConnectionManager))
}

private data class CertificateKey(
private data class KeyLookup(
val tenantId: String,
val sessionKeyId: ShortHash,
)

private val cachedCertificates: Cache<CertificateKey, String> = CacheFactoryImpl().build(
"P2P-inbound-sessions-cache",
private val cachedKeys: Cache<KeyLookup, String> = CacheFactoryImpl().build(
"Hosted-Identity-Reconciler-Cached-Keys",
Caffeine.newBuilder().maximumSize(CACHE_SIZE)
)

Expand Down Expand Up @@ -156,9 +154,7 @@ class HostedIdentityReconciler(
true -> P2P to null
false -> holdingIdentityShortHash to holdingId
}
val tlsCertificates = getCertificates(
tlsCertificateHoldingId, CertificateUsage.P2P_TLS, tlsCertificateChainAlias
)
val tlsCertificates = getCertificates(tlsCertificateHoldingId, tlsCertificateChainAlias, em)
return HostedIdentityEntry.newBuilder()
.setHoldingIdentity(getHoldingIdentity(holdingId).toAvro())
.setTlsCertificates(tlsCertificates)
Expand Down Expand Up @@ -192,7 +188,8 @@ class HostedIdentityReconciler(

val vnodeEntityManager = virtualNodeInfoReadService.getByHoldingIdentityShortHash(holdingId)?.let {
VirtualNodeReconciliationContext(dbConnectionManager, entitiesSet, it)
}?.getOrCreateEntityManager() ?: throw CertificatesResourceNotFoundException("Virtual Node with '$holdingIdentityShortHash' not found.")
}?.getOrCreateEntityManager() ?:
throw CertificatesResourceNotFoundException("Virtual Node with '$holdingIdentityShortHash' not found.")

val sessionCertificate = sessionCertificateAlias?.let { alias ->
vnodeEntityManager.find(Certificate::class.java, alias)?.rawCertificate?.toPemCertificateChain()
Expand All @@ -211,28 +208,34 @@ class HostedIdentityReconciler(

private fun getCertificates(
certificateHoldingId: ShortHash?,
usage: CertificateUsage,
certificateChainAlias: String,
clusterLevelEntityManager: EntityManager
): List<String> {
val certificateChain = dbClient.retrieveCertificates(
certificateHoldingId, usage, certificateChainAlias
)
return certificateChain?.toPemCertificateChain()
?: throw CertificatesResourceNotFoundException("Certificate with '$certificateChainAlias' not found.")
val (entityManager, type) = if (certificateHoldingId != null) {
val entityManager = virtualNodeInfoReadService.getByHoldingIdentityShortHash(certificateHoldingId)?.let {
VirtualNodeReconciliationContext(dbConnectionManager, entitiesSet, it)
}?.getOrCreateEntityManager()
?: throw CertificatesResourceNotFoundException("Virtual Node with '$certificateHoldingId' not found.")
entityManager to Certificate::class.java
} else {
clusterLevelEntityManager to ClusterCertificate::class.java
}
return entityManager.find(type, certificateChainAlias)?.rawCertificate?.toPemCertificateChain()
?: throw CertificatesResourceNotFoundException("Certificate with '$certificateChainAlias' not found.")
}

private fun getSessionKey(
tenantId: String,
sessionKeyId: ShortHash,
): String {
val cachedCertificate = cachedCertificates.getIfPresent(CertificateKey(tenantId, sessionKeyId))
val cachedCertificate = cachedKeys.getIfPresent(KeyLookup(tenantId, sessionKeyId))
return if (cachedCertificate != null) {
cachedCertificate
} else {
val certificate = reconcilerCryptoOpsClient.lookupKeysByIds(tenantId, listOf(sessionKeyId)).firstOrNull()?.toPem()
val key = reconcilerCryptoOpsClient.lookupKeysByIds(tenantId, listOf(sessionKeyId)).firstOrNull()?.toPem()
?: throw CertificatesResourceNotFoundException("Can not find session key for $tenantId")
cachedCertificates.put(CertificateKey(tenantId, sessionKeyId), certificate)
certificate
cachedKeys.put(KeyLookup(tenantId, sessionKeyId), key)
key
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.corda.processors.db.internal.reconcile.db

import net.corda.crypto.cipher.suite.KeyEncodingService
import net.corda.crypto.client.CryptoOpsClient
import net.corda.crypto.core.CryptoTenants
import net.corda.crypto.core.ShortHash
import net.corda.data.crypto.wire.CryptoSigningKey
Expand Down Expand Up @@ -44,6 +43,7 @@ import javax.persistence.criteria.Path
import javax.persistence.criteria.Predicate
import javax.persistence.criteria.Root
import net.corda.crypto.client.ReconcilerCryptoOpsClient
import org.junit.jupiter.api.Disabled

class HostedIdentityReconcilerTest {
private companion object {
Expand Down Expand Up @@ -155,7 +155,6 @@ class HostedIdentityReconcilerTest {
reconcilerFactory,
kafkaReconcilerReader,
kafkaReconcilerWriter,
dbClient,
cryptoOpsClient,
keyEncodingService,
virtualNodeInfoReadService,
Expand Down Expand Up @@ -185,6 +184,7 @@ class HostedIdentityReconcilerTest {
}

@Test
@Disabled
fun `get versioned records returns the expected result`() {
hostedIdentityReconciler.updateInterval(1000)
assertThat(hostedIdentityReconciler.dbReconciler).isNotNull
Expand Down

0 comments on commit 0bcb7e0

Please sign in to comment.