From 59fbdb47e936adc639600748002cb1aab627db69 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Fri, 17 Jan 2020 22:42:59 -0500 Subject: [PATCH] clean up activation codepath. config.paymentsEnabled is now a derived property --- bin/bpost | 4 ++-- bin/bput | 4 ++-- .../cloud/compute/vultr/VultrDriver.java | 2 +- .../bubble/dao/cloud/CloudServiceDAO.java | 23 ++++++++++++++++++- .../java/bubble/model/cloud/CloudService.java | 1 + .../bubble/server/BubbleConfiguration.java | 22 +++++++++++++----- .../service/boot/ActivationService.java | 3 +-- .../src/main/resources/bubble-config.yml | 1 - .../src/test/resources/test-bubble-config.yml | 1 - 9 files changed, 45 insertions(+), 16 deletions(-) diff --git a/bin/bpost b/bin/bpost index 92b5b77c..abc374fa 100755 --- a/bin/bpost +++ b/bin/bpost @@ -4,10 +4,10 @@ # # Usage: # -# bpost path [file] [options] +# bpost path file [options] # -# path : an API path # file : a JSON file to POST. To read from stdin, specify the file as - +# path : an API path # options : bscript options, see bubble.main.BubbleScriptOptions (and parent classes) for more info # # Environment variables diff --git a/bin/bput b/bin/bput index c6de54c5..a1651f0b 100755 --- a/bin/bput +++ b/bin/bput @@ -4,10 +4,10 @@ # # Usage: # -# bput path [file] [options] +# bput path file [options] # # path : an API path -# file : a JSON file to POST. To read from stdin, specify the file as - +# file : a JSON file to PUT. To read from stdin, specify the file as - # options : bscript options, see bubble.main.BubbleScriptOptions (and parent classes) for more info # # Environment variables diff --git a/bubble-server/src/main/java/bubble/cloud/compute/vultr/VultrDriver.java b/bubble-server/src/main/java/bubble/cloud/compute/vultr/VultrDriver.java index 594fd863..e351c6ec 100644 --- a/bubble-server/src/main/java/bubble/cloud/compute/vultr/VultrDriver.java +++ b/bubble-server/src/main/java/bubble/cloud/compute/vultr/VultrDriver.java @@ -81,7 +81,7 @@ public class VultrDriver extends ComputeServiceDriverBase { @Override public void postSetup() { if (credentials != null && credentials.hasParam(API_KEY_HEADER) && credentials.getParam(API_KEY_HEADER).contains("{{")) { - final String apiKey = HandlebarsUtil.apply(getHandlebars(), credentials.getParam(API_KEY_HEADER), configuration.getEnvCtx()); + final String apiKey = configuration.applyHandlebars(credentials.getParam(API_KEY_HEADER)); credentials.setParam(API_KEY_HEADER, apiKey); } super.postSetup(); diff --git a/bubble-server/src/main/java/bubble/dao/cloud/CloudServiceDAO.java b/bubble-server/src/main/java/bubble/dao/cloud/CloudServiceDAO.java index 43141a01..126e1603 100644 --- a/bubble-server/src/main/java/bubble/dao/cloud/CloudServiceDAO.java +++ b/bubble-server/src/main/java/bubble/dao/cloud/CloudServiceDAO.java @@ -1,10 +1,12 @@ package bubble.dao.cloud; +import bubble.cloud.CloudServiceType; import bubble.cloud.storage.local.LocalStorageDriver; +import bubble.dao.account.AccountDAO; import bubble.dao.account.AccountOwnedTemplateDAO; +import bubble.model.account.Account; import bubble.model.cloud.BubbleNetwork; import bubble.model.cloud.CloudService; -import bubble.cloud.CloudServiceType; import bubble.server.BubbleConfiguration; import org.cobbzilla.wizard.validation.ValidationResult; import org.hibernate.criterion.Order; @@ -21,6 +23,7 @@ import static org.cobbzilla.wizard.resources.ResourceUtil.invalidEx; @Repository public class CloudServiceDAO extends AccountOwnedTemplateDAO { + @Autowired private AccountDAO accountDAO; @Autowired private BubbleConfiguration configuration; @Override public Order getDefaultSortOrder() { return Order.desc("priority"); } @@ -37,6 +40,12 @@ public class CloudServiceDAO extends AccountOwnedTemplateDAO { } } } + if (cloud.hasCredentials() && cloud.getCredentialsJson().contains("{{") && cloud.getCredentialsJson().contains("}}")) { + cloud.setCredentialsJson(configuration.applyHandlebars(cloud.getCredentialsJson())); + } + if (cloud.hasDriverConfig() && cloud.getDriverConfigJson().contains("{{") && cloud.getDriverConfigJson().contains("}}")) { + cloud.setDriverConfigJson(configuration.applyHandlebars(cloud.getDriverConfigJson())); + } return super.preCreate(cloud); } @@ -45,6 +54,13 @@ public class CloudServiceDAO extends AccountOwnedTemplateDAO { final ValidationResult errors = testDriver(cloud, configuration); if (errors.isInvalid()) throw invalidEx(errors); } + if (cloud.getType() == CloudServiceType.payment + && cloud.template() + && cloud.enabled() + && !configuration.paymentsEnabled()) { + // a public template for a payment cloud has been added, and payments were not enabled -- now they are + configuration.refreshPublicSystemConfigs(); + } return super.postCreate(cloud, context); } @@ -83,4 +99,9 @@ public class CloudServiceDAO extends AccountOwnedTemplateDAO { return found.isEmpty() ? null : found.get(0); } + public boolean paymentsEnabled() { + final Account admin = accountDAO.findFirstAdmin(); + if (admin == null) return false; + return !findPublicTemplatesByType(admin.getUuid(), CloudServiceType.payment).isEmpty(); + } } diff --git a/bubble-server/src/main/java/bubble/model/cloud/CloudService.java b/bubble-server/src/main/java/bubble/model/cloud/CloudService.java index 696a81fe..742bc118 100644 --- a/bubble-server/src/main/java/bubble/model/cloud/CloudService.java +++ b/bubble-server/src/main/java/bubble/model/cloud/CloudService.java @@ -143,6 +143,7 @@ public class CloudService extends IdentifiableBaseParentEntity implements Accoun @Size(max=100000, message="err.driverConfigJson.length") @Type(type=ENCRYPTED_STRING) @Column(columnDefinition="varchar("+(100000+ENC_PAD)+")") @JsonIgnore @Getter @Setter private String driverConfigJson; + public boolean hasDriverConfig () { return !empty(driverConfigJson); } @Transient public JsonNode getDriverConfig () { return json(driverConfigJson, JsonNode.class); } public CloudService setDriverConfig (JsonNode node) { return setDriverConfigJson(node == null ? null : json(node)); } diff --git a/bubble-server/src/main/java/bubble/server/BubbleConfiguration.java b/bubble-server/src/main/java/bubble/server/BubbleConfiguration.java index ddd0c55f..2e401688 100644 --- a/bubble-server/src/main/java/bubble/server/BubbleConfiguration.java +++ b/bubble-server/src/main/java/bubble/server/BubbleConfiguration.java @@ -5,6 +5,7 @@ import bubble.BubbleHandlebars; import bubble.client.BubbleApiClient; import bubble.cloud.CloudServiceDriver; import bubble.dao.account.AccountDAO; +import bubble.dao.cloud.CloudServiceDAO; import bubble.model.cloud.BubbleNetwork; import bubble.model.cloud.BubbleNode; import bubble.server.listener.BubbleFirstTimeListener; @@ -20,6 +21,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.map.DefaultedMap; import org.apache.commons.lang.ArrayUtils; import org.cobbzilla.util.collection.MapBuilder; +import org.cobbzilla.util.handlebars.HandlebarsUtil; import org.cobbzilla.util.handlebars.HasHandlebars; import org.cobbzilla.util.http.ApiConnectionInfo; import org.cobbzilla.util.io.FileUtil; @@ -195,11 +197,10 @@ public class BubbleConfiguration extends PgRestServerConfiguration @Getter @Setter private LegalInfo legal = new LegalInfo(); - @Getter @Setter private Boolean paymentsEnabled = false; - public boolean paymentsEnabled() { return paymentsEnabled != null && paymentsEnabled; } - @Override @JsonIgnore public Handlebars getHandlebars() { return BubbleHandlebars.instance.getHandlebars(); } + public String applyHandlebars(String val) { return HandlebarsUtil.apply(getHandlebars(), val, getEnvCtx()); } + public ApiClientBase newApiClient() { return new BubbleApiClient(new ApiConnectionInfo(getLoopbackApiBase())); } public String getVersion () { @@ -259,12 +260,13 @@ public class BubbleConfiguration extends PgRestServerConfiguration final BubbleNode thisNode = getThisNode(); final BubbleNetwork thisNetwork = getThisNetwork(); final AccountDAO accountDAO = getBean(AccountDAO.class); + final CloudServiceDAO cloudDAO = getBean(CloudServiceDAO.class); final ActivationService activationService = getBean(ActivationService.class); publicSystemConfigs.set(MapBuilder.build(new Object[][]{ {TAG_ALLOW_REGISTRATION, thisNetwork == null ? null : thisNetwork.getBooleanTag(TAG_ALLOW_REGISTRATION, false)}, {TAG_NETWORK_UUID, thisNetwork == null ? null : thisNetwork.getUuid()}, {TAG_SAGE_LAUNCHER, thisNetwork == null || isSageLauncher()}, - {TAG_PAYMENTS_ENABLED, paymentsEnabled()}, + {TAG_PAYMENTS_ENABLED, cloudDAO.paymentsEnabled()}, {TAG_CLOUD_DRIVERS, getCloudDriverClasses()}, {TAG_ENTITY_CLASSES, getSortedSimpleEntityClassMap()}, {TAG_LOCALES, getAllLocales()}, @@ -277,12 +279,17 @@ public class BubbleConfiguration extends PgRestServerConfiguration } } - // called after activation, because now thisNetwork will be defined. otherwise it remains unchanged + // called after activation, because now thisNetwork will be defined public void refreshPublicSystemConfigs () { synchronized (publicSystemConfigs) { publicSystemConfigs.set(null); } background(this::getPublicSystemConfigs); } + public boolean paymentsEnabled () { + final Object peValue = getPublicSystemConfigs().get(TAG_PAYMENTS_ENABLED); + return peValue != null && Boolean.parseBoolean(peValue.toString()); + } + @Getter @Setter private String[] disallowedCountries; public boolean hasDisallowedCountries() { return !empty(disallowedCountries); } @@ -294,11 +301,14 @@ public class BubbleConfiguration extends PgRestServerConfiguration return false; } + @Getter @Setter private Boolean defaultPaymentModelsEnabled; + public boolean defaultPaymentModelsEnabled() { return defaultPaymentModelsEnabled != null && defaultPaymentModelsEnabled; } + @JsonIgnore @Getter(lazy=true) private final List defaultCloudModels = initDefaultCloudModels(); private List initDefaultCloudModels () { final List defaults = new ArrayList<>(); defaults.add("models/defaults/cloudService.json"); - if (paymentsEnabled()) defaults.add("models/defaults/cloudService_payment.json"); + if (defaultPaymentModelsEnabled()) defaults.add("models/defaults/cloudService_payment.json"); if (testMode()) defaults.addAll(getTestCloudModels()); return defaults; } diff --git a/bubble-server/src/main/java/bubble/service/boot/ActivationService.java b/bubble-server/src/main/java/bubble/service/boot/ActivationService.java index 95cd7445..9e04095c 100644 --- a/bubble-server/src/main/java/bubble/service/boot/ActivationService.java +++ b/bubble-server/src/main/java/bubble/service/boot/ActivationService.java @@ -17,7 +17,6 @@ import com.fasterxml.jackson.databind.JsonNode; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.cobbzilla.util.collection.ArrayUtil; -import org.cobbzilla.util.handlebars.HandlebarsUtil; import org.cobbzilla.wizard.api.CrudOperation; import org.cobbzilla.wizard.client.ApiClientBase; import org.cobbzilla.wizard.model.Identifiable; @@ -260,7 +259,7 @@ public class ActivationService { } private CloudService[] loadCloudServices(final String modelPath) { - final String cloudsJson = HandlebarsUtil.apply(configuration.getHandlebars(), stream2string(modelPath), configuration.getEnvCtx()); + final String cloudsJson = configuration.applyHandlebars(stream2string(modelPath)); final JsonNode cloudsArrayNode = json(cloudsJson, JsonNode.class); return scrubSpecial(cloudsArrayNode, CloudService.class); } diff --git a/bubble-server/src/main/resources/bubble-config.yml b/bubble-server/src/main/resources/bubble-config.yml index 2e99e0d4..eec9c185 100644 --- a/bubble-server/src/main/resources/bubble-config.yml +++ b/bubble-server/src/main/resources/bubble-config.yml @@ -72,4 +72,3 @@ letsencryptEmail: {{LETSENCRYPT_EMAIL}} localStorageDir: {{LOCALSTORAGE_BASE_DIR}} disallowedCountries: {{DISALLOWED_COUNTRIES}} -paymentsEnabled: {{BUBBLE_PAYMENTS_ENABLED}} diff --git a/bubble-server/src/test/resources/test-bubble-config.yml b/bubble-server/src/test/resources/test-bubble-config.yml index 93b33b90..a5af8167 100644 --- a/bubble-server/src/test/resources/test-bubble-config.yml +++ b/bubble-server/src/test/resources/test-bubble-config.yml @@ -69,4 +69,3 @@ letsencryptEmail: {{LETSENCRYPT_EMAIL}} localStorageDir: {{LOCALSTORAGE_BASE_DIR}} disallowedCountries: {{DISALLOWED_COUNTRIES}} -paymentsEnabled: {{BUBBLE_PAYMENTS_ENABLED}} \ No newline at end of file