Improving lib
This commit is contained in:
parent
f3a521c677
commit
d639b52e16
|
@ -0,0 +1,59 @@
|
||||||
|
package org.gcube.informationsystem.resourceregistry.contexts;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.gcube.common.scope.impl.ScopeBean;
|
||||||
|
import org.gcube.context.ContextElaborator;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextException;
|
||||||
|
import org.gcube.resourcemanagement.support.shared.types.datamodel.D4SEnvironment;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class ContextTester extends ContextElaborator {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(ContextTester.class);
|
||||||
|
|
||||||
|
protected Map<String, UUID> contexts;
|
||||||
|
|
||||||
|
public ContextTester() {
|
||||||
|
super();
|
||||||
|
contexts = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected UUID getContextUUID(ScopeBean scopeBean) throws ResourceRegistryException, IOException {
|
||||||
|
if(scopeBean!=null) {
|
||||||
|
try {
|
||||||
|
UUID uuid = contexts.get(scopeBean.toString());
|
||||||
|
if(uuid==null) {
|
||||||
|
uuid = ContextCache.getInstance().getUUIDByFullName(scopeBean.toString());
|
||||||
|
if(uuid!=null) {
|
||||||
|
contexts.put(scopeBean.toString(), uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return uuid;
|
||||||
|
}catch (ContextException e) {
|
||||||
|
logger.info("{} does not exists", scopeBean);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void elaborateContext(D4SEnvironment d4sEnvironment) throws Exception {
|
||||||
|
UUID uuid = null;
|
||||||
|
ScopeBean scopeBean = d4sEnvironment.getContext();
|
||||||
|
|
||||||
|
uuid = UUID.fromString(d4sEnvironment.getUuid());
|
||||||
|
|
||||||
|
logger.info("Context {} in old Is has been identified by UUID {}", scopeBean, uuid.toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -13,9 +13,8 @@ import org.gcube.common.authorization.utils.secret.JWTSecret;
|
||||||
import org.gcube.common.authorization.utils.secret.Secret;
|
import org.gcube.common.authorization.utils.secret.Secret;
|
||||||
import org.gcube.common.authorization.utils.secret.SecretUtility;
|
import org.gcube.common.authorization.utils.secret.SecretUtility;
|
||||||
import org.gcube.common.keycloak.KeycloakClientFactory;
|
import org.gcube.common.keycloak.KeycloakClientFactory;
|
||||||
|
import org.gcube.common.keycloak.KeycloakClientHelper;
|
||||||
import org.gcube.common.keycloak.model.TokenResponse;
|
import org.gcube.common.keycloak.model.TokenResponse;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
|
||||||
import org.gcube.informationsystem.model.reference.properties.Metadata;
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -24,44 +23,44 @@ import org.slf4j.LoggerFactory;
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
public class ContextTest {
|
public class ContextTest {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
||||||
|
|
||||||
protected static final String CONFIG_INI_FILENAME = "config.ini";
|
protected static final String CONFIG_INI_FILENAME = "config.properties";
|
||||||
|
|
||||||
public static final String ROOT_DEV;
|
public static final String ROOT_DEV;
|
||||||
public static final String ROOT_PREPROD;
|
public static final String ROOT_PREPROD;
|
||||||
public static final String ROOT_PROD;
|
public static final String ROOT_PROD;
|
||||||
|
|
||||||
protected static final Properties properties;
|
protected static final Properties properties;
|
||||||
|
|
||||||
protected static final String CLIENT_ID_PROPERTY_KEY = "client_id";
|
public static final String TYPE_PROPERTY_KEY = "type";
|
||||||
protected static final String CLIENT_SECRET_PROPERTY_KEY = "client_secret";
|
public static final String USERNAME_PROPERTY_KEY = "username";
|
||||||
|
public static final String PASSWORD_PROPERTY_KEY = "password";
|
||||||
protected static final String clientID;
|
public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
|
||||||
protected static final String clientSecret;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
ROOT_DEV = "/gcube";
|
||||||
|
ROOT_PREPROD = "/pred4s";
|
||||||
|
ROOT_PROD = "/d4science.research-infrastructures.eu";
|
||||||
|
|
||||||
properties = new Properties();
|
properties = new Properties();
|
||||||
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME);
|
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME);
|
||||||
try {
|
try {
|
||||||
// load the properties file
|
// load the properties file
|
||||||
properties.load(input);
|
properties.load(input);
|
||||||
|
|
||||||
clientID = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
|
|
||||||
clientSecret = properties.getProperty(CLIENT_SECRET_PROPERTY_KEY);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
ROOT_DEV = "/gcube";
|
|
||||||
ROOT_PREPROD = "/pred4s";
|
|
||||||
ROOT_PROD = "/d4science.research-infrastructures.eu";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum Type{
|
||||||
|
USER, CLIENT_ID
|
||||||
|
};
|
||||||
|
|
||||||
public static void set(Secret secret) throws Exception {
|
public static void set(Secret secret) throws Exception {
|
||||||
SecretManagerProvider.instance.reset();
|
SecretManagerProvider.instance.reset();
|
||||||
SecretManager secretManager = new SecretManager();
|
SecretManager secretManager = new SecretManager();
|
||||||
|
@ -71,15 +70,56 @@ public class ContextTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setContextByName(String fullContextName) throws Exception {
|
public static void setContextByName(String fullContextName) throws Exception {
|
||||||
|
logger.debug("Going to set credentials for context {}", fullContextName);
|
||||||
Secret secret = getSecretByContextName(fullContextName);
|
Secret secret = getSecretByContextName(fullContextName);
|
||||||
set(secret);
|
set(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static TokenResponse getJWTAccessToken(String context) throws Exception {
|
private static TokenResponse getJWTAccessToken(String context) throws Exception {
|
||||||
ScopeProvider.instance.set(context);
|
Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
|
||||||
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null);
|
|
||||||
return tr;
|
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 = KeycloakClientFactory.newInstance().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 = KeycloakClientHelper.getTokenForUser(context, username, password);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return tr;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Secret getSecretByContextName(String context) throws Exception {
|
public static Secret getSecretByContextName(String context) throws Exception {
|
||||||
|
@ -98,16 +138,6 @@ public class ContextTest {
|
||||||
return secret;
|
return secret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getUser() {
|
|
||||||
String user = Metadata.UNKNOWN_USER;
|
|
||||||
try {
|
|
||||||
user = SecretManagerProvider.instance.get().getUser().getUsername();
|
|
||||||
} catch(Exception e) {
|
|
||||||
logger.error("Unable to retrieve user. {} will be used", user);
|
|
||||||
}
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeClass() throws Exception {
|
public static void beforeClass() throws Exception {
|
||||||
setContextByName(ROOT_DEV);
|
setContextByName(ROOT_DEV);
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.informationsystem.resourceregistry;
|
||||||
|
|
||||||
|
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.Secret;
|
||||||
|
import org.gcube.common.authorization.utils.secret.SecretUtility;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class ContextTestOldUAth {
|
||||||
|
|
||||||
|
protected static Properties properties;
|
||||||
|
protected static final String PROPERTIES_FILENAME = "token.properties";
|
||||||
|
|
||||||
|
public static final String ROOT_DEV;
|
||||||
|
public static final String ROOT_PREPROD;
|
||||||
|
public static final String ROOT_PROD;
|
||||||
|
|
||||||
|
static {
|
||||||
|
ROOT_DEV = "/gcube";
|
||||||
|
ROOT_PREPROD = "/pred4s";
|
||||||
|
ROOT_PROD = "/d4science.research-infrastructures.eu";
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void set(Secret secret) throws Exception {
|
||||||
|
SecretManagerProvider.instance.reset();
|
||||||
|
SecretManager secretManager = new SecretManager();
|
||||||
|
SecretManagerProvider.instance.set(secretManager);
|
||||||
|
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(ROOT_DEV);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void afterClass() throws Exception {
|
||||||
|
SecretManagerProvider.instance.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -6,9 +6,11 @@ import org.gcube.informationsystem.contexts.impl.entities.ContextImpl;
|
||||||
import org.gcube.informationsystem.contexts.reference.entities.Context;
|
import org.gcube.informationsystem.contexts.reference.entities.Context;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
import org.gcube.informationsystem.resourceregistry.contexts.ContextCreator;
|
import org.gcube.informationsystem.resourceregistry.contexts.ContextCreator;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.contexts.ContextTester;
|
||||||
import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement;
|
import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement;
|
||||||
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
|
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
|
||||||
import org.gcube.informationsystem.serialization.ElementMapper;
|
import org.gcube.informationsystem.serialization.ElementMapper;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -28,13 +30,21 @@ public class DataBaseCreator extends ContextTest {
|
||||||
logger.debug("{} created", db);
|
logger.debug("{} created", db);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Test
|
@Ignore
|
||||||
|
@Test
|
||||||
public void createAllContexts() throws Exception {
|
public void createAllContexts() throws Exception {
|
||||||
ContextTest.setContextByName(ROOT_DEV);
|
ContextTest.setContextByName(ROOT_DEV);
|
||||||
ContextCreator contextCreator = new ContextCreator();
|
ContextCreator contextCreator = new ContextCreator();
|
||||||
contextCreator.all();
|
contextCreator.all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkAllContexts() throws Exception {
|
||||||
|
ContextTest.setContextByName(ROOT_DEV);
|
||||||
|
ContextTester contextTester = new ContextTester();
|
||||||
|
contextTester.all();
|
||||||
|
}
|
||||||
|
|
||||||
protected Context create(Context context) throws ResourceRegistryException, IOException {
|
protected Context create(Context context) throws ResourceRegistryException, IOException {
|
||||||
ContextManagement contextManagement = new ContextManagement();
|
ContextManagement contextManagement = new ContextManagement();
|
||||||
contextManagement.setJson(ElementMapper.marshal(context));
|
contextManagement.setJson(ElementMapper.marshal(context));
|
||||||
|
|
Loading…
Reference in New Issue