From ea803a29115209464458b20486f2f808ca11aad9 Mon Sep 17 00:00:00 2001 From: Kristijan Mitrovic Date: Thu, 1 Oct 2020 03:12:49 +0200 Subject: [PATCH] Build random string as Sendgrid Subuser username --- .../cloud/email/SendgridSmtpEmailDriver.java | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/bubble-server/src/main/java/bubble/cloud/email/SendgridSmtpEmailDriver.java b/bubble-server/src/main/java/bubble/cloud/email/SendgridSmtpEmailDriver.java index f5c7ce6c..8f7a5824 100644 --- a/bubble-server/src/main/java/bubble/cloud/email/SendgridSmtpEmailDriver.java +++ b/bubble-server/src/main/java/bubble/cloud/email/SendgridSmtpEmailDriver.java @@ -20,13 +20,12 @@ import static bubble.cloud.storage.StorageCryptStream.MIN_DISTINCT_LENGTH; import static bubble.cloud.storage.StorageCryptStream.MIN_KEY_LENGTH; import static org.apache.http.HttpHeaders.AUTHORIZATION; import static org.apache.http.HttpHeaders.CONTENT_TYPE; -import static org.cobbzilla.util.daemon.ZillaRuntime.die; -import static org.cobbzilla.util.daemon.ZillaRuntime.empty; +import static org.cobbzilla.util.daemon.ZillaRuntime.*; import static org.cobbzilla.util.http.HttpContentTypes.APPLICATION_JSON; import static org.cobbzilla.util.http.HttpUtil.getResponse; import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.util.security.CryptoUtil.generatePassword; -import static org.cobbzilla.util.string.StringUtil.repeat; +import static org.cobbzilla.wizard.model.IdentifiableBase.DEFAULT_SHORT_ID_LENGTH; /** * Only to be used with Sendgrid account with Subusers supported! @@ -40,13 +39,6 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver { return SENDGRID_SMTP.equals(serviceHost); } - /** - * Build username which will be used for Subuser created on SendGrid's service for specified Account's object. - */ - @NonNull private String sgUsername(@NonNull final CloudService delegatedService) { - return delegatedService.getShortId(); - } - @Override @NonNull public CloudService setupDelegatedCloudService(@NonNull final BubbleConfiguration configuration, @NonNull final CloudService parentService, @NonNull final CloudService delegatedService) { @@ -62,18 +54,17 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver { final Account accountWithDelegate = configuration.getBean(AccountDAO.class) .findByUuid(delegatedService.getAccount()); - final String user = sgUsername(delegatedService); + final String user = uuid().substring(0, DEFAULT_SHORT_ID_LENGTH); String password = generatePassword(MIN_KEY_LENGTH, MIN_DISTINCT_LENGTH); final CreateSubuserRequest data = new CreateSubuserRequest(user, accountWithDelegate.getEmail(), password, new String[]{ sgIp }); doRequest("subusers", HttpMethods.POST, json(data)); - delegatedService.setDelegated(null).setTemplate(false); - delegatedService.getCredentials() - .setParam(PARAM_USER, user) - .setParam(PARAM_PASSWORD, password) - .setParam(PARAM_PARENT_SERVICE, parentService.getUuid()); - password = repeat("x", MIN_KEY_LENGTH); // Override password (in memory) just in case + final CloudCredentials credentials = delegatedService.getCredentials(); + credentials.setParam(PARAM_USER, user) + .setParam(PARAM_PASSWORD, password) + .setParam(PARAM_PARENT_SERVICE, parentService.getUuid()); + delegatedService.setCredentials(credentials).setDelegated(null).setTemplate(false); return delegatedService; } @@ -90,8 +81,10 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver { .findByUuid(parentServiceUuid); if (parentService == null) return; - final String sgUserToDelete = sgUsername(service); - doRequest("subusers/" + sgUserToDelete, HttpMethods.DELETE, null); + final String user = service.getCredentials().getParam(PARAM_USER); + if (empty(user)) return; + + doRequest("subusers/" + user, HttpMethods.DELETE, null); } private String doRequest(@NonNull final String uri, @NonNull final String method, @Nullable final String json) {