Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gdpr notifications #48

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<gbif-httputils.version>0.10</gbif-httputils.version>
<gbif-microservice.version>0.7</gbif-microservice.version>
<gbif-parsers.version>0.30</gbif-parsers.version>
<gbif-postal-service.version>0.24</gbif-postal-service.version>
<gbif-postal-service.version>0.25-SNAPSHOT</gbif-postal-service.version>
<gbif-directory.version>0.11</gbif-directory.version>
<grizzly-servlet-webserver.version>1.9.60</grizzly-servlet-webserver.version>
<guava.version>18.0</guava.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.gbif.registry.cli.common;

import java.util.Properties;
import javax.validation.constraints.NotNull;

import com.beust.jcommander.Parameter;

/**
* Configuration for sending emails.
*/
@SuppressWarnings("PublicField")
public class MailConfiguration {

private static final String MAIL_PREFIX = "mail.";

@NotNull
@Parameter(names = "--mail-devmail-enabled")
public String devmailEnabled;

@NotNull
@Parameter(names = "--mail-smtp-host")
public String smtpHost;

@NotNull
@Parameter(names = "--mail-smtp-port")
public String smtpPort;

@NotNull
@Parameter(names = "--mail-devemail")
public String devemail;

@NotNull
@Parameter(names = "--mail-cc")
public String cc;

@NotNull
@Parameter(names = "--mail-from")
public String from;

public Properties toMailProperties() {
Properties props = new Properties();

props.put(MAIL_PREFIX + "devemail.enabled", this.devmailEnabled);
props.put(MAIL_PREFIX + "smtp.host", this.smtpHost);
props.put(MAIL_PREFIX + "smtp.port", this.smtpPort);
props.put(MAIL_PREFIX + "devemail", this.devemail);
props.put(MAIL_PREFIX + "cc", this.cc);
props.put(MAIL_PREFIX + "from", this.from);

return props;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.gbif.registry.cli.dataprivacynotification;

import org.gbif.cli.Command;
import org.gbif.cli.service.ServiceCommand;

import com.google.common.util.concurrent.Service;
import org.kohsuke.MetaInfServices;

@MetaInfServices(Command.class)
public class DataPrivacyNotificationCommand extends ServiceCommand {

private final DataPrivacyNotificationConfiguration configuration = new DataPrivacyNotificationConfiguration();

public DataPrivacyNotificationCommand() {
super("data-privacy-notification");
}

@Override
protected Service getService() {
return new DataPrivacyNotificationService(configuration);
}

@Override
protected Object getConfigurationObject() {
return configuration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.gbif.registry.cli.dataprivacynotification;

import org.gbif.common.messaging.config.MessagingConfiguration;
import org.gbif.registry.cli.common.DbConfiguration;
import org.gbif.registry.cli.common.MailConfiguration;

import java.util.Properties;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParametersDelegate;
import com.google.common.base.Objects;

@SuppressWarnings("PublicField")
public class DataPrivacyNotificationConfiguration {

@ParametersDelegate
@NotNull
@Valid
public MessagingConfiguration messaging = new MessagingConfiguration();

@ParametersDelegate
@Valid
@NotNull
public DbConfiguration registry = new DbConfiguration();

@ParametersDelegate
@NotNull
@Valid
public MailConfiguration mailConfig = new MailConfiguration();

@ParametersDelegate
@NotNull
@Valid
public DataPrivacyConfiguration dataPrivacyConfig = new DataPrivacyConfiguration();

@Parameter(names = "--queue-name")
@NotNull
public String queueName;

@Override
public String toString() {
return Objects.toStringHelper(this)
.add("queueName", queueName)
.add("messaging", messaging)
.add("registry.serverName", registry.serverName)
.add("registry.databaseName", registry.databaseName)
.add("registry.user", registry.user)
.add("registry.password", registry.password)
.add("registry.maximumPoolSize", registry.maximumPoolSize)
.add("registry.connectionTimeout", registry.connectionTimeout)
.toString();
}

/**
* Data privacy specific configuration.
*/
public static class DataPrivacyConfiguration {

private static final String URL_TEMPLATE_PROP_PREFIX = "urlTemplate.";
private static final String DATA_PRIVACY_PREFIX = "dataPrivacy.";
private static final String MAIL_PREFIX = DATA_PRIVACY_PREFIX + "surety.mail.";

@NotNull
@Parameter(names = "--dataPrivacy-version")
public String dataPrivacyVersion;

@Parameter(names = "--dataPrivacy-mail-enabled")
public boolean mailEnabled = false;

@NotNull
@Parameter(names = "--dataPrivacy-subject")
public String subject;

@NotNull
@Parameter(names = "--dataPrivacy-infoPage")
public String informationPage;

@NotNull
@Parameter(names = "--dataPrivacy-node-urlTemplate")
public String nodeUrlTemplate;

@NotNull
@Parameter(names = "--dataPrivacy-organization-urlTemplate")
public String organizationUrlTemplate;

@NotNull
@Parameter(names = "--dataPrivacy-installation-urlTemplate")
public String installationUrlTemplate;

@NotNull
@Parameter(names = "--dataPrivacy-network-urlTemplate")
public String networkUrlTemplate;

@NotNull
@Parameter(names = "--dataPrivacy-dataset-urlTemplate")
public String datasetUrlTemplate;

public Properties toProperties() {
Properties props = new Properties();

props.put(DATA_PRIVACY_PREFIX + "version", this.dataPrivacyVersion);
props.put(MAIL_PREFIX + "enabled", String.valueOf(this.mailEnabled));
props.put(MAIL_PREFIX + "subject", this.subject);
props.put(MAIL_PREFIX + "informationPage", this.informationPage);
props.put(MAIL_PREFIX + URL_TEMPLATE_PROP_PREFIX + "node", this.nodeUrlTemplate);
props.put(MAIL_PREFIX + URL_TEMPLATE_PROP_PREFIX + "organization", this.organizationUrlTemplate);
props.put(MAIL_PREFIX + URL_TEMPLATE_PROP_PREFIX + "installation", this.installationUrlTemplate);
props.put(MAIL_PREFIX + URL_TEMPLATE_PROP_PREFIX + "network", this.networkUrlTemplate);
props.put(MAIL_PREFIX + URL_TEMPLATE_PROP_PREFIX + "dataset", this.datasetUrlTemplate);

return props;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.gbif.registry.cli.dataprivacynotification;

import org.gbif.common.messaging.AbstractMessageCallback;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused

import org.gbif.common.messaging.api.messages.DataPrivacyNotificationMessage;
import org.gbif.registry.dataprivacy.DataPrivacyService;
import org.gbif.registry.dataprivacy.email.DataPrivacyEmailManager;

import java.util.Objects;

import com.google.inject.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataPrivacyNotificationListener extends AbstractMessageCallback<DataPrivacyNotificationMessage> {

private static final Logger LOG = LoggerFactory.getLogger(DataPrivacyNotificationListener.class);

private final DataPrivacyNotificationConfiguration config;
private final DataPrivacyService dataPrivacyService;
private final DataPrivacyEmailManager dataPrivacyEmailManager;

private DataPrivacyNotificationListener(DataPrivacyNotificationConfiguration config) {
this.config = config;
Injector inj = new DataPrivacyNotificationModule(config).getInjector();
dataPrivacyService = inj.getInstance(DataPrivacyService.class);
dataPrivacyEmailManager = inj.getInstance(DataPrivacyEmailManager.class);
}

static DataPrivacyNotificationListener newInstance(DataPrivacyNotificationConfiguration config) {
return new DataPrivacyNotificationListener(config);
}

@Override
public void handleMessage(DataPrivacyNotificationMessage dataPrivacyNotificationMessage) {
if (!config.dataPrivacyConfig.mailEnabled) {
LOG.info("Data privacy email notifications disabled");
return;
}

Objects.requireNonNull(dataPrivacyNotificationMessage.getEmail());

// double check that the email was not sent
if (dataPrivacyService.existsNotification(dataPrivacyNotificationMessage.getEmail(),
dataPrivacyNotificationMessage.getVersion())) {
// email already sent
LOG.info("Data privacy email notification was already sent before for {} - skipped",
dataPrivacyNotificationMessage.getEmail());
return;
}

// send email
boolean sent = dataPrivacyEmailManager.sendDataPrivacyNotification(dataPrivacyNotificationMessage.getEmail(),
dataPrivacyNotificationMessage.getContext());

// create notification in DB
if (sent) {
LOG.info("Data privacy email sent to {}", dataPrivacyNotificationMessage.getEmail());
dataPrivacyService.createNotification(dataPrivacyNotificationMessage.getEmail(),
dataPrivacyNotificationMessage.getVersion(),
dataPrivacyNotificationMessage.getContext());
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.gbif.registry.cli.dataprivacynotification;

import org.gbif.registry.dataprivacy.DataPrivacyModule;
import org.gbif.registry.dataprivacy.email.DataPrivacyEmailModule;
import org.gbif.registry.persistence.guice.RegistryMyBatisModule;
import org.gbif.registry.surety.email.EmailManagerModule;
import org.gbif.ws.client.guice.GbifApplicationAuthModule;

import java.util.Properties;

import com.google.inject.Guice;
import com.google.inject.Injector;

/**
* Guice module for data privacy notifications that adds all the necessary modules.
*/
public class DataPrivacyNotificationModule {

private final DataPrivacyNotificationConfiguration config;

public DataPrivacyNotificationModule(DataPrivacyNotificationConfiguration config) {
this.config = config;
}

public Injector getInjector() {
Properties dataPrivacyProperties = config.dataPrivacyConfig.toProperties();

return Guice.createInjector(new RegistryMyBatisModule(config.registry.toRegistryProperties()),
new GbifApplicationAuthModule(config.registry.user, config.registry.password),
new DataPrivacyModule(dataPrivacyProperties),
new DataPrivacyEmailModule(dataPrivacyProperties),
new EmailManagerModule(config.mailConfig.toMailProperties()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.gbif.registry.cli.dataprivacynotification;

import org.gbif.common.messaging.MessageListener;

import com.google.common.util.concurrent.AbstractIdleService;

public class DataPrivacyNotificationService extends AbstractIdleService {

// we use a pool size of 1 in order to avoid race conditions when consulting the DB.
private static final int POOL_SIZE = 1;

private final DataPrivacyNotificationConfiguration configuration;
private MessageListener listener;

public DataPrivacyNotificationService(DataPrivacyNotificationConfiguration configuration) {
this.configuration = configuration;
}

@Override
protected void startUp() throws Exception {
listener = new MessageListener(configuration.messaging.getConnectionParameters());
listener.listen(configuration.queueName, POOL_SIZE, DataPrivacyNotificationListener.newInstance(configuration));
}

@Override
protected void shutDown() throws Exception {
if (listener != null) {
listener.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.gbif.api.service.registry.DatasetService;
import org.gbif.registry.directory.DirectoryModule;
import org.gbif.registry.events.EventModule;
import org.gbif.registry.dataprivacy.DataPrivacyModule;
import org.gbif.registry.persistence.guice.RegistryMyBatisModule;
import org.gbif.registry.search.DatasetIndexService;
import org.gbif.registry.search.guice.RegistrySearchModule;
Expand All @@ -28,11 +29,12 @@ public void build() throws Exception {
new RegistrySearchModule(props),
new DirectoryModule(props),
new StubModule(),
EventModule.withoutRabbit(props)
EventModule.withoutRabbit(props),
new DataPrivacyModule(props)
);

DatasetIndexBuilder idxBuilder = new DatasetIndexBuilder(inj.getInstance(DatasetService.class), inj.getInstance(DatasetIndexService.class));
idxBuilder.build();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ oaipmh.adminEmail=gbifregistry@mailinator.com
directory.ws.url=${directory.ws.url}
directory.app.key=${directory.app.key}
directory.app.secret=${directory.app.secret}

# Data Privacy
dataPrivacy.version=1
dataPrivacy.enabled=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">

<changeSet id="53" author="mlopez" runInTransaction="false">
<sql splitStatements="false" stripComments="false">
CREATE TABLE data_privacy_notification (
email varchar(254) CHECK (assert_min_length(email, 5)) NOT NULL,
version char(1) CHECK (assert_min_length(version, 1)) NOT NULL,
timestamp_sent timestamp with time zone NOT NULL DEFAULT now(),
context hstore,
PRIMARY KEY (email, version)
);
</sql>
</changeSet>
</databaseChangeLog>
1 change: 1 addition & 0 deletions registry-liquibase/src/main/resources/liquibase/master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@
<include file="liquibase/049-challenge-code-table.xml"/>
<include file="liquibase/050-user-email-index.xml"/>
<include file="liquibase/051-download-erase.xml"/>
<include file="liquibase/053-add-data-privacy-notification-table.xml"/>
</databaseChangeLog>
Loading