Added GRSF utility tests

This commit is contained in:
luca.frosini 2023-07-14 10:58:03 +02:00
parent a4d0454263
commit b28546c518
3 changed files with 220 additions and 119 deletions

View File

@ -9,151 +9,70 @@ import java.util.Properties;
import org.gcube.common.authorization.utils.manager.SecretManager;
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.authorization.utils.secret.SecretUtility;
import org.gcube.common.keycloak.KeycloakClient;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.gcat.configuration.CatalogueConfigurationFactory;
import org.gcube.grsf.publisher.configuration.isproxies.impl.GRSFFacetBasedISConfigurationProxyFactory;
import org.gcube.grsf.publisher.configuration.isproxies.impl.GRSFGCoreISConfigurationProxyFactory;
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";
protected static final String CONFIG_INI_FILENAME = "config.properties";
public static final String GCUBE;
public static final String GCUBE_DEVNEXT;
public static final String GCUBE_DEVNEXT_NEXTNEXT;
public static final String GCUBE_DEVSEC;
public static final String GCUBE_DEVSEC_DEVVRE;
public static final String ROOT;
public static final String ROOT;
public static final String VO;
public static final String VRE;
protected static final Properties properties;
public static final String TYPE_PROPERTY_KEY = "type";
public static final String USERNAME_PROPERTY_KEY = "username";
public static final String PASSWORD_PROPERTY_KEY = "password";
public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
static {
GCUBE = "/gcube";
GCUBE_DEVNEXT = GCUBE + "/devNext";
GCUBE_DEVNEXT_NEXTNEXT = GCUBE_DEVNEXT + "/NextNext";
GCUBE_DEVSEC = GCUBE + "/devsec";
GCUBE_DEVSEC_DEVVRE = GCUBE_DEVSEC + "/devVRE";
ROOT = GCUBE;
VO = GCUBE_DEVSEC;
VRE = GCUBE_DEVSEC_DEVVRE;
properties = new Properties();
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME);
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
try {
// load the properties file
properties.load(input);
} catch (IOException e) {
} catch(IOException e) {
throw new RuntimeException(e);
}
ROOT = "/gcube";
VO = ROOT + "/devsec";
VRE = VO + "/devVRE";
}
public enum Type{
USER, CLIENT_ID
};
public static void set(Secret secret) throws Exception {
SecretManagerProvider.instance.reset();
SecretManager secretManager = new SecretManager();
secretManager.addSecret(secret);
SecretManager secretManager = new SecretManager();
SecretManagerProvider.instance.set(secretManager);
SecretManagerProvider.instance.get().set();
}
public static void setContextByName(String fullContextName) throws Exception {
logger.debug("Going to set credentials for context {}", fullContextName);
Secret secret = getSecretByContextName(fullContextName);
set(secret);
}
private static TokenResponse getJWTAccessToken(String context) throws Exception {
Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
KeycloakClient keycloakClient = KeycloakClientFactory.newInstance();
TokenResponse tr = null;
int index = context.indexOf('/', 1);
String root = context.substring(0, index == -1 ? context.length() : index);
switch (type) {
case CLIENT_ID:
String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
String clientSecret = properties.getProperty(root);
tr = keycloakClient.queryUMAToken(context, clientId, clientSecret, context, null);
break;
case USER:
default:
String username = properties.getProperty(USERNAME_PROPERTY_KEY);
String password = properties.getProperty(PASSWORD_PROPERTY_KEY);
switch (root) {
case "/gcube":
default:
clientId = "next.d4science.org";
break;
case "/pred4s":
clientId = "pre.d4science.org";
break;
case "/d4science.research-infrastructures.eu":
clientId = "services.d4science.org";
break;
}
clientSecret = null;
tr = keycloakClient.queryOIDCTokenOfUserWithContext(context, clientId, clientSecret, username, password, context);
break;
}
return tr;
}
public static Secret getSecretByContextName(String context) throws Exception {
TokenResponse tr = getJWTAccessToken(context);
Secret secret = new JWTSecret(tr.getAccessToken());
return secret;
secretManager.addSecret(secret);
secretManager.set();
}
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);
return getSecret(token);
}
@BeforeClass
public static void beforeClass() throws Exception {
setContextByName(VRE);

View File

@ -0,0 +1,171 @@
/**
*
*/
package org.gcube.grsf.publisher;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.gcube.common.authorization.utils.manager.SecretManager;
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.authorization.utils.secret.SecretUtility;
import org.gcube.common.keycloak.KeycloakClient;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.gcat.configuration.CatalogueConfigurationFactory;
import org.gcube.grsf.publisher.configuration.isproxies.impl.GRSFFacetBasedISConfigurationProxyFactory;
import org.gcube.grsf.publisher.configuration.isproxies.impl.GRSFGCoreISConfigurationProxyFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ContextTestNew {
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
protected static final String CONFIG_INI_FILENAME = "config.properties";
public static final String GCUBE;
public static final String GCUBE_DEVNEXT;
public static final String GCUBE_DEVNEXT_NEXTNEXT;
public static final String GCUBE_DEVSEC;
public static final String GCUBE_DEVSEC_DEVVRE;
public static final String ROOT;
public static final String VO;
public static final String VRE;
protected static final Properties properties;
public static final String TYPE_PROPERTY_KEY = "type";
public static final String USERNAME_PROPERTY_KEY = "username";
public static final String PASSWORD_PROPERTY_KEY = "password";
public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
static {
GCUBE = "/gcube";
GCUBE_DEVNEXT = GCUBE + "/devNext";
GCUBE_DEVNEXT_NEXTNEXT = GCUBE_DEVNEXT + "/NextNext";
GCUBE_DEVSEC = GCUBE + "/devsec";
GCUBE_DEVSEC_DEVVRE = GCUBE_DEVSEC + "/devVRE";
ROOT = GCUBE;
VO = GCUBE_DEVSEC;
VRE = GCUBE_DEVSEC_DEVVRE;
properties = new Properties();
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME);
try {
// load the properties file
properties.load(input);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public enum Type{
USER, CLIENT_ID
};
public static void set(Secret secret) throws Exception {
SecretManagerProvider.instance.reset();
SecretManager secretManager = new SecretManager();
secretManager.addSecret(secret);
SecretManagerProvider.instance.set(secretManager);
SecretManagerProvider.instance.get().set();
}
public static void setContextByName(String fullContextName) throws Exception {
logger.debug("Going to set credentials for context {}", fullContextName);
Secret secret = getSecretByContextName(fullContextName);
set(secret);
}
private static TokenResponse getJWTAccessToken(String context) throws Exception {
Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
KeycloakClient keycloakClient = KeycloakClientFactory.newInstance();
TokenResponse tr = null;
int index = context.indexOf('/', 1);
String root = context.substring(0, index == -1 ? context.length() : index);
switch (type) {
case CLIENT_ID:
String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
String clientSecret = properties.getProperty(root);
tr = keycloakClient.queryUMAToken(context, clientId, clientSecret, context, null);
break;
case USER:
default:
String username = properties.getProperty(USERNAME_PROPERTY_KEY);
String password = properties.getProperty(PASSWORD_PROPERTY_KEY);
switch (root) {
case "/gcube":
default:
clientId = "next.d4science.org";
break;
case "/pred4s":
clientId = "pre.d4science.org";
break;
case "/d4science.research-infrastructures.eu":
clientId = "services.d4science.org";
break;
}
clientSecret = null;
tr = keycloakClient.queryOIDCTokenOfUserWithContext(context, clientId, clientSecret, username, password, context);
break;
}
return tr;
}
public static Secret getSecretByContextName(String context) throws Exception {
TokenResponse tr = getJWTAccessToken(context);
Secret secret = new JWTSecret(tr.getAccessToken());
return secret;
}
public static void setContext(String token) throws Exception {
Secret secret = getSecret(token);
set(secret);
}
private static Secret getSecret(String token) throws Exception {
Secret secret = SecretUtility.getSecretByTokenString(token);
return secret;
}
@BeforeClass
public static void beforeClass() throws Exception {
setContextByName(VRE);
GRSFFacetBasedISConfigurationProxyFactory fbigcpf = new GRSFFacetBasedISConfigurationProxyFactory();
CatalogueConfigurationFactory.addISConfigurationProxyFactory(fbigcpf);
GRSFGCoreISConfigurationProxyFactory gcigcpf = new GRSFGCoreISConfigurationProxyFactory();
CatalogueConfigurationFactory.addISConfigurationProxyFactory(gcigcpf);
}
@AfterClass
public static void afterClass() throws Exception {
SecretManagerProvider.instance.reset();
}
}

View File

@ -23,6 +23,7 @@ import org.gcube.gcat.persistence.ckan.CKANUtility;
import org.gcube.grsf.publisher.ContextTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -58,7 +59,6 @@ public class GRSFUtilities extends ContextTest {
CKANGroup ckanGroupToCreate = new CKANGroup();
ckanGroupToCreate.setApiKey(sysAdminAPI);
ckanGroupToCreate.setName(name);
// ckanGroupToCreate.purge();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(groups.get(name));
ArrayNode array = (ArrayNode) jsonNode.get("groups");
@ -92,7 +92,8 @@ public class GRSFUtilities extends ContextTest {
return Arrays.asList(groupArray);
}
// @Test
@Ignore
@Test
public void createGRSFGroups() throws ObjectNotFound, Exception {
// Reading groups from a CKAN related to a VRE
@ -148,6 +149,7 @@ public class GRSFUtilities extends ContextTest {
return stringBuffer.toString();
}
@Ignore
@Test
public void testGroupName() throws Exception {
List<String> groupNames = listGroup(false);
@ -159,6 +161,7 @@ public class GRSFUtilities extends ContextTest {
}
}
@Ignore
@Test
public void createNewGRSFGroups() throws ObjectNotFound, Exception {
String sysAdminAPI = CKANUtility.getSysAdminAPI();
@ -178,36 +181,46 @@ public class GRSFUtilities extends ContextTest {
}
}
// @Test
public void deleteNewGRSFGroups() throws ObjectNotFound, Exception {
@Ignore
@Test
public void deleteGRSFGroups() throws ObjectNotFound, Exception {
String sysAdminAPI = CKANUtility.getSysAdminAPI();
List<String> groupNames = listGroup(true);
boolean fromCKAN = true;
List<String> groupNames = listGroup(fromCKAN);
logger.debug(SecretManagerProvider.instance.get().getUser().getUsername());
logger.debug(SecretManagerProvider.instance.get().getContext());
for(String name : groupNames) {
CKANGroup ckanGroupToCreate = new CKANGroup();
ckanGroupToCreate.setApiKey(sysAdminAPI);
// ckanGroupToCreate.setName(getGroupId(name));
if(!fromCKAN) {
name = getGroupId(name);
}
ckanGroupToCreate.setName(name);
try {
// ckanGroupToCreate.purge();
// ckanGroupToCreate.purge();
}catch (Exception e) {
logger.error("Error while purging group with name {}", name, e);
}
}
}
// @Test
@Ignore
@Test
public void associateUserToAllCKANGroupsAndOrganization() throws ObjectNotFound, Exception {
String[] usernames = new String[] { "grsf_publisher", "luca_frosini"};
String[] usernames = new String[] { "luca_frosini" };
String sysAdminAPI = CKANUtility.getSysAdminAPI();
List<String> groupNames = listGroup(false);
boolean fromCKAN = true;
List<String> groupNames = listGroup(fromCKAN);
for(String groupName : groupNames) {
CKANUser ckanUser = new CKANUser();
ckanUser.setApiKey(sysAdminAPI);
if(!fromCKAN) {
groupName = getGroupId(groupName);
}
for(String username : usernames) {
CKANUser ckanUser = new CKANUser();
ckanUser.setApiKey(sysAdminAPI);
ckanUser.setName(username);
ckanUser.addToGroup(groupName);
}
@ -223,11 +236,9 @@ public class GRSFUtilities extends ContextTest {
}
}
@Ignore
@Test
public void manageOrganizations() throws JsonProcessingException {
String context = SecretManagerProvider.instance.get().getContext();
ScopeBean scopeBean = new ScopeBean(context);
String sysAdminAPI = CKANUtility.getSysAdminAPI();
Map<String, String> organizations = getOrganizations();
ObjectMapper objectMapper = new ObjectMapper();