From d639b52e1607bb067d047fc143ad57e7db891ad8 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Mon, 17 Jul 2023 10:52:28 +0200 Subject: [PATCH] Improving lib --- .../contexts/ContextTester.java | 59 ++++++++++++ .../resourceregistry/ContextTest.java | 90 ++++++++++++------- .../resourceregistry/ContextTestOldUAth.java | 83 +++++++++++++++++ .../resourceregistry/DataBaseCreator.java | 12 ++- 4 files changed, 213 insertions(+), 31 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextTester.java create mode 100644 src/test/java/org/gcube/informationsystem/resourceregistry/ContextTestOldUAth.java diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextTester.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextTester.java new file mode 100644 index 0000000..afcccc6 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ContextTester.java @@ -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 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()); + + } + +} \ No newline at end of file diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/ContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/ContextTest.java index a3cbd9b..93f7467 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/ContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/ContextTest.java @@ -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.SecretUtility; import org.gcube.common.keycloak.KeycloakClientFactory; +import org.gcube.common.keycloak.KeycloakClientHelper; 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.BeforeClass; import org.slf4j.Logger; @@ -24,44 +23,44 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ -@SuppressWarnings("deprecation") public class ContextTest { 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_PREPROD; public static final String ROOT_PROD; - + protected static final Properties properties; - protected static final String CLIENT_ID_PROPERTY_KEY = "client_id"; - protected static final String CLIENT_SECRET_PROPERTY_KEY = "client_secret"; - - protected static final String clientID; - protected static final String clientSecret; + 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 { + ROOT_DEV = "/gcube"; + ROOT_PREPROD = "/pred4s"; + ROOT_PROD = "/d4science.research-infrastructures.eu"; + properties = new Properties(); InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME); try { // load the properties file properties.load(input); - - clientID = properties.getProperty(CLIENT_ID_PROPERTY_KEY); - clientSecret = properties.getProperty(CLIENT_SECRET_PROPERTY_KEY); - + } catch (IOException 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 { SecretManagerProvider.instance.reset(); SecretManager secretManager = new SecretManager(); @@ -71,15 +70,56 @@ public class ContextTest { } 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 { - ScopeProvider.instance.set(context); - TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null); - return tr; + Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString()); + + 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 { @@ -98,16 +138,6 @@ public class ContextTest { 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 public static void beforeClass() throws Exception { setContextByName(ROOT_DEV); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/ContextTestOldUAth.java b/src/test/java/org/gcube/informationsystem/resourceregistry/ContextTestOldUAth.java new file mode 100644 index 0000000..43d6a98 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/ContextTestOldUAth.java @@ -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(); + } + +} diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/DataBaseCreator.java b/src/test/java/org/gcube/informationsystem/resourceregistry/DataBaseCreator.java index 7cab4da..905394f 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/DataBaseCreator.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/DataBaseCreator.java @@ -6,9 +6,11 @@ import org.gcube.informationsystem.contexts.impl.entities.ContextImpl; import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; 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.dbinitialization.DatabaseEnvironment; import org.gcube.informationsystem.serialization.ElementMapper; +import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,13 +30,21 @@ public class DataBaseCreator extends ContextTest { logger.debug("{} created", db); } - // @Test + @Ignore + @Test public void createAllContexts() throws Exception { ContextTest.setContextByName(ROOT_DEV); ContextCreator contextCreator = new ContextCreator(); 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 { ContextManagement contextManagement = new ContextManagement(); contextManagement.setJson(ElementMapper.marshal(context));