# Email Notification Service

## Introduction

The GBDS Email Notification Service provides an auditing feature. Through this service, it is possible to configure an email list that will be notified whenever a facial 1:N search is requested.

You can use this feature with any SMTP server by configuring the service as described in the section [Configuring the Email Notifier](#configurando-o-notificador-de-email). When no SMTP server is available, the user must create it and install the service dependencies.

## Configuring the Email Notifier

### Properties

The configuration file for the Email Notification Service is located at `etc/griaule/conf/email-notifier/config.properties` and contains the following parameters:

```properties
# GBS Email Notifier

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://<mysql/mariadb ip>:<port>/enotifier?useSSL=false
jdbc.username=<username>
jdbc.password=<encrypted password>
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.showSql=false

locale=en_US

gbds.url=http://<gbds api host>:<port>
gbds.user=<gbds user>
gbds.key=<gbds password>
gbds.logLevel=INFO
gbds.timeout=300 # seconds
```

{% hint style="warning" %}
The parameter values between `<>` must be edited according to the environment.
{% endhint %}

### Database

The table `enotifier.settings` of the database contains the email template and subject and all information for SMTP connection:

<table><thead><tr><th width="300">Key</th><th>Description</th></tr></thead><tbody><tr><td>mail.smtp.auth</td><td>SMTP authorization: NONE, TLS, SSL</td></tr><tr><td>mail.smtp.from.email</td><td>SMTP sender email</td></tr><tr><td>mail.smtp.from.name</td><td>SMTP sender name</td></tr><tr><td>mail.smtp.host</td><td>SMTP host</td></tr><tr><td>mail.smtp.password</td><td>SMTP password for TLS and SSL</td></tr><tr><td>mail.smtp.port</td><td>SMTP port: NONE=25; TLS=587; SSL=465</td></tr><tr><td>mail.identify.request.single.subject</td><td>Email subject; the notifier will concatenate the hash id to separate threads.</td></tr><tr><td>mail.identify.request.single.template</td><td>Email body template: Fields:<br><br>&#x3C;username>: user name<br>&#x3C;timestamp:date/time pattern>: if no pattern is provided, MM/dd/YYYY HH:mm:ss will be used<br>&#x3C;biographics>: iterates over biographics; inside the iteration: &#x3C;biographic:key> and &#x3C;biographic:value> provide key/value<br><br>images will be sent as attachment.</td></tr><tr><td>mail.identify.result.subject</td><td>Subject of the email notification for the search result</td></tr><tr><td>mail.identify.result.template</td><td>Template of the email notification for the search result</td></tr><tr><td>mail.identify.request.multiple</td><td>Enables/disables sending multiple emails with notifications of face identification search requests</td></tr><tr><td>mail.identify.request.multiple.period</td><td>Period to consolidate email notifications</td></tr><tr><td>mail.identify.request.multiple.subject</td><td>Subject for multiple email notifications</td></tr><tr><td>mail.identify.request.multiple.template</td><td>Email body template for multiple notifications. The emails package a zip file with all listed images.</td></tr></tbody></table>

{% hint style="warning" %}
The zip file of the attached image list can have at most 24 MB. Above that, the listing is split into more than one email.
{% endhint %}

To create the table, run the required database dump file.

{% hint style="info" %}
If no dump file was provided, contact Griaule Support Team.
{% endhint %}

{% hint style="warning" %}
Any changes applied to this table will be reflected in the next sent email.
{% endhint %}

## Operation

The binaries will be placed in `/var/lib/griaule/email-notifier/gbs-email-notifier-xxx.jar` and `/var/lib/griaule/email-notifier/lib`.

### Starting and stopping the Email Notifier

To start the Email Notification Service, run:

```sh
/var/lib/griaule/email-notifier/scripts/start-email-notifier.sh
```

And to stop it:

```sh
/var/lib/griaule/email-notifier/scripts/kill-email-notifier.sh
```

### Logs

The email notifier uses the log4j configuration file at `/etc/griaule/conf/email-notifier/email-notifier-log4j.xml`. All logs will be in `/var/logs/griaule/email-notifier/`.

Shutdown log will be generated on `/var/log/griaule/email-notifier/email-notifier.log`

### Actions

The email service can contain an interest list via GBDS for some people. Some actions can be taken when a search is required and those people appear in the search results. The actions are:

* Hide person from the search result;
* Hide the person's information in the search result if the authorized user does not have access to view it;
* Notify by email the search result with the person.

To configure the action, it is necessary to access the tables `gbds.people_transparency` and `gbds.people_transparency_group` to inform if any person should be removed, hidden or notified at each identification search performed.

The table contains PGUID, action and flag to enable the transparency of that person. The groups table holds email groups for notification.

With the PGUID entered, the actions must be: REMOVE, CLASSIFIED, or NOTIFY:

* When removing (REMOVE), the get result endpoint will not return the person;
* When setting as hidden information (CLASSIFIED), the search result will return the person, but the fields pguid/tguid will be with text *classified*, all their matches with the score, query and reference index as -1. If the authenticated user has the permission `transparency_show_classified_people` all personal data will be shown again.
* In the notification (NOTIFY), the table `gbds.people_transparency_group` contains email groups stating which emails should be notified with the person's search result.

{% hint style="info" %}
In the table `gbds.people_transparency`, a pguid may appear more than once, so more than one action can be taken for this pguid, for example: remove and notify or classify and notify.
{% endhint %}

## Additional Information

### API and Database Settings

This service is enabled by some settings in `gbdsapi.properties` or in the table `gbds.settings` in the database. These are:

* gbds.transparency.search.identify.send-email.enabled

  > Enable or disable this service
* gbds.transparency.email-notifier.log-level

  > Sets the log level
* gbds.transparency.email-notifier.timeout

  > Sets the timeout.
* gbds.transparency.email-notifier.url

  > Sets the notification URL

More information about these settings can be seen in the GBDS API Configuration Manual.

### GBDS Endpoints for the Email Notification Service

GBDS provides a simple API to store and retrieve users/emails to send. The full documentation can be seen at the [GBDS API](https://app.gitbook.com/o/IUPwzGm81miB2T1d1ztv/s/MqtschPie46KAAzuKKeD/)

* Insert or update groups and emails: [POST Email Notify Group](https://gitbook.griaule.com/apis/gbds-4/email#post-notify-group)

  > This call inserts/updates groups and emails related to those groups.
* Retrieve Group: [Get Email Group](https://gitbook.griaule.com/apis/gbds-4/email#get-notify-group-group)

  > This call returns the group and the emails in the group.
* Insert or update the user and their groups: [POST Email Notify User](https://gitbook.griaule.com/apis/gbds-4/email#post-notify-user)

  > This call inserts/updates an individual user and associates them with groups.
* Retrieve user: [Get Email User](https://gitbook.griaule.com/apis/gbds-4/email#get-notify-user-user)

  > This call returns the user and their associated groups.

### Notifier Get List Endpoints

The email notifier has an endpoint to list the notified email requests:

* GET `http://host:port/gbs-email-notifier/notify/list` (the port is usually 8086)

  > Query filters:
  >
  > * status: one or more of: PENDING, PROCESSING, ERROR, DONE
  > * ini-date: start date in the format: YYYY-MM-dd-HH-mm-ss
  > * end-date: end date in the format: YYYY-MM-dd-HH-mm-ss
  > * username (user)
  > * email
  > * pageIndex, default 0, minimum 0
  > * pageSize, default 20, minimum 1, maximum 100

The sample response is:

```json
{
    "notifications": [
        {
            "tguid": "86A2BA4A-822C-4F1F-9017-46E0144F274C",
            "timestamp": "2021-08-24-10-52-07",
            "status": "DONE",
            "username": "rgiolo",
            "emails": [
                "email_01@griaule.com",
                "email_02@griaule.com"
            ],
            "biographics": {
                "information": "some value",
                "ip-address": "192.168.0.62",
                "face-score-threshold": "30",
                "big-text": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum ..."
            },
            "message": "Email sent"
        },
        ...
	]
}
```
