Porting plugin to use new IAM credentials

This commit is contained in:
Luca Frosini 2022-09-01 14:08:36 +02:00
parent c7a934bd4b
commit d574e3c797
8 changed files with 152 additions and 116 deletions

View File

@ -36,7 +36,7 @@
<dependency> <dependency>
<groupId>org.gcube.distribution</groupId> <groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId> <artifactId>gcube-bom</artifactId>
<version>2.1.0-SNAPSHOT</version> <version>2.0.2</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
@ -120,6 +120,11 @@
<version>20171018</version> <version>20171018</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-utils</artifactId>
<version>[2.0.0, 3.0.0-SNAPSHOT)</version>
</dependency>
<!-- Test Dependencies. Setting scope to provided to allow proper creation of uber-jar --> <!-- Test Dependencies. Setting scope to provided to allow proper creation of uber-jar -->
<dependency> <dependency>

View File

@ -18,6 +18,7 @@ import org.gcube.accounting.accounting.summary.access.model.internal.Dimension;
import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord; import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore; import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.dataharvest.harvester.CatalogueAccessesHarvester; import org.gcube.dataharvest.harvester.CatalogueAccessesHarvester;
@ -214,12 +215,12 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
Properties properties = getConfigParameters(); Properties properties = getConfigParameters();
getProperties().set(properties); getProperties().set(properties);
ContextAuthorization contextAuthorization = new ContextAuthorization(); ContextAuthorization contextAuthorization = new ContextAuthorization(properties);
SortedSet<String> contexts = contextAuthorization.getContexts(); SortedSet<String> contexts = contextAuthorization.getContexts();
String root = contexts.first(); String root = contexts.first();
Utils.setContext(contextAuthorization.getTokenForContext(root)); Utils.setContext(contextAuthorization.getSecretForContext(root));
AccountingDao dao = AccountingDao.get(); AccountingDao dao = AccountingDao.get();
@ -240,16 +241,16 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
ArrayList<AccountingRecord> accountingRecords = new ArrayList<AccountingRecord>(); ArrayList<AccountingRecord> accountingRecords = new ArrayList<AccountingRecord>();
String initialToken = SecurityTokenProvider.instance.get();
VREAccessesHarvester vreAccessesHarvester = null; VREAccessesHarvester vreAccessesHarvester = null;
JupyterAccessesHarvester jupyterAccessesHarvester = null; JupyterAccessesHarvester jupyterAccessesHarvester = null;
RStudioAccessesHarvester rstudioAccessesHarvester = null; RStudioAccessesHarvester rstudioAccessesHarvester = null;
Secret rootSecret = null;
for (String context : contexts) { for (String context : contexts) {
// Setting the token for the context // Setting the token for the context
Utils.setContext(contextAuthorization.getTokenForContext(context)); Secret secret = contextAuthorization.getSecretForContext(context);
Utils.setContext(secret);
ScopeBean scopeBean = new ScopeBean(context); ScopeBean scopeBean = new ScopeBean(context);
@ -263,6 +264,8 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
if (scopeBean.is(Type.INFRASTRUCTURE)) { if (scopeBean.is(Type.INFRASTRUCTURE)) {
try { try {
rootSecret = secret;
CatalogueAccessesHarvester catalogueHarvester = new CatalogueAccessesHarvester(start, end); CatalogueAccessesHarvester catalogueHarvester = new CatalogueAccessesHarvester(start, end);
List<AccountingRecord> harvested = catalogueHarvester.getAccountingRecords(); List<AccountingRecord> harvested = catalogueHarvester.getAccountingRecords();
accountingRecords.addAll(harvested); accountingRecords.addAll(harvested);
@ -291,12 +294,12 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
} }
// Setting back token for the context // Setting back token for the context
Utils.setContext(contextAuthorization.getTokenForContext(parent.toString())); Utils.setContext(contextAuthorization.getSecretForContext(parent.toString()));
vreAccessesHarvester = new VREAccessesHarvester(start, end); vreAccessesHarvester = new VREAccessesHarvester(start, end);
// Setting back token for the context // Setting back token for the context
Utils.setContext(contextAuthorization.getTokenForContext(context)); Utils.setContext(contextAuthorization.getSecretForContext(context));
} }
} }
@ -315,12 +318,12 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
} }
// Setting back token for the context // Setting back token for the context
Utils.setContext(contextAuthorization.getTokenForContext(parent.toString())); Utils.setContext(contextAuthorization.getSecretForContext(parent.toString()));
rstudioAccessesHarvester = new RStudioAccessesHarvester(start, end); rstudioAccessesHarvester = new RStudioAccessesHarvester(start, end);
// Setting back token for the context // Setting back token for the context
Utils.setContext(contextAuthorization.getTokenForContext(context)); Utils.setContext(contextAuthorization.getSecretForContext(context));
} }
} }
@ -339,12 +342,12 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
} }
// Setting back token for the context // Setting back token for the context
Utils.setContext(contextAuthorization.getTokenForContext(parent.toString())); Utils.setContext(contextAuthorization.getSecretForContext(parent.toString()));
jupyterAccessesHarvester = new JupyterAccessesHarvester(start, end); jupyterAccessesHarvester = new JupyterAccessesHarvester(start, end);
// Setting back token for the context // Setting back token for the context
Utils.setContext(contextAuthorization.getTokenForContext(context)); Utils.setContext(contextAuthorization.getSecretForContext(context));
} }
} }
@ -508,7 +511,7 @@ public class AccountingDashboardHarvesterPlugin extends Plugin {
} }
} }
Utils.setContext(initialToken); Utils.setContext(rootSecret);
logger.debug("Harvest Measures from {} to {} are {}", DateUtils.format(start), DateUtils.format(end), logger.debug("Harvest Measures from {} to {} are {}", DateUtils.format(start), DateUtils.format(end),
accountingRecords); accountingRecords);

