Refactor threat emails for daily+monthly usage

Eventually the Publish action will control daily/monthly sending and provide
the correct threats to email. The goal of this PR is to entirely separate
the "sending email" functionality from the "parsing threat matches"
functionality.

The PublishAction will figure out if the monthly emails should be sent out,
then will ask the Spec11ThreatMatchesParser for the monthly threats (if
appropriate) and the new threat matches for today. It will then pass those
matches and the appropriate email subject+body to the email utils class,
whose only job is to format and send the emails.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=224869643
This commit is contained in:
jianglai 2018-12-10 13:15:09 -08:00
parent 4dad0a8a73
commit ea154a8378
4 changed files with 77 additions and 42 deletions

View file

@ -16,63 +16,58 @@ package google.registry.reporting.spec11;
import static com.google.common.base.Throwables.getRootCause;
import com.google.common.collect.ImmutableList;
import google.registry.beam.spec11.ThreatMatch;
import google.registry.config.RegistryConfig.Config;
import google.registry.util.Retrier;
import google.registry.util.SendEmailService;
import java.io.IOException;
import java.util.List;
import javax.inject.Inject;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import org.joda.time.YearMonth;
import org.joda.time.LocalDate;
/** Provides e-mail functionality for Spec11 tasks, such as sending Spec11 reports to registrars. */
public class Spec11EmailUtils {
private final SendEmailService emailService;
private final YearMonth yearMonth;
private final LocalDate date;
private final String outgoingEmailAddress;
private final String alertRecipientAddress;
private final String spec11ReplyToAddress;
private final String spec11EmailBodyTemplate;
private final Spec11RegistrarThreatMatchesParser spec11RegistrarThreatMatchesParser;
private final Retrier retrier;
@Inject
Spec11EmailUtils(
SendEmailService emailService,
YearMonth yearMonth,
LocalDate date,
@Config("gSuiteOutgoingEmailAddress") String outgoingEmailAddress,
@Config("alertRecipientEmailAddress") String alertRecipientAddress,
@Config("spec11ReplyToEmailAddress") String spec11ReplyToAddress,
@Config("spec11EmailBodyTemplate") String spec11EmailBodyTemplate,
Spec11RegistrarThreatMatchesParser spec11RegistrarThreatMatchesParser,
Retrier retrier) {
this.emailService = emailService;
this.yearMonth = yearMonth;
this.date = date;
this.outgoingEmailAddress = outgoingEmailAddress;
this.alertRecipientAddress = alertRecipientAddress;
this.spec11ReplyToAddress = spec11ReplyToAddress;
this.spec11RegistrarThreatMatchesParser = spec11RegistrarThreatMatchesParser;
this.spec11EmailBodyTemplate = spec11EmailBodyTemplate;
this.retrier = retrier;
}
/**
* Processes a Spec11 report on GCS for a given month and e-mails registrars based on the
* contents.
* Processes a list of registrar/list-of-threat pairings and sends a notification email to the
* appropriate address.
*/
void emailSpec11Reports() {
void emailSpec11Reports(
String spec11EmailBodyTemplate,
String subject,
List<RegistrarThreatMatches> registrarThreatMatchesList) {
try {
retrier.callWithRetry(
() -> {
ImmutableList<RegistrarThreatMatches> registrarThreatMatchesList =
spec11RegistrarThreatMatchesParser.getRegistrarThreatMatches();
for (RegistrarThreatMatches registrarThreatMatches : registrarThreatMatchesList) {
emailRegistrar(registrarThreatMatches);
emailRegistrar(spec11EmailBodyTemplate, subject, registrarThreatMatches);
}
},
IOException.class,
@ -80,16 +75,17 @@ public class Spec11EmailUtils {
} catch (Throwable e) {
// Send an alert with the root cause, unwrapping the retrier's RuntimeException
sendAlertEmail(
String.format("Spec11 Emailing Failure %s", yearMonth.toString()),
String.format("Spec11 Emailing Failure %s", date),
String.format("Emailing spec11 reports failed due to %s", getRootCause(e).getMessage()));
throw new RuntimeException("Emailing spec11 report failed", e);
}
sendAlertEmail(
String.format("Spec11 Pipeline Success %s", yearMonth.toString()),
String.format("Spec11 Pipeline Success %s", date),
"Spec11 reporting completed successfully.");
}
private void emailRegistrar(RegistrarThreatMatches registrarThreatMatches)
private void emailRegistrar(
String spec11EmailBodyTemplate, String subject, RegistrarThreatMatches registrarThreatMatches)
throws MessagingException {
String registrarEmail = registrarThreatMatches.registrarEmailAddress();
StringBuilder threatList = new StringBuilder();
@ -103,8 +99,7 @@ public class Spec11EmailUtils {
.replace("{REPLY_TO_EMAIL}", spec11ReplyToAddress)
.replace("{LIST_OF_THREATS}", threatList.toString());
Message msg = emailService.createMessage();
msg.setSubject(
String.format("Google Registry Monthly Threat Detector [%s]", yearMonth.toString()));
msg.setSubject(subject);
msg.setText(body);
msg.setFrom(new InternetAddress(outgoingEmailAddress));
msg.addRecipient(RecipientType.TO, new InternetAddress(registrarEmail));