Added token generations by iterating contexts read dinamically

Refs #11756: Refactor DataHArvesterPlugin to support scheduled execution from smart-executor 

Task-Url: https://support.d4science.org/issues/11756

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-dashboard-harvester-se-plugin@167556 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2018-05-17 10:52:17 +00:00
parent d14cdc9675
commit 1430fb676f
10 changed files with 228 additions and 10 deletions

24
pom.xml
View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-dashboard-harvester-se-plugin</artifactId>
@ -22,6 +24,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-smartgears-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
@ -70,7 +79,7 @@
<artifactId>registry-publisher</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>common-gcore-resources</artifactId>
</dependency>
@ -93,7 +102,7 @@
<artifactId>accounting-analytics</artifactId>
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
</dependency>
<!-- -->
<!-- -->
<dependency>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-analytics-persistence-couchbase</artifactId>
@ -105,7 +114,14 @@
<artifactId>accounting-lib</artifactId>
<scope>provided</scope>
</dependency>
<!-- -->
<!-- -->
<dependency>
<groupId>org.gcube.portlets.admin</groupId>
<artifactId>rmp-common-library</artifactId>
<version>[2.7.1-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.ancoron.postgresql</groupId>
<artifactId>org.postgresql</artifactId>

View File

@ -1,9 +1,12 @@
package org.gcube.dataharvest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.gcube.dataharvest.dao.DatabaseManager;
import org.gcube.dataharvest.datamodel.Harvest;
@ -12,6 +15,7 @@ import org.gcube.dataharvest.harvester.MethodInvocationHarvester;
import org.gcube.dataharvest.harvester.ResourceCatalogueHarvester;
import org.gcube.dataharvest.harvester.SocialHarvester;
import org.gcube.dataharvest.harvester.VreUsersHarvester;
import org.gcube.dataharvest.utils.ContextAuthorization;
import org.gcube.vremanagement.executor.plugin.Plugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -20,6 +24,8 @@ public class AccountingDataHarvesterPlugin extends Plugin<DataHarvestPluginDecla
private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterPlugin.class);
private static final String PROPERTY_FILENAME = "config.properties";
public static final String PARAMETER_FROM = "from";
public static final String PARAMETER_TO = "to";
public static final String TEST = "test";
@ -27,15 +33,31 @@ public class AccountingDataHarvesterPlugin extends Plugin<DataHarvestPluginDecla
private boolean testMode = false, updateFlag = false;
private Date dateFrom, dateTo;
private Properties properties;
private void getConfigParameters() throws IOException {
properties = new Properties();
InputStream input = AccountingDataHarvesterPlugin.class.getClassLoader().getResourceAsStream(PROPERTY_FILENAME);
properties.load(input);
}
public AccountingDataHarvesterPlugin(DataHarvestPluginDeclaration pluginDeclaration) {
super(pluginDeclaration);
logger.debug("DataHarvestPlugin: constructor");
}
/** {@inheritDoc} */
@Override
public void launch(Map<String, Object> inputs) throws Exception {
logger.debug("DataHarvestPlugin: launch()");
try {
getConfigParameters();
}catch (Exception e) {
logger.warn("Unable to load {} file containing configuration properties. AccountingDataHarvesterPlugin will use defaults", PROPERTY_FILENAME);
}
ContextAuthorization contextAuthorization = new ContextAuthorization(properties);
String message = "";
DatabaseManager dbaseManager = new DatabaseManager(testMode);

View File

@ -11,6 +11,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DatabaseDataExplorer {
private static Logger logger = LoggerFactory.getLogger(DatabaseDataExplorer.class);
private boolean testMode = false;
private boolean productionMode = false;

View File

@ -0,0 +1,123 @@
package org.gcube.dataharvest.utils;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
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;
public class ContextAuthorization {
private static Logger logger = LoggerFactory.getLogger(ContextAuthorization.class);
protected Properties properties;
public static final String VO_FILE = "VO_FILE";
public static final String DEFAULT_VO_FILENAME = "scopedata.xml";
public static final String USERNAME = "USERNAME";
public static final String DEFAULT_USERNAME = "luca.frosini";
public static final String SERVICE_NAME = "SERVICE_NAME";
public static final String DEFAULT_SERVICE_NAME = "accounting-harvester";
/**
* Contains Context full name as key and Token as Value
*/
protected Map<String, String> contextToToken;
/**
* Contains Token as key and Context full name as Value
*/
protected Map<String, String> tokenToContext;
/**
* Contains Properties used to generate tokens
*/
public ContextAuthorization(Properties properties) throws Exception {
this.contextToToken = new HashMap<>();
this.tokenToContext = new HashMap<>();
if(properties!=null) {
this.properties = properties;
}else {
this.properties = new Properties();
}
//retrieveContextsAndTokens();
}
public File getVOFile() {
try {
String voFileName = properties.getProperty(VO_FILE, DEFAULT_VO_FILENAME);
URL url = ContextAuthorization.class.getClassLoader().getResource(voFileName);
File voFile = new File(url.toURI());
logger.trace("VO file is {}", voFile);
if(!voFile.exists()) {
throw new Exception("No VO file found. Unable to continue without it");
}
return voFile;
}catch (Exception e) {
throw new RuntimeException(e);
}
}
protected void retrieveContextsAndTokens() throws Exception {
String initialToken = SecurityTokenProvider.instance.get();
try {
LinkedHashMap<String, ScopeBean> map = ScopeManager.readScopes(getVOFile().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(properties.getProperty(USERNAME, DEFAULT_USERNAME), new ArrayList<>());
String userToken = authorizationService().generateUserToken(userInfo, context);
SecurityTokenProvider.instance.set(userToken);
String generatedToken = authorizationService().generateExternalServiceToken(properties.getProperty(SERVICE_NAME, DEFAULT_SERVICE_NAME));
logger.trace("Token for Context {} is {}", context, generatedToken);
contextToToken.put(context, generatedToken);
tokenToContext.put(generatedToken, context);
}catch (Exception e) {
logger.error("Error while elaborating {}", scope, e);
throw e;
} finally {
SecurityTokenProvider.instance.reset();
}
}
} catch (Exception ex) {
throw ex;
} finally {
SecurityTokenProvider.instance.set(initialToken);
}
}
public String getTokenForContext(String contextFullName) {
return contextToToken.get(contextFullName);
}
public String getContextFromToken(String token) {
return tokenToContext.get(token);
}
}

View File

@ -0,0 +1,3 @@
VO_FILE=scopedata.xml
USERNAME=luca.frosini
SERVICE_NAME=accounting-harvester

View File

@ -7,7 +7,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import org.gcube.utils.ScopedTest;
import org.gcube.dataharvest.utils.ScopedTest;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -0,0 +1,48 @@
package org.gcube.dataharvest.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.gcube.dataharvest.AccountingDataHarvesterPlugin;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ContextAuthorizationTest extends ScopedTest {
private static Logger logger = LoggerFactory.getLogger(ContextAuthorizationTest.class);
private static final String PROPERTY_FILENAME = "config.properties";
private Properties properties;
private void getConfigParameters() throws IOException {
properties = new Properties();
InputStream input = AccountingDataHarvesterPlugin.class.getClassLoader().getResourceAsStream(PROPERTY_FILENAME);
properties.load(input);
}
@Test
public void testGetVOFile() throws Exception {
try {
getConfigParameters();
}catch (Exception e) {
logger.warn("Unable to load {} file containing configuration properties. AccountingDataHarvesterPlugin will use defaults", PROPERTY_FILENAME);
}
ContextAuthorization contextAuthorization = new ContextAuthorization(properties);
contextAuthorization.getVOFile();
}
@Test
public void testRetrieveContextsAndTokens() throws Exception {
try {
getConfigParameters();
}catch (Exception e) {
logger.warn("Unable to load {} file containing configuration properties. AccountingDataHarvesterPlugin will use defaults", PROPERTY_FILENAME);
}
ContextAuthorization contextAuthorization = new ContextAuthorization(properties);
contextAuthorization.retrieveContextsAndTokens();
}
}

View File

@ -1,4 +1,4 @@
package org.gcube.utils;
package org.gcube.dataharvest.utils;
public class DataFiller {
private static String[] data10 = {

View File

@ -1,7 +1,7 @@
/**
*
*/
package org.gcube.utils;
package org.gcube.dataharvest.utils;
import java.io.IOException;
import java.io.InputStream;
@ -73,7 +73,7 @@ public class ScopedTest {
TAGME = properties.getProperty(TAGME_VARNAME);
DEFAULT_TEST_SCOPE = GCUBE_DEVSEC;
DEFAULT_TEST_SCOPE = GCUBE;
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{

View File

@ -0,0 +1,5 @@
VO_FILE=scopedata.xml
USERNAME=luca.frosini
SERVICE_NAME=accounting-harvester
LOCAL_DB=true