From 0d3e72028fd14adf67032656f6fb64165acf9ea7 Mon Sep 17 00:00:00 2001 From: sgiannopoulos Date: Fri, 5 Apr 2024 11:34:30 +0300 Subject: [PATCH 1/2] fix cache to use tenant --- .../AffiliationCacheService.java | 17 ++++++++++--- .../AuthorizationContentResolverImpl.java | 24 +++++++++++++++---- .../web/src/main/resources/config/cache.yml | 2 +- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/dmp-backend/core/src/main/java/eu/eudat/authorization/authorizationcontentresolver/AffiliationCacheService.java b/dmp-backend/core/src/main/java/eu/eudat/authorization/authorizationcontentresolver/AffiliationCacheService.java index 71f583cb5..755e94acd 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/authorization/authorizationcontentresolver/AffiliationCacheService.java +++ b/dmp-backend/core/src/main/java/eu/eudat/authorization/authorizationcontentresolver/AffiliationCacheService.java @@ -20,14 +20,16 @@ public class AffiliationCacheService extends CacheService keyParts = new HashMap<>(); keyParts.put("$user$", userId.toString().replace("-", "").toLowerCase(Locale.ROOT)); + keyParts.put("$tenant$", tenantId == null ? "" : userId.toString().replace("-", "").toLowerCase(Locale.ROOT)); keyParts.put("$entity$", entityId.toString().replace("-", "").toLowerCase(Locale.ROOT)); keyParts.put("$type$", entityType); return this.generateKey(keyParts); diff --git a/dmp-backend/core/src/main/java/eu/eudat/authorization/authorizationcontentresolver/AuthorizationContentResolverImpl.java b/dmp-backend/core/src/main/java/eu/eudat/authorization/authorizationcontentresolver/AuthorizationContentResolverImpl.java index 85bf84759..60fb36533 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/authorization/authorizationcontentresolver/AuthorizationContentResolverImpl.java +++ b/dmp-backend/core/src/main/java/eu/eudat/authorization/authorizationcontentresolver/AuthorizationContentResolverImpl.java @@ -3,6 +3,7 @@ package eu.eudat.authorization.authorizationcontentresolver; import eu.eudat.authorization.AffiliatedResource; import eu.eudat.authorization.PermissionNameProvider; import eu.eudat.commons.enums.IsActive; +import eu.eudat.commons.scope.tenant.TenantScope; import eu.eudat.commons.scope.user.UserScope; import eu.eudat.data.DescriptionEntity; import eu.eudat.data.DmpDescriptionTemplateEntity; @@ -19,6 +20,7 @@ import gr.cite.tools.fieldset.BaseFieldSet; import org.springframework.stereotype.Service; import org.springframework.web.context.annotation.RequestScope; +import javax.management.InvalidApplicationException; import java.util.*; import java.util.stream.Collectors; @@ -27,11 +29,13 @@ import java.util.stream.Collectors; public class AuthorizationContentResolverImpl implements AuthorizationContentResolver { private final QueryFactory queryFactory; private final UserScope userScope; + private final TenantScope tenantScope; private final AffiliationCacheService affiliationCacheService; private final PermissionNameProvider permissionNameProvider; - public AuthorizationContentResolverImpl(QueryFactory queryFactory, UserScope userScope, AffiliationCacheService affiliationCacheService, PermissionNameProvider permissionNameProvider) { + public AuthorizationContentResolverImpl(QueryFactory queryFactory, UserScope userScope, TenantScope tenantScope, AffiliationCacheService affiliationCacheService, PermissionNameProvider permissionNameProvider) { this.queryFactory = queryFactory; this.userScope = userScope; + this.tenantScope = tenantScope; this.affiliationCacheService = affiliationCacheService; this.permissionNameProvider = permissionNameProvider; } @@ -138,21 +142,31 @@ public class AuthorizationContentResolverImpl implements AuthorizationContentRes return affiliatedResources; } - private List getAffiliatedFromCache(List ids, UUID userId, Map affiliatedResources, String entityType){ + private List getAffiliatedFromCache(List ids, UUID userId, Map affiliatedResources, String entityType) { List idsToResolve = new ArrayList<>(); for (UUID id : ids){ - AffiliationCacheService.AffiliationCacheValue cacheValue = this.affiliationCacheService.lookup(this.affiliationCacheService.buildKey(userId, id, entityType)); + AffiliationCacheService.AffiliationCacheValue cacheValue = null; + try { + cacheValue = this.affiliationCacheService.lookup(this.affiliationCacheService.buildKey(this.tenantScope.isSet() ? this.tenantScope.getTenant(): null, userId, id, entityType)); + } catch (InvalidApplicationException e) { + throw new RuntimeException(e); + } if (cacheValue != null) affiliatedResources.put(id, cacheValue.getAffiliatedResource()); else idsToResolve.add(id); } return idsToResolve; } - private void ensureAffiliatedInCache(List idsToResolve, UUID userId, Map affiliatedResources, String entityType){ + private void ensureAffiliatedInCache(List idsToResolve, UUID userId, Map affiliatedResources, String entityType) { for (UUID id : idsToResolve){ AffiliatedResource affiliatedResource = affiliatedResources.getOrDefault(id, null); if (affiliatedResource != null) { - AffiliationCacheService.AffiliationCacheValue cacheValue = new AffiliationCacheService.AffiliationCacheValue(userId, id, entityType, affiliatedResource); + AffiliationCacheService.AffiliationCacheValue cacheValue = null; + try { + cacheValue = new AffiliationCacheService.AffiliationCacheValue(this.tenantScope.isSet() ? this.tenantScope.getTenant(): null, userId, id, entityType, affiliatedResource); + } catch (InvalidApplicationException e) { + throw new RuntimeException(e); + } this.affiliationCacheService.put(cacheValue); } } diff --git a/dmp-backend/web/src/main/resources/config/cache.yml b/dmp-backend/web/src/main/resources/config/cache.yml index c79b7f88f..a34735def 100644 --- a/dmp-backend/web/src/main/resources/config/cache.yml +++ b/dmp-backend/web/src/main/resources/config/cache.yml @@ -110,4 +110,4 @@ cache: keyPattern: resolve_$keyhash$:v0 affiliation: name: affiliation - keyPattern: affiliation_$entity$_$user$_$type$:v0 \ No newline at end of file + keyPattern: affiliation_$entity$_$user$_$tenant$_$type$:v0 \ No newline at end of file From c35fb3ad3c3591e2eed0063fe2a90cc17a4df47e Mon Sep 17 00:00:00 2001 From: sgiannopoulos Date: Fri, 5 Apr 2024 11:35:37 +0300 Subject: [PATCH 2/2] migration fixes --- dmp-migration-tool/pom.xml | 15 +++++++++++++-- .../eudat/logic/proxy/fetching/RemoteFetcher.java | 6 ++++-- .../DescriptionTemplateXmlMigrationService.java | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/dmp-migration-tool/pom.xml b/dmp-migration-tool/pom.xml index d8fe2b713..f0c3012ab 100644 --- a/dmp-migration-tool/pom.xml +++ b/dmp-migration-tool/pom.xml @@ -10,11 +10,12 @@ org.springframework.boot spring-boot-starter-parent - 3.1.2 + 3.2.4 queryable + ../dmp-backend/core web data elastic @@ -301,16 +302,26 @@ io.micrometer micrometer-core + + org.jboss + jandex + 3.1.2 + gr.cite data-tools - 2.1.0 + 2.1.2 gr.cite exceptions + 2.2.0 + + + gr.cite + oidc-authz 2.1.0 diff --git a/dmp-migration-tool/web/src/main/java/eu/old/eudat/logic/proxy/fetching/RemoteFetcher.java b/dmp-migration-tool/web/src/main/java/eu/old/eudat/logic/proxy/fetching/RemoteFetcher.java index 323ecccd8..75d698d8b 100644 --- a/dmp-migration-tool/web/src/main/java/eu/old/eudat/logic/proxy/fetching/RemoteFetcher.java +++ b/dmp-migration-tool/web/src/main/java/eu/old/eudat/logic/proxy/fetching/RemoteFetcher.java @@ -387,7 +387,8 @@ public class RemoteFetcher { JsonNode jsonBody = new ObjectMapper().readTree(replacedBody); entity = new HttpEntity<>(jsonBody, headers); - response = restTemplate.exchange(replacedPath, HttpMethod.resolve(requestType), entity, String.class); +// response = restTemplate.exchange(replacedPath, HttpMethod.resolve(requestType), entity, String.class); + response = null; if (response.getStatusCode() == HttpStatus.OK) { if (response.getHeaders().get("Content-Type").get(0).contains("json")) { DocumentContext jsonContext = JsonPath.parse(response.getBody()); @@ -423,7 +424,8 @@ public class RemoteFetcher { JsonNode jsonBody = new ObjectMapper().readTree(requestBody); entity = new HttpEntity<>(jsonBody, headers); - response = restTemplate.exchange(urlString, HttpMethod.resolve(requestType), entity, String.class); +// response = restTemplate.exchange(urlString, HttpMethod.resolve(requestType), entity, String.class); + response = null; if (response.getStatusCode() == HttpStatus.OK) { // success //do here all the parsing Results results = new Results(); diff --git a/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DescriptionTemplateXmlMigrationService.java b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DescriptionTemplateXmlMigrationService.java index 33a279f2f..ed5350214 100644 --- a/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DescriptionTemplateXmlMigrationService.java +++ b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DescriptionTemplateXmlMigrationService.java @@ -25,6 +25,7 @@ import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.logging.LoggerService; import jakarta.persistence.EntityManager; +import jakarta.ws.rs.NotSupportedException; import jakarta.xml.bind.JAXBException; import org.jetbrains.annotations.NotNull; import org.slf4j.LoggerFactory; @@ -34,7 +35,6 @@ import org.w3c.dom.Document; import org.xml.sax.SAXException; import javax.management.InvalidApplicationException; -import javax.ws.rs.NotSupportedException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; import java.io.IOException;