From 01d1a84dd923eab03b6c02fdd05a5f392418f5c7 Mon Sep 17 00:00:00 2001 From: "lucio.lelii" Date: Thu, 23 Jun 2016 16:14:16 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/Common/authorization-common-client@129385 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 15 ++++- .../client/proxy/AuthorizationProxy.java | 5 +- .../proxy/DefaultAuthorizationProxy.java | 60 +++++++++++++------ 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index 2a21aa5..85b441f 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,18 @@ 1.0.0 + + + + org.gcube.distribution + gcube-bom + 1.0.0-SNAPSHOT + pom + import + + + + distro @@ -20,19 +32,16 @@ org.gcube.common common-authorization - [2.0.0-SNAPSHOT,3.0.0-SNAPSHOT) org.gcube.core common-encryption - [2.0.0-SNAPSHOT,3.0.0-SNAPSHOT) org.slf4j slf4j-api - 1.7.5 junit diff --git a/src/main/java/org/gcube/common/authorization/client/proxy/AuthorizationProxy.java b/src/main/java/org/gcube/common/authorization/client/proxy/AuthorizationProxy.java index b14c609..3a61cf3 100644 --- a/src/main/java/org/gcube/common/authorization/client/proxy/AuthorizationProxy.java +++ b/src/main/java/org/gcube/common/authorization/client/proxy/AuthorizationProxy.java @@ -1,6 +1,7 @@ package org.gcube.common.authorization.client.proxy; import java.util.List; +import java.util.Map; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.library.AuthorizationEntry; @@ -11,9 +12,9 @@ import org.gcube.common.authorization.library.provider.ServiceInfo; public interface AuthorizationProxy { - AuthorizationEndpoint getEndpoint(); + AuthorizationEndpoint getEndpoint(int infrastructureHash); - void setEndpoint(AuthorizationEndpoint endpoint); + void setEndpoint(Map endpoints); AuthorizationEntry get(String token) throws ObjectNotFound, Exception; diff --git a/src/main/java/org/gcube/common/authorization/client/proxy/DefaultAuthorizationProxy.java b/src/main/java/org/gcube/common/authorization/client/proxy/DefaultAuthorizationProxy.java index 5d80662..67c1105 100644 --- a/src/main/java/org/gcube/common/authorization/client/proxy/DefaultAuthorizationProxy.java +++ b/src/main/java/org/gcube/common/authorization/client/proxy/DefaultAuthorizationProxy.java @@ -13,7 +13,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.WeakHashMap; import org.gcube.common.authorization.client.Binder; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; @@ -23,6 +23,7 @@ import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint; import org.gcube.common.authorization.library.enpoints.AuthorizationEndpointScanner; import org.gcube.common.authorization.library.policies.Policy; import org.gcube.common.authorization.library.provider.ClientInfo; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.ServiceInfo; import org.gcube.common.authorization.library.provider.UserInfo; import org.gcube.common.encryption.StringEncrypter; @@ -33,18 +34,19 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { private static Logger log = LoggerFactory.getLogger(AuthorizationProxy.class); - private static Map cache = new ConcurrentHashMap(); + private static Map cache = Collections.synchronizedMap(new WeakHashMap()); - private List endpoints; + private Map endpoints; + private static final String TOKEN_SEPARATOR ="|"; + public DefaultAuthorizationProxy() { endpoints = AuthorizationEndpointScanner.endpoints(); - this.setEndpoint(endpoints.get(0)); } - private String getInternalEnpoint(){ - StringBuilder endpoint = new StringBuilder("http://").append(getEndpoint().getHost()).append(":") - .append(getEndpoint().getPort()).append("/authorization-service/gcube/service"); + private String getInternalEnpoint(int infrastructureHash){ + StringBuilder endpoint = new StringBuilder("http://").append(getEndpoint(infrastructureHash).getHost()).append(":") + .append(getEndpoint(infrastructureHash).getPort()).append("/authorization-service/gcube/service"); return endpoint.toString(); } @@ -56,8 +58,10 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { if (client instanceof UserInfo) methodPath+="user"; else methodPath+="service"; - - StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append("?") + + int infrastructureHash = getInfrastructureHashfromContext(context); + + StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath).append("?") .append(CONTEXT_PARAM).append("=").append(context); URL url = new URL(callUrl.toString()); @@ -86,6 +90,24 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { return StringEncrypter.getEncrypter().decrypt(encryptedToken, context); } + private int getInfrastructureHashfromContext(String context) { + try{ + String infrastructure = context.split("/")[1]; + return infrastructure.hashCode(); + }catch(Exception e){ + throw new RuntimeException("invalid contex"); + } + } + + private int getInfrastructureHashFromToken(String token) { + try{ + String hashCodeAsString = token.split(TOKEN_SEPARATOR)[1]; + return Integer.parseInt(hashCodeAsString); + }catch(Exception e){ + throw new RuntimeException("invalid token"); + } + } + @Override public String requestActivation(ServiceInfo container, String context) throws Exception { @@ -93,7 +115,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { - StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append("?") + StringBuilder callUrl = new StringBuilder(getInternalEnpoint(getInfrastructureHashfromContext(context))).append(methodPath).append("?") .append(CONTEXT_PARAM).append("=").append(context); URL url = new URL(callUrl.toString()); @@ -129,7 +151,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { final String methodPath = "/retrieve/"; - StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append(token); + StringBuilder callUrl = new StringBuilder(getInternalEnpoint(getInfrastructureHashFromToken(token))).append(methodPath).append(token); URL url = new URL(callUrl.toString()); HttpURLConnection connection = makeRequest(url, "GET"); @@ -150,7 +172,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { public void addPolicies(List policies) throws Exception { final String methodPath = "/policyManager"; - StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath); + StringBuilder callUrl = new StringBuilder(getInternalEnpoint(getInfrastructureHashFromToken(SecurityTokenProvider.instance.get()))).append(methodPath); URL url = new URL(callUrl.toString()); HttpURLConnection connection = makeRequest(url, "POST"); @@ -168,7 +190,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { @Override public void removePolicies(long... ids) throws Exception { final String methodPath = "/policyManager/"; - StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath); + StringBuilder callUrl = new StringBuilder(getInternalEnpoint(getInfrastructureHashFromToken(SecurityTokenProvider.instance.get()))).append(methodPath); List errorIds = new ArrayList(); for (long id: ids){ URL url = new URL(callUrl.toString()+id); @@ -183,7 +205,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { public List getPolicies(String context) throws Exception{ final String methodPath = "/policyManager/"; - StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append("?").append(CONTEXT_PARAM).append("=").append(context); + StringBuilder callUrl = new StringBuilder(getInternalEnpoint(getInfrastructureHashfromContext(context))).append(methodPath).append("?").append(CONTEXT_PARAM).append("=").append(context); URL url = new URL(callUrl.toString()); HttpURLConnection connection = makeRequest(url, "GET"); @@ -205,13 +227,15 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { } @Override - public AuthorizationEndpoint getEndpoint() { - return this.endpoints.get(0); + public AuthorizationEndpoint getEndpoint(int infrastructureHash) { + if (!this.endpoints.containsKey(infrastructureHash)) + throw new RuntimeException("Authorization Endpoint not found for the required infrastructure"); + return this.endpoints.get(infrastructureHash); } @Override - public void setEndpoint(AuthorizationEndpoint authEndpoint) { - this.endpoints = Collections.singletonList(authEndpoint); + public void setEndpoint(Map endpoints) { + this.endpoints = endpoints; }