From e7fb9d0d1d3e23ceeee635957a9fca031e904600 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 6 Apr 2018 09:40:20 +0000 Subject: [PATCH] Creating a jar can be invoke from cli to generate tokens for all scopes refs #11584 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/context-enumeration@165652 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 36 +++++ .project | 23 +++ pom.xml | 152 ++++++++++++++++++ .../org/gcube/context/ContextElaborator.java | 43 +++++ .../context/authorization/TokensUtil.java | 78 +++++++++ .../org/gcube/context/names/ContextList.java | 43 +++++ .../gcube/context/names/TestContextList.java | 24 +++ src/test/resources/logback-test.xml | 19 +++ src/test/resources/scopedata.xml | 53 ++++++ 9 files changed, 471 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/context/ContextElaborator.java create mode 100644 src/main/java/org/gcube/context/authorization/TokensUtil.java create mode 100644 src/main/java/org/gcube/context/names/ContextList.java create mode 100644 src/test/java/org/gcube/context/names/TestContextList.java create mode 100644 src/test/resources/logback-test.xml create mode 100644 src/test/resources/scopedata.xml diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fae1a2b --- /dev/null +++ b/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..fbef3b5 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + context-enumeration + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ea3f521 --- /dev/null +++ b/pom.xml @@ -0,0 +1,152 @@ + + 4.0.0 + + org.gcube.tools + maven-parent + 1.0.0 + + org.gcube.information-system + context-enumeration + 1.0.0-SNAPSHOT + Context Creation + + + UTF-8 + ${project.basedir}/src/main/webapp/WEB-INF + ${project.basedir}/distro + InformationSystem + + + + scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId} + scm:https://svn.d4science.research-infrastructures.eu/gcube//trunk/information-system/${project.artifactId} + https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId} + + + + + + org.gcube.distribution + gcube-bom + LATEST + pom + import + + + org.gcube.distribution + gcube-smartgears-bom + LATEST + pom + import + + + org.gcube.information-system + information-system-bom + LATEST + pom + import + + + + + + + org.gcube.portlets.admin + rmp-common-library + [2.7.1-SNAPSHOT,) + + + + org.gcube.core + common-scope + + + + org.gcube.resources + registry-publisher + + + org.gcube.resources.discovery + ic-client + + + org.gcube.resources.discovery + discovery-client + + + org.gcube.common + authorization-client + + + org.gcube.common + common-authorization + + + org.gcube.core + common-configuration-scanner + compile + + + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.core + jackson-annotations + + + com.fasterxml.jackson.core + jackson-core + + + + org.slf4j + slf4j-api + + + + + junit + junit + 4.11 + test + + + ch.qos.logback + logback-classic + 1.0.13 + test + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + true + org.gcube.context.authorization.TokensUtil + + + + jar-with-dependencies + + + + + package + + single + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/context/ContextElaborator.java b/src/main/java/org/gcube/context/ContextElaborator.java new file mode 100644 index 0000000..ee93c6a --- /dev/null +++ b/src/main/java/org/gcube/context/ContextElaborator.java @@ -0,0 +1,43 @@ +package org.gcube.context; + +import java.io.File; +import java.util.LinkedHashMap; + +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.resourcemanagement.support.server.managers.scope.ScopeManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public abstract class ContextElaborator { + + protected Logger logger = LoggerFactory.getLogger(this.getClass()); + + public ContextElaborator(){ + + } + + public void all(File voFile) throws Exception{ + try { + + LinkedHashMap map = ScopeManager.readScopes(voFile.getAbsolutePath()); + for(String scope : map.keySet()) { + try { + String context = map.get(scope).toString(); + logger.debug("Going to elaborate {}", context); + elaborateContext(map.get(scope)); + }catch (Exception e) { + logger.error("Error while elaborating {}", scope, e); + throw e; + } + } + } catch (Exception ex) { + throw ex; + } + } + + protected abstract void elaborateContext(ScopeBean scopeBean) throws Exception; + +} diff --git a/src/main/java/org/gcube/context/authorization/TokensUtil.java b/src/main/java/org/gcube/context/authorization/TokensUtil.java new file mode 100644 index 0000000..3202fb8 --- /dev/null +++ b/src/main/java/org/gcube/context/authorization/TokensUtil.java @@ -0,0 +1,78 @@ +package org.gcube.context.authorization; + +import static org.gcube.common.authorization.client.Constants.authorizationService; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.authorization.library.provider.UserInfo; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.resourcemanagement.support.server.managers.scope.ScopeManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +public class TokensUtil { + + private static final Logger logger = LoggerFactory.getLogger(TokensUtil.class); + + + public static Map getTokenForContexts(File voFile, String username, String serviceName) throws Exception{ + Map scopeTokenMap = new HashMap(); + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode objectNode = objectMapper.createObjectNode(); + + try { + + LinkedHashMap map = ScopeManager.readScopes(voFile.getAbsolutePath()); + for(String scope : map.keySet()) { + try { + String context = map.get(scope).toString(); + System.out.println("Going to generate Token for Context " + context); + logger.info("Going to generate Token for Context {}", context); + UserInfo userInfo = new UserInfo(username, new ArrayList<>()); + String userToken = authorizationService().generateUserToken(userInfo, context); + SecurityTokenProvider.instance.set(userToken); + String generatedToken = authorizationService().generateExternalServiceToken(serviceName); + scopeTokenMap.put(context, generatedToken); + objectNode.put(context, generatedToken); + + logger.info("Token for Context {} is {}", context, generatedToken); + }catch (Exception e) { + logger.error("Error while elaborating {}", scope, e); + throw e; + } finally { + SecurityTokenProvider.instance.reset(); + } + } + } catch (Exception ex) { + throw ex; + } + + File tokenFile = new File("tokens-"+serviceName+".json"); + objectMapper.writeValue(tokenFile, objectNode); + + return scopeTokenMap; + } + + public static void main(String[] args) throws Exception { + System.out.println(args); + if(args.length!=4) { + System.out.println("Please provide 'vo file path' as first argument 'username' as second and 'external application name' as third"); + return; + } + File voFile = new File(args[1]); + if(!voFile.exists()) { + System.out.println(args[1] + " does not exists"); + return; + } + getTokenForContexts(voFile, args[2], args[3]); + } + +} diff --git a/src/main/java/org/gcube/context/names/ContextList.java b/src/main/java/org/gcube/context/names/ContextList.java new file mode 100644 index 0000000..cb8697a --- /dev/null +++ b/src/main/java/org/gcube/context/names/ContextList.java @@ -0,0 +1,43 @@ +package org.gcube.context.names; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; + +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.context.ContextElaborator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ContextList extends ContextElaborator { + + protected Logger logger = LoggerFactory.getLogger(ContextList.class); + + protected File outputFile; + + public ContextList(File outputFile) { + this.outputFile = outputFile; + } + + public static void printLine(File file, String line) throws Exception { + synchronized (file) { + try (FileWriter fw = new FileWriter(file, true); + BufferedWriter bw = new BufferedWriter(fw); + PrintWriter out = new PrintWriter(bw)) { + out.println(line); + out.flush(); + } catch (IOException e) { + throw e; + } + } + } + + + @Override + protected void elaborateContext(ScopeBean scopeBean) throws Exception { + printLine(outputFile, scopeBean.toString()); + } + +} diff --git a/src/test/java/org/gcube/context/names/TestContextList.java b/src/test/java/org/gcube/context/names/TestContextList.java new file mode 100644 index 0000000..d15746c --- /dev/null +++ b/src/test/java/org/gcube/context/names/TestContextList.java @@ -0,0 +1,24 @@ +package org.gcube.context.names; + +import java.io.File; + +import org.junit.Test; + +public class TestContextList { + + @Test + public void test() throws Exception { + File src = new File("src"); + File test = new File(src, "test"); + File resources = new File(test, "resources"); + + File voFile = new File(resources, "scopedata.xml"); + + File outputFile = new File(resources, "output.txt"); + + ContextList contextList = new ContextList(outputFile); + contextList.all(voFile); + + } + +} diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 0000000..0c67654 --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,19 @@ + + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/scopedata.xml b/src/test/resources/scopedata.xml new file mode 100644 index 0000000..a84d476 --- /dev/null +++ b/src/test/resources/scopedata.xml @@ -0,0 +1,53 @@ + + + + d4science + + + d4science + ServiceMap_d4science.research-infrastructures.eu.xml + /d4science.research-infrastructures.eu + + + d4science/D4Research + ServiceMap_D4Research.xml + /d4science.research-infrastructures.eu/D4Research + + + d4science/Edison + ServiceMap_Edison.xml + /d4science.research-infrastructures.eu/Edison + + + d4science/FARM + ServiceMap_FARM.xml + /d4science.research-infrastructures.eu/FARM + + + d4science/gCubeApps + ServiceMap_gCubeApps.xml + /d4science.research-infrastructures.eu/gCubeApps + + + d4science/OpenAIRE + ServiceMap_OpenAIRE.xml + /d4science.research-infrastructures.eu/OpenAIRE + + + d4science/ParthenosVO + ServiceMap_ParthenosVO.xml + /d4science.research-infrastructures.eu/ParthenosVO + + + d4science/SmartArea + ServiceMap_SmartArea.xml + /d4science.research-infrastructures.eu/SmartArea + + + d4science/SoBigData + ServiceMap_SoBigData.xml + /d4science.research-infrastructures.eu/SoBigData + + + +