Skip to content

Commit

Permalink
Merge pull request #114 from provenance-io/fix-api-key-consumer-calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rromanowski-figure authored Aug 18, 2023
2 parents 61ae5b4 + f6d9ffb commit 1c50031
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ sealed interface Entity { val id: String }
data class KongConsumer(
val entityId: String,
val username: String?,
val customId: String,
val customId: String?,
) : Entity {
override val id: String
get() = entityId
get() = customId ?: username ?: entityId
}

data class MemberUUID(val value: UUID) : Entity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ApproveContractBatchExecution(
}
}.fold(
onSuccess = {
log.info("Successfully processed batch $index of ${chunked.size}")
log.debug { "Successfully processed batch $index of ${chunked.size}" }
},
onFailure = { error ->
errors.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ContractUtilities(
participantsMap,
scopeToUse,
config.contract.scopeSpecificationName,
audiences.map { it.encryptionKey.toJavaPublicKey() }.toSet()
audiences.map { audience -> audience.encryptionKey.toJavaPublicKey() }.toSet()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ExecuteContractBatch(
}
}.fold(
onSuccess = {
log.info("Successfully processed batch $index of ${chunkedSignedResult.size}")
log.debug { "Successfully processed batch $index of ${chunkedSignedResult.size}" }
},
onFailure = { error ->
errors.add(
Expand Down Expand Up @@ -111,7 +111,7 @@ class ExecuteContractBatch(
)
}.fold(
onSuccess = {
log.info("Successfully processed batch $index of ${chunkedFragResult.size}")
log.debug { "Successfully processed batch $index of ${chunkedFragResult.size}" }
},
onFailure = {
errors.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class SubmitContractBatchExecutionResult(
}
}.fold(
onSuccess = {
log.info("Successfully processed batch $index of ${pair.size}")
log.debug { "Successfully processed batch $index of ${pair.size}" }
},
onFailure = {
errors.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class EnableReplication(
args.targetSigningPublicKey.toJavaPublicKey(),
args.targetEncryptionPublicKey.toJavaPublicKey(),
args.targetObjectStoreAddress,
) ?: throw IllegalStateException("Error performing operation")
}
log.info("createPublicKey() response: ${publicKeyResponse.toJson()}")
)
} ?: throw IllegalStateException("Error performing operation")

log.debug { "createPublicKey() response: ${publicKeyResponse.toJson()}" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import java.util.UUID
sealed interface GetSignerRequest {

companion object {
operator fun invoke(id: Entity, account: AccountInfo): GetSignerRequest = when (id) {
is KongConsumer -> GetSignerByAddressRequest(id.customId, account)
is MemberUUID -> GetSignerByUUIDRequest(id.value, account)
operator fun invoke(entity: Entity, account: AccountInfo): GetSignerRequest = when (entity) {
is KongConsumer -> GetSignerByAddressRequest(entity.id, account)
is MemberUUID -> GetSignerByUUIDRequest(entity.value, account)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,12 @@ class UpdateScopeDataAccess(
DataAccessChangeType.ADD -> {
runCatching {
addAction(change)
}
.fold(
onSuccess = { result -> result },
onFailure = { error -> errors.add(error) }
)
}.getOrElse { error -> errors.add(error) }
}
DataAccessChangeType.REMOVE -> {
runCatching {
removeAction(change)
}
.fold(
onSuccess = { result -> result },
onFailure = { error -> errors.add(error) }
)
}.getOrElse { error -> errors.add(error) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.springframework.stereotype.Component

@Component
class P8eContractService : ContractService {
private val log = KotlinLogging.logger { }
private val log = KotlinLogging.logger("P8eContractService")

override fun getContract(contractName: String): Class<out P8eContract> {
return Class.forName(contractName).asSubclass(P8eContract::class.java)
Expand Down Expand Up @@ -64,29 +64,31 @@ class P8eContractService : ContractService {
}

override fun executeContract(client: Client, session: Session): ExecutionResult =
runCatchingExecutionResult<Session>() {
runCatchingExecutionResult {
client.execute(session)
}

override fun executeContract(client: Client, envelope: Envelope): ExecutionResult =
runCatchingExecutionResult<Envelope>() {
runCatchingExecutionResult {
client.execute(envelope)
}

private fun <T> runCatchingExecutionResult(execution: () -> ExecutionResult): ExecutionResult =
runCatching {
execution()
}.fold(
onSuccess = { result -> result },
onFailure = { throwable ->
throw ContractExecutionException("Contract execution failed: ${throwable.message}", throwable)
}
)
private fun runCatchingExecutionResult(execution: () -> ExecutionResult): ExecutionResult = runCatching {
execution()
}.getOrElse { throwable ->
throw ContractExecutionException("Contract execution failed: ${throwable.message}", throwable)
}

private fun Session.Builder.configureSession(records: Map<String, Message>, sessionUuid: UUID? = null, participants: Map<Specifications.PartyType, KeyEntity>?, audiences: Set<PublicKey>): Session =
private fun Session.Builder.configureSession(
records: Map<String, Message>,
sessionUuid: UUID? = null,
participants: Map<Specifications.PartyType, KeyEntity>?,
audiences: Set<PublicKey>,
): Session =
this.setSessionUuid(sessionUuid ?: UUID.randomUUID())
.also { records.forEach { record -> it.addProposedRecord(record.key, record.value) } }
.also {
records.forEach { record -> it.addProposedRecord(record.key, record.value) }

participants?.forEach { participant ->
it.addParticipant(
participant.key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,12 @@ class ProvenanceService : Provenance {
val account = getBaseAccount(pbClient, signer.address())
val cachedOffset = cachedSequenceMap.getOrPut(signer.address()) { CachedAccountSequence() }

runCatching {
return runCatching {
action(pbClient, account, cachedOffset.getAndIncrementOffset(account.sequence))
}.fold(
onSuccess = {
return it
},
onFailure = {
cachedOffset.getAndDecrement(account.sequence)
throw it
}
)
}.getOrElse {
cachedOffset.getAndDecrement(account.sequence)
throw it
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ class CeeApi {
POST("/execute", handler::executeContract)
POST("/submit", handler::submitContractResult)
POST("/reject", handler::rejectContractExecution)
GET("/headers", handler::showHeaders)
"/batch".nest {
POST("/execute", handler::executeContractBatch)
POST("/submit", handler::submitContractBatchResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ import io.provenance.api.domain.usecase.cee.submit.models.SubmitContractBatchExe
import io.provenance.api.domain.usecase.cee.submit.models.SubmitContractExecutionResultRequestWrapper
import io.provenance.api.frameworks.web.misc.foldToServerResponse
import io.provenance.api.frameworks.web.misc.getEntity
import mu.KotlinLogging
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.awaitBody

private val log = KotlinLogging.logger("CeeHandler")

@Component
class CeeHandler(
private val executeContract: ExecuteContract,
Expand Down Expand Up @@ -68,8 +65,4 @@ class CeeHandler(
suspend fun rejectContractBatchExecution(req: ServerRequest): ServerResponse = runCatching {
rejectContractBatchExecution.execute(RejectContractBatchRequestWrapper(req.getEntity(), req.awaitBody()))
}.foldToServerResponse()

suspend fun showHeaders(req: ServerRequest): ServerResponse = runCatching {
req.headers().also { log.info { "headers: $it" } }
}.foldToServerResponse()
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ class ProvenanceHandler(
)
}.foldToServerResponse()

suspend fun checkCustody(req: ServerRequest): ServerResponse = kotlin.runCatching {
suspend fun checkCustody(req: ServerRequest): ServerResponse = runCatching {
getSigner.execute(
GetSignerRequest(
id = req.getEntity(),
account = req.awaitBodyOrNull() ?: AccountInfo()
entity = req.getEntity(),
account = req.awaitBodyOrNull() ?: AccountInfo(),
)
)
}.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.awaitBodyOrNull
import java.util.UUID

private val log = KotlinLogging.logger {}
private val log = KotlinLogging.logger("ServerRequest")

val ServerRequest.ipAddress: String
get() = headers().firstHeader("x-real-ip") ?: remoteAddress().get().address.toString()
Expand Down Expand Up @@ -37,9 +37,7 @@ private fun ServerRequest.getConsumerOrNull(): KongConsumer? {
KongConsumer(
entityId = id,
username = headers().firstHeader("x-consumer-username"),
customId = requireNotNull(headers().firstHeader("x-consumer-custom-id")) {
"Custom ID for consumer (x-consumer-custom-id) missing"
},
customId = headers().firstHeader("x-consumer-custom-id"),
)
}
}

0 comments on commit 1c50031

Please sign in to comment.