ENT-5131: Avoid NPE by throwing a catchable exception when openAttachment fails. (#6408)

This commit is contained in:
Ryan Fowler 2020-07-06 11:42:36 +01:00 committed by GitHub
parent 6aa19723e6
commit 0d5bed5243
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 2 deletions

View File

@ -302,7 +302,12 @@ interface CordaRPCOps : RPCOps {
/** Checks whether an attachment with the given hash is stored on the node. */ /** Checks whether an attachment with the given hash is stored on the node. */
fun attachmentExists(id: SecureHash): Boolean fun attachmentExists(id: SecureHash): Boolean
/** Download an attachment JAR by ID. */ /**
* Download an attachment JAR by ID.
* @param id the id of the attachment to open
* @return the stream of the JAR
* @throws RPCException if the attachment doesn't exist
* */
fun openAttachment(id: SecureHash): InputStream fun openAttachment(id: SecureHash): InputStream
/** Uploads a jar to the node, returns it's hash. */ /** Uploads a jar to the node, returns it's hash. */

View File

@ -1,5 +1,6 @@
package net.corda.node.internal package net.corda.node.internal
import net.corda.client.rpc.RPCException
import net.corda.client.rpc.notUsed import net.corda.client.rpc.notUsed
import net.corda.common.logging.CordaVersion import net.corda.common.logging.CordaVersion
import net.corda.core.CordaRuntimeException import net.corda.core.CordaRuntimeException
@ -263,7 +264,8 @@ internal class CordaRPCOpsImpl(
} }
override fun openAttachment(id: SecureHash): InputStream { override fun openAttachment(id: SecureHash): InputStream {
return services.attachments.openAttachment(id)!!.open() return services.attachments.openAttachment(id)?.open() ?:
throw RPCException("Unable to open attachment with id: $id")
} }
override fun uploadAttachment(jar: InputStream): SecureHash { override fun uploadAttachment(jar: InputStream): SecureHash {

View File

@ -2,11 +2,13 @@ package net.corda.node
import co.paralleluniverse.fibers.Suspendable import co.paralleluniverse.fibers.Suspendable
import net.corda.client.rpc.PermissionException import net.corda.client.rpc.PermissionException
import net.corda.client.rpc.RPCException
import net.corda.core.context.AuthServiceId import net.corda.core.context.AuthServiceId
import net.corda.core.context.InvocationContext import net.corda.core.context.InvocationContext
import net.corda.core.contracts.Amount import net.corda.core.contracts.Amount
import net.corda.core.contracts.ContractState import net.corda.core.contracts.ContractState
import net.corda.core.contracts.Issued import net.corda.core.contracts.Issued
import net.corda.core.crypto.SecureHash
import net.corda.core.crypto.isFulfilledBy import net.corda.core.crypto.isFulfilledBy
import net.corda.core.crypto.keys import net.corda.core.crypto.keys
import net.corda.core.flows.FlowLogic import net.corda.core.flows.FlowLogic
@ -353,6 +355,17 @@ class CordaRPCOpsImplTest {
} }
} }
@Test(timeout=300_000)
fun `trying to open attachment which doesnt exist throws error`() {
CURRENT_RPC_CONTEXT.set(RpcAuthContext(InvocationContext.rpc(testActor()), buildSubject("TEST_USER", emptySet())))
withPermissions(invokeRpc(CordaRPCOps::openAttachment)) {
assertThatThrownBy {
rpc.openAttachment(SecureHash.zeroHash)
}.isInstanceOf(RPCException::class.java)
.withFailMessage("Unable to open attachment with id: ${SecureHash.zeroHash}")
}
}
@Test(timeout=300_000) @Test(timeout=300_000)
fun `attachment uploaded with metadata has specified uploader`() { fun `attachment uploaded with metadata has specified uploader`() {
CURRENT_RPC_CONTEXT.set(RpcAuthContext(InvocationContext.rpc(testActor()), buildSubject("TEST_USER", emptySet()))) CURRENT_RPC_CONTEXT.set(RpcAuthContext(InvocationContext.rpc(testActor()), buildSubject("TEST_USER", emptySet())))