Added facilities to manage decrypted and encrypted values

This commit is contained in:
Luca Frosini 2022-02-17 16:32:54 +01:00
parent 0155efbd99
commit 91fe3633c2
6 changed files with 261 additions and 68 deletions

View File

@ -4,27 +4,26 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.gcube.common.authorization.utils.manager.SecretManager; import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
public class CatalogueConfigurationFactory { public class CatalogueConfigurationFactory {
private static final Map<String, CatalogueConfiguration> catalogueConfigurations; private static final Map<String, ServiceCatalogueConfiguration> catalogueConfigurations;
static { static {
catalogueConfigurations = new HashMap<>(); catalogueConfigurations = new HashMap<>();
} }
protected static CatalogueConfiguration load(String context) { protected static ServiceCatalogueConfiguration load(String context) {
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context); GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
return gCoreISConfigurationProxy.getCatalogueConfiguration(); return gCoreISConfigurationProxy.getCatalogueConfiguration();
} }
public synchronized static CatalogueConfiguration getInstance() { public synchronized static ServiceCatalogueConfiguration getInstance() {
String context = SecretManager.instance.get().getContext(); String context = SecretManager.instance.get().getContext();
CatalogueConfiguration catalogueConfiguration = catalogueConfigurations.get(context); ServiceCatalogueConfiguration catalogueConfiguration = catalogueConfigurations.get(context);
if(catalogueConfiguration == null) { if(catalogueConfiguration == null) {
catalogueConfiguration = load(context); catalogueConfiguration = load(context);
catalogueConfigurations.put(context, catalogueConfiguration); catalogueConfigurations.put(context, catalogueConfiguration);
@ -35,7 +34,7 @@ public class CatalogueConfigurationFactory {
public synchronized static void renew() { public synchronized static void renew() {
String context = SecretManager.instance.get().getContext(); String context = SecretManager.instance.get().getContext();
catalogueConfigurations.remove(context); catalogueConfigurations.remove(context);
CatalogueConfiguration catalogueConfiguration = load(context); ServiceCatalogueConfiguration catalogueConfiguration = load(context);
catalogueConfigurations.put(context, catalogueConfiguration); catalogueConfigurations.put(context, catalogueConfiguration);
} }
@ -47,7 +46,7 @@ public class CatalogueConfigurationFactory {
gCoreISConfigurationProxy.delete(); gCoreISConfigurationProxy.delete();
} }
public synchronized static CatalogueConfiguration createOrUpdate(CatalogueConfiguration catalogueConfiguration) throws Exception { public synchronized static ServiceCatalogueConfiguration createOrUpdate(ServiceCatalogueConfiguration catalogueConfiguration) throws Exception {
String context = SecretManager.instance.get().getContext(); String context = SecretManager.instance.get().getContext();
catalogueConfigurations.remove(context); catalogueConfigurations.remove(context);
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context, catalogueConfiguration); GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context, catalogueConfiguration);

View File

@ -26,7 +26,6 @@ import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime; import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime;
import org.gcube.common.resources.gcore.common.Platform; import org.gcube.common.resources.gcore.common.Platform;
import org.gcube.common.resources.gcore.utils.Group; import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.gcat.api.configuration.CKANDB;
import org.gcube.gcat.api.configuration.CatalogueConfiguration; import org.gcube.gcat.api.configuration.CatalogueConfiguration;
import org.gcube.informationsystem.publisher.RegistryPublisher; import org.gcube.informationsystem.publisher.RegistryPublisher;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory; import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
@ -68,21 +67,21 @@ public class GCoreISConfigurationProxy {
private final static String NAME = "CKanDataCatalogue"; private final static String NAME = "CKanDataCatalogue";
protected final String context; protected final String context;
protected CatalogueConfiguration catalogueConfiguration; protected ServiceCatalogueConfiguration catalogueConfiguration;
public GCoreISConfigurationProxy(String context) { public GCoreISConfigurationProxy(String context) {
this.context = context; this.context = context;
} }
public GCoreISConfigurationProxy(String context, CatalogueConfiguration catalogueConfiguration) { public GCoreISConfigurationProxy(String context, ServiceCatalogueConfiguration catalogueConfiguration) {
this(context); this(context);
this.catalogueConfiguration = catalogueConfiguration; this.catalogueConfiguration = catalogueConfiguration;
} }
public CatalogueConfiguration getCatalogueConfiguration() { public ServiceCatalogueConfiguration getCatalogueConfiguration() {
if (catalogueConfiguration == null) { if (catalogueConfiguration == null) {
getCatalogueConfigurationFromIS(); catalogueConfiguration = getCatalogueConfigurationFromIS();
} }
return catalogueConfiguration; return catalogueConfiguration;
} }
@ -102,12 +101,11 @@ public class GCoreISConfigurationProxy {
return stringBuffer.toString(); return stringBuffer.toString();
} }
protected CatalogueConfiguration getCatalogueConfigurationFromIS() { protected ServiceCatalogueConfiguration getCatalogueConfigurationFromIS() {
ServiceCatalogueConfiguration catalogueConfiguration = new ServiceCatalogueConfiguration(context);
try { try {
boolean mustBeUpdated = false; boolean mustBeUpdated = false;
catalogueConfiguration = new CatalogueConfiguration(context);
ServiceEndpoint serviceEndpoint = getServiceEndpoint(); ServiceEndpoint serviceEndpoint = getServiceEndpoint();
if (serviceEndpoint == null) { if (serviceEndpoint == null) {
throw new InternalServerErrorException("No CKAN configuration on IS"); throw new InternalServerErrorException("No CKAN configuration on IS");
@ -126,22 +124,21 @@ public class GCoreISConfigurationProxy {
// retrieve sys admin token // retrieve sys admin token
String encryptedSysAdminToken = propertyMap.get(API_KEY_PROPERTY_KEY).value(); String encryptedSysAdminToken = propertyMap.get(API_KEY_PROPERTY_KEY).value();
catalogueConfiguration.setSysAdminToken(StringEncrypter.getEncrypter().decrypt(encryptedSysAdminToken)); catalogueConfiguration.setEncryptedSysAdminToken(encryptedSysAdminToken);
String defaultOrganization = null; String defaultOrganization = CatalogueConfiguration.getOrganizationName(context);;
if (propertyMap.containsKey(DEFAULT_ORGANIZATION_PROPERTY_KEY)) { if (propertyMap.containsKey(DEFAULT_ORGANIZATION_PROPERTY_KEY)) {
String org = propertyMap.get(DEFAULT_ORGANIZATION_PROPERTY_KEY).value().trim(); String org = propertyMap.get(DEFAULT_ORGANIZATION_PROPERTY_KEY).value().trim();
if(org!=null && org.compareTo("")==0) { if(org!=null && org.compareTo("")==0) {
mustBeUpdated = true; mustBeUpdated = true;
}else { }else {
defaultOrganization = org; catalogueConfiguration.setDefaultOrganization(org);
} }
}else { }else {
mustBeUpdated = true; mustBeUpdated = true;
} }
catalogueConfiguration.setDefaultOrganization(defaultOrganization);
String solrURL = null; String solrURL = null;
if (propertyMap.containsKey(SOLR_INDEX_ADDRESS_PROPERTY_KEY)) { if (propertyMap.containsKey(SOLR_INDEX_ADDRESS_PROPERTY_KEY)) {
@ -202,16 +199,16 @@ public class GCoreISConfigurationProxy {
} }
} }
CKANDB ckanDB = null; ServiceCKANDB ckanDB = null;
if (propertyMap.containsKey(CKAN_DB_URL_PROPERTY_KEY)) { if (propertyMap.containsKey(CKAN_DB_URL_PROPERTY_KEY)) {
String ckanDBURL = propertyMap.get(CKAN_DB_URL_PROPERTY_KEY).value().trim(); String ckanDBURL = propertyMap.get(CKAN_DB_URL_PROPERTY_KEY).value().trim();
ckanDB = new CKANDB(); ckanDB = new ServiceCKANDB();
ckanDB.setUrl(ckanDBURL); ckanDB.setUrl(ckanDBURL);
String ckanDBUsername = propertyMap.get(CKAN_DB_USERNAME_PROPERTY_KEY).value().trim(); String ckanDBUsername = propertyMap.get(CKAN_DB_USERNAME_PROPERTY_KEY).value().trim();
ckanDB.setUsername(ckanDBUsername); ckanDB.setUsername(ckanDBUsername);
// Password is encrypted // Password is encrypted
String ckanDBPassword = propertyMap.get(CKAN_DB_PASSWORD_PROPERTY_KEY).value().trim(); String ckanDBPassword = propertyMap.get(CKAN_DB_PASSWORD_PROPERTY_KEY).value().trim();
ckanDB.setPassword(StringEncrypter.getEncrypter().decrypt(ckanDBPassword)); ckanDB.setEncryptedPassword(ckanDBPassword);
}else { }else {
mustBeUpdated = true; mustBeUpdated = true;
ckanDB = getCKANDBFromIS(); ckanDB = getCKANDBFromIS();
@ -320,7 +317,7 @@ public class GCoreISConfigurationProxy {
return serviceEndpoints; return serviceEndpoints;
} }
protected CKANDB getCKANDBFromIS() { protected ServiceCKANDB getCKANDBFromIS() {
try { try {
List<ServiceEndpoint> serviceEndpoints = getCKANDBServiceEndpoints(); List<ServiceEndpoint> serviceEndpoints = getCKANDBServiceEndpoints();
ServiceEndpoint serviceEndpoint = null; ServiceEndpoint serviceEndpoint = null;
@ -366,11 +363,11 @@ public class GCoreISConfigurationProxy {
String host = accessPoint.address(); String host = accessPoint.address();
String db = accessPoint.name(); String db = accessPoint.name();
CKANDB ckanDB = new CKANDB(); ServiceCKANDB ckanDB = new ServiceCKANDB();
String url = String.format("jdbc:postgresql://%s/%s", host, db); String url = String.format("jdbc:postgresql://%s/%s", host, db);
ckanDB.setUrl(url); ckanDB.setUrl(url);
ckanDB.setUsername(accessPoint.username()); ckanDB.setUsername(accessPoint.username());
ckanDB.setPassword(StringEncrypter.getEncrypter().decrypt(accessPoint.password())); ckanDB.setEncryptedPassword(accessPoint.password());
return ckanDB; return ckanDB;
} }
@ -452,7 +449,7 @@ public class GCoreISConfigurationProxy {
logger.trace( logger.trace(
"{} with SecondaryType {} and Name %s not found. Item will be only be created in {} CKAN organization", "{} with SecondaryType {} and Name %s not found. Item will be only be created in {} CKAN organization",
GenericResource.class.getSimpleName(), GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS, GenericResource.class.getSimpleName(), GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS,
GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, CatalogueConfiguration.getOrganizationName(context)); GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, ServiceCatalogueConfiguration.getOrganizationName(context));
return null; return null;
} }
@ -647,7 +644,7 @@ public class GCoreISConfigurationProxy {
return serviceEndpoint; return serviceEndpoint;
} }
public CatalogueConfiguration createOnIS(ServiceEndpoint serviceEndpoint) throws Exception { public ServiceCatalogueConfiguration createOnIS(ServiceEndpoint serviceEndpoint) throws Exception {
RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
String id = catalogueConfiguration.getID(); String id = catalogueConfiguration.getID();
if(id==null || id.compareTo("")==0) { if(id==null || id.compareTo("")==0) {
@ -659,7 +656,7 @@ public class GCoreISConfigurationProxy {
return catalogueConfiguration; return catalogueConfiguration;
} }
public CatalogueConfiguration updateOnIS(ServiceEndpoint serviceEndpoint) throws Exception { public ServiceCatalogueConfiguration updateOnIS(ServiceEndpoint serviceEndpoint) throws Exception {
RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
String id = serviceEndpoint.id(); String id = serviceEndpoint.id();
if(catalogueConfiguration.getID().compareTo(id)!=0) { if(catalogueConfiguration.getID().compareTo(id)!=0) {
@ -671,7 +668,7 @@ public class GCoreISConfigurationProxy {
return catalogueConfiguration; return catalogueConfiguration;
} }
public CatalogueConfiguration createOrUpdateOnIS() throws Exception { public ServiceCatalogueConfiguration createOrUpdateOnIS() throws Exception {
ServiceEndpoint serviceEndpoint = getServiceEndpoint(); ServiceEndpoint serviceEndpoint = getServiceEndpoint();
if(serviceEndpoint!=null) { if(serviceEndpoint!=null) {
// It's an update // It's an update

View File

@ -0,0 +1,60 @@
package org.gcube.gcat.configuration;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import org.gcube.com.fasterxml.jackson.annotation.JsonGetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.gcube.com.fasterxml.jackson.annotation.JsonSetter;
import org.gcube.common.encryption.encrypter.StringEncrypter;
import org.gcube.gcat.api.configuration.CKANDB;
/**
* @author Luca Frosini (ISTI-CNR)
*/
public class ServiceCKANDB extends CKANDB {
protected String encryptedPassword;
@JsonIgnore
public String getPassword() {
return password;
}
@JsonIgnore
public String getPlainPassword() {
return password;
}
@JsonGetter(value=PASSWORD_KEY)
public String getEncryptedPassword() {
return encryptedPassword;
}
public void setEncryptedPassword(String encryptedPassword) throws Exception {
this.encryptedPassword = encryptedPassword;
this.password = StringEncrypter.getEncrypter().decrypt(encryptedPassword);
}
public void setPlainPassword(String plainPassword) throws Exception {
this.password = plainPassword;
this.encryptedPassword = StringEncrypter.getEncrypter().encrypt(plainPassword);
}
@Override
@JsonSetter(value = PASSWORD_KEY)
public void setPassword(String password) {
try {
try {
this.password = StringEncrypter.getEncrypter().decrypt(password);
this.encryptedPassword = password;
}catch (IllegalBlockSizeException | BadPaddingException e) {
this.password = password;
this.encryptedPassword = StringEncrypter.getEncrypter().encrypt(password);
}
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,142 @@
package org.gcube.gcat.configuration;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import org.gcube.com.fasterxml.jackson.annotation.JsonGetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.gcube.com.fasterxml.jackson.annotation.JsonSetter;
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.encryption.encrypter.StringEncrypter;
import org.gcube.gcat.api.configuration.CKANDB;
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
import org.gcube.gcat.api.roles.Role;
import org.gcube.gcat.persistence.ckan.CKANUser;
import org.gcube.gcat.persistence.ckan.CKANUserCache;
/**
* @author Luca Frosini (ISTI-CNR)
*/
public class ServiceCatalogueConfiguration extends CatalogueConfiguration {
protected ObjectMapper mapper;
public ServiceCatalogueConfiguration() {
super();
mapper = new ObjectMapper();
}
public ServiceCatalogueConfiguration(String context) {
super(context);
mapper = new ObjectMapper();
}
@JsonIgnore
protected String encryptedSysAdminToken;
@JsonIgnore
public String getSysAdminToken() {
return sysAdminToken;
}
@JsonIgnore
public String getPlainSysAdminToken() {
return sysAdminToken;
}
@JsonGetter(value=SYS_ADMIN_TOKEN_KEY)
public String getEncryptedSysAdminToken() {
return encryptedSysAdminToken;
}
public void setEncryptedSysAdminToken(String encryptedSysAdminToken) throws Exception {
this.encryptedSysAdminToken = encryptedSysAdminToken;
this.sysAdminToken = StringEncrypter.getEncrypter().decrypt(encryptedSysAdminToken);
}
public void setPlainSysAdminToken(String plainSysAdminToken) throws Exception {
this.sysAdminToken = plainSysAdminToken;
this.encryptedSysAdminToken = StringEncrypter.getEncrypter().encrypt(plainSysAdminToken);
}
@Override
@JsonSetter(value = SYS_ADMIN_TOKEN_KEY)
public void setSysAdminToken(String sysAdminToken) {
try {
try {
this.sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken);
this.encryptedSysAdminToken = sysAdminToken;
}catch (IllegalBlockSizeException | BadPaddingException e) {
this.sysAdminToken = sysAdminToken;
this.encryptedSysAdminToken = StringEncrypter.getEncrypter().encrypt(sysAdminToken);
}
}catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
@JsonGetter(value = CKAN_DB_KEY)
public ServiceCKANDB getCkanDB() {
return (ServiceCKANDB) ckanDB;
}
@Override
public void setCkanDB(CKANDB ckanDB) {
this.ckanDB = new ServiceCKANDB();
this.ckanDB.setUrl(ckanDB.getUrl());
this.ckanDB.setUsername(ckanDB.getUsername());
this.ckanDB.setPassword(ckanDB.getPassword());
}
@JsonSetter(value=CKAN_DB_KEY)
public void setCkanDB(ServiceCKANDB ckanDB) {
this.ckanDB = ckanDB;
}
public ObjectNode toObjetcNode() throws JsonProcessingException {
return toObjetcNode(false);
}
public ObjectNode toObjetcNode(boolean decryptedValues) throws JsonProcessingException {
ObjectNode configuration = mapper.valueToTree(this);
CKANUser ckanUser = CKANUserCache.getCurrrentCKANUser();
if(ckanUser.getRole() != Role.MANAGER) {
configuration.remove(ServiceCatalogueConfiguration.SYS_ADMIN_TOKEN_KEY);
configuration.remove(ServiceCatalogueConfiguration.CKAN_DB_KEY);
}else {
if(decryptedValues) {
configuration.put(ServiceCatalogueConfiguration.SYS_ADMIN_TOKEN_KEY, getPlainSysAdminToken());
ObjectNode node = (ObjectNode) configuration.get(ServiceCatalogueConfiguration.CKAN_DB_KEY);
node.put(ServiceCKANDB.PASSWORD_KEY, ((ServiceCKANDB) ckanDB).getPlainPassword());
}
}
return configuration;
}
public String toJsonString() throws Exception {
return toJsonString(false);
}
public String toJsonString(boolean decryptedValues) throws Exception {
ObjectNode objectNode = toObjetcNode(decryptedValues);
return mapper.writeValueAsString(objectNode);
}
public static ServiceCatalogueConfiguration getServiceCatalogueConfiguration(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
ServiceCatalogueConfiguration catalogueConfiguration = mapper.readValue(json, ServiceCatalogueConfiguration.class);
return catalogueConfiguration;
}
public static ServiceCatalogueConfiguration getServiceCatalogueConfiguration(ObjectNode objectNode) throws Exception {
ObjectMapper mapper = new ObjectMapper();
ServiceCatalogueConfiguration catalogueConfiguration = mapper.treeToValue(objectNode, ServiceCatalogueConfiguration.class);
return catalogueConfiguration;
}
}

View File

@ -5,6 +5,7 @@ import java.util.Iterator;
import javax.ws.rs.BadRequestException; import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.POST; import javax.ws.rs.POST;
@ -16,16 +17,15 @@ import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import javax.xml.ws.WebServiceException; 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.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.common.authorization.utils.manager.SecretManager; import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.ResourceInitializer;
import org.gcube.gcat.annotation.PATCH; import org.gcube.gcat.annotation.PATCH;
import org.gcube.gcat.annotation.PURGE; import org.gcube.gcat.annotation.PURGE;
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
import org.gcube.gcat.api.roles.Role; import org.gcube.gcat.api.roles.Role;
import org.gcube.gcat.configuration.CatalogueConfigurationFactory; import org.gcube.gcat.configuration.CatalogueConfigurationFactory;
import org.gcube.gcat.configuration.ServiceCatalogueConfiguration;
import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.gcat.persistence.ckan.CKANUser;
import org.gcube.gcat.persistence.ckan.CKANUserCache; import org.gcube.gcat.persistence.ckan.CKANUserCache;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -56,7 +56,7 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf
return c; return c;
} }
protected String checkContext(String context, CatalogueConfiguration catalogueConfiguration) { protected String checkContext(String context, ServiceCatalogueConfiguration catalogueConfiguration) {
String c = checkContext(context); String c = checkContext(context);
if(c.compareTo(catalogueConfiguration.getContext())!=0) { if(c.compareTo(catalogueConfiguration.getContext())!=0) {
throw new BadRequestException("Context provided in the configuration (i.e. " + catalogueConfiguration.getContext() + ") does not match with token request context (i.e. " + c + ")"); throw new BadRequestException("Context provided in the configuration (i.e. " + catalogueConfiguration.getContext() + ") does not match with token request context (i.e. " + c + ")");
@ -64,21 +64,17 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf
return c; return c;
} }
protected String getConfigurationAsString(CatalogueConfiguration catalogueConfiguration) throws JsonProcessingException { protected void checkRole(Role required) {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode configuration = objectMapper.valueToTree(catalogueConfiguration);
CKANUser ckanUser = CKANUserCache.getCurrrentCKANUser(); CKANUser ckanUser = CKANUserCache.getCurrrentCKANUser();
if(ckanUser.getRole() != Role.MANAGER ) { if(ckanUser.getRole().ordinal() < required.ordinal()) {
configuration.remove(CatalogueConfiguration.SYS_ADMIN_TOKEN_KEY); throw new ForbiddenException("To perform such a request you must have " + required.getPortalRole() + " role");
configuration.remove(CatalogueConfiguration.CKAN_DB_KEY);
} }
return objectMapper.writeValueAsString(objectMapper);
} }
private String createOrUpdate(CatalogueConfiguration catalogueConfiguration) throws WebServiceException { private String createOrUpdate(ServiceCatalogueConfiguration catalogueConfiguration) throws WebServiceException {
try { try {
CatalogueConfiguration gotCatalogueConfiguration = CatalogueConfigurationFactory.createOrUpdate(catalogueConfiguration); ServiceCatalogueConfiguration gotCatalogueConfiguration = CatalogueConfigurationFactory.createOrUpdate(catalogueConfiguration);
String configuration = getConfigurationAsString(gotCatalogueConfiguration); String configuration = gotCatalogueConfiguration.toJsonString();
logger.debug("The new configuration in context {} is {}", catalogueConfiguration.getContext(), configuration); logger.debug("The new configuration in context {} is {}", catalogueConfiguration.getContext(), configuration);
return configuration; return configuration;
}catch (WebServiceException e) { }catch (WebServiceException e) {
@ -94,8 +90,7 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf
@Override @Override
public Response create(String json) throws WebServiceException { public Response create(String json) throws WebServiceException {
try { try {
ObjectMapper mapper = new ObjectMapper(); ServiceCatalogueConfiguration catalogueConfiguration = ServiceCatalogueConfiguration.getServiceCatalogueConfiguration(json);
CatalogueConfiguration catalogueConfiguration = mapper.readValue(json, CatalogueConfiguration.class);
checkContext(CURRENT_CONTEXT_PATH_PARAMETER, catalogueConfiguration); checkContext(CURRENT_CONTEXT_PATH_PARAMETER, catalogueConfiguration);
String ret = createOrUpdate(catalogueConfiguration); String ret = createOrUpdate(catalogueConfiguration);
ResponseBuilder responseBuilder = Response.status(Status.CREATED); ResponseBuilder responseBuilder = Response.status(Status.CREATED);
@ -126,9 +121,8 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf
@Override @Override
public String read() throws WebServiceException { public String read() throws WebServiceException {
try { try {
CatalogueConfiguration catalogueConfiguration = CatalogueConfigurationFactory.getInstance(); ServiceCatalogueConfiguration catalogueConfiguration = CatalogueConfigurationFactory.getInstance();
ObjectMapper mapper = new ObjectMapper(); String configuration = catalogueConfiguration.toJsonString();
String configuration = mapper.writeValueAsString(catalogueConfiguration);
logger.debug("Configuration in context {} is {}", catalogueConfiguration.getContext(), configuration); logger.debug("Configuration in context {} is {}", catalogueConfiguration.getContext(), configuration);
return configuration; return configuration;
}catch (WebServiceException e) { }catch (WebServiceException e) {
@ -143,8 +137,7 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String createOrUpdate(String context, String json) throws WebServiceException { public String createOrUpdate(String context, String json) throws WebServiceException {
try { try {
ObjectMapper mapper = new ObjectMapper(); ServiceCatalogueConfiguration catalogueConfiguration = ServiceCatalogueConfiguration.getServiceCatalogueConfiguration(json);
CatalogueConfiguration catalogueConfiguration = mapper.readValue(json, CatalogueConfiguration.class);
checkContext(context, catalogueConfiguration); checkContext(context, catalogueConfiguration);
return createOrUpdate(catalogueConfiguration); return createOrUpdate(catalogueConfiguration);
}catch (WebServiceException e) { }catch (WebServiceException e) {
@ -157,11 +150,10 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf
@Override @Override
public String update(String json) throws WebServiceException { public String update(String json) throws WebServiceException {
try { try {
ObjectMapper mapper = new ObjectMapper(); ServiceCatalogueConfiguration catalogueConfiguration = ServiceCatalogueConfiguration.getServiceCatalogueConfiguration(json);
CatalogueConfiguration catalogueConfiguration = mapper.readValue(json, CatalogueConfiguration.class);
checkContext(CURRENT_CONTEXT_PATH_PARAMETER); checkContext(CURRENT_CONTEXT_PATH_PARAMETER);
catalogueConfiguration = CatalogueConfigurationFactory.createOrUpdate(catalogueConfiguration); catalogueConfiguration = CatalogueConfigurationFactory.createOrUpdate(catalogueConfiguration);
String configuration = mapper.writeValueAsString(catalogueConfiguration); String configuration = catalogueConfiguration.toJsonString();
logger.debug("Configuration in context {} has been updated to {}", catalogueConfiguration.getContext(), configuration); logger.debug("Configuration in context {} has been updated to {}", catalogueConfiguration.getContext(), configuration);
return configuration; return configuration;
}catch (WebServiceException e) { }catch (WebServiceException e) {
@ -188,12 +180,12 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf
@Override @Override
public String patch(String json) throws WebServiceException { public String patch(String json) throws WebServiceException {
try { try {
CatalogueConfiguration catalogueConfiguration = CatalogueConfigurationFactory.getInstance(); ServiceCatalogueConfiguration catalogueConfiguration = CatalogueConfigurationFactory.getInstance();
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
ObjectNode node = (ObjectNode) mapper.readTree(json); ObjectNode node = (ObjectNode) mapper.readTree(json);
if(node.has(CatalogueConfiguration.CONTEXT_KEY)) { if(node.has(ServiceCatalogueConfiguration.CONTEXT_KEY)) {
String context = node.get(CatalogueConfiguration.CONTEXT_KEY).asText(); String context = node.get(ServiceCatalogueConfiguration.CONTEXT_KEY).asText();
String c = SecretManager.instance.get().getContext(); String c = SecretManager.instance.get().getContext();
if(c.compareTo(context)!=0) { if(c.compareTo(context)!=0) {
throw new BadRequestException("Context provided in the configuration (i.e. " + catalogueConfiguration.getContext() + ") does not match with token request context (i.e. " + c + ")"); throw new BadRequestException("Context provided in the configuration (i.e. " + catalogueConfiguration.getContext() + ") does not match with token request context (i.e. " + c + ")");
@ -209,13 +201,9 @@ public class Configuration extends BaseREST implements org.gcube.gcat.api.interf
configuration.set(fieldName, node.get(fieldName)); configuration.set(fieldName, node.get(fieldName));
} }
CatalogueConfiguration newCatalogueConfiguration = mapper.treeToValue(configuration, CatalogueConfiguration.class); ServiceCatalogueConfiguration newCatalogueConfiguration = ServiceCatalogueConfiguration.getServiceCatalogueConfiguration(configuration);
newCatalogueConfiguration = CatalogueConfigurationFactory.createOrUpdate(newCatalogueConfiguration); newCatalogueConfiguration = CatalogueConfigurationFactory.createOrUpdate(newCatalogueConfiguration);
String ret = newCatalogueConfiguration.toJsonString();
catalogueConfiguration = CatalogueConfigurationFactory.createOrUpdate(catalogueConfiguration);
String ret = getConfigurationAsString(catalogueConfiguration);
logger.debug("Configuration in context {} has been patched to {}", catalogueConfiguration.getContext(), ret); logger.debug("Configuration in context {} has been patched to {}", catalogueConfiguration.getContext(), ret);
return ret; return ret;
}catch (WebServiceException e) { }catch (WebServiceException e) {

View File

@ -2,10 +2,8 @@ package org.gcube.gcat.configuration;
import java.util.Set; import java.util.Set;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.authorization.utils.manager.SecretManager; import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.gcat.ContextTest; import org.gcube.gcat.ContextTest;
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -15,7 +13,7 @@ public class GCoreISConfigurationProxyTest extends ContextTest {
private static final Logger logger = LoggerFactory.getLogger(GCoreISConfigurationProxyTest.class); private static final Logger logger = LoggerFactory.getLogger(GCoreISConfigurationProxyTest.class);
@Test // @Test
public void testGetSupportedOrganizationsFromIS() throws Exception { public void testGetSupportedOrganizationsFromIS() throws Exception {
ContextTest.setContextByName("/gcube/devNext/NextNext"); ContextTest.setContextByName("/gcube/devNext/NextNext");
String context = SecretManager.instance.get().getContext(); String context = SecretManager.instance.get().getContext();
@ -29,9 +27,18 @@ public class GCoreISConfigurationProxyTest extends ContextTest {
ContextTest.setContextByName("/gcube/devNext"); ContextTest.setContextByName("/gcube/devNext");
String context = SecretManager.instance.get().getContext(); String context = SecretManager.instance.get().getContext();
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context); GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
CatalogueConfiguration catalogueConfiguration = gCoreISConfigurationProxy.getCatalogueConfigurationFromIS(); ServiceCatalogueConfiguration catalogueConfiguration = gCoreISConfigurationProxy.getCatalogueConfigurationFromIS();
ObjectMapper mapper = new ObjectMapper(); String json = catalogueConfiguration.toJsonString();
logger.info("Configuration in context {} is {}", context, mapper.writeValueAsString(catalogueConfiguration)); logger.info("Configuration in context {} is {}", context, json);
ServiceCatalogueConfiguration secondCatalogueConfiguration = ServiceCatalogueConfiguration.getServiceCatalogueConfiguration(json);
String secondJson = secondCatalogueConfiguration.toJsonString();
logger.info("After marshalling and unmarshalling configuration in context {} is {}", context, secondJson);
String decryptedJson = secondCatalogueConfiguration.toJsonString(true);
logger.info("Decrypted configuration in context {} is {}", context, decryptedJson);
ServiceCatalogueConfiguration thirdCatalogueConfiguration = ServiceCatalogueConfiguration.getServiceCatalogueConfiguration(decryptedJson);
String thirdJson = thirdCatalogueConfiguration.toJsonString();
logger.info("After marshalling and unmarshalling decrypted configuration in context {} is {}", context, thirdJson);
logger.info("All as JsonArray [{},{},{},{}]", json, secondJson, decryptedJson, thirdJson);
} }