Compare commits

..

1 Commits

Author SHA1 Message Date
Luca Frosini 96b10b3b1d Improved code 2024-11-12 17:03:56 +01:00
4 changed files with 39 additions and 23 deletions

View File

@ -2,11 +2,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for Resource Registry API
# [v6.0.0-SNAPSHOT]
- Migrating code to comply with Smartgears 4
# [v5.1.0-SNAPSHOT]
- Added Contexts as Teee in ContextCache [#24555]

18
pom.xml
View File

@ -5,12 +5,12 @@
<parent>
<groupId>org.gcube.tools</groupId>
<artifactId>maven-parent</artifactId>
<version>1.2.0</version>
<version>1.1.0</version>
</parent>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-api</artifactId>
<version>6.0.0-SNAPSHOT</version>
<version>5.1.0-SNAPSHOT</version>
<name>Resource Registry API</name>
<description>Resource Registry API is a library containing classes shared across resource-registry-* components</description>
@ -30,7 +30,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>2.4.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@ -44,11 +44,7 @@
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-security</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common.security</groupId>
<artifactId>gcube-secrets</artifactId>
<artifactId>common-utility-sg3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
@ -66,5 +62,11 @@
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-utils</artifactId>
<version>[2.2.0, 3.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,6 +1,6 @@
package org.gcube.informationsystem.resourceregistry.api.rest;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.context.ContextUtility;
/**
* @author Luca Frosini (ISTI - CNR)
@ -12,7 +12,7 @@ public abstract class ServiceInstance {
public static final String BASE_URL = "https://url.d4science.org";
public static String getServiceURL() {
String context = SecretManagerProvider.get().getContext();
String context = ContextUtility.getCurrentContextFullName();
return getServiceURL(context);
}

View File

@ -7,11 +7,14 @@ 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.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.AccessTokenSecret;
import org.gcube.common.security.secrets.Secret;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@ -21,6 +24,7 @@ import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
@SuppressWarnings("deprecation")
public class ContextTest {
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
@ -72,8 +76,11 @@ public class ContextTest {
}
public static void set(Secret secret) throws Exception {
SecretManagerProvider.reset();
SecretManagerProvider.set(secret);
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 {
@ -81,21 +88,33 @@ public class ContextTest {
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;
}
public static Secret getSecretByContextName(String context) throws Exception {
TokenResponse tr = getJWTAccessToken(context);
Secret secret = new AccessTokenSecret(tr.getAccessToken(), 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;
}
public static String getUser() {
String user = Metadata.UNKNOWN_USER;
try {
user = SecretManagerProvider.get().getOwner().getId();
user = SecretManagerProvider.instance.get().getUser().getUsername();
} catch(Exception e) {
logger.error("Unable to retrieve user. {} will be used", user);
}
@ -109,7 +128,7 @@ public class ContextTest {
@AfterClass
public static void afterClass() throws Exception {
SecretManagerProvider.reset();
SecretManagerProvider.instance.reset();
}
}