Skip to content

Commit d9d8320

Browse files
authored
Verify user can send email (#3045)
Change the CannedScriptExecutionAction to send a email message as a user-specified G workspace user. This change is part of b/510340944, to verify that a newly added dedicated sender is properly set up for sending emails. Once the new sender is tested, the changes in this PR can be dropped.
1 parent 56fe588 commit d9d8320

4 files changed

Lines changed: 177 additions & 34 deletions

File tree

console-webapp/package-lock.json

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/main/java/google/registry/batch/BatchModule.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ public class BatchModule {
5454
static final int DEFAULT_MAX_QPS = 10;
5555

5656
@Provides
57-
@Parameter("url")
58-
static String provideUrl(HttpServletRequest req) {
59-
return extractRequiredParameter(req, "url");
57+
@Parameter("sender")
58+
static String provideSender(HttpServletRequest req) {
59+
return extractRequiredParameter(req, "sender");
60+
}
61+
62+
@Provides
63+
@Parameter("receiver")
64+
static String provideReceiver(HttpServletRequest req) {
65+
return extractRequiredParameter(req, "receiver");
6066
}
6167

6268
@Provides

core/src/main/java/google/registry/batch/CannedScriptExecutionAction.java

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,32 @@
1616

1717
import static google.registry.request.Action.Method.GET;
1818
import static google.registry.request.Action.Method.POST;
19-
import static java.nio.charset.StandardCharsets.UTF_8;
2019

20+
import com.google.api.services.gmail.Gmail;
2121
import com.google.common.flogger.FluentLogger;
22+
import dagger.Lazy;
23+
import google.registry.config.RegistryConfig.Config;
24+
import google.registry.groups.GmailClient;
2225
import google.registry.request.Action;
2326
import google.registry.request.Parameter;
2427
import google.registry.request.Response;
25-
import google.registry.request.UrlConnectionService;
26-
import google.registry.request.UrlConnectionUtils;
2728
import google.registry.request.auth.Auth;
29+
import google.registry.util.EmailMessage;
30+
import google.registry.util.Retrier;
2831
import jakarta.inject.Inject;
29-
import java.net.URL;
30-
import javax.net.ssl.HttpsURLConnection;
32+
import jakarta.mail.internet.AddressException;
33+
import jakarta.mail.internet.InternetAddress;
3134

3235
/**
3336
* Action that executes a canned script specified by the caller.
3437
*
3538
* <p>This class provides a hook for invoking hard-coded methods. The main use case is to verify in
3639
* Sandbox and Production environments new features that depend on environment-specific
37-
* configurations. For example, the {@code DelegatedCredential}, which requires correct GWorkspace
38-
* configuration, has been tested this way. Since it is a hassle to add or remove endpoints, we keep
39-
* this class all the time.
40+
* configurations.
4041
*
4142
* <p>This action can be invoked using the Nomulus CLI command: {@code nomulus -e ${env} curl
42-
* --service BACKEND -X POST -u '/_dr/task/executeCannedScript}'}
43+
* --service BACKEND -X POST -d 'sender=sender@example.com' -d 'receiver=receiver@example.com' -u
44+
* '/_dr/task/executeCannedScript'}
4345
*/
4446
@Action(
4547
service = Action.Service.BACKEND,
@@ -50,39 +52,50 @@
5052
public class CannedScriptExecutionAction implements Runnable {
5153
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
5254

53-
@Inject UrlConnectionService urlConnectionService;
55+
@Inject Lazy<Gmail> gmail;
56+
@Inject Retrier retrier;
57+
58+
@Inject
59+
@Config("isEmailSendingEnabled")
60+
boolean isEmailSendingEnabled;
61+
5462
@Inject Response response;
5563

5664
@Inject
57-
@Parameter("url")
58-
String url;
65+
@Parameter("sender")
66+
String sender;
67+
68+
@Inject
69+
@Parameter("receiver")
70+
String receiver;
5971

6072
@Inject
6173
CannedScriptExecutionAction() {}
6274

6375
@Override
6476
public void run() {
65-
Integer responseCode = null;
66-
String responseContent = null;
77+
// For b/510340944, validating a new G Workspace user can send email. Code below can be
78+
// removed or changed afterward.
6779
try {
68-
logger.atInfo().log("Connecting to: %s", url);
69-
HttpsURLConnection connection =
70-
(HttpsURLConnection) urlConnectionService.createConnection(new URL(url));
71-
responseCode = connection.getResponseCode();
72-
logger.atInfo().log("Code: %d", responseCode);
73-
logger.atInfo().log("Headers: %s", connection.getHeaderFields());
74-
responseContent = new String(UrlConnectionUtils.getResponseBytes(connection), UTF_8);
75-
logger.atInfo().log("Response: %s", responseContent);
80+
logger.atInfo().log("Sending email from %s to %s", sender, receiver);
81+
GmailClient gmailClient =
82+
new GmailClient(
83+
gmail, retrier, isEmailSendingEnabled, sender, sender, new InternetAddress(sender));
84+
gmailClient.sendEmail(
85+
EmailMessage.newBuilder()
86+
.addRecipient(new InternetAddress(receiver))
87+
.setSubject(String.format("Email send test from %s", sender))
88+
.setBody(String.format("This is a test email sent from %s to %s.", sender, receiver))
89+
.build());
90+
response.setPayload("Email sent successfully.");
91+
} catch (AddressException e) {
92+
logger.atWarning().withCause(e).log(
93+
"Invalid email address: sender=%s, receiver=%s", sender, receiver);
94+
response.setStatus(400);
95+
response.setPayload("Invalid email address provided.");
7696
} catch (Exception e) {
77-
logger.atWarning().withCause(e).log("Connection to %s failed", url);
97+
logger.atSevere().withCause(e).log("Failed to send email");
7898
throw new RuntimeException(e);
79-
} finally {
80-
if (responseCode != null) {
81-
response.setStatus(responseCode);
82-
}
83-
if (responseContent != null) {
84-
response.setPayload(responseContent);
85-
}
8699
}
87100
}
88101
}

core/src/main/java/google/registry/groups/GmailClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ public final class GmailClient {
5656
private final InternetAddress outgoingEmailAddressWithUsername;
5757
private final InternetAddress replyToEmailAddress;
5858

59+
// TODO(b/510340944): make package private after feature is rolled out
5960
@Inject
60-
GmailClient(
61+
public GmailClient(
6162
Lazy<Gmail> gmail,
6263
Retrier retrier,
6364
@Config("isEmailSendingEnabled") boolean isEmailSendingEnabled,

0 commit comments

Comments
 (0)