From 0246563bd2643ebbec4209f321bd8e5dd2c64202 Mon Sep 17 00:00:00 2001 From: Abhijeet <41686026+abhvsn@users.noreply.github.com> Date: Thu, 10 Jun 2021 10:52:23 +0530 Subject: [PATCH] Bugfix for applications with MongoDB datasources authenticated with SRV string (#4944) * Avoid updating authenticationDTO for mongo-plugin with SRV if the authentication object is already present --- .../com/external/plugins/MongoPlugin.java | 18 ++++++++++----- .../solutions/ExamplesOrganizationCloner.java | 6 +---- .../ImportExportApplicationService.java | 22 +++++++++---------- .../ExamplesOrganizationClonerTests.java | 2 ++ 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/app/server/appsmith-plugins/mongoPlugin/src/main/java/com/external/plugins/MongoPlugin.java b/app/server/appsmith-plugins/mongoPlugin/src/main/java/com/external/plugins/MongoPlugin.java index 1bbeff0b17..bf780549a6 100644 --- a/app/server/appsmith-plugins/mongoPlugin/src/main/java/com/external/plugins/MongoPlugin.java +++ b/app/server/appsmith-plugins/mongoPlugin/src/main/java/com/external/plugins/MongoPlugin.java @@ -688,6 +688,7 @@ public class MongoPlugin extends BasePlugin { public Set validateDatasource(DatasourceConfiguration datasourceConfiguration) { Set invalids = new HashSet<>(); List properties = datasourceConfiguration.getProperties(); + DBAuth authentication = (DBAuth) datasourceConfiguration.getAuthentication(); if (isUsingURI(datasourceConfiguration)) { if (!hasNonEmptyURI(datasourceConfiguration)) { invalids.add("'Mongo Connection String URI' field is empty. Please edit the 'Mongo Connection " + @@ -702,11 +703,10 @@ public class MongoPlugin extends BasePlugin { if (extractedInfo == null) { invalids.add("Mongo Connection String URI does not seem to be in the correct format. " + "Please check the URI once."); - } else { + } else if (!isAuthenticated(authentication, mongoUri)) { String mongoUriWithHiddenPassword = buildURIfromExtractedInfo(extractedInfo, "****"); properties.get(DATASOURCE_CONFIG_MONGO_URI_PROPERTY_INDEX).setValue(mongoUriWithHiddenPassword); - DBAuth authentication = datasourceConfiguration.getAuthentication() == null ? - new DBAuth() : (DBAuth) datasourceConfiguration.getAuthentication(); + authentication = (authentication == null) ? new DBAuth(): authentication; authentication.setUsername((String) extractedInfo.get(KEY_USERNAME)); authentication.setPassword((String) extractedInfo.get(KEY_PASSWORD)); authentication.setDatabaseName((String) extractedInfo.get(KEY_URI_DBNAME)); @@ -746,8 +746,7 @@ public class MongoPlugin extends BasePlugin { " directly."); } } - - DBAuth authentication = (DBAuth) datasourceConfiguration.getAuthentication(); + if (authentication != null) { DBAuth.Type authType = authentication.getAuthType(); @@ -1013,5 +1012,14 @@ public class MongoPlugin extends BasePlugin { return object; } + + private static boolean isAuthenticated(DBAuth authentication, String mongoUri) { + if (authentication != null && authentication.getUsername() != null + && authentication.getPassword() != null && mongoUri.contains("****")) { + + return true; + } + return false; + } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ExamplesOrganizationCloner.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ExamplesOrganizationCloner.java index aaaa2f72db..948022ac68 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ExamplesOrganizationCloner.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ExamplesOrganizationCloner.java @@ -21,7 +21,6 @@ import com.appsmith.server.repositories.OrganizationRepository; import com.appsmith.server.services.ApplicationPageService; import com.appsmith.server.services.ApplicationService; import com.appsmith.server.services.ConfigService; -import com.appsmith.server.services.DatasourceContextService; import com.appsmith.server.services.DatasourceService; import com.appsmith.server.services.LayoutActionService; import com.appsmith.server.services.NewActionService; @@ -60,7 +59,6 @@ public class ExamplesOrganizationCloner { private final UserService userService; private final ApplicationService applicationService; private final ApplicationPageService applicationPageService; - private final DatasourceContextService datasourceContextService; private final NewPageRepository newPageRepository; private final NewActionService newActionService; private final LayoutActionService layoutActionService; @@ -403,9 +401,7 @@ public class ExamplesOrganizationCloner { .switchIfEmpty(Mono.defer(() -> { // No matching existing datasource found, so create a new one. makePristine(templateDatasource); - templateDatasource.setOrganizationId(toOrganizationId); - return createSuffixedDatasource(templateDatasource); })); }); @@ -458,7 +454,7 @@ public class ExamplesOrganizationCloner { throw error; }); } - + public void makePristine(BaseDomain domain) { // Set the ID to null for this domain object so that it is saved a new document in the database (as opposed to // updating an existing document). If it contains any policies, they are also reset. diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ImportExportApplicationService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ImportExportApplicationService.java index 9e76603ce7..fe4a597d75 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ImportExportApplicationService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ImportExportApplicationService.java @@ -79,7 +79,7 @@ public class ImportExportApplicationService { private static final Set ALLOWED_CONTENT_TYPES = Set.of(MediaType.APPLICATION_JSON); public final String INVALID_JSON_FILE = "invalid json file"; private enum PublishType { - UNPUBLISH, PUBLISH + UNPUBLISHED, PUBLISHED } public Mono exportApplicationById(String applicationId) { @@ -141,7 +141,7 @@ public class ImportExportApplicationService { if (newPage.getUnpublishedPage() != null) { pageIdToNameMap.put( - newPage.getId() + PublishType.UNPUBLISH, newPage.getUnpublishedPage().getName() + newPage.getId() + PublishType.UNPUBLISHED, newPage.getUnpublishedPage().getName() ); PageDTO unpublishedPageDTO = newPage.getUnpublishedPage(); if (StringUtils.equals( @@ -160,7 +160,7 @@ public class ImportExportApplicationService { if (newPage.getPublishedPage() != null) { pageIdToNameMap.put( - newPage.getId() + PublishType.PUBLISH, newPage.getPublishedPage().getName() + newPage.getId() + PublishType.PUBLISHED, newPage.getPublishedPage().getName() ); PageDTO publishedPageDTO = newPage.getPublishedPage(); if (applicationJson.getPublishedDefaultPageName() != null && @@ -215,11 +215,11 @@ public class ImportExportApplicationService { } if (newAction.getUnpublishedAction() != null) { ActionDTO actionDTO = newAction.getUnpublishedAction(); - actionDTO.setPageId(pageIdToNameMap.get(actionDTO.getPageId() + PublishType.UNPUBLISH)); + actionDTO.setPageId(pageIdToNameMap.get(actionDTO.getPageId() + PublishType.UNPUBLISHED)); } if (newAction.getPublishedAction() != null) { ActionDTO actionDTO = newAction.getPublishedAction(); - actionDTO.setPageId(pageIdToNameMap.get(actionDTO.getPageId() + PublishType.PUBLISH)); + actionDTO.setPageId(pageIdToNameMap.get(actionDTO.getPageId() + PublishType.PUBLISHED)); } }); applicationJson @@ -363,8 +363,8 @@ public class ImportExportApplicationService { .flatMap(savedApp -> { importedApplication.setId(savedApp.getId()); Map> applicationPages = Map.of( - PublishType.UNPUBLISH, new ArrayList<>(), - PublishType.PUBLISH, new ArrayList<>() + PublishType.UNPUBLISHED, new ArrayList<>(), + PublishType.PUBLISHED, new ArrayList<>() ); return importAndSavePages( @@ -396,16 +396,16 @@ public class ImportExportApplicationService { publishedAppPage.setId(newPage.getId()); pageNameMap.put(newPage.getPublishedPage().getName(), newPage); } - applicationPages.get(PublishType.UNPUBLISH).add(unpublishedAppPage); - applicationPages.get(PublishType.PUBLISH).add(publishedAppPage); + applicationPages.get(PublishType.UNPUBLISHED).add(unpublishedAppPage); + applicationPages.get(PublishType.PUBLISHED).add(publishedAppPage); return applicationPages; }) .then() .thenReturn(applicationPages); }) .flatMap(applicationPageMap -> { - importedApplication.setPages(applicationPageMap.get(PublishType.UNPUBLISH)); - importedApplication.setPublishedPages(applicationPageMap.get(PublishType.PUBLISH)); + importedApplication.setPages(applicationPageMap.get(PublishType.UNPUBLISHED)); + importedApplication.setPublishedPages(applicationPageMap.get(PublishType.PUBLISHED)); importedNewActionList.forEach(newAction -> { NewPage parentPage = new NewPage(); diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/ExamplesOrganizationClonerTests.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/ExamplesOrganizationClonerTests.java index bf54cf7efc..85c12176ab 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/ExamplesOrganizationClonerTests.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/ExamplesOrganizationClonerTests.java @@ -473,6 +473,7 @@ public class ExamplesOrganizationClonerTests { final Datasource ds1 = new Datasource(); ds1.setName("datasource 1"); ds1.setOrganizationId(organization.getId()); + ds1.setPluginId(installedPlugin.getId()); final DatasourceConfiguration datasourceConfiguration = new DatasourceConfiguration(); ds1.setDatasourceConfiguration(datasourceConfiguration); datasourceConfiguration.setUrl("http://httpbin.org/get"); @@ -483,6 +484,7 @@ public class ExamplesOrganizationClonerTests { final Datasource ds2 = new Datasource(); ds2.setName("datasource 2"); ds2.setOrganizationId(organization.getId()); + ds2.setPluginId(installedPlugin.getId()); ds2.setDatasourceConfiguration(new DatasourceConfiguration()); DBAuth auth = new DBAuth(); auth.setPassword("answer-to-life");