diff --git a/pom.xml b/pom.xml index d945233..985a2b5 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.gcube.common authorization-client - 2.0.7 + 3.0.0-SNAPSHOT authorization service client library @@ -11,20 +11,19 @@ org.gcube.tools 1.1.0 - scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git @@ -41,13 +40,11 @@ org.gcube.common common-authorization - [2.0.0, 3.0.0-SNAPSHOT) org.slf4j slf4j-api - 1.7.2 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 1a76ca8..64f24f8 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,18 +1,12 @@ package org.gcube.common.authorization.client.proxy; -import java.io.File; import java.util.List; -import java.util.Map; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint; import org.gcube.common.authorization.library.enpoints.EndpointsContainer; -import org.gcube.common.authorization.library.policies.Policy; -import org.gcube.common.authorization.library.provider.ContainerInfo; -import org.gcube.common.authorization.library.provider.ServiceInfo; import org.gcube.common.authorization.library.provider.UserInfo; -import org.gcube.common.authorization.library.utils.MultiServiceTokenRequest; public interface AuthorizationProxy { @@ -23,39 +17,13 @@ public interface AuthorizationProxy { AuthorizationEntry get(String token) throws ObjectNotFound, Exception; List get(List tokens) throws ObjectNotFound, Exception; - - void addPolicies(List policies) throws Exception; - - void removePolicies(long ... ids) throws Exception; - - List getPolicies(String context) throws Exception; - - String generateApiKey(String apiQualifier) throws Exception; - - String generateServiceToken(ServiceInfo client) throws Exception; - - List generateServiceToken(ServiceInfo client, List containerTokens) throws Exception; - String generateUserToken(UserInfo client, String context) throws Exception; - String requestActivation(ContainerInfo container) throws Exception; - - String requestActivation(ContainerInfo container, String context) throws Exception; - - Map retrieveApiKeys() throws Exception; - - File getSymmKey(String filePath) throws Exception; - String resolveTokenByUserAndContext(String user, String context) throws Exception; - String generateExternalServiceToken(String serviceId) - throws Exception; - - Map retrieveExternalServiceGenerated() throws Exception; - void removeAllReleatedToken(String clientId, String context) throws Exception; void setTokenRoles(String token, List roles) throws 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 63597a6..6c83f75 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 @@ -1,12 +1,10 @@ package org.gcube.common.authorization.client.proxy; -import static org.gcube.common.authorization.client.Constants.CONTEXT_PARAM; import static org.gcube.common.authorization.client.Constants.CLIENT_ID_PARAM; +import static org.gcube.common.authorization.client.Constants.CONTEXT_PARAM; import java.io.BufferedOutputStream; import java.io.BufferedReader; -import java.io.File; -import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; @@ -17,29 +15,18 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; import javax.net.ssl.HttpsURLConnection; import org.gcube.common.authorization.client.Binder; -import org.gcube.common.authorization.client.Constants; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.library.AuthorizationEntry; -import org.gcube.common.authorization.library.ExternalServiceList; -import org.gcube.common.authorization.library.Policies; -import org.gcube.common.authorization.library.QualifiersList; import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint; import org.gcube.common.authorization.library.enpoints.AuthorizationEndpointScanner; import org.gcube.common.authorization.library.enpoints.EndpointsContainer; -import org.gcube.common.authorization.library.policies.Policy; -import org.gcube.common.authorization.library.provider.ContainerInfo; -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.authorization.library.utils.AuthorizationEntryList; import org.gcube.common.authorization.library.utils.ListMapper; -import org.gcube.common.authorization.library.utils.MultiServiceTokenRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -68,111 +55,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { } - @Override - public String generateServiceToken(ServiceInfo client) throws Exception { - - String methodPath = "/token/service"; - - int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()); - - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "PUT", true); - connection.setDoOutput(true); - connection.setDoInput(true); - connection.setRequestProperty("Content-type", "application/xml"); - - try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){ - Binder.getContext().createMarshaller().marshal(client, os); - } - - log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage()); - - if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); - String token= ""; - try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()))){ - StringBuilder result = new StringBuilder(); - String line; - while((line = reader.readLine()) != null) - result.append(line); - token = result.toString(); - } - - return Utils.addInfrastructureHashToToken(token, infrastructureHash); - } - - - - @Override - public List generateServiceToken(ServiceInfo client, List containerTokens) throws Exception { - - String methodPath = "/token/service/bunch"; - - int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()); - - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath); - - - - List realTokens = containerTokens.stream().map(t -> Utils.getRealToken(t)).collect(Collectors.toList()); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "PUT", true); - connection.setDoOutput(true); - connection.setDoInput(true); - connection.setRequestProperty("Content-type", "application/xml"); - - try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){ - Binder.getContext().createMarshaller().marshal(new MultiServiceTokenRequest(realTokens, client), os); - } - - log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage()); - - if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); - List tokensToReturn = new ArrayList(); - - try(InputStream stream = (InputStream)connection.getContent();){ - ListMapper entries = (ListMapper)Binder.getContext().createUnmarshaller().unmarshal(stream); - - for (String token: entries.getList()) - tokensToReturn.add(Utils.addInfrastructureHashToToken(token, infrastructureHash)); - return tokensToReturn; - } - - - } - - @Override - public String generateExternalServiceToken(String serviceId) throws Exception { - - String methodPath = "/token/external/"; - - int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()); - - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath).append(serviceId); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "PUT", true); - connection.setDoInput(true); - connection.setRequestProperty("Content-type", "application/xml"); - - log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage()); - - if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); - String token= ""; - try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()))){ - StringBuilder result = new StringBuilder(); - String line; - while((line = reader.readLine()) != null) - result.append(line); - token = result.toString(); - } - - return Utils.addInfrastructureHashToToken(token, infrastructureHash); - } - @Override public String resolveTokenByUserAndContext(String user, String context) throws ObjectNotFound, Exception { @@ -183,7 +66,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath).append(user).append("?context=").append(context); URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "GET", false); + HttpURLConnection connection = makeRequest(url, "GET"); log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage()); @@ -213,7 +96,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { .append(CONTEXT_PARAM).append("=").append(context); URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "PUT", false); + HttpURLConnection connection = makeRequest(url, "PUT"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "application/xml"); @@ -248,7 +131,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath); URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "PUT", false); + HttpURLConnection connection = makeRequest(url, "PUT"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "application/xml"); @@ -274,7 +157,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { .append(CONTEXT_PARAM).append("=").append(context).append("&").append(CLIENT_ID_PARAM).append("=").append(clientId); URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "DELETE", false); + HttpURLConnection connection = makeRequest(url, "DELETE"); //connection.setDoOutput(false); connection.setDoInput(true); @@ -283,181 +166,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { if (connection.getResponseCode()!=200 && connection.getResponseCode()!=204) throw new Exception("error contacting authorization service"); } - - @Override - public String generateApiKey(String apiQualifier) throws Exception { - - - String methodPath = String.format("/apikey?qualifier=%s",apiQualifier); - - int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()); - - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "PUT", true); - connection.setDoInput(true); - connection.setDoOutput(true); - connection.setFixedLengthStreamingMode(0); - connection.setRequestProperty("Content-type", "application/xml"); - - - log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage()); - - if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); - String token= ""; - try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()))){ - StringBuilder result = new StringBuilder(); - String line; - while((line = reader.readLine()) != null) - result.append(line); - token = result.toString(); - } - - return Utils.addInfrastructureHashToToken(token, infrastructureHash); - } - - @Override - /** - * return a map with key qualifier and value token - */ - public Map retrieveApiKeys() throws Exception{ - String methodPath = "/apikey/"; - - int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()); - - - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "GET", true); - connection.setDoInput(true); - connection.setDoOutput(true); - - if (connection.getResponseCode()!=200) throw new Exception("error retrieving keys (error code is "+connection.getResponseCode()+")"); - if (connection.getContentLengthLong()==0) return Collections.emptyMap(); - - Map tokensQulifiersMap; - try(InputStream stream = (InputStream)connection.getContent();){ - QualifiersList entries = (QualifiersList)Binder.getContext().createUnmarshaller().unmarshal(stream); - tokensQulifiersMap = entries.getQualifiers(); - } - - if (tokensQulifiersMap!=null && !tokensQulifiersMap.isEmpty()){ - Map toReturnMap = new HashMap(); - for (Entry entry: tokensQulifiersMap.entrySet()) - toReturnMap.put(entry.getKey(), Utils.addInfrastructureHashToToken(entry.getValue(), infrastructureHash)); - return toReturnMap; - } else return Collections.emptyMap(); - - } - - @Override - /** - * return a map with key external service id and value token - */ - public Map retrieveExternalServiceGenerated() throws Exception{ - String methodPath = "/token/external"; - - int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()); - - - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "GET", true); - connection.setDoInput(true); - connection.setDoOutput(true); - - if (connection.getResponseCode()!=200) throw new Exception("error retrieving externalServices (error code is "+connection.getResponseCode()+")"); - if (connection.getContentLengthLong()==0) return Collections.emptyMap(); - - Map externalServiceMap; - try(InputStream stream = (InputStream)connection.getContent();){ - ExternalServiceList entries = (ExternalServiceList)Binder.getContext().createUnmarshaller().unmarshal(stream); - externalServiceMap = entries.getExternalServiceMap(); - } - - if (externalServiceMap!=null && !externalServiceMap.isEmpty()){ - Map toReturnMap = new HashMap(); - for (Entry entry: externalServiceMap.entrySet()) - toReturnMap.put(entry.getKey(), Utils.addInfrastructureHashToToken(entry.getValue(), infrastructureHash)); - return toReturnMap; - } else return Collections.emptyMap(); - - } - - @Override - public String requestActivation(ContainerInfo container, String context) throws Exception { - - String methodPath = "/token/node"; - - int infrastructureHash = Utils.getInfrastructureHashfromContext(context); - - StringBuilder callUrl; - - callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath).append("?context=").append(context); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "PUT", false); - connection.setDoOutput(true); - connection.setDoInput(true); - connection.setRequestProperty("Content-type", "application/xml"); - - try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){ - Binder.getContext().createMarshaller().marshal(container, os); - } - - log.debug("response code is "+connection.getResponseCode()); - - if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); - String token= ""; - try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()))){ - StringBuilder result = new StringBuilder(); - String line; - while((line = reader.readLine()) != null) - result.append(line); - token = result.toString(); - } - return Utils.addInfrastructureHashToToken(token, infrastructureHash); - } - - - @Override - public String requestActivation(ContainerInfo container) throws Exception { - - String methodPath = "/token/node"; - - int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()); - - StringBuilder callUrl; - - callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "PUT", true); - connection.setDoOutput(true); - connection.setDoInput(true); - connection.setRequestProperty("Content-type", "application/xml"); - - try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){ - Binder.getContext().createMarshaller().marshal(container, os); - } - - log.debug("response code is "+connection.getResponseCode()); - - if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); - String token= ""; - try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()))){ - StringBuilder result = new StringBuilder(); - String line; - while((line = reader.readLine()) != null) - result.append(line); - token = result.toString(); - } - return Utils.addInfrastructureHashToToken(token, infrastructureHash); - } @Override public AuthorizationEntry get(String token) throws ObjectNotFound, Exception{ @@ -478,7 +187,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { .append(methodPath).append(realToken); URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "GET", false); + HttpURLConnection connection = makeRequest(url, "GET"); connection.setDoInput(true); if (connection.getResponseCode()==404) throw new ObjectNotFound("token "+maskedToken+" not found"); @@ -529,7 +238,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { } URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "GET", false); + HttpURLConnection connection = makeRequest(url, "GET"); connection.setDoInput(true); if (connection.getResponseCode()==404) throw new ObjectNotFound("token not found"); @@ -543,103 +252,12 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { } - @Override - public void addPolicies(List policies) throws Exception { - final String methodPath = "/policyManager"; - - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()))).append(methodPath); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "POST", true); - connection.setDoOutput(true); - connection.setRequestProperty("Content-type", "application/xml"); - - try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){ - Binder.getContext().createMarshaller().marshal(new Policies(policies), os); - } - - if (connection.getResponseCode()!=200) throw new Exception("error adding policies"); - - } - - @Override - public void removePolicies(long... ids) throws Exception { - final String methodPath = "/policyManager/"; - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()))).append(methodPath); - List errorIds = new ArrayList(); - for (long id: ids){ - URL url = new URL(callUrl.toString()+id); - HttpURLConnection connection = makeRequest(url, "DELETE", true); - if (connection.getResponseCode()!=200) errorIds.add(id); - } - if (!errorIds.isEmpty()) - throw new Exception("error removing policies with ids: "+errorIds); - } - - @Override - public List getPolicies(String context) throws Exception{ - final String methodPath = "/policyManager/"; - - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashfromContext(context))).append(methodPath).append("?").append(CONTEXT_PARAM).append("=").append(context); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "GET", true); - connection.setDoInput(true); - if (connection.getResponseCode()!=200){ - log.info("response code is not 200"); - throw new Exception("error retrieving policies"); - } - if (connection.getContentLengthLong()==0) - return Collections.emptyList(); - - try(InputStreamReader stream = new InputStreamReader((InputStream)connection.getContent())){ - Policies policies = (Policies)Binder.getContext().createUnmarshaller().unmarshal(stream); - return policies.getPolicies(); - } - } - - - @Override - public File getSymmKey(String filePath) throws Exception{ - final String methodPath = "/symmKey/"; - - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()))) - .append(methodPath); - - URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "GET", true); - connection.setDoInput(true); - if (connection.getResponseCode()!=200) throw new Exception("error retrieving key"); - if (connection.getContentLengthLong()<=0) return null; - - String resourceName = (String)connection.getHeaderField("resource-name"); - File toReturnFile = new File(filePath+"/"+resourceName); - toReturnFile.createNewFile(); - - try(InputStream stream = (InputStream)connection.getContent(); - OutputStream os = new FileOutputStream(toReturnFile)){ - - int read = 0; - byte[] bytes = new byte[1024]; - - while ((read = stream.read(bytes)) != -1) { - os.write(bytes, 0, read); - } - } - return toReturnFile; - - } - - private HttpURLConnection makeRequest(URL url, String method, boolean includeTokenInHeader) throws Exception{ + private HttpURLConnection makeRequest(URL url, String method) throws Exception{ HttpURLConnection connection; if (url.toString().startsWith("https://")) connection = (HttpsURLConnection)url.openConnection(); else connection = (HttpURLConnection)url.openConnection(); - - if (includeTokenInHeader){ - if (SecurityTokenProvider.instance.get()==null) throw new RuntimeException("null token passed"); - connection.setRequestProperty(Constants.TOKEN_HEADER_ENTRY,Utils.getRealToken(SecurityTokenProvider.instance.get())); - } + connection.setRequestMethod(method); return connection; } diff --git a/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java b/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java index 9a251eb..854cd3b 100644 --- a/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java +++ b/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java @@ -2,32 +2,10 @@ package org.gcube.common.authorizationservice.cl; import static org.gcube.common.authorization.client.Constants.authorizationService; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.FileInputStream; -import java.io.FileWriter; -import java.io.InputStreamReader; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.gcube.common.authorization.client.proxy.AuthorizationProxy; import org.gcube.common.authorization.library.AuthorizationEntry; -import org.gcube.common.authorization.library.policies.Action; -import org.gcube.common.authorization.library.policies.Policy; -import org.gcube.common.authorization.library.policies.ServiceAccess; -import org.gcube.common.authorization.library.policies.User2ServicePolicy; -import org.gcube.common.authorization.library.policies.Users; -import org.gcube.common.authorization.library.provider.ContainerInfo; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.gcube.common.authorization.library.provider.ServiceIdentifier; -import org.gcube.common.authorization.library.provider.ServiceInfo; import org.gcube.common.authorization.library.provider.UserInfo; -import org.junit.Ignore; import org.junit.Test; public class CallTest { @@ -37,12 +15,11 @@ public class CallTest { System.out.println("pred4s".hashCode() & 0xfffffff); } - - @Test public void getToken() throws Exception { - System.out.println(authorizationService().get("a6cec25b-3844-4901-83f3-95eee83319ba-980114272").getClientInfo().getRoles()); - System.out.println(authorizationService().get("a6cec25b-3844-4901-83f3-95eee83319ba-980114272").getContext()); + System.out.println(authorizationService().get("2b04d8-2bda-4f5b-a07e-c0468f95ce3e-843339462")); + //System.out.println(authorizationService().get("25bd7116-59f3-46dc-8ff3-c5c1a31c95cf-980114272").getContext()); + //System.out.println(authorizationService().get("b7c80297-e4ed-42ab-ab42-fdc0b8b0eabf-98187548").getMap()); } @Test @@ -50,84 +27,11 @@ public class CallTest { authorizationService().removeAllReleatedToken("lucio.lelii", "/gcube/devsec"); } - @Test - public void requestExternalServiceToken() throws Exception { - SecurityTokenProvider.instance.set(requestTestToken("/gcube")); - System.out.println(authorizationService().generateExternalServiceToken("URIResolver")); - } - - @Test - public void getExternalServiceToken() throws Exception { - SecurityTokenProvider.instance.set(requestTestToken("/gcube/devsec")); - System.out.println(authorizationService().retrieveExternalServiceGenerated()); - } - - @Test - public void requestNodeToken() throws Exception { - System.out.println(_requestNodeToken()); - } - - @Test - public void addPolicy() throws Exception { - SecurityTokenProvider.instance.set(requestTestToken("/gcube/devNext/NextNext")); - List policies = new ArrayList(); - policies.add(new User2ServicePolicy("/gcube/devNext/NextNext", new ServiceAccess(), Users.one("lucio.lelii"), Action.ACCESS )); - authorizationService().addPolicies(policies); - } - - - @Test - public void getPolicies() throws Exception{ - SecurityTokenProvider.instance.set(requestTestToken("/gcube/devNext")); - List policies = authorizationService().getPolicies("/gcube/devsec"); - for (Policy policy: policies) - System.out.println(policy); - } - - @Test - public void removePolicy() throws Exception { - authorizationService().removePolicies(2, 3, 4); - } @Test public void generateToken() throws Exception{ System.out.println(authorizationService().generateUserToken(new UserInfo("guest", new ArrayList()), "/pred4s")); } - @Test(expected=RuntimeException.class) - public void createKeyWithError() throws Exception { - authorizationService().generateApiKey("TEST"); - } - - @Test - public void getSymmKey() throws Exception{ - SecurityTokenProvider.instance.set(_requestNodeToken()); - authorizationService().getSymmKey("/tmp"); - } - - - @Test - public void createKey() throws Exception { - String token = requestTestToken("/gcube"); - SecurityTokenProvider.instance.set(token); - String key = authorizationService().generateApiKey("PIPPO"); - System.out.println("key : "+key); - System.out.println(resolveToken(key)); - } - - @Test - public void retrieveApiKeys() throws Exception { - String token = requestTestToken("/gcube/devNext"); - SecurityTokenProvider.instance.set(token); - Map keys = authorizationService().retrieveApiKeys(); - System.out.println("keys : "+keys); - - } - - public String _requestNodeToken() throws Exception { - SecurityTokenProvider.instance.set(requestTestToken("/gcube")); - String token = authorizationService().requestActivation(new ContainerInfo("workspace-repository1-d.d4science.org",80), "/gcube/devsec"); - return token; - } @Test public void createTestToken() throws Exception { @@ -143,131 +47,5 @@ public class CallTest { return entry; } - /* List scopes = Arrays.asList("/d4science.research-infrastructures.eu/gCubeApps/TabularDataLab", -"/d4science.research-infrastructures.eu/FARM/AquaMaps", -"/d4science.research-infrastructures.eu/FARM/WECAFC-FIRMS", -"/d4science.research-infrastructures.eu/gCubeApps/PGFA-UFMT", -"/d4science.research-infrastructures.eu/FARM", -"/d4science.research-infrastructures.eu/gCubeApps/EcologicalModelling", -"/d4science.research-infrastructures.eu/gCubeApps/EuBrazilOpenBio", -"/d4science.research-infrastructures.eu/gCubeApps/AlieiaVRE", - /d4science.research-infrastructures.eu/gCubeApps/ENVRIPlus", - /d4science.research-infrastructures.eu/gCubeApps/ENVRI", - /d4science.research-infrastructures.eu/gCubeApps/ICES_DASC", - /d4science.research-infrastructures.eu/gCubeApps/FAO_TunaAtlas", - /d4science.research-infrastructures.eu/SoBigData/ResourceCatalogue", - /d4science.research-infrastructures.eu/gCubeApps/StocksAndFisheriesKB", - /d4science.research-infrastructures.eu/SoBigData/TagMe", - /d4science.research-infrastructures.eu/gCubeApps/BlueCommons", - /d4science.research-infrastructures.eu/gCubeApps/ICES_TCSSM", - /d4science.research-infrastructures.eu/gCubeApps/OpenIt", - /d4science.research-infrastructures.eu/SmartArea/SmartApps", - /d4science.research-infrastructures.eu", - /d4science.research-infrastructures.eu/gCubeApps/Parthenos", - /d4science.research-infrastructures.eu/gCubeApps/AquacultureAtlasGeneration", - /d4science.research-infrastructures.eu/gCubeApps/IGDI", - /d4science.research-infrastructures.eu/gCubeApps/RStudioLab", - /d4science.research-infrastructures.eu/SoBigData", - /d4science.research-infrastructures.eu/gCubeApps/BlueBridgeProject", - /d4science.research-infrastructures.eu/gCubeApps/rScience", - /d4science.research-infrastructures.eu/FARM/VME-DB", - /d4science.research-infrastructures.eu/gCubeApps/EllinikaPsariaVRE", - /d4science.research-infrastructures.eu/gCubeApps/gCube", - /d4science.research-infrastructures.eu/FARM/TBTI_VRE", - /d4science.research-infrastructures.eu/FARM/GRSF", - /d4science.research-infrastructures.eu/gCubeApps/DocumentsWorkflow", - /d4science.research-infrastructures.eu/gCubeApps/ICOS_ETC", - /d4science.research-infrastructures.eu/SoBigData/CityOfCitizens", - /d4science.research-infrastructures.eu/gCubeApps/SoBigData.eu", - /d4science.research-infrastructures.eu/gCubeApps/BiOnym", - /d4science.research-infrastructures.eu/gCubeApps/PerformanceEvaluationInAquaculture", - /d4science.research-infrastructures.eu/gCubeApps/SmartArea", - /d4science.research-infrastructures.eu/gCubeApps/ICES_TCRE", - /d4science.research-infrastructures.eu/gCubeApps/CNR_OpenScienceTF", - /d4science.research-infrastructures.eu/gCubeApps/BlueBRIDGE-PSC", - /d4science.research-infrastructures.eu/gCubeApps/BOBLME_HilsaAWG", - /d4science.research-infrastructures.eu/gCubeApps/ScalableDataMining", - /d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab", - /d4science.research-infrastructures.eu/gCubeApps/DESCRAMBLE", - /d4science.research-infrastructures.eu/gCubeApps/ICES_FIACO", - /d4science.research-infrastructures.eu/gCubeApps/BlueBRIDGE-EAB", - /d4science.research-infrastructures.eu/gCubeApps/ARIADNE", - /d4science.research-infrastructures.eu/SmartArea/SmartBuilding", - /d4science.research-infrastructures.eu/gCubeApps/ProtectedAreaImpactMaps", - /d4science.research-infrastructures.eu/gCubeApps/ForkysVRE", - /d4science.research-infrastructures.eu/gCubeApps/EGIEngage", - /d4science.research-infrastructures.eu/gCubeApps/ICES_StockAssessmentAdvanced", - /d4science.research-infrastructures.eu/FARM/GRSF", - /d4science.research-infrastructures.eu/SmartArea", - /d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab", - /d4science.research-infrastructures.eu/gCubeApps/TCom", - /d4science.research-infrastructures.eu/gCubeApps/ICCAT_BFT-E", - /d4science.research-infrastructures.eu/gCubeApps/ICES_DALSA", - /d4science.research-infrastructures.eu/gCubeApps/SoBigData.it", - /d4science.research-infrastructures.eu/gCubeApps/EGIP", - /d4science.research-infrastructures.eu/gCubeApps/BlueUptake", - /d4science.research-infrastructures.eu/FARM/iMarineBoardVRE", - /d4science.research-infrastructures.eu/gCubeApps/KnowledgeBridging", - /d4science.research-infrastructures.eu/gCubeApps/EFG", - /d4science.research-infrastructures.eu/gCubeApps/StockAssessment", - /d4science.research-infrastructures.eu/gCubeApps/iSearch", -"/d4science.research-infrastructures.eu/gCubeApps", -"/d4science.research-infrastructures.eu/gCubeApps/StrategicInvestmentAnalysis")*/ - - @Test - public void requestListOfTokenForVREs() throws Exception{ - SecurityTokenProvider.instance.set(requestTestToken("/d4science.research-infrastructures.eu")); - - String jrNode ="tabulardata.d4science.org"; - int jrPort =8080; - - AuthorizationProxy proxy = authorizationService(); - - try(BufferedReader isr = new BufferedReader(new InputStreamReader(new FileInputStream("./src/test/resources/Scopes.txt"))); - BufferedWriter bw = new BufferedWriter(new FileWriter("./src/test/resources/tokens-"+jrNode+".txt"))){ - String line = null; - - - while ( (line=isr.readLine())!=null){ - System.out.println(" retrieving token for scope "+line); - String token = proxy.requestActivation(new ContainerInfo(jrNode,jrPort), line.trim()); - bw.write(String.format("%s", token)); - bw.newLine(); - } - } - } - @Ignore @Test - public void checkDiff() throws Exception{ - - - Set scopes = new HashSet(); - - try(BufferedReader isr = new BufferedReader(new InputStreamReader(new FileInputStream("./src/test/resources/Scopes.txt")))){ - String line = null; - - while ( (line=isr.readLine())!=null){ - System.out.println(scopes.size()+" "+line); - if (scopes.contains(line.trim())) - System.out.println("already contians "+line); - else scopes.add(line.trim()); - } - } - - System.out.println("initial scope count is "+scopes.size()); - - - try(BufferedReader isr = new BufferedReader(new InputStreamReader(new FileInputStream("./src/test/resources/createdScope.txt")))){ - String line = null; - - - while ( (line=isr.readLine())!=null){ - if (!scopes.remove(line.trim())) - System.out.println("cannot remove scope "+line.trim()); - } - } - - System.out.println("final scope count is "+scopes.size()); - - } }