mirror of
https://github.com/google/nomulus.git
synced 2025-05-08 07:48:21 +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;
|
package google.registry.util;
|
||||||
|
|
||||||
|
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||||
import static com.google.common.collect.Iterables.toArray;
|
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 com.google.common.net.MediaType;
|
||||||
import google.registry.util.EmailMessage.Attachment;
|
import google.registry.util.EmailMessage.Attachment;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -44,6 +48,10 @@ public class SendEmailService {
|
||||||
private final Retrier retrier;
|
private final Retrier retrier;
|
||||||
private final TransportEmailSender transportEmailSender;
|
private final TransportEmailSender transportEmailSender;
|
||||||
|
|
||||||
|
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
||||||
|
private static final ImmutableSet<String> ALLOWED_ENVS =
|
||||||
|
ImmutableSet.of("PRODUCTION", "UNITTEST");
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SendEmailService(Retrier retrier, TransportEmailSender transportEmailSender) {
|
SendEmailService(Retrier retrier, TransportEmailSender transportEmailSender) {
|
||||||
this.retrier = retrier;
|
this.retrier = retrier;
|
||||||
|
@ -55,6 +63,18 @@ public class SendEmailService {
|
||||||
* on transient failures.
|
* on transient failures.
|
||||||
*/
|
*/
|
||||||
public void sendEmail(EmailMessage emailMessage) {
|
public void sendEmail(EmailMessage emailMessage) {
|
||||||
|
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(
|
retrier.callWithRetry(
|
||||||
() -> {
|
() -> {
|
||||||
Message msg =
|
Message msg =
|
||||||
|
@ -89,3 +109,4 @@ public class SendEmailService {
|
||||||
MessagingException.class);
|
MessagingException.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue