Skip to content

Commit

Permalink
[FIX] JMAP AuditTrail logging was not triggered (#2605)
Browse files Browse the repository at this point in the history
  • Loading branch information
quantranhong1999 authored Jan 20, 2025
1 parent 0bc5508 commit 800c459
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import eu.timepit.refined.auto._
import eu.timepit.refined.types.string.NonEmptyString
import jakarta.inject.Inject
import org.apache.commons.lang3.StringUtils
import org.apache.james.jmap.api.model.Preview
import org.apache.james.jmap.api.model.Size.{Size, sanitizeSize}
import org.apache.james.jmap.api.model.{EmailAddress, Preview}
import org.apache.james.jmap.api.projections.{MessageFastViewPrecomputedProperties, MessageFastViewProjection}
import org.apache.james.jmap.core.Id.{Id, IdConstraint}
import org.apache.james.jmap.core.{JmapRfc8621Configuration, Properties, UTCDate}
import org.apache.james.jmap.core.{Properties, UTCDate}
import org.apache.james.jmap.mail.BracketHeader.sanitize
import org.apache.james.jmap.mail.EmailFullViewFactory.extractBodyValues
import org.apache.james.jmap.mail.EmailGetRequest.MaxBodyValueBytes
Expand All @@ -55,8 +55,8 @@ import org.apache.james.mime4j.field.{AddressListFieldLenientImpl, LenientFieldP
import org.apache.james.mime4j.message.DefaultMessageBuilder
import org.apache.james.mime4j.stream.{Field, MimeConfig, RawFieldParser}
import org.apache.james.mime4j.util.MimeUtil
import org.apache.james.util.AuditTrail
import org.apache.james.util.html.HtmlTextExtractor
import org.apache.james.util.{AuditTrail, ReactorUtils}
import org.slf4j.{Logger, LoggerFactory}
import reactor.core.scala.publisher.{SFlux, SMono}
import reactor.core.scheduler.Schedulers
Expand Down Expand Up @@ -662,19 +662,17 @@ private class EmailFullViewReader @Inject()(messageIdManager: MessageIdManager,
private val reader: GenericEmailViewReader[EmailFullView] = new GenericEmailViewReader[EmailFullView](messageIdManager, FULL_CONTENT, htmlTextExtractor, fullViewFactory)


override def read[T >: EmailFullView](ids: Seq[MessageId], request: EmailGetRequest, mailboxSession: MailboxSession): SFlux[T] = {
AuditTrail.entry
override def read[T >: EmailFullView](ids: Seq[MessageId], request: EmailGetRequest, mailboxSession: MailboxSession): SFlux[T] =
SMono(ReactorUtils.logAsMono(() => AuditTrail.entry
.username(() => mailboxSession.getUser.asString())
.protocol("JMAP")
.action("Email full view read")
.parameters(() => ImmutableMap.of("messageIds", StringUtils.join(ids.asJava),
"loggedInUser", mailboxSession.getLoggedInUser.toScala
.map(_.asString())
.getOrElse("")))
.log("JMAP Email full view read.")

reader.read(ids, request, mailboxSession)
}
.log("JMAP Email full view read.")))
.thenMany(reader.read(ids, request, mailboxSession))
}

object EmailFastViewReader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ class DelegateSetCreatePerformer @Inject()(delegationStore: DelegationStore,
SMono.fromPublisher(usersRepository.containsReactive(request.username))
.filter(bool => bool)
.flatMap(_ => SMono.fromPublisher(delegationStore.addAuthorizedUser(mailboxSession.getUser, request.username))
.doOnSuccess(_ => ReactorUtils.logAsMono(() => AuditTrail.entry
.`then`(SMono(ReactorUtils.logAsMono(() => AuditTrail.entry
.username(() => mailboxSession.getUser.asString())
.protocol("JMAP")
.action("DelegateSet/create")
.parameters(() => ImmutableMap.of("delegator", mailboxSession.getUser.asString(),
"delegatee", request.username.asString()))
.log("Delegation added.")))
.log("Delegation added."))))
.`then`(SMono.just[CreationResult](CreationSuccess(delegateCreationId, evaluateCreationResponse(request, mailboxSession))))
.onErrorResume(e => SMono.just[CreationResult](CreationFailure(delegateCreationId, e))))
.switchIfEmpty(SMono.just[CreationResult](CreationFailure(delegateCreationId, new UserDoesNotExistException(request.username))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ class DelegateSetDeletePerformer @Inject()(delegationStore: DelegationStore) {
.filter(authorizedUser => DelegationId.from(baseUser, authorizedUser).equals(id))
.next()
.flatMap(authorizedUser => SMono(delegationStore.removeAuthorizedUser(baseUser, authorizedUser))
.doOnSuccess(_ => ReactorUtils.logAsMono(() => AuditTrail.entry
.`then`(SMono(ReactorUtils.logAsMono(() => AuditTrail.entry
.username(() => baseUser.asString())
.protocol("JMAP")
.action("DelegateSet/destroy")
.parameters(() => ImmutableMap.of("delegator", baseUser.asString(),
"delegatee", authorizedUser.asString()))
.log("Delegation removed."))))
.log("Delegation removed.")))))
.`then`(SMono.just[DelegateDeletionResult](DelegateDeletionSuccess(id))))
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ class DelegatedAccountDeletePerformer @Inject()(delegationStore: DelegationStore
.filter(delegatedUser => DelegationId.from(delegatedUser, baseUser).equals(id))
.next()
.flatMap(delegatedUser => SMono(delegationStore.removeDelegatedUser(baseUser, delegatedUser))
.doOnSuccess(_ => ReactorUtils.logAsMono(() => AuditTrail.entry
.`then`(SMono(ReactorUtils.logAsMono(() => AuditTrail.entry
.username(() => baseUser.asString())
.protocol("JMAP")
.action("DelegatedAccountSet/destroy")
.parameters(() => ImmutableMap.of("delegator", delegatedUser.asString(),
"delegatee", baseUser.asString()))
.log("Delegation removed."))))
.log("Delegation removed.")))))
.`then`(SMono.just[DelegatedAccountDeletionResult](DelegatedAccountDeletionSuccess(id))))
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import org.apache.james.mailbox.model.{DeleteResult, MessageId}
import org.apache.james.mailbox.{MailboxSession, MessageIdManager}
import org.apache.james.util.{AuditTrail, ReactorUtils}
import org.slf4j.LoggerFactory
import reactor.core.publisher.Mono
import reactor.core.scala.publisher.SMono

import scala.jdk.CollectionConverters._
Expand Down Expand Up @@ -98,7 +97,8 @@ class EmailSetDeletePerformer @Inject()(messageIdManager: MessageIdManager,
}

SMono(messageIdManager.delete(messageIds.toSet.asJava, mailboxSession))
.doOnSuccess(auditTrail(_, mailboxSession))
.flatMap(deleteResult => auditTrail(deleteResult, mailboxSession)
.`then`(SMono.just(deleteResult)))
.map(DestroyResult.from)
.onErrorResume(e => SMono.just(messageIds.map(id => DestroyFailure(EmailSet.asUnparsed(id), e))))
.map(_ ++ parsingErrors)
Expand All @@ -108,16 +108,18 @@ class EmailSetDeletePerformer @Inject()(messageIdManager: MessageIdManager,
}
}

private def auditTrail(deleteResult: DeleteResult, mailboxSession: MailboxSession): Unit =
private def auditTrail(deleteResult: DeleteResult, mailboxSession: MailboxSession): SMono[Void] =
if (!deleteResult.getDestroyed.isEmpty) {
ReactorUtils.logAsMono(() => AuditTrail.entry
SMono(ReactorUtils.logAsMono(() => AuditTrail.entry
.username(() => mailboxSession.getUser.asString())
.protocol("JMAP")
.action("Email/set destroy")
.parameters(() => ImmutableMap.of("messageIds", StringUtils.join(deleteResult.getDestroyed),
"loggedInUser", mailboxSession.getLoggedInUser.toScala
.map(_.asString())
.getOrElse("")))
.log("Mails deleted."))
.log("Mails deleted.")))
} else {
SMono.empty
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class EmailSubmissionSetMethod @Inject()(serializer: EmailSubmissionSetSerialize
private def enqueue(mail: Mail, delay: Duration, mailboxSession: MailboxSession): SMono[Unit] =
(delay match {
case d if d.isNegative || d.isZero => SMono(queue.enqueueReactive(mail))
.doOnSuccess(_ => ReactorUtils.logAsMono(() => AuditTrail.entry
.`then`(SMono(ReactorUtils.logAsMono(() => AuditTrail.entry
.username(() => mailboxSession.getUser.asString())
.protocol("JMAP")
.action("EmailSubmission")
Expand All @@ -312,9 +312,9 @@ class EmailSubmissionSetMethod @Inject()(serializer: EmailSubmissionSetSerialize
"loggedInUser", mailboxSession.getLoggedInUser.toScala
.map(_.asString())
.getOrElse("")))
.log("JMAP mail spooled.")))
.log("JMAP mail spooled."))))
case _ => SMono(queue.enqueueReactive(mail, delay))
.doOnSuccess(_ => ReactorUtils.logAsMono(() => AuditTrail.entry
.`then`(SMono(ReactorUtils.logAsMono(() => AuditTrail.entry
.username(() => mailboxSession.getUser.asString())
.protocol("JMAP")
.action("EmailSubmission")
Expand All @@ -329,7 +329,7 @@ class EmailSubmissionSetMethod @Inject()(serializer: EmailSubmissionSetSerialize
"loggedInUser", mailboxSession.getLoggedInUser.toScala
.map(_.asString())
.getOrElse("")))
.log("JMAP mail spooled.")))
.log("JMAP mail spooled."))))
}).`then`(SMono.fromCallable(() => LifecycleUtil.dispose(mail)).subscribeOn(Schedulers.boundedElastic()))

private def retrieveDelay(mailParameters: Option[Map[ParameterName, Option[ParameterValue]]]): Try[Duration] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class MailboxSetUpdatePerformer @Inject()(serializer: MailboxSerializer,

val partialUpdatesOperation: SMono[Unit] = SFlux.fromIterable(validatedPatch.rightsPartialUpdates)
.flatMap(partialUpdate => SMono.fromCallable(() => mailboxManager.applyRightsCommand(mailboxId, partialUpdate.asACLCommand(), mailboxSession))
.doOnSuccess(_ => ReactorUtils.logAsMono(() => AuditTrail.entry
.`then`(SMono(ReactorUtils.logAsMono(() => AuditTrail.entry
.username(() => mailboxSession.getUser.asString())
.protocol("JMAP")
.action("Mailbox/set update")
Expand All @@ -260,7 +260,7 @@ class MailboxSetUpdatePerformer @Inject()(serializer: MailboxSerializer,
"delegatee", partialUpdate.entryKey.getName,
"mailboxId", mailboxId.serialize(),
"rights", partialUpdate.rights.asJava.serialize()))
.log("JMAP mailbox shared."))),
.log("JMAP mailbox shared.")))),
maxConcurrency = 5)
.`then`()

Expand Down

0 comments on commit 800c459

Please sign in to comment.