From 3ca48b3441c29c52fc3382adeb8a7b3a0482b3b3 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 3 Jul 2015 15:07:23 +0000 Subject: [PATCH] refs #200: Create accouting-lib library https://support.d4science.org/issues/200 Fixing tests git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@116497 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 5 -- .../persistence/PersistenceConfiguration.java | 14 ++-- .../PersistenceConfigurationTest.java | 71 ++++++++++++++---- .../accounting/persistence/SymmetricKey.java | 75 +++++++++++++++++++ 4 files changed, 137 insertions(+), 28 deletions(-) create mode 100644 src/test/java/org/gcube/accounting/persistence/SymmetricKey.java diff --git a/pom.xml b/pom.xml index 52eafd5..818566d 100644 --- a/pom.xml +++ b/pom.xml @@ -62,11 +62,6 @@ registry-publisher test - - org.gcube.core - common-smartgears - test - junit junit diff --git a/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java b/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java index 70eabc0..57f045c 100644 --- a/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java +++ b/src/main/java/org/gcube/accounting/persistence/PersistenceConfiguration.java @@ -115,15 +115,11 @@ public class PersistenceConfiguration { return serviceEndpoints.get(0); } - private static String decrypt(String encrypted) throws Exception { - /* - Key key = null; + private static String decrypt(String encrypted, Key... key) throws Exception { return StringEncrypter.getEncrypter().decrypt(encrypted, key); - */ - return encrypted; } - private static PersistenceConfiguration createPersistenceConfiguration(ServiceEndpoint serviceEndpoint) throws Exception{ + private static PersistenceConfiguration createPersistenceConfiguration(ServiceEndpoint serviceEndpoint, Key... key) throws Exception{ PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration(); Group accessPoints = serviceEndpoint.profile().accessPoints(); for(AccessPoint accessPoint : accessPoints){ @@ -131,7 +127,7 @@ public class PersistenceConfiguration { persistenceConfiguration.username = accessPoint.name(); String encryptedPassword = accessPoint.password(); - String password = decrypt(encryptedPassword); + String password = decrypt(encryptedPassword, key); persistenceConfiguration.password = password; persistenceConfiguration.propertyMap = accessPoint.propertyMap(); @@ -146,9 +142,9 @@ public class PersistenceConfiguration { * @return * @throws Exception */ - protected static PersistenceConfiguration getPersistenceConfiguration(String scope, String persistenceClassName) throws Exception { + protected static PersistenceConfiguration getPersistenceConfiguration(String scope, String persistenceClassName, Key... key) throws Exception { ServiceEndpoint serviceEndpoint = getServiceEndpoint(scope, persistenceClassName); - return createPersistenceConfiguration(serviceEndpoint); + return createPersistenceConfiguration(serviceEndpoint, key); } diff --git a/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java b/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java index db18fad..8b74a97 100644 --- a/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java +++ b/src/test/java/org/gcube/accounting/persistence/PersistenceConfigurationTest.java @@ -4,9 +4,8 @@ package org.gcube.accounting.persistence; import java.io.StringWriter; -import java.util.ArrayList; +import java.security.Key; import java.util.List; -import java.util.Map; import org.gcube.common.resources.gcore.Resource; import org.gcube.common.resources.gcore.Resources; @@ -22,7 +21,9 @@ import org.gcube.informationsystem.publisher.AdvancedScopedPublisher; import org.gcube.informationsystem.publisher.RegistryPublisherFactory; import org.gcube.informationsystem.publisher.ScopedPublisher; import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException; -import org.gcube.smartgears.configuration.Mode; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,6 +39,10 @@ public class PersistenceConfigurationTest { public static final String PROFILE_DESCRIPTION = "This ServiceEndpoint contains the parameter to connect to DB to persist log accounting."; public static final String RUNNING_ON = "http://localhost:5984"; + public static final String LOAD_BALANCER = "loadBalancer"; + + public static final String READY = "READY"; + public static final String TEST_VERSION = "1.0.0"; public static final short[] VERSION_SLICES = new short[]{1,6,0,0}; @@ -47,7 +52,9 @@ public class PersistenceConfigurationTest { public static final String FAKE_USERNAME = "fakeusername"; public static final String FAKE_PASSWORD = "fakepassword"; - public static final String TEST_SCOPE = "/gcube/devsec"; + public static final String[] SCOPES = new String[]{"/gcube", "/gcube/devsec"}; + public static final String GCUBE_SCOPE = SCOPES[1]; + public static final String GCUBE_DEVSEC_SCOPE = SCOPES[1]; public static final String DB_NAME_PROPERTY_NAME = "dbName"; public static final String DB_NAME_PROPERTY_VALUE = "accounting"; @@ -61,7 +68,7 @@ public class PersistenceConfigurationTest { * @throws RegistryNotFoundException if the Registry is not found so the * resource has not be published */ - private static void publishScopedResource(Resource resource, List scopes) throws RegistryNotFoundException, Exception { + private static void publishScopedResource(Resource resource, List scopes) throws Exception { StringWriter stringWriter = new StringWriter(); Resources.marshal(resource, stringWriter); @@ -69,7 +76,7 @@ public class PersistenceConfigurationTest { try { logger.debug("Trying to publish to {}:\n{}", scopes, stringWriter); scopedPublisher.create(resource, scopes); - } catch (RegistryNotFoundException e) { + } catch (Exception e) { logger.error("The resource was not published", e); throw e; } @@ -116,7 +123,7 @@ public class PersistenceConfigurationTest { Platform platform = profile.newPlatform(); platform.name(RUNNING_ON); - + platform.version(VERSION_SLICES[0]); platform.minorVersion(VERSION_SLICES[1]); platform.buildVersion(VERSION_SLICES[2]); @@ -124,13 +131,18 @@ public class PersistenceConfigurationTest { Runtime runtime = profile.newRuntime(); runtime.hostedOn(RUNNING_ON); - runtime.status(Mode.online.toString()); + runtime.status(READY); + + Group accessPoints = profile.accessPoints(); AccessPoint accessPointElement = new AccessPoint(); + accessPoints.add(accessPointElement); accessPointElement.description(DESCRIPTION); - accessPointElement.credentials(FAKE_USERNAME, FAKE_PASSWORD); + accessPointElement.address(RUNNING_ON); + accessPointElement.name(LOAD_BALANCER); + Group properties = accessPointElement.properties(); Property className = new Property(); @@ -148,15 +160,46 @@ public class PersistenceConfigurationTest { return serviceEndpoint; } + private void clean(){ + ScopeProvider.instance.set(GCUBE_DEVSEC_SCOPE); + + SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class) + .addCondition(String.format("$resource/Profile/Category/text() eq '%s'", PersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY)) + .addCondition(String.format("$resource/Profile/Name/text() eq '%s'", PersistenceConfiguration.SERVICE_ENDPOINT_NAME)) + .addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'", RUNNING_ON)) + .setResult("$resource"); + + DiscoveryClient client = ICFactory.clientFor(ServiceEndpoint.class); + List serviceEndpoints = client.submit(query); + + for (ServiceEndpoint serviceEndpoint : serviceEndpoints) { + try { + logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}", + serviceEndpoint.id(), GCUBE_DEVSEC_SCOPE); + unPublishScopedResource(serviceEndpoint); + } catch(Exception e){ + logger.debug("Exception trying to unpublish the old ServiceEndpoint with ID {} from scope {}", + serviceEndpoint.id(), GCUBE_DEVSEC_SCOPE, e); + } + } + } + @Test - public void testPersistenceFromIS() throws RegistryNotFoundException, Exception{ - List scopes = new ArrayList(); - scopes.add(TEST_SCOPE); + public void testPersistenceFromIS() throws Exception{ + + /* + List scopes = Arrays.asList(SCOPES); ServiceEndpoint serviceEndpoint = createServiceEndpoint(); publishScopedResource(serviceEndpoint, scopes); + */ - PersistenceConfiguration.getPersistenceConfiguration(TEST_SCOPE, COUCHDB_CLASS_NAME); + try { + String keyName = "devsec.gcubekey"; + Key key = SymmetricKey.getKey(System.getProperty(PersistenceTest.HOME_SYSTEM_PROPERTY)+ "/" + keyName); + PersistenceConfiguration.getPersistenceConfiguration(GCUBE_DEVSEC_SCOPE, COUCHDB_CLASS_NAME, key); + } finally { + //unPublishScopedResource(serviceEndpoint); + } - unPublishScopedResource(serviceEndpoint); } } diff --git a/src/test/java/org/gcube/accounting/persistence/SymmetricKey.java b/src/test/java/org/gcube/accounting/persistence/SymmetricKey.java new file mode 100644 index 0000000..80c98d0 --- /dev/null +++ b/src/test/java/org/gcube/accounting/persistence/SymmetricKey.java @@ -0,0 +1,75 @@ +package org.gcube.accounting.persistence; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.InvalidKeyException; +import java.security.Key; + +import javax.crypto.spec.SecretKeySpec; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.common.scope.impl.ScopeBean.Type; +//import org.apache.xml.security.utils.JavaUtils; + + +final class SymmetricKey { + + private static Key key; + + private static String keyAlgorithm = "AES"; + + //private constructor + private SymmetricKey() {} + + /** + * Gets the key for encryption/decryption + * @return the key + * @throws InvalidKeyException if the key is not available or is invalid + */ + public static Key getKey(String keyPath) throws InvalidKeyException { + if (key == null) load(keyPath); + return key; + } + + /** + * Loads the key from the classpaht + * @throws InvalidKeyException if the key is not available or is invalid + */ + private static void load(String keyPath) throws InvalidKeyException { + byte[] rawKey; + String keyFileName=null; + try { + InputStream is = new FileInputStream(new File(keyPath)); + rawKey = getBytesFromStream(is); + } catch (Exception e) { + System.out.println("Unable to load the Key "+keyFileName+" from the classpath"); + e.printStackTrace(); + throw new InvalidKeyException("Unable to load the Key "+keyFileName+" from the classpath"); + } + try { + key = new SecretKeySpec(rawKey, 0, rawKey.length, keyAlgorithm); + }catch (Exception e) { + e.printStackTrace(); + throw new InvalidKeyException(); + } + } + + private static byte[] getBytesFromStream(InputStream is) throws IOException { + byte[] rawKey; + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + int nRead; + byte[] data = new byte[16384]; + while ((nRead = is.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + buffer.flush(); + rawKey= buffer.toByteArray(); + return rawKey; + } + + +}