mirror of
https://github.com/google/nomulus.git
synced 2025-04-29 19:47:51 +02:00
Limit environments allowed to send emails out (#1807)
This commit is contained in:
parent
b83c970af4
commit
a64d086708
1 changed files with 51 additions and 30 deletions
|
@ -14,8 +14,12 @@
|
|||
|
||||
package google.registry.util;
|
||||
|
||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||
import static com.google.common.collect.Iterables.toArray;
|
||||
|
||||
import com.google.common.base.Ascii;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
import com.google.common.net.MediaType;
|
||||
import google.registry.util.EmailMessage.Attachment;
|
||||
import java.io.IOException;
|
||||
|
@ -44,6 +48,10 @@ public class SendEmailService {
|
|||
private final Retrier retrier;
|
||||
private final TransportEmailSender transportEmailSender;
|
||||
|
||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
||||
private static final ImmutableSet<String> ALLOWED_ENVS =
|
||||
ImmutableSet.of("PRODUCTION", "UNITTEST");
|
||||
|
||||
@Inject
|
||||
SendEmailService(Retrier retrier, TransportEmailSender transportEmailSender) {
|
||||
this.retrier = retrier;
|
||||
|
@ -55,37 +63,50 @@ public class SendEmailService {
|
|||
* on transient failures.
|
||||
*/
|
||||
public void sendEmail(EmailMessage emailMessage) {
|
||||
retrier.callWithRetry(
|
||||
() -> {
|
||||
Message msg =
|
||||
new MimeMessage(
|
||||
Session.getDefaultInstance(new Properties(), /* authenticator= */ null));
|
||||
msg.setFrom(emailMessage.from());
|
||||
msg.addRecipients(
|
||||
RecipientType.TO, toArray(emailMessage.recipients(), InternetAddress.class));
|
||||
msg.setSubject(emailMessage.subject());
|
||||
if (!ALLOWED_ENVS.contains(
|
||||
Ascii.toUpperCase(System.getProperty("google.registry.environment", "UNITTEST")))) {
|
||||
logger.atInfo().log(
|
||||
String.format(
|
||||
"Email with subject %s would have been sent to recipients %s",
|
||||
emailMessage.subject().substring(0, Math.min(emailMessage.subject().length(), 15)),
|
||||
String.join(
|
||||
" , ",
|
||||
emailMessage.recipients().stream()
|
||||
.map(ia -> ia.toString())
|
||||
.collect(toImmutableSet()))));
|
||||
} else {
|
||||
retrier.callWithRetry(
|
||||
() -> {
|
||||
Message msg =
|
||||
new MimeMessage(
|
||||
Session.getDefaultInstance(new Properties(), /* authenticator= */ null));
|
||||
msg.setFrom(emailMessage.from());
|
||||
msg.addRecipients(
|
||||
RecipientType.TO, toArray(emailMessage.recipients(), InternetAddress.class));
|
||||
msg.setSubject(emailMessage.subject());
|
||||
|
||||
Multipart multipart = new MimeMultipart();
|
||||
BodyPart bodyPart = new MimeBodyPart();
|
||||
bodyPart.setContent(
|
||||
emailMessage.body(),
|
||||
emailMessage.contentType().orElse(MediaType.PLAIN_TEXT_UTF_8).toString());
|
||||
multipart.addBodyPart(bodyPart);
|
||||
Multipart multipart = new MimeMultipart();
|
||||
BodyPart bodyPart = new MimeBodyPart();
|
||||
bodyPart.setContent(
|
||||
emailMessage.body(),
|
||||
emailMessage.contentType().orElse(MediaType.PLAIN_TEXT_UTF_8).toString());
|
||||
multipart.addBodyPart(bodyPart);
|
||||
|
||||
if (emailMessage.attachment().isPresent()) {
|
||||
Attachment attachment = emailMessage.attachment().get();
|
||||
BodyPart attachmentPart = new MimeBodyPart();
|
||||
attachmentPart.setContent(attachment.content(), attachment.contentType().toString());
|
||||
attachmentPart.setFileName(attachment.filename());
|
||||
multipart.addBodyPart(attachmentPart);
|
||||
}
|
||||
msg.addRecipients(RecipientType.BCC, toArray(emailMessage.bccs(), Address.class));
|
||||
msg.addRecipients(RecipientType.CC, toArray(emailMessage.ccs(), Address.class));
|
||||
msg.setContent(multipart);
|
||||
msg.saveChanges();
|
||||
transportEmailSender.sendMessage(msg);
|
||||
},
|
||||
IOException.class,
|
||||
MessagingException.class);
|
||||
if (emailMessage.attachment().isPresent()) {
|
||||
Attachment attachment = emailMessage.attachment().get();
|
||||
BodyPart attachmentPart = new MimeBodyPart();
|
||||
attachmentPart.setContent(attachment.content(), attachment.contentType().toString());
|
||||
attachmentPart.setFileName(attachment.filename());
|
||||
multipart.addBodyPart(attachmentPart);
|
||||
}
|
||||
msg.addRecipients(RecipientType.BCC, toArray(emailMessage.bccs(), Address.class));
|
||||
msg.addRecipients(RecipientType.CC, toArray(emailMessage.ccs(), Address.class));
|
||||
msg.setContent(multipart);
|
||||
msg.saveChanges();
|
||||
transportEmailSender.sendMessage(msg);
|
||||
},
|
||||
IOException.class,
|
||||
MessagingException.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue