ckanDB and sysAdminToken are provided only to Catalogue-Manager

This commit is contained in:
Luca Frosini 2022-02-16 22:59:27 +01:00
parent b66b1fcd56
commit 0155efbd99
2 changed files with 23 additions and 5 deletions

View File

@ -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<String> 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) {

View File

@ -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));
}