Skip to content

Commit

Permalink
XWIKI-20371: Consider mail obfuscation settings in the Solr indexer
Browse files Browse the repository at this point in the history
* Introduce a new event GeneralMailConfigurationUpdatedEvent to notify
  the indexer when the mail configuration changed.
* Don't index emails when obfuscation is enabled.
* Add a test for the object property metadata extractor.
* Add a migration to clear the index.
* Make sure that indexing is executed with the indexed document in
  context.
  • Loading branch information
michitux committed Aug 10, 2023
1 parent 179f4e2 commit 3e5272f
Show file tree
Hide file tree
Showing 17 changed files with 995 additions and 11 deletions.
Expand Up @@ -44,6 +44,11 @@
<artifactId>xwiki-commons-component-api</artifactId>
<version>${commons.version}</version>
</dependency>
<dependency>
<groupId>org.xwiki.commons</groupId>
<artifactId>xwiki-commons-observation-api</artifactId>
<version>${commons.version}</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
Expand Down
@@ -0,0 +1,108 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.mail;

import java.util.Objects;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.xwiki.observation.event.Event;
import org.xwiki.stability.Unstable;

/**
* An event triggered after the general mail configuration has been changed.
* <p>
* The event also sends the following parameters:
* </p>
* <ul>
* <li>source: the wiki id (as string) where the configuration has been changed, or {@code null} if the change was on
* the main wiki and thus affects all wikis.</li>
* <li>data: {@code null}</li>
* </ul>
* <p>This event is intentionally not serializable as it will be triggered on every node of the cluster separately.</p>
*
* @since 14.10.15
* @since 15.5.2
* @since 15.7RC1
* @version $Id$
*/
@Unstable
public class GeneralMailConfigurationUpdatedEvent implements Event
{
private String wikiId;

/**
* Default constructor, used to get notified about a mail configuration change in any wiki or to trigger an event
* for a configuration change on the main wiki.
*/
public GeneralMailConfigurationUpdatedEvent()
{
}

/**
* An event for changes that affect the passed wiki id. Used to get notified about a change that affects the
* specified wiki or to trigger an event on a subwiki.
*
* @param wikiId the id of the wiki where the configuration has been changed
*/
public GeneralMailConfigurationUpdatedEvent(String wikiId)
{
this.wikiId = wikiId;
}

@Override
public boolean matches(Object otherEvent)
{
if (otherEvent == this) {
return true;
}

boolean isMatching = false;

if (this.getClass().isAssignableFrom(otherEvent.getClass())) {
GeneralMailConfigurationUpdatedEvent other = (GeneralMailConfigurationUpdatedEvent) otherEvent;
isMatching = this.wikiId == null || other.wikiId == null || Objects.equals(this.wikiId, other.wikiId);
}

return isMatching;
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

GeneralMailConfigurationUpdatedEvent that = (GeneralMailConfigurationUpdatedEvent) o;

return new EqualsBuilder().append(this.wikiId, that.wikiId).isEquals();
}

@Override
public int hashCode()
{
return new HashCodeBuilder(17, 37).append(this.wikiId).toHashCode();
}
}
Expand Up @@ -73,5 +73,13 @@
<artifactId>javax.servlet-api</artifactId>
<scope>test</scope>
</dependency>
<!-- Needed for ReferenceComponentList -->
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-oldcore</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -43,4 +43,16 @@ protected LocalDocumentReference getClassReference()
{
return GENERAL_MAILCONFIGCLASS_REFERENCE;
}

/**
* Clear the cache.
*
* @since 14.10.15
* @since 15.5.2
* @since 15.7RC1
*/
void clearCache()
{
this.cache.removeAll();
}
}
@@ -0,0 +1,134 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.mail.internal.configuration;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;
import org.xwiki.configuration.ConfigurationSource;
import org.xwiki.mail.GeneralMailConfigurationUpdatedEvent;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.model.reference.LocalDocumentReference;
import org.xwiki.model.reference.RegexEntityReference;
import org.xwiki.observation.EventListener;
import org.xwiki.observation.ObservationManager;
import org.xwiki.observation.event.Event;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.internal.event.XObjectAddedEvent;
import com.xpn.xwiki.internal.event.XObjectDeletedEvent;
import com.xpn.xwiki.internal.event.XObjectUpdatedEvent;
import com.xpn.xwiki.objects.BaseObjectReference;

