100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Added support for OR_EMAIL_X_HEADERS environment variable (to configure headers on outbound emails)
Rich Turnere4866c372ad4
diff --git a/.ci_cd/aws/cloudformation-create-ec2.yml b/.ci_cd/aws/cloudformation-create-ec2.ymlindex 8a365718d..66671a1de 100644--- a/.ci_cd/aws/cloudformation-create-ec2.yml+++ b/.ci_cd/aws/cloudformation-create-ec2.yml@@ -7,6 +7,8 @@# A standard access security group is also created for the standard OpenRemote stack services; the ssh-access security# group is also linked to the EC2 instance and this security group must already exist within the account.#+# Optionally assign an Elastic IP address by setting the ElasticIP parameter to true.+## Systemd services are also created to:## - Mount EFS volume named 'hosts-filesystem' at /deployment.local/efs on restart@@ -53,6 +55,13 @@ Parameters:SMTPORArn:Description: SES Verified identity trust policy ARN for SMTP X-HeadersType: String+ ElasticIP:+ Description: Assign elastic IP address+ Type: String+ AllowedValues:+ - true+ - false+ Default: falseSMTPKey:Type: AWS::SSM::Parameter::Value<String>Default: SMTP-User-Key@@ -65,6 +74,7 @@ Parameters:Conditions:CreateDNSRecord: !Equals [!Ref DNSHostedZoneRoleArn, ""]+ AssignElasticIP: !Equals [!Ref ElasticIP, "true"]Resources:@@ -596,3 +606,18 @@ Resources:# All done so signal success/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region}++ IPAddress:+ Type: AWS::EC2::EIP+ Condition: AssignElasticIP+ Properties:+ Tags:+ - Key: "Name"+ Value: !Ref Host++ IPAssoc:+ Type: AWS::EC2::EIPAssociation+ Condition: AssignElasticIP+ Properties:+ InstanceId: !Ref 'EC2Instance'+ EIP: !Ref 'IPAddress'diff --git a/docker-compose.yml b/docker-compose.ymlindex 7ed830a3f..69acc39d0 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -69,6 +69,7 @@ services:OR_EMAIL_HOST:OR_EMAIL_USER:OR_EMAIL_PASSWORD:+ OR_EMAIL_X_HEADERS:OR_EMAIL_FROM:OR_EMAIL_ADMIN:OR_HOSTNAME: ${OR_HOSTNAME:-localhost}diff --git a/manager/Dockerfile b/manager/Dockerfileindex 4dbfbbfe2..6816679ec 100644--- a/manager/Dockerfile+++ b/manager/Dockerfile@@ -20,6 +20,7 @@ ENV OR_EMAIL_PORT ${OR_EMAIL_PORT:-587}ENV OR_EMAIL_TLS ${OR_EMAIL_TLS:-true}ENV OR_EMAIL_FROM ${OR_EMAIL_FROM}ENV OR_EMAIL_ADMIN ${OR_EMAIL_ADMIN}+ENV OR_EMAIL_X_HEADERS ${OR_EMAIL_X_HEADERS}ENV OR_FIREBASE_CONFIG_FILE ${OR_FIREBASE_CONFIG_FILE:-/deployment/manager/fcm.json}ENV OR_DEV_MODE ${OR_DEV_MODE:-false}ENV OR_SETUP_RUN_ON_RESTART ${OR_SETUP_RUN_ON_RESTART:-false}diff --git a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.javaindex 6f698c7a0..e5e81b5ad 100644--- a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java+++ b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java@@ -40,10 +40,7 @@ import org.simplejavamail.mailer.Mailer;import org.simplejavamail.mailer.MailerBuilder;import org.simplejavamail.mailer.config.TransportStrategy;-import java.util.ArrayList;-import java.util.Arrays;-import java.util.Collections;-import java.util.List;+import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;import java.util.stream.Collectors;@@ -58,6 +55,7 @@ public class EmailNotificationHandler implements NotificationHandler {private static final Logger LOG = Logger.getLogger(EmailNotificationHandler.class.getName());protected String defaultFrom;protected Mailer mailer;+ protected Map<String, String> headers;protected ManagerIdentityService managerIdentityService;protected AssetStorageService assetStorageService;@@ -78,6 +76,16 @@ public class EmailNotificationHandler implements NotificationHandler {String user = container.getConfig().getOrDefault(OR_EMAIL_USER, null);String password = container.getConfig().getOrDefault(OR_EMAIL_PASSWORD, null);+ String headersStr = container.getConfig().getOrDefault(OR_EMAIL_X_HEADERS, null);+ if (!TextUtil.isNullOrEmpty(headersStr)) {+ headers = Arrays.stream(headersStr.split("\\R"))+ .map(s -> s.split("=", 2))+ .collect(Collectors.toMap(+ arr -> arr[0],+ arr -> arr[1]+ ));+ }+defaultFrom = container.getConfig().getOrDefault(OR_EMAIL_FROM, OR_EMAIL_FROM_DEFAULT);if (!TextUtil.isNullOrEmpty(host) && !TextUtil.isNullOrEmpty(user) && !TextUtil.isNullOrEmpty(password)) {@@ -329,7 +337,7 @@ public class EmailNotificationHandler implements NotificationHandler {return sendMessage(emailBuilder.buildEmail());}- public NotificationSendResult sendMessage(Email email) {+ protected NotificationSendResult sendMessage(Email email) {try {mailer.sendMail(email);return NotificationSendResult.success();@@ -346,6 +354,10 @@ public class EmailNotificationHandler implements NotificationHandler {.withPlainText(emailNotificationMessage.getText()).withHTMLText(emailNotificationMessage.getHtml());+ if (headers != null) {+ emailBuilder.withHeaders(headers);+ }+if (emailNotificationMessage.getFrom() != null) {emailBuilder.from(convertRecipient(emailNotificationMessage.getFrom()));}diff --git a/model/src/main/java/org/openremote/model/Constants.java b/model/src/main/java/org/openremote/model/Constants.javaindex 4e501425f..7871a0802 100644--- a/model/src/main/java/org/openremote/model/Constants.java+++ b/model/src/main/java/org/openremote/model/Constants.java@@ -59,6 +59,7 @@ public interface Constants {int OR_EMAIL_PORT_DEFAULT = 587;String OR_EMAIL_TLS = "OR_EMAIL_TLS";boolean OR_EMAIL_TLS_DEFAULT = true;+ String OR_EMAIL_X_HEADERS = "OR_EMAIL_X_HEADERS";String OR_EMAIL_FROM = "OR_EMAIL_FROM";String OR_EMAIL_FROM_DEFAULT = "no-reply@localhost";String OR_EMAIL_ADMIN = "OR_EMAIL_ADMIN";diff --git a/profile/demo.yml b/profile/demo.ymlindex 7d3b9bea5..62188bb35 100644--- a/profile/demo.yml+++ b/profile/demo.yml@@ -91,6 +91,7 @@ services:OR_EMAIL_HOST:OR_EMAIL_USER:OR_EMAIL_PASSWORD:+ OR_EMAIL_X_HEADERS:OR_EMAIL_FROM:OR_EMAIL_ADMIN:OR_SSL_PORT: ${OR_SSL_PORT:-443}diff --git a/profile/deploy.yml b/profile/deploy.ymlindex 722b3ff4c..033ea7fec 100644--- a/profile/deploy.yml+++ b/profile/deploy.yml@@ -162,6 +162,7 @@ services:OR_EMAIL_PASSWORD:OR_EMAIL_PORT: ${OR_EMAIL_PORT}OR_EMAIL_TLS: ${OR_EMAIL_TLS}+ OR_EMAIL_X_HEADERS: ${OR_EMAIL_X_HEADERS:-} # Headers to set on all outbound emails# Where to send admin emails toOR_EMAIL_ADMIN: ${OR_EMAIL_ADMIN}diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovyindex ee493c9c4..5d6106c66 100644--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy@@ -33,6 +33,8 @@ import java.time.Instantimport java.time.temporal.ChronoUnitimport java.util.concurrent.TimeUnit+import static org.openremote.container.timer.TimerService.Clock.REAL+import static org.openremote.container.timer.TimerService.TIMER_CLOCK_TYPEimport static org.openremote.container.util.MapAccess.getStringimport static org.openremote.manager.security.ManagerIdentityProvider.OR_ADMIN_PASSWORDimport static org.openremote.manager.security.ManagerIdentityProvider.OR_ADMIN_PASSWORD_DEFAULT@@ -551,7 +553,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {given: "the container environment is started with the mock handler"def conditions = new PollingConditions(timeout: 10, delay: 0.2)- def container = startContainer(defaultConfig(), defaultServices())+ def container = startContainer(defaultConfig() << [(OR_EMAIL_X_HEADERS): "Test 1=Hello World 1\nTest2=Hello World 2"], defaultServices())def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)def notificationService = container.getService(NotificationService.class)@@ -591,6 +593,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {assert sentEmails.size() == 2assert sentEmails.every {it.getPlainText() == "Hello world!"}assert sentEmails.every {it.getSubject() == "Test"}+ assert sentEmails.every {it.getHeaders() != null && it.getHeaders().size() == 2 && it.getHeaders().get("Test 1") == "Hello World 1" && it.getHeaders().get("Test2") == "Hello World 2" }assert sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}assert !sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}assert sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}