This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
The following commit(s) were added to refs/heads/master by this push: new 52805cc519 Add auditrail to imap and jmap delete mailbox (#2586) 52805cc519 is described below commit 52805cc5194154988aa92db2efbc6de734ac7588 Author: hungphan227 <45198168+hungphan...@users.noreply.github.com> AuthorDate: Tue Jan 14 09:20:41 2025 +0700 Add auditrail to imap and jmap delete mailbox (#2586) --- .../apache/james/imap/processor/DeleteProcessor.java | 16 ++++++++++++++++ .../james/jmap/method/MailboxSetDeletePerformer.scala | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/DeleteProcessor.java b/protocols/imap/src/main/java/org/apache/james/imap/processor/DeleteProcessor.java index e9b2e530f5..add768ee41 100644 --- a/protocols/imap/src/main/java/org/apache/james/imap/processor/DeleteProcessor.java +++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/DeleteProcessor.java @@ -21,6 +21,7 @@ package org.apache.james.imap.processor; import jakarta.inject.Inject; +import org.apache.james.core.Username; import org.apache.james.imap.api.display.HumanReadableText; import org.apache.james.imap.api.message.response.StatusResponseFactory; import org.apache.james.imap.api.process.ImapSession; @@ -34,11 +35,14 @@ import org.apache.james.mailbox.exception.MailboxNotFoundException; import org.apache.james.mailbox.exception.TooLongMailboxNameException; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.metrics.api.MetricFactory; +import org.apache.james.util.AuditTrail; import org.apache.james.util.MDCBuilder; import org.apache.james.util.ReactorUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.ImmutableMap; + import reactor.core.publisher.Mono; public class DeleteProcessor extends AbstractMailboxProcessor<DeleteRequest> { @@ -68,6 +72,7 @@ public class DeleteProcessor extends AbstractMailboxProcessor<DeleteRequest> { .then(unsolicitedResponses(session, responder, false)) .then(Mono.fromRunnable(() -> okComplete(request, responder))) .then() + .doOnSuccess(any -> auditTrail(session, selected)) .onErrorResume(MailboxNotFoundException.class, e -> { no(request, responder, HumanReadableText.FAILURE_NO_SUCH_MAILBOX); return ReactorUtils.logAsMono(() -> LOGGER.debug("Delete failed for mailbox {} as it doesn't exist", mailboxPath, e)); @@ -101,4 +106,15 @@ public class DeleteProcessor extends AbstractMailboxProcessor<DeleteRequest> { .addToContext(MDCBuilder.ACTION, "DELETE") .addToContext("mailbox", request.getMailboxName()); } + + private void auditTrail(ImapSession session, SelectedMailbox selected) { + AuditTrail.entry() + .username(() -> session.getUserName().asString()) + .sessionId(() -> session.sessionId().asString()) + .protocol("IMAP") + .action("DELETE") + .parameters(() -> ImmutableMap.of("loggedInUser", session.getMailboxSession().getLoggedInUser().map(Username::asString).orElse(""), + "mailboxId", selected.getMailboxId().serialize())) + .log(String.format("IMAP DELETE succeeded.")); + } } diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxSetDeletePerformer.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxSetDeletePerformer.scala index 56f7a8ea85..de1f7d7463 100644 --- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxSetDeletePerformer.scala +++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxSetDeletePerformer.scala @@ -19,7 +19,9 @@ package org.apache.james.jmap.method +import com.google.common.collect.ImmutableMap import jakarta.inject.Inject +import org.apache.commons.lang3.StringUtils import org.apache.james.jmap.core.SetError import org.apache.james.jmap.core.SetError.SetErrorDescription import org.apache.james.jmap.mail.{MailboxGet, MailboxSetError, MailboxSetRequest, RemoveEmailsOnDestroy, UnparsedMailboxId} @@ -27,10 +29,13 @@ import org.apache.james.jmap.method.MailboxSetDeletePerformer.{MailboxDeletionFa import org.apache.james.mailbox.exception.MailboxNotFoundException import org.apache.james.mailbox.model.{FetchGroup, MailboxId, MessageRange} import org.apache.james.mailbox.{MailboxManager, MailboxSession, MessageManager, Role, SubscriptionManager} +import org.apache.james.util.{AuditTrail, ReactorUtils} import org.slf4j.LoggerFactory import reactor.core.publisher.SynchronousSink import reactor.core.scala.publisher.{SFlux, SMono} +import scala.jdk.OptionConverters._ + object MailboxSetDeletePerformer { private val LOGGER = LoggerFactory.getLogger(classOf[MailboxSetDeletePerformer]) sealed trait MailboxDeletionResult @@ -84,6 +89,7 @@ class MailboxSetDeletePerformer @Inject()(mailboxManager: MailboxManager, maxConcurrency = 5) .collectSeq() .map(MailboxDeletionResults) + .doOnSuccess(auditTrail(mailboxSession, _)) private def delete(mailboxSession: MailboxSession, id: UnparsedMailboxId, onDestroy: RemoveEmailsOnDestroy): SMono[MailboxDeletionResult] = MailboxGet.parse(mailboxIdFactory)(id) @@ -121,5 +127,17 @@ class MailboxSetDeletePerformer @Inject()(mailboxManager: MailboxManager, } })) + private def auditTrail(mailboxSession: MailboxSession, mailboxDeletionResults: MailboxDeletionResults): Unit = { + if (mailboxDeletionResults.destroyed.nonEmpty) { + ReactorUtils.logAsMono(() => AuditTrail.entry + .username(() => mailboxSession.getUser.asString()) + .protocol("JMAP") + .action("Mailbox/set destroy") + .parameters(() => ImmutableMap.of("loggedInUser", mailboxSession.getLoggedInUser.toScala.map(_.asString()).getOrElse(""), + "mailboxIds", StringUtils.join(mailboxDeletionResults.destroyed))) + .log("JMAP mailbox delete succeeded.")) + } + } + private def isASystemMailbox(mailbox: MessageManager): Boolean = Role.from(mailbox.getMailboxPath.getName).isPresent } --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org