diff --git a/pom.xml b/pom.xml index 98161c1..4b2b2b8 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,16 @@ + + org.gcube.resources.discovery + ic-client + provided + + + org.gcube.core + common-encryption + [1.0.2-SNAPSHOT, 2.0.0-SNAPSHOT) + org.slf4j slf4j-api diff --git a/src/main/java/org/gcube/accounting/persistence/FallbackPersistence.java b/src/main/java/org/gcube/accounting/persistence/FallbackPersistence.java index 7db444f..c9e2443 100644 --- a/src/main/java/org/gcube/accounting/persistence/FallbackPersistence.java +++ b/src/main/java/org/gcube/accounting/persistence/FallbackPersistence.java @@ -27,7 +27,7 @@ public class FallbackPersistence extends Persistence { * {@inheritDoc} */ @Override - public void prepareConnection() { + public void prepareConnection(PersistenceConfiguration configuration) { // Nothing TO DO } diff --git a/src/main/java/org/gcube/accounting/persistence/Persistence.java b/src/main/java/org/gcube/accounting/persistence/Persistence.java index 8f46c8a..a584905 100644 --- a/src/main/java/org/gcube/accounting/persistence/Persistence.java +++ b/src/main/java/org/gcube/accounting/persistence/Persistence.java @@ -63,8 +63,11 @@ public abstract class Persistence { continue; } try { - logger.debug(String.format("Testing %s", foundPersistence.getClass().getSimpleName())); - foundPersistence.prepareConnection(); + String foundPersistenceClassName = foundPersistence.getClass().getSimpleName(); + logger.debug("Testing {}", foundPersistenceClassName); + String scope = null; // TODO + PersistenceConfiguration configuration = PersistenceConfiguration.getPersistenceConfiguration(scope, foundPersistenceClassName); + foundPersistence.prepareConnection(configuration); /* * Uncomment the following line of code if you want to try * to create a test UsageRecord before setting the @@ -114,9 +117,10 @@ public abstract class Persistence { * Prepare the connection to persistence. * This method must be used by implementation class to open * the connection with the persistence storage, DB, file etc. + * @param configuration The configuration to create the connection * @throws Exception if fails */ - protected abstract void prepareConnection() throws Exception; + protected abstract void prepareConnection(PersistenceConfiguration configuration) throws Exception; /* * * Prepare the connection and try to write a test record on default diff --git a/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java b/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java new file mode 100644 index 0000000..c0d9d95 --- /dev/null +++ b/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java @@ -0,0 +1,151 @@ +/** + * + */ +package org.gcube.accounting.persistence; + +import java.net.URI; +import java.security.Key; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.gcube.common.encryption.StringEncrypter; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.Property; +import org.gcube.common.resources.gcore.utils.Group; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + */ +public class PersistenceConfiguration { + + protected final static String SERVICE_ENDPOINT_CATEGORY = "Accounting"; + protected final static String SERVICE_ENDPOINT_NAME = "Persistence"; + + protected static final String PERSISTENCE_CLASS_NAME = "persistenceClassName"; + + protected URI uri; + protected String username; + protected String password; + + protected Map propertyMap; + + public PersistenceConfiguration(){ + propertyMap = new HashMap(); + } + + public PersistenceConfiguration(URI uri, String username, String password){ + this.uri = uri; + this.username = username; + this.password = password; + this.propertyMap = new HashMap(); + } + + /** + * @return the uri + */ + public URI getUri() { + return uri; + } + + /** + * @param uri the uri to set + */ + public void setUri(URI uri) { + this.uri = uri; + } + + /** + * @return the username + */ + public String getUsername() { + return username; + } + + /** + * @param username the username to set + */ + public void setUsername(String username) { + this.username = username; + } + + /** + * @return the password + */ + public String getPassword() { + return password; + } + + /** + * @param password the password to set + */ + public void setPassword(String password) { + this.password = password; + } + + /** + * @return the propertyMap + * @throws Exception + */ + public String getProperty(String propertyKey) throws Exception { + Property propertyValue = propertyMap.get(propertyKey); + String value = propertyValue.value(); + if(propertyValue.isEncrypted()){ + value = decrypt(value); + } + return value; + } + + private static ServiceEndpoint getServiceEndpoint(String scope, String persistenceClassName){ + ScopeProvider.instance.set(scope); + SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class); + query.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", SERVICE_ENDPOINT_CATEGORY)); + query.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", SERVICE_ENDPOINT_NAME)); + query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Name/text() eq '%s'", PERSISTENCE_CLASS_NAME)); + query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", persistenceClassName)); + query.setResult("$resource"); + + DiscoveryClient client = ICFactory.clientFor(ServiceEndpoint.class); + List serviceEndpoints = client.submit(query); + return serviceEndpoints.get(0); + } + + private static String decrypt(String encrypted) throws Exception { + Key key = null; + return StringEncrypter.getEncrypter().decrypt(encrypted, key); + } + + + private static PersistenceConfiguration createPersistenceConfiguration(ServiceEndpoint serviceEndpoint) throws Exception{ + PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration(); + Group accessPoints = serviceEndpoint.profile().accessPoints(); + for(AccessPoint accessPoint : accessPoints){ + persistenceConfiguration.uri = new URI(accessPoint.address()); + persistenceConfiguration.username = accessPoint.name(); + String encryptedPassword = accessPoint.password(); + String password = decrypt(encryptedPassword); + persistenceConfiguration.password = password; + persistenceConfiguration.propertyMap = accessPoint.propertyMap(); + } + return persistenceConfiguration; + } + + /** + * + * @param scope + * @param persistenceClassName + * @return + * @throws Exception + */ + protected static PersistenceConfiguration getPersistenceConfiguration(String scope, String persistenceClassName) throws Exception { + ServiceEndpoint serviceEndpoint = getServiceEndpoint(scope, persistenceClassName); + return createPersistenceConfiguration(serviceEndpoint); + } + + +}