Fix Email Test Issues (#15183)

This commit is contained in:
Mohit Yadav 2024-02-14 21:22:32 +05:30 committed by GitHub
parent e6c2c36f11
commit 20876fa1ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 64 additions and 21 deletions

View File

@ -13,13 +13,16 @@
package org.openmetadata.service.events;
import java.util.UUID;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.schema.EntityInterface;
import org.openmetadata.schema.EntityTimeSeriesInterface;
import org.openmetadata.schema.entity.feed.Thread;
import org.openmetadata.schema.type.AuditLog;
import org.openmetadata.schema.type.EntityReference;
import org.openmetadata.schema.type.ChangeEvent;
import org.openmetadata.service.Entity;
import org.openmetadata.service.OpenMetadataApplicationConfig;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
@ -50,23 +53,35 @@ public class AuditEventHandler implements EventHandler {
username = requestContext.getSecurityContext().getUserPrincipal().getName();
}
try {
EntityReference entityReference;
// TODO: EntityInterface and EntityTimeSeriesInterface share some common implementation and
// diverge at the edge (e.g. EntityTimeSeriesInterface does not expect owners, etc.).
// We should implement a parent class that captures the common fields and then have
// EntityInterface and EntityTimeSeriesInterface extend it.
// TODO: if we are just interested in entity's we can just do else and return null.
UUID entityId = null;
String entityType = "";
if (responseContext.getEntity()
instanceof EntityTimeSeriesInterface entityTimeSeriesInterface) {
entityReference = entityTimeSeriesInterface.getEntityReference();
entityId = entityTimeSeriesInterface.getEntityReference().getId();
entityType = entityTimeSeriesInterface.getEntityReference().getType();
} else if (responseContext.getEntity() instanceof EntityInterface entityInterface) {
entityId = entityInterface.getEntityReference().getId();
entityType = entityInterface.getEntityReference().getType();
} else if (responseContext.getEntity() instanceof ChangeEvent changeEvent) {
entityId = changeEvent.getId();
entityType = "CHANGE_EVENT";
} else if (responseContext.getEntity() instanceof Thread thread) {
entityId = thread.getId();
entityType = Entity.THREAD;
} else {
entityReference = ((EntityInterface) responseContext.getEntity()).getEntityReference();
return null;
}
AuditLog auditLog =
new AuditLog()
.withPath(path)
.withTimestamp(System.currentTimeMillis())
.withEntityId(entityReference.getId())
.withEntityType(entityReference.getType())
.withEntityId(entityId)
.withEntityType(entityType)
.withMethod(AuditLog.Method.fromValue(method))
.withUserName(username)
.withResponseCode(responseCode);

View File

@ -39,6 +39,7 @@ import org.openmetadata.schema.util.EntitiesCount;
import org.openmetadata.schema.util.ServicesCount;
import org.openmetadata.service.Entity;
import org.openmetadata.service.OpenMetadataApplicationConfig;
import org.openmetadata.service.exception.UnhandledServerException;
import org.openmetadata.service.jdbi3.ListFilter;
import org.openmetadata.service.jdbi3.SystemRepository;
import org.openmetadata.service.resources.Collection;
@ -167,7 +168,15 @@ public class SystemResource {
}
authorizer.authorizeAdmin(securityContext);
EmailUtil.sendTestEmail(emailRequest.getEmail());
try {
EmailUtil.testConnection();
EmailUtil.sendTestEmail(emailRequest.getEmail(), false);
} catch (Exception ex) {
LOG.error("Failed in sending mail. Message: {}", ex.getMessage(), ex);
throw new UnhandledServerException(ex.getMessage());
}
return Response.status(Response.Status.OK).entity("Test Email Sent Successfully.").build();
}

View File

@ -344,7 +344,8 @@ public class BasicAuthenticator implements AuthenticatorHandler {
templatePopulator,
user.getEmail(),
EmailUtil.EMAIL_TEMPLATE_BASEPATH,
EmailUtil.INVITE_RANDOM_PWD);
EmailUtil.INVITE_RANDOM_PWD,
true);
} catch (TemplateException ex) {
LOG.error(
"Failed in sending Mail to user [{}]. Reason : {}",

View File

@ -90,6 +90,10 @@ public class EmailUtil {
private static final String EMAIL_IGNORE_MSG =
"Email was not sent to {} as SMTP setting is not enabled";
static {
getSmtpSettings();
}
private EmailUtil() {
try {
getSmtpSettings();
@ -140,7 +144,8 @@ public class EmailUtil {
templatePopulator,
user.getEmail(),
EMAIL_TEMPLATE_BASEPATH,
ACCOUNT_STATUS_TEMPLATE_FILE);
ACCOUNT_STATUS_TEMPLATE_FILE,
true);
} else {
LOG.warn(EMAIL_IGNORE_MSG, user.getEmail());
}
@ -160,7 +165,8 @@ public class EmailUtil {
templatePopulator,
user.getEmail(),
EMAIL_TEMPLATE_BASEPATH,
EMAIL_VERIFICATION_TEMPLATE_PATH);
EMAIL_VERIFICATION_TEMPLATE_PATH,
true);
} else {
LOG.warn(EMAIL_IGNORE_MSG, user.getEmail());
}
@ -178,7 +184,12 @@ public class EmailUtil {
templatePopulator.put(EXPIRATION_TIME_KEY, DEFAULT_EXPIRATION_TIME);
sendMail(
subject, templatePopulator, user.getEmail(), EMAIL_TEMPLATE_BASEPATH, templateFilePath);
subject,
templatePopulator,
user.getEmail(),
EMAIL_TEMPLATE_BASEPATH,
templateFilePath,
true);
} else {
LOG.warn(EMAIL_IGNORE_MSG, user.getEmail());
}
@ -203,7 +214,7 @@ public class EmailUtil {
templatePopulator.put("fieldNewValue", thread.getTask().getSuggestion());
templatePopulator.put("taskLink", taskLink);
sendMail(subject, templatePopulator, email, EMAIL_TEMPLATE_BASEPATH, templateFilePath);
sendMail(subject, templatePopulator, email, EMAIL_TEMPLATE_BASEPATH, templateFilePath, true);
} else {
LOG.warn(EMAIL_IGNORE_MSG, email);
}
@ -214,7 +225,8 @@ public class EmailUtil {
Map<String, Object> model,
String to,
String baseTemplatePackage,
String templatePath)
String templatePath,
boolean async)
throws IOException, TemplateException {
if (Boolean.TRUE.equals(getSmtpSettings().getEnableSmtpServer())) {
EmailPopulatingBuilder emailBuilder = EmailBuilder.startingBlank();
@ -230,7 +242,7 @@ public class EmailUtil {
template.process(model, stringWriter);
String mailContent = stringWriter.toString();
emailBuilder.withHTMLText(mailContent);
sendMail(emailBuilder.buildEmail());
sendMail(emailBuilder.buildEmail(), async);
} else {
LOG.warn(EMAIL_IGNORE_MSG, to);
}
@ -257,13 +269,15 @@ public class EmailUtil {
template.process(model, stringWriter);
String mailContent = stringWriter.toString();
emailBuilder.withHTMLText(mailContent);
sendMail(emailBuilder.buildEmail());
sendMail(emailBuilder.buildEmail(), true);
}
}
public static void sendMail(Email email) {
public static void sendMail(Email email, boolean async) {
if (mailer != null && getSmtpSettings().getEnableSmtpServer()) {
mailer.sendMail(email, true);
mailer.sendMail(email, async);
} else {
LOG.error("Mailer is not initialized or Smtp is not Enabled.");
}
}
@ -281,7 +295,8 @@ public class EmailUtil {
templatePopulator,
user.getEmail(),
EmailUtil.EMAIL_TEMPLATE_BASEPATH,
EmailUtil.INVITE_RANDOM_PWD);
EmailUtil.INVITE_RANDOM_PWD,
true);
} catch (Exception ex) {
LOG.error(
"Failed in sending Mail to user [{}]. Reason : {}", user.getEmail(), ex.getMessage());
@ -309,7 +324,8 @@ public class EmailUtil {
templatePopulator,
receiverMail,
EmailUtil.EMAIL_TEMPLATE_BASEPATH,
EmailUtil.CHANGE_EVENT_TEMPLATE);
EmailUtil.CHANGE_EVENT_TEMPLATE,
true);
} catch (Exception ex) {
LOG.error(
"Failed in sending Mail to user [{}]. Reason : {}", receiverMail, ex.getMessage());
@ -344,7 +360,8 @@ public class EmailUtil {
}
}
public static void sendTestEmail(String email) throws IOException, TemplateException {
public static void sendTestEmail(String email, boolean async)
throws IOException, TemplateException {
if (Boolean.TRUE.equals(getSmtpSettings().getEnableSmtpServer())) {
Map<String, Object> templatePopulator = new HashMap<>();
templatePopulator.put("userName", email.split("@")[0]);
@ -355,7 +372,8 @@ public class EmailUtil {
templatePopulator,
email,
EMAIL_TEMPLATE_BASEPATH,
TEST_EMAIL_TEMPLATE);
TEST_EMAIL_TEMPLATE,
async);
} else {
LOG.warn(EMAIL_IGNORE_MSG, email);
}