View File

@ -1,8 +1,5 @@
package org.gcube.dataharvest.utils; package org.gcube.dataharvest.utils;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -10,8 +7,13 @@ import java.util.Properties;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import javax.ws.rs.InternalServerErrorException;
import org.gcube.common.authorization.library.provider.UserInfo;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.JWTSecret;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.dataharvest.AccountingDashboardHarvesterPlugin; import org.gcube.dataharvest.AccountingDashboardHarvesterPlugin;
import org.gcube.resourcemanagement.support.server.managers.context.ContextManager; import org.gcube.resourcemanagement.support.server.managers.context.ContextManager;
@ -31,82 +33,99 @@ public class ContextAuthorization {
public static final String SERVICE_NAME = "SERVICE_NAME"; public static final String SERVICE_NAME = "SERVICE_NAME";
public static final String DEFAULT_SERVICE_NAME = "accounting-harvester"; public static final String DEFAULT_SERVICE_NAME = "accounting-harvester";
public static final String CLIENT_ID = "accounting-dashboard-harvester-se-plugin";
protected String clientSecret;
/** /**
* Contains Context full name as key and Token as Value * Contains Context full name as key and Token as Value
*/ */
protected Map<String,String> contextToToken; protected Map<String,Secret> contextToToken;
/** /**
* Contains Token as key and Context full name as Value * Contains Token as key and Context full name as Value
*/ */
protected Map<String,String> tokenToContext; protected Map<Secret,String> tokenToContext;
protected Properties properties;
/**
* Contains Properties used to generate tokens
*/
public ContextAuthorization(Properties properties) throws Exception {
this.properties = properties;
this.contextToToken = new HashMap<>();
this.tokenToContext = new HashMap<>();
retrieveContextsAndTokens();
}
/** /**
* Contains Properties used to generate tokens * Contains Properties used to generate tokens
*/ */
public ContextAuthorization() throws Exception { public ContextAuthorization() throws Exception {
this.properties = AccountingDashboardHarvesterPlugin.getProperties().get();
this.contextToToken = new HashMap<>(); this.contextToToken = new HashMap<>();
this.tokenToContext = new HashMap<>(); this.tokenToContext = new HashMap<>();
retrieveContextsAndTokens(); retrieveContextsAndTokens();
} }
public String generateTokenForContext(String context, Properties properties) throws Exception { private String getClientSecret(String context) {
if(properties==null) { try {
properties = AccountingDashboardHarvesterPlugin.getProperties().get(); if(clientSecret==null) {
int index = context.indexOf('/', 1);
String root = context.substring(0, index == -1 ? context.length() : index);
clientSecret = properties.getProperty(root);
}
return clientSecret;
} catch(Exception e) {
throw new InternalServerErrorException(
"Unable to retrieve Application Token for context " + SecretManagerProvider.instance.get().getContext(), e);
} }
logger.info("Going to generate Token for Context {}", context); }
UserInfo userInfo = new UserInfo(properties.getProperty(USERNAME, DEFAULT_USERNAME),
new ArrayList<>()); private TokenResponse getJWTAccessToken(String context) throws Exception {
String userToken = authorizationService().generateUserToken(userInfo, context); TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(CLIENT_ID, getClientSecret(context), context, null);
SecurityTokenProvider.instance.set(userToken); return tr;
String generatedToken = authorizationService() }
.generateExternalServiceToken(properties.getProperty(SERVICE_NAME, DEFAULT_SERVICE_NAME));
public Secret getCatalogueSecretForContext(String context) throws Exception {
logger.trace("Token for Context {} is {}", context, generatedToken); TokenResponse tr = getJWTAccessToken(context);
Secret secret = new JWTSecret(tr.getAccessToken());
return generatedToken; return secret;
} }
protected void retrieveContextsAndTokens() throws Exception { protected void retrieveContextsAndTokens() throws Exception {
String initialToken = SecurityTokenProvider.instance.get();
try { try {
Properties properties = AccountingDashboardHarvesterPlugin.getProperties().get();
LinkedHashMap<String,ScopeBean> map = ContextManager.readContexts(); LinkedHashMap<String,ScopeBean> map = ContextManager.readContexts();
for(String scope : map.keySet()) { for(String scope : map.keySet()) {
try { try {
String context = map.get(scope).toString(); String context = map.get(scope).toString();
String generatedToken = generateTokenForContext(context, properties); Secret secret = getCatalogueSecretForContext(context);
contextToToken.put(context, generatedToken); contextToToken.put(context, secret);
tokenToContext.put(generatedToken, context); tokenToContext.put(secret, context);
} catch(Exception e) { } catch(Exception e) {
logger.error("Error while elaborating {}", scope, e); logger.error("Error while elaborating {}", scope, e);
throw e; throw e;
} finally { }
SecurityTokenProvider.instance.reset();
}
} }
} catch(Exception ex) { } catch(Exception ex) {
throw ex; throw ex;
} finally { }
SecurityTokenProvider.instance.set(initialToken);
}
} }
public String getTokenForContext(String contextFullName) { public Secret getSecretForContext(String context) {
return contextToToken.get(contextFullName); return contextToToken.get(context);
} }
public String getContextFromToken(String token) { public String getContextFromSecret(Secret secret) {
return tokenToContext.get(token); return tokenToContext.get(secret);
} }
public SortedSet<String> getContexts() { public SortedSet<String> getContexts() {

View File

@ -11,6 +11,9 @@ import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -50,9 +53,17 @@ public class Utils {
return context; return context;
} }
public static void setContext(String token) throws ObjectNotFound, Exception { public static void setContext(Secret secret) throws Exception {
SecurityTokenProvider.instance.set(token); SecretManagerProvider.instance.reset();
ScopeProvider.instance.set(getCurrentContext(token)); SecretManager secretManager = new SecretManager();
SecretManagerProvider.instance.set(secretManager);
secretManager.addSecret(secret);
secretManager.set();
} }
// public static void setContext(String token) throws ObjectNotFound, Exception {
// SecurityTokenProvider.instance.set(token);
// ScopeProvider.instance.set(getCurrentContext(token));
// }
} }

View File

@ -101,7 +101,7 @@ public class AccountingDataHarvesterJupyterTest extends ContextTest {
JupyterAccessesHarvester jupyterAccessesHarvester = new JupyterAccessesHarvester(start, end); JupyterAccessesHarvester jupyterAccessesHarvester = new JupyterAccessesHarvester(start, end);
for(String context : contexts) { for(String context : contexts) {
ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ContextTest.set(contextAuthorization.getSecretForContext(context));
ScopeBean scopeBean = new ScopeBean(context); ScopeBean scopeBean = new ScopeBean(context);

View File

@ -248,7 +248,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
for (String context : contexts) { for (String context : contexts) {
// Setting the token for the context // Setting the token for the context
ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ContextTest.set(contextAuthorization.getSecretForContext(context));
ScopeBean scopeBean = new ScopeBean(context); ScopeBean scopeBean = new ScopeBean(context);
@ -266,12 +266,12 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
} }
// Setting back token for the context // Setting back token for the context
ContextTest.setContext(contextAuthorization.getTokenForContext(parent.toString())); ContextTest.set(contextAuthorization.getSecretForContext(parent.toString()));
vreAccessesHarvester = new VREAccessesHarvester(start, end); vreAccessesHarvester = new VREAccessesHarvester(start, end);
// Setting back token for the context // Setting back token for the context
ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ContextTest.set(contextAuthorization.getSecretForContext(context));
} }
} }
@ -446,7 +446,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
for (String context : contexts) { for (String context : contexts) {
// Setting the token for the context // Setting the token for the context
ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ContextTest.set(contextAuthorization.getSecretForContext(context));
ScopeBean scopeBean = new ScopeBean(context); ScopeBean scopeBean = new ScopeBean(context);
@ -464,12 +464,12 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
} }
// Setting back token for the context // Setting back token for the context
ContextTest.setContext(contextAuthorization.getTokenForContext(parent.toString())); ContextTest.set(contextAuthorization.getSecretForContext(parent.toString()));
vreAccessesHarvester = new VREAccessesHarvester(start, end); vreAccessesHarvester = new VREAccessesHarvester(start, end);
// Setting back token for the context // Setting back token for the context
ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ContextTest.set(contextAuthorization.getSecretForContext(context));
} }
} }
@ -530,7 +530,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
for (String context : contexts) { for (String context : contexts) {
// Setting the token for the context // Setting the token for the context
ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ContextTest.set(contextAuthorization.getSecretForContext(context));
try { try {
// Collecting info on social (posts, replies and likes) // Collecting info on social (posts, replies and likes)
logger.info("Going to harvest Social Interactions for {}", context); logger.info("Going to harvest Social Interactions for {}", context);
@ -563,9 +563,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
ContextTest.setContextByName(ROOT); ContextTest.setContextByName(ROOT);
ContextAuthorization contextAuthorization = new ContextAuthorization(); ContextAuthorization contextAuthorization = new ContextAuthorization();
String stockAssessmentToken = contextAuthorization.generateTokenForContext(STOCK_ASSESMENT_VRE, null); ContextTest.set(contextAuthorization.getSecretForContext(STOCK_ASSESMENT_VRE));
ContextTest.setContext(stockAssessmentToken);
AggregationType measureType = AggregationType.MONTHLY; AggregationType measureType = AggregationType.MONTHLY;
@ -614,7 +612,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
for (String context : contexts) { for (String context : contexts) {
// Setting the token for the context // Setting the token for the context
ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ContextTest.set(contextAuthorization.getSecretForContext(context));
ScopeBean scopeBean = new ScopeBean(context); ScopeBean scopeBean = new ScopeBean(context);
@ -666,9 +664,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
protected void setContextByNameAndScopeDescriptor(String contextFullName) throws ObjectNotFound, Exception { protected void setContextByNameAndScopeDescriptor(String contextFullName) throws ObjectNotFound, Exception {
ContextAuthorization contextAuthorization = new ContextAuthorization(); ContextAuthorization contextAuthorization = new ContextAuthorization();
String tagMeToken = contextAuthorization.getTokenForContext(contextFullName); ContextTest.set(contextAuthorization.getSecretForContext(contextFullName));
ContextTest.setContext(tagMeToken);
ScopeBean scopeBean = new ScopeBean(contextFullName); ScopeBean scopeBean = new ScopeBean(contextFullName);
ScopeDescriptor actualScopeDescriptor = AccountingDashboardHarvesterPlugin.scopeDescriptors.get() ScopeDescriptor actualScopeDescriptor = AccountingDashboardHarvesterPlugin.scopeDescriptors.get()
@ -732,8 +728,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
// AccountingDao dao = AccountingDao.get(); // AccountingDao dao = AccountingDao.get();
ContextAuthorization contextAuthorization = new ContextAuthorization(); ContextAuthorization contextAuthorization = new ContextAuthorization();
ContextTest.setContext(contextAuthorization ContextTest.set(contextAuthorization.getSecretForContext("/d4science.research-infrastructures.eu/SoBigData/SportsDataScience"));
.getTokenForContext("/d4science.research-infrastructures.eu/SoBigData/SportsDataScience"));
AggregationType measureType = AggregationType.MONTHLY; AggregationType measureType = AggregationType.MONTHLY;
@ -921,7 +916,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
if (context.startsWith(TAGME_VRE)) { if (context.startsWith(TAGME_VRE)) {
continue; continue;
} }
ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ContextTest.set(contextAuthorization.getSecretForContext(context));
DataMethodDownloadHarvester dataMethodDownloadHarvester = new DataMethodDownloadHarvester(start, DataMethodDownloadHarvester dataMethodDownloadHarvester = new DataMethodDownloadHarvester(start,
end, contexts); end, contexts);
List<AccountingRecord> data = dataMethodDownloadHarvester.getAccountingRecords(); List<AccountingRecord> data = dataMethodDownloadHarvester.getAccountingRecords();
@ -975,7 +970,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest {
String context = E_LEARNING_AREA_VRE; String context = E_LEARNING_AREA_VRE;
// Setting the token for the context // Setting the token for the context
ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ContextTest.set(contextAuthorization.getSecretForContext(context));
ScopeBean scopeBean = new ScopeBean(context); ScopeBean scopeBean = new ScopeBean(context);
ScopeDescriptor scopeDescriptor = new ScopeDescriptor(scopeBean.name(), context); ScopeDescriptor scopeDescriptor = new ScopeDescriptor(scopeBean.name(), context);

View File

@ -105,7 +105,7 @@ public class AccountingDataHarvesterRStudioTest extends ContextTest {
RStudioAccessesHarvester rstudioAccessesHarvester = new RStudioAccessesHarvester(start, end); RStudioAccessesHarvester rstudioAccessesHarvester = new RStudioAccessesHarvester(start, end);
for(String context : contexts) { for(String context : contexts) {
ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ContextTest.set(contextAuthorization.getSecretForContext(context));
ScopeBean scopeBean = new ScopeBean(context); ScopeBean scopeBean = new ScopeBean(context);

View File

@ -7,29 +7,24 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Properties; import java.util.Properties;
import org.gcube.common.authorization.client.Constants; import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.authorization.library.provider.ClientInfo; import org.junit.AfterClass;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.junit.BeforeClass;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*
*/ */
public class ContextTest { public class ContextTest {
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
protected static Properties properties; protected static Properties properties;
protected static final String PROPERTIES_FILENAME = "token.properties"; protected static final String PROPERTIES_FILENAME = "token.properties";
public static final String DEFAULT_TEST_SCOPE_NAME; public static final String ROOT;
public static final String VO;
public static final String VRE;
static { static {
properties = new Properties(); properties = new Properties();
@ -42,45 +37,53 @@ public class ContextTest {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
//DEFAULT_TEST_SCOPE_NAME = "/pred4s/preprod/preVRE"; // DEFAULT_TEST_SCOPE_NAME = "/pred4s/preprod/preVRE";
// DEFAULT_TEST_SCOPE_NAME = "/gcube/devsec/devVRE";
DEFAULT_TEST_SCOPE_NAME = "/d4science.research-infrastructures.eu";
ROOT = "/gcube";
VO = ROOT + "/devsec";
VRE = VO + "/devVRE";
// VO = ROOT + "/devNext";
// VRE = VO + "/NextNext";
} }
public static String getCurrentScope(String token) throws ObjectNotFound, Exception { public static void set(Secret secret) throws Exception {
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); SecretManagerProvider.instance.reset();
String context = authorizationEntry.getContext(); SecretManager secretManager = new SecretManager();
logger.info("Context of token {} is {}", token, context); SecretManagerProvider.instance.set(secretManager);
return context; secretManager.addSecret(secret);
secretManager.set();
} }
public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception { public static void setContext(String token) throws Exception {
Secret secret = getSecret(token);
set(secret);
}
public static void setContextByName(String fullContextName) throws Exception {
Secret secret = getSecretByContextName(fullContextName);
set(secret);
}
private static Secret getSecret(String token) throws Exception {
Secret secret = SecretUtility.getSecretByTokenString(token);
return secret;
}
private static Secret getSecretByContextName(String fullContextName) throws Exception {
String token = ContextTest.properties.getProperty(fullContextName); String token = ContextTest.properties.getProperty(fullContextName);
setContext(token); return getSecret(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 @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
setContextByName(DEFAULT_TEST_SCOPE_NAME); setContextByName(VRE);
} }
@AfterClass @AfterClass
public static void afterClass() throws Exception { public static void afterClass() throws Exception {
SecurityTokenProvider.instance.reset(); SecretManagerProvider.instance.reset();
ScopeProvider.instance.reset();
} }
*/
} }