Skip to content

Commit

Permalink
Adding keys (#47)
Browse files Browse the repository at this point in the history
* Adding keys

* cleanup
  • Loading branch information
cworsnop-figure authored Jul 15, 2022
1 parent e754858 commit 99b6084
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@ class ContractUtilities(
val parsedRecords = getRecords(contractParser, records, contract, config.contract.parserConfig)

val participantsMap = participants.associate {
it.partyType to entityManager.getEntity(KeyManagementConfigWrapper(it.uuid, config.account.keyManagementConfig))
it.partyType to entityManager.getEntity(KeyManagementConfigWrapper(it.uuid.toString(), config.account.keyManagementConfig))
}

return scopes.map {
val scope = provenanceService.getScope(config.provenanceConfig, it.scopeUuid)
val scopeToUse: ScopeResponse? = if (scope.scope.scope.isSet() && !scope.scope.scope.scopeId.isEmpty) scope else null

if (scope.scope.scope.dataAccessList.any()) {
entityManager.hydrateKeys(scope.scope.scope.dataAccessList).forEach { kp ->
client.inner.affiliateRepository.addAffiliate(kp.signingKey.toJavaPublicKey(), kp.encryptionKey.toJavaPublicKey())
}
}

contractService.setupContract(
client,
contract,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ import io.provenance.scope.sdk.Affiliate
import io.provenance.scope.sdk.Client
import io.provenance.scope.sdk.ClientConfig
import io.provenance.scope.sdk.SharedClient
import org.springframework.stereotype.Component
import java.net.URI
import java.security.KeyPair
import java.security.PrivateKey
import java.security.PublicKey
import java.util.concurrent.TimeUnit
import org.springframework.stereotype.Component

@Component
class CreateClient(
private val provenanceProperties: ProvenanceProperties,
private val entityManager: EntityManager,
) : AbstractUseCase<CreateClientRequest, Client>() {
override suspend fun execute(args: CreateClientRequest): Client {
val originator = entityManager.getEntity(KeyManagementConfigWrapper(args.uuid, args.account.keyManagementConfig))
val originator = entityManager.getEntity(KeyManagementConfigWrapper(args.uuid.toString(), args.account.keyManagementConfig))
val affiliate = Affiliate(
signingKeyRef = DirectKeyRef(KeyPair(originator.signingPublicKey() as PublicKey, originator.signingPrivateKey() as PrivateKey)),
encryptionKeyRef = DirectKeyRef(KeyPair(originator.encryptionPublicKey() as PublicKey, originator.encryptionPrivateKey() as PrivateKey)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class ExecuteContract(
) : AbstractUseCase<ExecuteContractRequestWrapper, ContractExecutionResponse>() {

override suspend fun execute(args: ExecuteContractRequestWrapper): ContractExecutionResponse {

val signer = getSigner.execute(GetSignerRequest(args.uuid, args.request.config.account))
contractUtilities.createClient(args.uuid, args.request.permissions, args.request.participants, args.request.config).use { client ->
val session = contractUtilities.createSession(args.uuid, client, args.request.permissions, args.request.participants, args.request.config, args.request.records, listOf(args.request.scope)).single()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class EntityManager(
)

val token = fetchToken(config)
return manager.get(args.uuid.toString(), VaultSpec(args.uuid.toString(), "${config.address}/${args.uuid}", token))
return manager.get(args.entity, VaultSpec(args.entity, "${config.address}/${args.entity}", token))
}

fun hydrateKeys(permissions: PermissionInfo?, participants: List<Participant> = emptyList(), keyManagementConfig: KeyManagementConfig? = null): Set<AudienceKeyPair> {
Expand All @@ -47,7 +47,7 @@ class EntityManager(
)

fun getEntityKeys(uuid: UUID) {
val originator = getEntity(KeyManagementConfigWrapper(uuid, config))
val originator = getEntity(KeyManagementConfigWrapper(uuid.toString(), config))
additionalAudiences.add(
AudienceKeyPair(
originator.keys[KeyType.ENCRYPTION_PUBLIC_KEY].toString(),
Expand Down Expand Up @@ -76,6 +76,16 @@ class EntityManager(
return additionalAudiences
}

fun hydrateKeys(addresses: List<String>, keyManagementConfig: KeyManagementConfig? = null): Set<AudienceKeyPair> =
addresses.map {
getEntity(KeyManagementConfigWrapper(it, keyManagementConfig)).let { entity ->
AudienceKeyPair(
entity.keys[KeyType.ENCRYPTION_PUBLIC_KEY].toString(),
entity.keys[KeyType.ENCRYPTION_PUBLIC_KEY].toString(),
)
}
}.toSet()

@Suppress("UnsafeCallOnNullableType")
private fun fetchToken(config: KeyManagementConfig): String {

Expand All @@ -99,7 +109,7 @@ class EntityManager(

private fun getMemberKeyPair(audience: DefaultAudience, keyManagementConfig: KeyManagementConfig): AudienceKeyPair =
provenanceProperties.members.firstOrNull { it.name == audience }?.let {
val entity = getEntity(KeyManagementConfigWrapper(it.uuid, keyManagementConfig))
val entity = getEntity(KeyManagementConfigWrapper(it.uuid.toString(), keyManagementConfig))
AudienceKeyPair(
entity.keys[KeyType.ENCRYPTION_PUBLIC_KEY].toString(),
entity.keys[KeyType.SIGNING_PUBLIC_KEY].toString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.provenance.api.domain.usecase.common.originator.models

import io.provenance.api.models.account.KeyManagementConfig
import java.util.UUID

data class KeyManagementConfigWrapper(
val uuid: UUID,
val entity: String,
val config: KeyManagementConfig?
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RetrieveAndDecrypt(
private val objectStoreConfig: ObjectStoreConfig,
) : AbstractUseCase<RetrieveAndDecryptRequest, ByteArray>() {
override suspend fun execute(args: RetrieveAndDecryptRequest): ByteArray {
val originator = entityManager.getEntity(KeyManagementConfigWrapper(args.uuid, args.keyManagementConfig))
val originator = entityManager.getEntity(KeyManagementConfigWrapper(args.uuid.toString(), args.keyManagementConfig))
val publicKey = (originator.encryptionPublicKey() as? PublicKey)
?: throw IllegalStateException("Public key was not present for originator: ${args.uuid}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class StoreFile(
additionalAudiences = entityManager.hydrateKeys(permissions)
}

val originator = entityManager.getEntity(KeyManagementConfigWrapper(args.uuid, keyConfig))
val originator = entityManager.getEntity(KeyManagementConfigWrapper(args.uuid.toString(), keyConfig))
val file = args.request.getAsType<FilePart>("file")
var message: Any = ByteArrayInputStream(file.awaitAllBytes())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class StoreProto(
private val parser: MessageParser
) : AbstractUseCase<StoreProtoRequestWrapper, StoreProtoResponse>() {
override suspend fun execute(args: StoreProtoRequestWrapper): StoreProtoResponse {
val originator = entityManager.getEntity(KeyManagementConfigWrapper(args.uuid, args.request.account.keyManagementConfig))
val originator = entityManager.getEntity(KeyManagementConfigWrapper(args.uuid.toString(), args.request.account.keyManagementConfig))
val additionalAudiences = entityManager.hydrateKeys(args.request.permissions)

val asset = parser.parse(args.request.message, Class.forName(args.request.type))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.provenance.api.domain.usecase.provenance.account

import io.provenance.client.grpc.Signer
import io.provenance.api.domain.usecase.AbstractUseCase
import io.provenance.api.domain.usecase.common.originator.EntityManager
import io.provenance.api.domain.usecase.common.originator.models.KeyManagementConfigWrapper
import io.provenance.api.domain.usecase.provenance.account.models.GetSignerRequest
import io.provenance.api.frameworks.config.ProvenanceProperties
import io.provenance.api.frameworks.provenance.utility.ProvenanceUtils
import org.springframework.stereotype.Component
import io.provenance.client.grpc.Signer
import java.security.PrivateKey
import java.security.PublicKey
import org.springframework.stereotype.Component

@Component
class GetSigner(
Expand All @@ -19,7 +19,7 @@ class GetSigner(
override suspend fun execute(args: GetSignerRequest): Signer {
val utils = ProvenanceUtils()

val originator = entityManager.getEntity(KeyManagementConfigWrapper(args.uuid, args.account.keyManagementConfig))
val originator = entityManager.getEntity(KeyManagementConfigWrapper(args.uuid.toString(), args.account.keyManagementConfig))

return originator.signingPublicKey().let { public ->
originator.signingPrivateKey().let { private ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class ProvenanceService : Provenance {
sequenceOffset = cachedOffset.getAndIncrementOffset(account.sequence)
)

log.info("Sending tx.")
val result = pbClient.estimateAndBroadcastTx(
txBody = tx,
signers = listOf(baseSigner),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class StoreAssetTest : FunSpec({
val storeAssetResponse = StoreProtoResponse("HASH", "URI", "BUCKET", "NAME")

every { mockObjectStore.store(any(), any<Message>(), any(), any()) } returns storeAssetResponse
every { mockEntityManager.hydrateKeys(any()) } returns emptySet()
every { mockEntityManager.hydrateKeys(any<PermissionInfo>()) } returns emptySet()
every { mockOriginator.encryptionPublicKey() } returns mockOriginatorPublicKey
every { mockParser.parse(any(), any()) } returns Asset.getDefaultInstance()

Expand Down Expand Up @@ -108,7 +108,7 @@ class StoreAssetTest : FunSpec({

test("exception when public key is not set") {
every { mockOriginator.encryptionPublicKey() } returns FakeKey()
every { mockEntityManager.hydrateKeys(any()) } returns emptySet()
every { mockEntityManager.hydrateKeys(any<PermissionInfo>()) } returns emptySet()
every { mockParser.parse(any(), any()) } returns Asset.getDefaultInstance()

// Execute enable replication code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.provenance.api.domain.usecase.common.originator.EntityManager
import io.provenance.api.domain.usecase.provenance.account.GetSigner
import io.provenance.api.domain.usecase.provenance.tx.create.CreateTx
import io.provenance.api.domain.usecase.provenance.tx.create.models.CreateTxRequestWrapper
import io.provenance.api.frameworks.config.ProvenanceProperties
import io.provenance.api.models.account.AccountInfo
import io.provenance.api.models.p8e.Audience
import io.provenance.api.models.p8e.AudienceKeyPair
import io.provenance.api.models.p8e.tx.CreateTxRequest
import io.provenance.api.models.p8e.PermissionInfo
import io.provenance.api.models.p8e.tx.CreateTxRequest
import io.provenance.client.grpc.Signer
import io.provenance.core.Originator
import io.provenance.api.domain.usecase.provenance.account.GetSigner
import io.provenance.api.domain.usecase.provenance.tx.create.CreateTx
import io.provenance.api.domain.usecase.provenance.tx.create.models.CreateTxRequestWrapper
import io.provenance.api.frameworks.config.ProvenanceProperties
import io.provenance.scope.encryption.util.toJavaPublicKey
import io.provenance.scope.util.toUuid
import org.junit.jupiter.api.Assertions.assertNotNull
import java.security.PublicKey
import org.junit.jupiter.api.Assertions.assertNotNull

const val ADD_ASSET_AUDIENCE_PUBLIC_KEY =
"0A41046C57E9E25101D5E553AE003E2F79025E389B51495607C796B4E95C0A94001FBC24D84CD0780819612529B803E8AD0A397F474C965D957D33DD64E642B756FBC4"
Expand Down Expand Up @@ -69,7 +69,7 @@ class CreateTxTest : FunSpec({

test("happy path") {
every { mockOriginator.encryptionPublicKey() } returns mockOriginatorPublicKey
every { mockEntityManager.hydrateKeys(any()) } returns emptySet()
every { mockEntityManager.hydrateKeys(any<PermissionInfo>()) } returns emptySet()

// Execute enable replication code
val response = createTx.execute(
Expand Down

0 comments on commit 99b6084

Please sign in to comment.