fix: Inbox処理のトランザクションを修正

This commit is contained in:
usbharu 2023-12-03 10:28:19 +09:00
parent be7bd590eb
commit 158cd3a6df
1 changed files with 38 additions and 22 deletions

View File

@ -34,25 +34,37 @@ class InboxJobProcessor(
private val transaction: Transaction private val transaction: Transaction
) : JobProcessor<InboxJobParam, InboxJob> { ) : JobProcessor<InboxJobParam, InboxJob> {
private suspend fun verifyHttpSignature(httpRequest: HttpRequest, signature: Signature): Boolean { private suspend fun verifyHttpSignature(
httpRequest: HttpRequest,
signature: Signature,
transaction: Transaction
): Boolean {
val requiredHeaders = when (httpRequest.method) { val requiredHeaders = when (httpRequest.method) {
HttpMethod.GET -> getRequiredHeaders HttpMethod.GET -> getRequiredHeaders
HttpMethod.POST -> postRequiredHeaders HttpMethod.POST -> postRequiredHeaders
} }
if (signature.headers.containsAll(requiredHeaders).not()) { if (signature.headers.containsAll(requiredHeaders).not()) {
logger.warn("FAILED Invalid signature. require: {}", requiredHeaders)
return false return false
} }
val user = try { val user = transaction.transaction {
try {
userQueryService.findByKeyId(signature.keyId) userQueryService.findByKeyId(signature.keyId)
} catch (_: FailedToGetResourcesException) { } catch (_: FailedToGetResourcesException) {
apUserService.fetchPersonWithEntity(signature.keyId).second apUserService.fetchPersonWithEntity(signature.keyId).second
} }
}
val verify = signatureVerifier.verify( val verify = try {
signatureVerifier.verify(
httpRequest, httpRequest,
PublicKey(RsaUtil.decodeRsaPublicKeyPem(user.publicKey), signature.keyId) PublicKey(RsaUtil.decodeRsaPublicKeyPem(user.publicKey), signature.keyId)
) )
} catch (e: Exception) {
logger.warn("FAILED Verify Http Signature", e)
return false
}
return verify.success return verify.success
} }
@ -60,6 +72,7 @@ class InboxJobProcessor(
@Suppress("TooGenericExceptionCaught") @Suppress("TooGenericExceptionCaught")
private fun parseSignatureHeader(httpHeaders: HttpHeaders): Signature? { private fun parseSignatureHeader(httpHeaders: HttpHeaders): Signature? {
return try { return try {
println("Signature Header =" + httpHeaders.get("Signature").single())
signatureHeaderParser.parse(httpHeaders) signatureHeaderParser.parse(httpHeaders)
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
logger.trace("FAILED parse signature header", e) logger.trace("FAILED parse signature header", e)
@ -67,7 +80,8 @@ class InboxJobProcessor(
} }
} }
override suspend fun process(param: InboxJobParam) = transaction.transaction { override suspend fun process(param: InboxJobParam) {
val jsonNode = objectMapper.readTree(param.json) val jsonNode = objectMapper.readTree(param.json)
logger.info("START Process inbox. type: {}", param.type) logger.info("START Process inbox. type: {}", param.type)
@ -83,8 +97,9 @@ class InboxJobProcessor(
logger.debug("Has signature? {}", signature != null) logger.debug("Has signature? {}", signature != null)
val verify = signature?.let { verifyHttpSignature(httpRequest, it) } ?: false val verify = signature?.let { verifyHttpSignature(httpRequest, it, transaction) } ?: false
transaction.transaction {
logger.debug("Is verifying success? {}", verify) logger.debug("Is verifying success? {}", verify)
val activityPubProcessor = val activityPubProcessor =
@ -100,6 +115,7 @@ class InboxJobProcessor(
logger.info("SUCCESS Process inbox. type: {}", param.type) logger.info("SUCCESS Process inbox. type: {}", param.type)
} }
}
override fun job(): InboxJob = InboxJob override fun job(): InboxJob = InboxJob