From d1de33b01910e52d11cd5087675532743ca263b2 Mon Sep 17 00:00:00 2001 From: Abhijeet <41686026+abhvsn@users.noreply.github.com> Date: Tue, 20 Aug 2024 18:36:32 +0530 Subject: [PATCH] chore: Add cleanup and update stage for tenant at the server restart (#35786) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description PR to add the cleanup and update default tenant at the server restart. This was required because whenever we update tenant via migrations the result gets reverted because the cached tenant is not getting updated as we generally end up updating only the MongoDB state and not the cached entry. /test Sanity ### :mag: Cypress test results > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: > Commit: e288cfe1084bdbf2104a656bf60eb9c24708fe77 > Cypress dashboard. > Tags: `@tag.Sanity` > Spec: >
Tue, 20 Aug 2024 13:01:42 UTC ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No ## Summary by CodeRabbit - **New Features** - Implemented a mechanism to refresh tenant policies and manage cache cleanup during server restarts, ensuring accurate policy enforcement for multi-tenant applications. --- .../server/configurations/TenantConfig.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/TenantConfig.java diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/TenantConfig.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/TenantConfig.java new file mode 100644 index 0000000000..544430ca03 --- /dev/null +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/TenantConfig.java @@ -0,0 +1,45 @@ +package com.appsmith.server.configurations; + +import com.appsmith.server.constants.FieldName; +import com.appsmith.server.domains.Tenant; +import com.appsmith.server.helpers.CollectionUtils; +import com.appsmith.server.repositories.CacheableRepositoryHelper; +import com.appsmith.server.repositories.TenantRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.annotation.Configuration; +import reactor.core.publisher.Mono; + +import static com.appsmith.external.models.BaseDomain.policySetToMap; + +@Configuration +@RequiredArgsConstructor +@Slf4j +public class TenantConfig implements ApplicationListener { + + private final TenantRepository tenantRepository; + private final CacheableRepositoryHelper cachableRepositoryHelper; + + // Method to cleanup the cache and update the default tenant policies if the policyMap is empty. This will make sure + // cache will be updated if we update the tenant via startup DB migrations. + // As we have mocked the TenantService in the tests, we had to manually evict the cache and save the object to DB + private Mono cleanupAndUpdateRefreshDefaultTenantPolicies() { + log.debug("Cleaning up and updating default tenant policies on server startup"); + return tenantRepository.findBySlug(FieldName.DEFAULT).flatMap(tenant -> { + if (CollectionUtils.isNullOrEmpty(tenant.getPolicyMap())) { + tenant.setPolicyMap(policySetToMap(tenant.getPolicies())); + return cachableRepositoryHelper + .evictCachedTenant(tenant.getId()) + .then(tenantRepository.save(tenant)); + } + return Mono.just(tenant); + }); + } + + @Override + public void onApplicationEvent(ApplicationStartedEvent event) { + cleanupAndUpdateRefreshDefaultTenantPolicies().block(); + } +}