Sfoglia il codice sorgente

Delete Sendgrid Subusers on account deletion

pull/61/head
Kristijan Mitrovic 4 anni fa
parent
commit
a02284a911
4 ha cambiato i file con 20 aggiunte e 16 eliminazioni
  1. +2
    -1
      bubble-server/src/main/java/bubble/cloud/CloudServiceDriver.java
  2. +9
    -13
      bubble-server/src/main/java/bubble/cloud/email/SendgridSmtpEmailDriver.java
  3. +6
    -0
      bubble-server/src/main/java/bubble/dao/account/AccountDAO.java
  4. +3
    -2
      bubble-server/src/main/java/bubble/dao/cloud/CloudServiceDAO.java

+ 2
- 1
bubble-server/src/main/java/bubble/cloud/CloudServiceDriver.java Vedi File

@@ -5,6 +5,7 @@
package bubble.cloud;

import bubble.BubbleHandlebars;
import bubble.dao.cloud.CloudServiceDAO;
import bubble.model.cloud.CloudCredentials;
import bubble.model.cloud.CloudService;
import bubble.server.BubbleConfiguration;
@@ -37,7 +38,7 @@ public interface CloudServiceDriver {
.setTemplate(false);
}

default void postServiceDelete(@NonNull final CloudService service) {
default void postServiceDelete(@NonNull final CloudServiceDAO serviceDAO, @NonNull final CloudService service) {
// noop
}



+ 9
- 13
bubble-server/src/main/java/bubble/cloud/email/SendgridSmtpEmailDriver.java Vedi File

@@ -47,7 +47,7 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver {
return super.setupDelegatedCloudService(configuration, parentService, delegatedService);
}

final String sgIpsJson = doRequest("ips", HttpMethods.GET, null);
final String sgIpsJson = doRequest("ips", HttpMethods.GET, null, parentService);
final List<Map<String, Object>> sgIps = json(sgIpsJson, List.class);
if (empty(sgIps)) die("No IPs set for the specified main Sendgrid user");
final String sgIp = (String) sgIps.get(0).get("ip");
@@ -58,7 +58,7 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver {
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));
doRequest("subusers", HttpMethods.POST, json(data), parentService);

final CloudCredentials credentials = delegatedService.getCredentials();
credentials.setParam(PARAM_USER, user)
@@ -68,32 +68,28 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver {
return delegatedService;
}

@Override public void postServiceDelete(@NonNull final CloudService service) {
// TODO: there must be configuration present here. If this happens, try some other way:
if (getConfiguration() == null) {
die("postServiceDelete: Cannot do this action with server configuration set within");
}

@Override public void postServiceDelete(@NonNull final CloudServiceDAO serviceDAO,
@NonNull final CloudService service) {
final String parentServiceUuid = service.getCredentials().getParam(PARAM_PARENT_SERVICE);
if (parentServiceUuid == null) return;

final CloudService parentService = getConfiguration().getBean(CloudServiceDAO.class)
.findByUuid(parentServiceUuid);
final CloudService parentService = serviceDAO.findByUuid(parentServiceUuid);
if (parentService == null) return;

final String user = service.getCredentials().getParam(PARAM_USER);
if (empty(user)) return;

doRequest("subusers/" + user, HttpMethods.DELETE, null);
doRequest("subusers/" + user, HttpMethods.DELETE, null, parentService);
}

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,
@NonNull CloudService parentService) {
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));
.setHeader(AUTHORIZATION, "Bearer " + parentService.getCredentials().getParam(PARAM_PASSWORD));

final HttpResponseBean response;
try {


+ 6
- 0
bubble-server/src/main/java/bubble/dao/account/AccountDAO.java Vedi File

@@ -413,6 +413,12 @@ public class AccountDAO extends AbstractCRUDDAO<Account> implements SqlViewSearc
final List<BubbleNetwork> networks = networkDAO.findByAccount(account.getUuid());
networks.forEach(n -> networkDAO.delete(n.getUuid())); // deleting all networks dependency objects

// Separate deletion of dependant CloudServices to fully respect their post delete methods
final List<CloudService> services = cloudDAO.findByAccount(account.getUuid());
for (final CloudService cs : services) {
cs.getDriver().postServiceDelete(cloudDAO, cs);
}

configuration.deleteDependencies(account);
log.info("delete: finished deleting account-dependent objects - " + currentThread().getName());



+ 3
- 2
bubble-server/src/main/java/bubble/dao/cloud/CloudServiceDAO.java Vedi File

@@ -152,6 +152,7 @@ public class CloudServiceDAO extends AccountOwnedTemplateDAO<CloudService> {

@Override public int bulkDeleteWhere(@NonNull String whereClause, @Nullable Map<String, Object> parameters) {
// TODO for these maybe an outside cron would be better solution. BulkDelete is used here to be fast.
// For now this postServiceDelete is called within a single place where this method is used - Account Deletion.
log.warn("Not calling postServiceDelete for services deleted in this way");
return super.bulkDeleteWhere(whereClause, parameters);
}
@@ -161,7 +162,7 @@ public class CloudServiceDAO extends AccountOwnedTemplateDAO<CloudService> {

final CloudService cs = findByUuid(uuid);
if (cs == null) return;
cs.getDriver().postServiceDelete(cs);
cs.getDriver().postServiceDelete(this, cs);

super.delete(uuid);
}
@@ -170,7 +171,7 @@ public class CloudServiceDAO extends AccountOwnedTemplateDAO<CloudService> {
if (empty(entities)) return;

for (final CloudService cs : entities) {
cs.getDriver().postServiceDelete(cs);
cs.getDriver().postServiceDelete(this, cs);
}

super.delete(entities);


Caricamento…
Annulla
Salva