From 4880954689e5f615567e38ddcba78c4037c4b3d4 Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Fri, 22 Jul 2016 10:11:00 +0000 Subject: [PATCH] git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/common-encryption@130698 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 38 ++++++++-- .settings/org.eclipse.jdt.core.prefs | 7 +- pom.xml | 62 ++++++++++------- .../gcube/common/encryption/SymmetricKey.java | 23 +++++-- .../gcube/common/encryption/LocalKeyTest.java | 25 ------- .../encryption/StringEncrypterTest.java | 7 +- .../common/encryption/SymmetricKeyTest.java | 69 +++++++++---------- 7 files changed, 125 insertions(+), 106 deletions(-) delete mode 100644 src/test/java/org/gcube/common/encryption/LocalKeyTest.java diff --git a/.classpath b/.classpath index 0f53f3e..e43402f 100644 --- a/.classpath +++ b/.classpath @@ -1,10 +1,36 @@ - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 8e8278c..6249222 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,13 +1,12 @@ -#Tue Mar 06 09:17:02 EST 2012 eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.6 +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/pom.xml b/pom.xml index 7089e20..4e5becd 100644 --- a/pom.xml +++ b/pom.xml @@ -16,42 +16,58 @@ scm:svn:https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/common-utils-encryption http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/common-utils-encryption + + + + + org.gcube.distribution + gcube-bom + 1.0.0-SNAPSHOT + pom + import + + + + distro + + org.gcube.common + authorization-client + + + org.gcube.common + common-authorization + junit junit 4.7 test - - org.gcube.core - common-scope - [2.0.0-SNAPSHOT,3.0.0-SNAPSHOT) - - - org.apache.maven.plugins - maven-assembly-plugin - - - ${distroDirectory}/descriptor.xml - - - - - servicearchive - install - - single - - - - + + org.apache.maven.plugins + maven-assembly-plugin + + + ${distroDirectory}/descriptor.xml + + + + + servicearchive + install + + single + + + + org.apache.maven.plugins diff --git a/src/main/java/org/gcube/common/encryption/SymmetricKey.java b/src/main/java/org/gcube/common/encryption/SymmetricKey.java index 58a2843..4d71425 100644 --- a/src/main/java/org/gcube/common/encryption/SymmetricKey.java +++ b/src/main/java/org/gcube/common/encryption/SymmetricKey.java @@ -8,9 +8,14 @@ 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.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.scope.impl.ContextBean; +import org.gcube.common.scope.impl.ContextBean.Type; + +import static org.gcube.common.authorization.client.Constants.authorizationService; + //import org.apache.xml.security.utils.JavaUtils; @@ -50,7 +55,15 @@ final class SymmetricKey { * @throws InvalidKeyException if the key is not available or is invalid */ private static void load() throws InvalidKeyException { - load(getKeyFileName(ScopeProvider.instance.get())); + String token = SecurityTokenProvider.instance.get(); + AuthorizationEntry entry; + try { + entry = authorizationService().get(token); + load(getKeyFileName(entry.getContext())); + } catch (Exception e) { + throw new InvalidKeyException("Unable to load the Key, token not valid",e); + } + } /** @@ -89,7 +102,7 @@ final class SymmetricKey { protected static String getKeyFileName(String context) throws InvalidKeyException{ String keyFile=null; if(context!=null){ - ScopeBean bean = new ScopeBean(context); + ContextBean bean = new ContextBean(context); if(bean.is(Type.VRE)) bean = bean.enclosingScope(); String name = bean.name(); diff --git a/src/test/java/org/gcube/common/encryption/LocalKeyTest.java b/src/test/java/org/gcube/common/encryption/LocalKeyTest.java deleted file mode 100644 index da05838..0000000 --- a/src/test/java/org/gcube/common/encryption/LocalKeyTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.gcube.common.encryption; - -import static org.junit.Assert.*; - -import java.security.InvalidKeyException; - -import org.gcube.common.encryption.SymmetricKey; -import org.gcube.common.scope.api.ScopeProvider; -import org.junit.BeforeClass; -import org.junit.Test; - -public class LocalKeyTest { - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - ScopeProvider.instance.set("/gcube/devsec"); - } - - @Test - public void test() throws InvalidKeyException { - String key=SymmetricKey.getKeyFileName(ScopeProvider.instance.get()); - System.out.println("file key founded: "+key); - } - -} diff --git a/src/test/java/org/gcube/common/encryption/StringEncrypterTest.java b/src/test/java/org/gcube/common/encryption/StringEncrypterTest.java index 26eb0bd..fc1665a 100644 --- a/src/test/java/org/gcube/common/encryption/StringEncrypterTest.java +++ b/src/test/java/org/gcube/common/encryption/StringEncrypterTest.java @@ -1,13 +1,9 @@ package org.gcube.common.encryption; -import static org.junit.Assert.*; +import static org.junit.Assert.fail; -import org.gcube.common.encryption.StringEncrypter; -import org.gcube.common.encryption.SymmetricKey; -import org.gcube.common.scope.api.ScopeProvider; import org.junit.AfterClass; import org.junit.BeforeClass; -import org.junit.Test; public class StringEncrypterTest { @@ -18,7 +14,6 @@ public class StringEncrypterTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - ScopeProvider.instance.set("/gcube/devsec"); key = SymmetricKey.getKey(); } diff --git a/src/test/java/org/gcube/common/encryption/SymmetricKeyTest.java b/src/test/java/org/gcube/common/encryption/SymmetricKeyTest.java index 394baf6..35a8674 100644 --- a/src/test/java/org/gcube/common/encryption/SymmetricKeyTest.java +++ b/src/test/java/org/gcube/common/encryption/SymmetricKeyTest.java @@ -1,28 +1,23 @@ package org.gcube.common.encryption; -import static org.junit.Assert.*; +import static org.junit.Assert.fail; -import java.io.InputStream; import java.security.InvalidKeyException; import java.security.Key; -import javax.crypto.spec.SecretKeySpec; - -//import org.apache.xml.security.utils.JavaUtils; -import org.gcube.common.encryption.SymmetricKey; -import org.gcube.common.scope.api.ScopeProvider; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +//import org.apache.xml.security.utils.JavaUtils; public class SymmetricKeyTest { Key key1; Key key2; - + @BeforeClass public static void setUpBeforeClass() throws Exception { - ScopeProvider.instance.set("/gcube/devsec"); + } @AfterClass @@ -42,33 +37,33 @@ public class SymmetricKeyTest { } } -// @Test -// public final void testGetKeyOld() throws InvalidKeyException{ -// String keyAlgorithm = "AES"; -// String localKey = "/symm.key"; -// byte[] rawKey; -// try { -// rawKey = JavaUtils.getBytesFromStream(SymmetricKey.class.getResourceAsStream(localKey)); -// } catch (Exception e) { -// System.out.println("Unable to load the Key from the classpath"); -// e.printStackTrace(); -// throw new InvalidKeyException(); -// } -// try { -// key2 = new SecretKeySpec(rawKey, 0, rawKey.length, keyAlgorithm); -// }catch (Exception e) { -// e.printStackTrace(); -// throw new InvalidKeyException(); -// } -// System.out.println("key successfully loaded"); -// System.out.println("key " + key2.getEncoded()); -// System.out.println("key algorithm " + key2.getAlgorithm()); -// } -// -// @Test -// public final void compare(){ -// assertEquals(key1, key2); -// } - + // @Test + // public final void testGetKeyOld() throws InvalidKeyException{ + // String keyAlgorithm = "AES"; + // String localKey = "/symm.key"; + // byte[] rawKey; + // try { + // rawKey = JavaUtils.getBytesFromStream(SymmetricKey.class.getResourceAsStream(localKey)); + // } catch (Exception e) { + // System.out.println("Unable to load the Key from the classpath"); + // e.printStackTrace(); + // throw new InvalidKeyException(); + // } + // try { + // key2 = new SecretKeySpec(rawKey, 0, rawKey.length, keyAlgorithm); + // }catch (Exception e) { + // e.printStackTrace(); + // throw new InvalidKeyException(); + // } + // System.out.println("key successfully loaded"); + // System.out.println("key " + key2.getEncoded()); + // System.out.println("key algorithm " + key2.getAlgorithm()); + // } + // + // @Test + // public final void compare(){ + // assertEquals(key1, key2); + // } + }