Fixed lib

This commit is contained in:
luca.frosini 2023-09-12 16:24:44 +02:00
parent a729616546
commit 21a8102d18
4 changed files with 125 additions and 39 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
target target
.classpath .classpath
.project .project
/.DS_Store

21
pom.xml
View File

@ -28,7 +28,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.4.0-SNAPSHOT</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
@ -69,6 +69,19 @@
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
</dependency> </dependency>
<!-- Added to support Java 11 JDK -->
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- END Added to support Java 11 JDK -->
<!-- Test Dependency --> <!-- Test Dependency -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
@ -81,6 +94,12 @@
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-utils</artifactId>
<version>[2.1.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -7,14 +7,14 @@ 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.JWTSecret;
import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.library.provider.ClientInfo; import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.authorization.library.utils.Caller; import org.gcube.common.keycloak.KeycloakClientHelper;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.keycloak.model.TokenResponse;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -22,65 +22,130 @@ 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); private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
protected static Properties properties; protected static final String CONFIG_INI_FILENAME = "config.ini";
protected static final String PROPERTIES_FILENAME = "token.properties";
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 { 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(PROPERTIES_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);
} catch(IOException e) {
} catch (IOException e) {
throw new RuntimeException(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 { public enum Type{
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); USER, CLIENT_ID
String context = authorizationEntry.getContext(); };
logger.info("Context of token {} is {}", token, context);
return context; 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 { public static void setContextByName(String fullContextName) throws Exception {
String token = ContextTest.properties.getProperty(fullContextName); logger.debug("Going to set credentials for context {}", fullContextName);
setContext(token); Secret secret = getSecretByContextName(fullContextName);
set(secret);
} }
public static void setContext(String token) throws ObjectNotFound, Exception {
SecurityTokenProvider.instance.set(token); private static TokenResponse getJWTAccessToken(String context) throws Exception {
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
ClientInfo clientInfo = authorizationEntry.getClientInfo();
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name()); TokenResponse tr = null;
String qualifier = authorizationEntry.getQualifier();
Caller caller = new Caller(clientInfo, qualifier); int index = context.indexOf('/', 1);
AuthorizationProvider.instance.set(caller); String root = context.substring(0, index == -1 ? context.length() : index);
ScopeProvider.instance.set(getCurrentScope(token));
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 @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
setContextByName(DEFAULT_TEST_SCOPE_NAME); setContextByName(ROOT_PREPROD);
} }
@AfterClass @AfterClass
public static void afterClass() throws Exception { public static void afterClass() throws Exception {
SecurityTokenProvider.instance.reset(); SecretManagerProvider.instance.reset();
ScopeProvider.instance.reset();
} }
} }

View File

@ -3,3 +3,4 @@
/d4science.research-infrastructures.eu.gcubekey /d4science.research-infrastructures.eu.gcubekey
/gcube.gcubekey /gcube.gcubekey
/output.txt /output.txt
/config.ini