|
@@ -20,13 +20,12 @@ import static bubble.cloud.storage.StorageCryptStream.MIN_DISTINCT_LENGTH; |
|
|
import static bubble.cloud.storage.StorageCryptStream.MIN_KEY_LENGTH; |
|
|
import static bubble.cloud.storage.StorageCryptStream.MIN_KEY_LENGTH; |
|
|
import static org.apache.http.HttpHeaders.AUTHORIZATION; |
|
|
import static org.apache.http.HttpHeaders.AUTHORIZATION; |
|
|
import static org.apache.http.HttpHeaders.CONTENT_TYPE; |
|
|
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.HttpContentTypes.APPLICATION_JSON; |
|
|
import static org.cobbzilla.util.http.HttpUtil.getResponse; |
|
|
import static org.cobbzilla.util.http.HttpUtil.getResponse; |
|
|
import static org.cobbzilla.util.json.JsonUtil.json; |
|
|
import static org.cobbzilla.util.json.JsonUtil.json; |
|
|
import static org.cobbzilla.util.security.CryptoUtil.generatePassword; |
|
|
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! |
|
|
* Only to be used with Sendgrid account with Subusers supported! |
|
@@ -40,13 +39,6 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver { |
|
|
return SENDGRID_SMTP.equals(serviceHost); |
|
|
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, |
|
|
@Override @NonNull public CloudService setupDelegatedCloudService(@NonNull final BubbleConfiguration configuration, |
|
|
@NonNull final CloudService parentService, |
|
|
@NonNull final CloudService parentService, |
|
|
@NonNull final CloudService delegatedService) { |
|
|
@NonNull final CloudService delegatedService) { |
|
@@ -62,18 +54,17 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver { |
|
|
|
|
|
|
|
|
final Account accountWithDelegate = configuration.getBean(AccountDAO.class) |
|
|
final Account accountWithDelegate = configuration.getBean(AccountDAO.class) |
|
|
.findByUuid(delegatedService.getAccount()); |
|
|
.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); |
|
|
String password = generatePassword(MIN_KEY_LENGTH, MIN_DISTINCT_LENGTH); |
|
|
final CreateSubuserRequest data = new CreateSubuserRequest(user, accountWithDelegate.getEmail(), password, |
|
|
final CreateSubuserRequest data = new CreateSubuserRequest(user, accountWithDelegate.getEmail(), password, |
|
|
new String[]{ sgIp }); |
|
|
new String[]{ sgIp }); |
|
|
doRequest("subusers", HttpMethods.POST, json(data)); |
|
|
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; |
|
|
return delegatedService; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -90,8 +81,10 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver { |
|
|
.findByUuid(parentServiceUuid); |
|
|
.findByUuid(parentServiceUuid); |
|
|
if (parentService == null) return; |
|
|
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) { |
|
|
private String doRequest(@NonNull final String uri, @NonNull final String method, @Nullable final String json) { |
|
|