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
This commit is contained in:
Luca Frosini 2015-07-03 15:07:23 +00:00
parent c4cfd8b5a4
commit 3ca48b3441
4 changed files with 137 additions and 28 deletions

View File

@ -62,11 +62,6 @@
<artifactId>registry-publisher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

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

View File

@ -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<String> scopes) throws RegistryNotFoundException, Exception {
private static void publishScopedResource(Resource resource, List<String> 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<AccessPoint> 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<Property> 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<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> 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<String> scopes = new ArrayList<String>();
scopes.add(TEST_SCOPE);
public void testPersistenceFromIS() throws Exception{
/*
List<String> 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);
}
}

View File

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