|
|
@@ -6,26 +6,28 @@ import bubble.model.account.Account; |
|
|
|
import bubble.model.cloud.CloudCredentials; |
|
|
|
import bubble.model.cloud.CloudService; |
|
|
|
import bubble.server.BubbleConfiguration; |
|
|
|
import com.sendgrid.Method; |
|
|
|
import com.sendgrid.Request; |
|
|
|
import com.sendgrid.Response; |
|
|
|
import com.sendgrid.SendGrid; |
|
|
|
import lombok.*; |
|
|
|
import org.cobbzilla.util.http.HttpMethods; |
|
|
|
import org.cobbzilla.util.http.HttpRequestBean; |
|
|
|
import org.cobbzilla.util.http.HttpResponseBean; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
|
|
import javax.annotation.Nullable; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import static bubble.cloud.storage.StorageCryptStream.MIN_DISTINCT_LENGTH; |
|
|
|
import static bubble.cloud.storage.StorageCryptStream.MIN_KEY_LENGTH; |
|
|
|
import static java.net.HttpURLConnection.HTTP_NO_CONTENT; |
|
|
|
import static java.net.HttpURLConnection.HTTP_OK; |
|
|
|
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.json.JsonUtil.EMPTY_JSON; |
|
|
|
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; |
|
|
|
|
|
|
|
public class SendgridSmtpEmailDriver extends SmtpEmailDriver { |
|
|
|
public static final String SG_API_BASE = "https://api.sendgrid.com/v3/"; |
|
|
|
|
|
|
|
private static final String PARAM_PARENT_SERVICE = "parentService"; |
|
|
|
|
|
|
@@ -49,26 +51,12 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver { |
|
|
|
return super.setupDelegatedCloudService(configuration, parentService, delegatedService); |
|
|
|
} |
|
|
|
|
|
|
|
final SendGrid sg = new SendGrid(parentCredentials.getParam(PARAM_PASSWORD)); |
|
|
|
final Account accountWithDelegate = accountDAO.findByUuid(delegatedService.getAccount()); |
|
|
|
final String user = sgUsername(delegatedService); |
|
|
|
String password = generatePassword(MIN_KEY_LENGTH, MIN_DISTINCT_LENGTH); |
|
|
|
final CreateSubuserRequest data = new CreateSubuserRequest(user, accountWithDelegate.getEmail(), password, |
|
|
|
new String[]{}); |
|
|
|
final Request req = new Request(); |
|
|
|
req.setMethod(Method.POST); |
|
|
|
req.setEndpoint("subusers"); |
|
|
|
req.setBody(json(data)); |
|
|
|
|
|
|
|
final Response res; |
|
|
|
try { |
|
|
|
res = sg.api(req); |
|
|
|
} catch (IOException e) { |
|
|
|
return die("Cannot create SendGrid Subuser", e); |
|
|
|
} |
|
|
|
if (res.getStatusCode() != HTTP_OK) { |
|
|
|
return die("Wrong response when creating SendGrid Subuser: " + res.getStatusCode() + " : " + res.getBody()); |
|
|
|
} |
|
|
|
doRequest("subusers", HttpMethods.POST, json(data)); |
|
|
|
|
|
|
|
delegatedService.setDelegated(null).setTemplate(false); |
|
|
|
delegatedService.getCredentials() |
|
|
@@ -86,24 +74,29 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver { |
|
|
|
final CloudService parentService = serviceDAO.findByUuid(parentServiceUuid); |
|
|
|
if (parentService == null) return; |
|
|
|
|
|
|
|
final SendGrid sg = new SendGrid(parentService.getCredentials().getParam(PARAM_PASSWORD)); |
|
|
|
final String sgUserToDelete = sgUsername(service); |
|
|
|
doRequest("subusers/" + sgUserToDelete, HttpMethods.DELETE, null); |
|
|
|
} |
|
|
|
|
|
|
|
final Request req = new Request(); |
|
|
|
req.setMethod(Method.DELETE); |
|
|
|
req.setEndpoint("subusers/" + sgUserToDelete); |
|
|
|
req.setBody(EMPTY_JSON); |
|
|
|
private String doRequest(@NonNull final String uri, @NonNull final String method, @Nullable final String json) { |
|
|
|
final HttpRequestBean request = new HttpRequestBean(); |
|
|
|
request.setMethod(method) |
|
|
|
.setUri(SG_API_BASE + uri) |
|
|
|
.setEntity(json) |
|
|
|
.setHeader(CONTENT_TYPE, APPLICATION_JSON) |
|
|
|
.setHeader(AUTHORIZATION, "Bearer " + getCredentials().getParam(PARAM_PASSWORD)); |
|
|
|
|
|
|
|
final Response res; |
|
|
|
final HttpResponseBean response; |
|
|
|
try { |
|
|
|
res = sg.api(req); |
|
|
|
response = getResponse(request); |
|
|
|
} catch (IOException e) { |
|
|
|
die("Cannot delete SendGrid Subuser " + sgUserToDelete, e); |
|
|
|
return; |
|
|
|
return die("doGet(" + uri + "): " + e.getMessage(), e); |
|
|
|
} |
|
|
|
if (res.getStatusCode() != HTTP_NO_CONTENT) { |
|
|
|
die("Wrong response when creating SendGrid Subuser: " + res.getStatusCode() + " : " + res.getBody()); |
|
|
|
if (!response.isOk()) { |
|
|
|
return die("doPost(" + uri + "): HTTP " + response.getStatus() + " : " + response.getEntityString()); |
|
|
|
} |
|
|
|
|
|
|
|
return response.getEntityString(); |
|
|
|
} |
|
|
|
|
|
|
|
@AllArgsConstructor @NoArgsConstructor |
|
|
@@ -113,4 +106,5 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver { |
|
|
|
@Getter @Setter private String password; |
|
|
|
@Getter @Setter private String[] ips; |
|
|
|
} |
|
|
|
|
|
|
|
} |