/**
* Event generator for {@link GeneralMailConfigurationUpdatedEvent}.
*
* @since 14.10.15
* @since 15.5.2
* @since 15.7RC1
* @version $Id$
*/
@Component
@Named(GeneralMailConfigurationUpdatedEventGenerator.NAME)
@Singleton
public class GeneralMailConfigurationUpdatedEventGenerator implements EventListener
{
static final String NAME = "generalmailconfigurationchangedeventgenerator";

@Inject
protected EntityReferenceSerializer<String> referenceSerializer;

@Inject
private Provider<ObservationManager> observationManagerProvider;

@Inject
private Provider<XWikiContext> contextProvider;

@Inject
@Named("mailgeneral")
private ConfigurationSource currentWikiMailConfigSource;

@Inject
@Named("mailgeneralmainwiki")
private ConfigurationSource mainWikiMailConfigSource;

@Override
public String getName()
{
return NAME;
}

@Override
public List<Event> getEvents()
{
String serializedClassReference = this.referenceSerializer.serialize(
AbstractGeneralMailConfigClassDocumentConfigurationSource.GENERAL_MAILCONFIGCLASS_REFERENCE);
RegexEntityReference filter = BaseObjectReference.any(serializedClassReference);

return List.of(new XObjectAddedEvent(filter), new XObjectDeletedEvent(filter), new XObjectUpdatedEvent(filter));
}

@Override
public void onEvent(Event event, Object source, Object data)
{
ObservationManager observationManager = this.observationManagerProvider.get();

if (source instanceof XWikiDocument) {
XWikiDocument document = (XWikiDocument) source;

// Test that the document is really a mail configuration document.
LocalDocumentReference localDocumentReference = new LocalDocumentReference(document.getDocumentReference());
if (!AbstractMailConfigClassDocumentConfigurationSource.MAILCONFIG_REFERENCE
.equals(localDocumentReference))
{
return;
}

// Clear the cache of the current wiki mail config source to ensure that any listener of the new events will
// see the new configuration value regardless of which listener is called first.
clearCache(this.currentWikiMailConfigSource);

// Get the wiki id from the document reference. If it is the main wiki, notify without wiki, otherwise
// notify with the wiki id.
String wikiId = document.getDocumentReference().getWikiReference().getName();
if (this.contextProvider.get().isMainWiki(wikiId)) {
// Clear the cache of the main wiki mail config source to ensure that any listener of the events will
// see the new configuration value regardless of which listener is called first.
clearCache(this.mainWikiMailConfigSource);
observationManager.notify(new GeneralMailConfigurationUpdatedEvent(), null);
} else {
observationManager.notify(new GeneralMailConfigurationUpdatedEvent(wikiId), wikiId);
}
}
}

private void clearCache(ConfigurationSource mainWikiMailConfigSource)
{
if (mainWikiMailConfigSource instanceof AbstractGeneralMailConfigClassDocumentConfigurationSource) {
((AbstractGeneralMailConfigClassDocumentConfigurationSource) mainWikiMailConfigSource).clearCache();
}
}
}
Expand Up @@ -3,6 +3,7 @@ org.xwiki.mail.internal.AddressConverter
org.xwiki.mail.internal.InternetAddressConverter
org.xwiki.mail.internal.configuration.DefaultGeneralMailConfiguration
org.xwiki.mail.internal.configuration.GeneralMailConfigClassDocumentConfigurationSource
org.xwiki.mail.internal.configuration.GeneralMailConfigurationUpdatedEventGenerator
org.xwiki.mail.internal.configuration.MainWikiGeneralMailConfigClassDocumentConfigurationSource
org.xwiki.mail.internal.DefaultEmailAddressObfuscator
org.xwiki.mail.script.GeneralMailScriptService

0 comments on commit 3e5272f

Please sign in to comment.