From 21a8102d183767340fa53080e105f9f497271011 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Tue, 12 Sep 2023 16:24:44 +0200 Subject: [PATCH] Fixed lib --- .gitignore | 1 + pom.xml | 21 ++- .../org/gcube/context/names/ContextTest.java | 141 +++++++++++++----- src/test/resources/.gitignore | 1 + 4 files changed, 125 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 2061c9e..663b123 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target .classpath .project +/.DS_Store diff --git a/pom.xml b/pom.xml index 2211dc7..58ae643 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ org.gcube.distribution gcube-bom - 2.1.0-SNAPSHOT + 2.4.0-SNAPSHOT pom import @@ -69,6 +69,19 @@ slf4j-api + + + javax.xml.ws + jaxws-api + provided + + + org.projectlombok + lombok + provided + + + junit @@ -81,6 +94,12 @@ logback-classic test + + org.gcube.common + authorization-utils + [2.1.0-SNAPSHOT, 3.0.0-SNAPSHOT) + test + \ No newline at end of file diff --git a/src/test/java/org/gcube/context/names/ContextTest.java b/src/test/java/org/gcube/context/names/ContextTest.java index a1b5ada..6bc3b8a 100644 --- a/src/test/java/org/gcube/context/names/ContextTest.java +++ b/src/test/java/org/gcube/context/names/ContextTest.java @@ -7,14 +7,14 @@ import java.io.IOException; import java.io.InputStream; import java.util.Properties; -import org.gcube.common.authorization.client.Constants; -import org.gcube.common.authorization.client.exceptions.ObjectNotFound; -import org.gcube.common.authorization.library.AuthorizationEntry; -import org.gcube.common.authorization.library.provider.AuthorizationProvider; -import org.gcube.common.authorization.library.provider.ClientInfo; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.gcube.common.authorization.library.utils.Caller; -import org.gcube.common.scope.api.ScopeProvider; +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.KeycloakClientFactory; +import org.gcube.common.keycloak.KeycloakClientHelper; +import org.gcube.common.keycloak.model.TokenResponse; import org.junit.AfterClass; import org.junit.BeforeClass; import org.slf4j.Logger; @@ -22,65 +22,130 @@ 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.ini"; - public static final String DEFAULT_TEST_SCOPE_NAME; + public static final String ROOT_DEV; + public static final String ROOT_PREPROD; + public static final String ROOT_PROD; + + 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 { + ROOT_DEV = "/gcube"; + ROOT_PREPROD = "/pred4s"; + ROOT_PROD = "/d4science.research-infrastructures.eu"; + properties = new Properties(); - InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME); - + InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME); try { // load the properties file properties.load(input); - } catch(IOException e) { + + } catch (IOException e) { throw new RuntimeException(e); } - //DEFAULT_TEST_SCOPE_NAME = "/gcube"; - //DEFAULT_TEST_SCOPE_NAME = "/pred4s"; - DEFAULT_TEST_SCOPE_NAME = "/d4science.research-infrastructures.eu"; } - public static String getCurrentScope(String token) throws ObjectNotFound, Exception { - AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); - String context = authorizationEntry.getContext(); - logger.info("Context of token {} is {}", token, context); - return context; + 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 ObjectNotFound, Exception { - String token = ContextTest.properties.getProperty(fullContextName); - setContext(token); + public static void setContextByName(String fullContextName) throws Exception { + logger.debug("Going to set credentials for context {}", fullContextName); + Secret secret = getSecretByContextName(fullContextName); + set(secret); } - 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)); + + private static TokenResponse getJWTAccessToken(String context) throws Exception { + 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 { + 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(DEFAULT_TEST_SCOPE_NAME); + setContextByName(ROOT_PREPROD); } @AfterClass public static void afterClass() throws Exception { - SecurityTokenProvider.instance.reset(); - ScopeProvider.instance.reset(); + SecretManagerProvider.instance.reset(); } } diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore index 41fc597..30b89d2 100644 --- a/src/test/resources/.gitignore +++ b/src/test/resources/.gitignore @@ -3,3 +3,4 @@ /d4science.research-infrastructures.eu.gcubekey /gcube.gcubekey /output.txt +/config.ini