From c2057e08939fc5e1c537eefec60c9252e6700a4b Mon Sep 17 00:00:00 2001 From: Stefano Franz Date: Sun, 1 Sep 2019 06:57:46 +0000 Subject: [PATCH] CORDA-3180: Added ability to lookup the associated UUID for a public key to KeyManagementService (#5411) * expose identity cache to KMSinternal * apply shams comments * Addressed review comments. --- .../net/corda/core/node/services/KeyManagementService.kt | 9 +++++++++ docs/source/changelog.rst | 2 ++ .../node/services/keys/BasicHSMKeyManagementService.kt | 5 +++++ .../node/services/keys/E2ETestKeyManagementService.kt | 5 +++++ .../testing/node/internal/MockKeyManagementService.kt | 6 ++++++ 5 files changed, 27 insertions(+) diff --git a/core/src/main/kotlin/net/corda/core/node/services/KeyManagementService.kt b/core/src/main/kotlin/net/corda/core/node/services/KeyManagementService.kt index 9f81abf51a..8b75e31b67 100644 --- a/core/src/main/kotlin/net/corda/core/node/services/KeyManagementService.kt +++ b/core/src/main/kotlin/net/corda/core/node/services/KeyManagementService.kt @@ -90,4 +90,13 @@ interface KeyManagementService { */ @Suspendable fun sign(signableData: SignableData, publicKey: PublicKey): TransactionSignature + + /** + * This method allows lookups of [PublicKey]s to an associated "external ID" / [UUID]. Providing a [PublicKey] that is unknown by the node + * or is not mapped to an external ID will return null. Otherwise, if the [PublicKey] has been mapped to an external ID, then the [UUID] + * for that external ID will be returned. + * @param publicKey the [PublicKey] used to perform the lookup to external ID + */ + @Suspendable + fun externalIdForPublicKey(publicKey: PublicKey): UUID? } \ No newline at end of file diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst index e7ff5e49ef..94b8717de9 100644 --- a/docs/source/changelog.rst +++ b/docs/source/changelog.rst @@ -7,6 +7,8 @@ release, see :doc:`app-upgrade-notes`. Unreleased ---------- +* Introduced a new API on ``KeyManagementService`` which facilitates lookups of ``PublicKey`` s to ``externalId`` s (Account IDs). + * Introduced a new low level flow diagnostics tool: checkpoint agent (that can be used standalone or in conjunction with the ``dumpCheckpoints`` shell command). See :doc:`checkpoint-tooling` for more information. diff --git a/node/src/main/kotlin/net/corda/node/services/keys/BasicHSMKeyManagementService.kt b/node/src/main/kotlin/net/corda/node/services/keys/BasicHSMKeyManagementService.kt index aeeac903c3..ae856eeaa0 100644 --- a/node/src/main/kotlin/net/corda/node/services/keys/BasicHSMKeyManagementService.kt +++ b/node/src/main/kotlin/net/corda/node/services/keys/BasicHSMKeyManagementService.kt @@ -35,6 +35,7 @@ class BasicHSMKeyManagementService(cacheFactory: NamedCacheFactory, private val database: CordaPersistence, private val cryptoService: SignOnlyCryptoService, private val pkToIdCache: WritablePublicKeyToOwningIdentityCache) : SingletonSerializeAsToken(), KeyManagementServiceInternal { + @Entity @Table(name = "${NODE_DATABASE_PREFIX}our_key_pairs") class PersistentKey( @@ -156,4 +157,8 @@ class BasicHSMKeyManagementService(cacheFactory: NamedCacheFactory, keyPair.sign(signableData) } } + + override fun externalIdForPublicKey(publicKey: PublicKey): UUID? { + return pkToIdCache[publicKey]?.uuid + } } diff --git a/node/src/main/kotlin/net/corda/node/services/keys/E2ETestKeyManagementService.kt b/node/src/main/kotlin/net/corda/node/services/keys/E2ETestKeyManagementService.kt index c5f38f0b8e..d65845b6ce 100644 --- a/node/src/main/kotlin/net/corda/node/services/keys/E2ETestKeyManagementService.kt +++ b/node/src/main/kotlin/net/corda/node/services/keys/E2ETestKeyManagementService.kt @@ -27,6 +27,7 @@ import javax.annotation.concurrent.ThreadSafe */ @ThreadSafe class E2ETestKeyManagementService(override val identityService: IdentityService, private val cryptoService: CryptoService? = null) : SingletonSerializeAsToken(), KeyManagementServiceInternal { + private class InnerState { val keys = HashMap() } @@ -87,4 +88,8 @@ class E2ETestKeyManagementService(override val identityService: IdentityService, val keyPair = getSigningKeyPair(publicKey) return keyPair.sign(signableData) } + + override fun externalIdForPublicKey(publicKey: PublicKey): UUID? { + throw UnsupportedOperationException("This operation is only supported by persistent key management service variants.") + } } diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/MockKeyManagementService.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/MockKeyManagementService.kt index 0537412073..629d1135ee 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/MockKeyManagementService.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/MockKeyManagementService.kt @@ -21,6 +21,8 @@ import java.util.* class MockKeyManagementService(override val identityService: IdentityService, vararg initialKeys: KeyPair, private val pkToIdCache: WritablePublicKeyToOwningIdentityCache) : SingletonSerializeAsToken(), KeyManagementServiceInternal { + + private val keyStore: MutableMap = initialKeys.associateByTo(HashMap(), { it.public }, { it.private }) override val keys: Set get() = keyStore.keys @@ -57,4 +59,8 @@ class MockKeyManagementService(override val identityService: IdentityService, val keyPair = getSigningKeyPair(publicKey) return keyPair.sign(signableData) } + + override fun externalIdForPublicKey(publicKey: PublicKey): UUID? { + return pkToIdCache[publicKey]?.uuid + } } \ No newline at end of file