From 0155efbd994aaf6ca44d8536548f1f47a7231177 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 16 Feb 2022 22:59:27 +0100 Subject: [PATCH] ckanDB and sysAdminToken are provided only to Catalogue-Manager --- .../org/gcube/gcat/rest/Configuration.java | 24 +++++++++++++++---- .../GCoreISConfigurationProxyTest.java | 4 +++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/gcube/gcat/rest/Configuration.java b/src/main/java/org/gcube/gcat/rest/Configuration.java index 04745e6..85e3dc6 100644 --- a/src/main/java/org/gcube/gcat/rest/Configuration.java +++ b/src/main/java/org/gcube/gcat/rest/Configuration.java @@ -16,6 +16,7 @@ import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; import javax.xml.ws.WebServiceException; +import org.gcube.com.fasterxml.jackson.core.JsonProcessingException; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.common.authorization.utils.manager.SecretManager; @@ -23,7 +24,10 @@ import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.annotation.PATCH; import org.gcube.gcat.annotation.PURGE; import org.gcube.gcat.api.configuration.CatalogueConfiguration; +import org.gcube.gcat.api.roles.Role; import org.gcube.gcat.configuration.CatalogueConfigurationFactory; +import org.gcube.gcat.persistence.ckan.CKANUser; +import org.gcube.gcat.persistence.ckan.CKANUserCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,11 +63,22 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf } return c; } - + + protected String getConfigurationAsString(CatalogueConfiguration catalogueConfiguration) throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode configuration = objectMapper.valueToTree(catalogueConfiguration); + CKANUser ckanUser = CKANUserCache.getCurrrentCKANUser(); + if(ckanUser.getRole() != Role.MANAGER ) { + configuration.remove(CatalogueConfiguration.SYS_ADMIN_TOKEN_KEY); + configuration.remove(CatalogueConfiguration.CKAN_DB_KEY); + } + return objectMapper.writeValueAsString(objectMapper); + } + private String createOrUpdate(CatalogueConfiguration catalogueConfiguration) throws WebServiceException { try { CatalogueConfiguration gotCatalogueConfiguration = CatalogueConfigurationFactory.createOrUpdate(catalogueConfiguration); - String configuration = gotCatalogueConfiguration.toJsonString(); + String configuration = getConfigurationAsString(gotCatalogueConfiguration); logger.debug("The new configuration in context {} is {}", catalogueConfiguration.getContext(), configuration); return configuration; }catch (WebServiceException e) { @@ -186,7 +201,7 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf node.remove(CURRENT_CONTEXT_PATH_PARAMETER); } - ObjectNode configuration = catalogueConfiguration.toObjetNode(); + ObjectNode configuration = mapper.valueToTree(catalogueConfiguration); Iterator fieldNames = node.fieldNames(); while(fieldNames.hasNext()) { @@ -199,7 +214,8 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf newCatalogueConfiguration = CatalogueConfigurationFactory.createOrUpdate(newCatalogueConfiguration); catalogueConfiguration = CatalogueConfigurationFactory.createOrUpdate(catalogueConfiguration); - String ret = mapper.writeValueAsString(catalogueConfiguration); + + String ret = getConfigurationAsString(catalogueConfiguration); logger.debug("Configuration in context {} has been patched to {}", catalogueConfiguration.getContext(), ret); return ret; }catch (WebServiceException e) { diff --git a/src/test/java/org/gcube/gcat/configuration/GCoreISConfigurationProxyTest.java b/src/test/java/org/gcube/gcat/configuration/GCoreISConfigurationProxyTest.java index 52a537c..830f37f 100644 --- a/src/test/java/org/gcube/gcat/configuration/GCoreISConfigurationProxyTest.java +++ b/src/test/java/org/gcube/gcat/configuration/GCoreISConfigurationProxyTest.java @@ -2,6 +2,7 @@ package org.gcube.gcat.configuration; import java.util.Set; +import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.common.authorization.utils.manager.SecretManager; import org.gcube.gcat.ContextTest; import org.gcube.gcat.api.configuration.CatalogueConfiguration; @@ -29,7 +30,8 @@ public class GCoreISConfigurationProxyTest extends ContextTest { String context = SecretManager.instance.get().getContext(); GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context); CatalogueConfiguration catalogueConfiguration = gCoreISConfigurationProxy.getCatalogueConfigurationFromIS(); - logger.info("Configuration inn context {} is {}", context, catalogueConfiguration.toJsonString()); + ObjectMapper mapper = new ObjectMapper(); + logger.info("Configuration in context {} is {}", context, mapper.writeValueAsString(catalogueConfiguration)); }