Added tests

This commit is contained in:
Luca Frosini 2020-01-20 10:33:56 +01:00
parent 1e8df14b84
commit ea72ce75e3
6 changed files with 138 additions and 78 deletions

18
pom.xml
View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@ -44,6 +45,21 @@
<groupId>org.gcube.common</groupId>
<artifactId>authorization-client</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>discovery-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>common-gcore-resources</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>

View File

@ -2,7 +2,6 @@ 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;
@ -10,42 +9,43 @@ 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.gcube.resourcemanagement.support.server.managers.context.ContextManager;
import org.gcube.resourcemanagement.support.shared.types.datamodel.D4SEnvironment;
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<String, String> getTokenForContexts(File voFile, String username, String serviceName) throws Exception{
Map<String, String> scopeTokenMap = new HashMap<String, String>();
public static Map<String, String> getTokenForContexts(String username, String serviceName) throws Exception{
Map<String, String> contextTokenMap = new HashMap<String, String>();
/*
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();
*/
try {
LinkedHashMap<String, ScopeBean> map = ScopeManager.readScopes(voFile.getAbsolutePath());
for(String scope : map.keySet()) {
LinkedHashMap<String, D4SEnvironment> contexts = ContextManager.readContextsWithUUIDs();
for(String context : contexts.keySet()) {
try {
String context = map.get(scope).toString();
//D4SEnvironment d4sEnvironment = contexts.get(context);
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);
contextTokenMap.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);
logger.error("Error while elaborating {}", context, e);
throw e;
} finally {
SecurityTokenProvider.instance.reset();
@ -55,23 +55,20 @@ public class TokensUtil {
throw ex;
}
/*
File tokenFile = new File("tokens-"+serviceName+".json");
objectMapper.writeValue(tokenFile, objectNode);
*/
return scopeTokenMap;
return contextTokenMap;
}
public static void main(String[] args) throws Exception {
if(args.length!=3) {
if(args.length!=2) {
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[0]);
if(!voFile.exists()) {
System.out.println(args[0] + " does not exists");
return;
}
getTokenForContexts(voFile, args[1], args[2]);
getTokenForContexts(args[1], args[2]);
}
}

View File

@ -0,0 +1,86 @@
/**
*
*/
package org.gcube.context.names;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
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.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
public class ContextTest {
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
protected static Properties properties;
protected static final String PROPERTIES_FILENAME = "token.properties";
public static final String DEFAULT_TEST_SCOPE_NAME;
static {
properties = new Properties();
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
try {
// load the properties file
properties.load(input);
} catch(IOException e) {
throw new RuntimeException(e);
}
//DEFAULT_TEST_SCOPE_NAME = "/gcube";
DEFAULT_TEST_SCOPE_NAME = "/pred4s";
//DEFAULT_TEST_SCOPE_NAME = "/d4science.research-infrastructures.eu";
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception {
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext();
logger.info("Context of token {} is {}", token, context);
return context;
}
public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception {
String token = ContextTest.properties.getProperty(fullContextName);
setContext(token);
}
public static void setContext(String token) throws ObjectNotFound, Exception {
SecurityTokenProvider.instance.set(token);
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
ClientInfo clientInfo = authorizationEntry.getClientInfo();
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name());
String qualifier = authorizationEntry.getQualifier();
Caller caller = new Caller(clientInfo, qualifier);
AuthorizationProvider.instance.set(caller);
ScopeProvider.instance.set(getCurrentScope(token));
}
@BeforeClass
public static void beforeClass() throws Exception {
setContextByName(DEFAULT_TEST_SCOPE_NAME);
}
@AfterClass
public static void afterClass() throws Exception {
SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset();
}
}

View File

@ -1,11 +1,17 @@
package org.gcube.context.names;
import java.io.File;
import java.util.Map;
import org.gcube.context.authorization.TokensUtil;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestContextList {
public class TestContextList extends ContextTest {
private static final Logger logger = LoggerFactory.getLogger(TokensUtil.class);
@Test
public void test() throws Exception {
File src = new File("src");
@ -19,4 +25,11 @@ public class TestContextList {
}
@Test
public void generateTokens() throws Exception {
Map<String, String> tokens = TokensUtil.getTokenForContexts("luca.frosini", "gCat");
logger.debug("{}", tokens);
}
}

1
src/test/resources/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/token.properties

View File

@ -1,53 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<infrastructures>
<infrastructure>
<name>d4science</name>
<vos>
<vo>
<name>d4science</name>
<src>ServiceMap_d4science.research-infrastructures.eu.xml</src>
<scope>/d4science.research-infrastructures.eu</scope>
</vo>
<vo>
<name>d4science/D4Research</name>
<src>ServiceMap_D4Research.xml</src>
<scope>/d4science.research-infrastructures.eu/D4Research</scope>
</vo>
<vo>
<name>d4science/Edison</name>
<src>ServiceMap_Edison.xml</src>
<scope>/d4science.research-infrastructures.eu/Edison</scope>
</vo>
<vo>
<name>d4science/FARM</name>
<src>ServiceMap_FARM.xml</src>
<scope>/d4science.research-infrastructures.eu/FARM</scope>
</vo>
<vo>
<name>d4science/gCubeApps</name>
<src>ServiceMap_gCubeApps.xml</src>
<scope>/d4science.research-infrastructures.eu/gCubeApps</scope>
</vo>
<vo>
<name>d4science/OpenAIRE</name>
<src>ServiceMap_OpenAIRE.xml</src>
<scope>/d4science.research-infrastructures.eu/OpenAIRE</scope>
</vo>
<vo>
<name>d4science/ParthenosVO</name>
<src>ServiceMap_ParthenosVO.xml</src>
<scope>/d4science.research-infrastructures.eu/ParthenosVO</scope>
</vo>
<vo>
<name>d4science/SmartArea</name>
<src>ServiceMap_SmartArea.xml</src>
<scope>/d4science.research-infrastructures.eu/SmartArea</scope>
</vo>
<vo>
<name>d4science/SoBigData</name>
<src>ServiceMap_SoBigData.xml</src>
<scope>/d4science.research-infrastructures.eu/SoBigData</scope>
</vo>
</vos>
</infrastructure>
</infrastructures>