Compare commits

...

3 Commits

Author SHA1 Message Date
Luca Frosini 200abca6f8 updated gcube-bom 2024-11-28 12:05:58 +01:00
Luca Frosini 560faafab5 Ported fix 2024-11-28 12:04:48 +01:00
Luca Frosini deabc4a4fc Porting library to SG4 2024-05-24 11:03:12 +02:00
5 changed files with 36 additions and 45 deletions

View File

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

18
pom.xml
View File

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

View File

@ -62,9 +62,12 @@ public class ContextCache {
protected Map<String, UUID> contextFullNameToUUID; protected Map<String, UUID> contextFullNameToUUID;
protected Tree<Context> contextsTree; protected Tree<Context> contextsTree;
protected boolean initialized;
public ContextCache() { public ContextCache() {
Calendar now = Calendar.getInstance(); Calendar now = Calendar.getInstance();
cleanCache(now); cleanCache(now);
initialized = false;
} }
public void cleanCache() { public void cleanCache() {
@ -72,7 +75,7 @@ public class ContextCache {
} }
protected void cleanCache(Calendar calendar) { protected void cleanCache(Calendar calendar) {
this.contexts = null; this.contexts = new ArrayList<>();
this.uuidToContext = new LinkedHashMap<>(); this.uuidToContext = new LinkedHashMap<>();
this.uuidToContextFullName = new LinkedHashMap<>(); this.uuidToContextFullName = new LinkedHashMap<>();
this.contextFullNameToUUID = new TreeMap<>(); this.contextFullNameToUUID = new TreeMap<>();
@ -84,6 +87,7 @@ public class ContextCache {
this.expiringTime.setTimeInMillis(calendar.getTimeInMillis()); this.expiringTime.setTimeInMillis(calendar.getTimeInMillis());
this.expiringTime.add(Calendar.MILLISECOND, -1); this.expiringTime.add(Calendar.MILLISECOND, -1);
this.expiringTime.add(Calendar.MILLISECOND, expiringTimeout); this.expiringTime.add(Calendar.MILLISECOND, expiringTimeout);
initialized = false;
} }
public void renew() throws ResourceRegistryException { public void renew() throws ResourceRegistryException {
@ -103,14 +107,17 @@ public class ContextCache {
public synchronized void refreshContextsIfNeeded() throws ResourceRegistryException { public synchronized void refreshContextsIfNeeded() throws ResourceRegistryException {
Calendar now = Calendar.getInstance(); Calendar now = Calendar.getInstance();
if((now.after(expiringTime) || (contexts==null)) && contextCacheRenewal!=null) { if((now.after(expiringTime) || (!initialized)) && contextCacheRenewal!=null) {
try { try {
List<Context> contexts = contextCacheRenewal.renew(); List<Context> contexts = contextCacheRenewal.renew();
setContexts(now, contexts); setContexts(now, contexts);
initialized = true;
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
logger.error("Unable to refresh Cache", e); if(!initialized) {
if(contexts==null) { logger.error("Unable to initialize Context Cache", e);
throw e; throw e;
}else {
logger.error("Unable to refresh Context Cache", e);
} }
} }
@ -129,7 +136,6 @@ public class ContextCache {
protected void setContexts(Calendar calendar, List<Context> contexts) { protected void setContexts(Calendar calendar, List<Context> contexts) {
cleanCache(calendar); cleanCache(calendar);
this.contexts = new ArrayList<>();
for(Context c : contexts) { for(Context c : contexts) {
UUID uuid = c.getID(); UUID uuid = c.getID();
@ -232,7 +238,8 @@ public class ContextCache {
return new TreeMap<>(contextFullNameToUUID); return new TreeMap<>(contextFullNameToUUID);
} }
public Tree<Context> getContextsTree() { public Tree<Context> getContextsTree() throws ResourceRegistryException {
refreshContextsIfNeeded();
return contextsTree; return contextsTree;
} }

View File

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

View File

@ -7,14 +7,11 @@ 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.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.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse; import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.AccessTokenSecret;
import org.gcube.common.security.secrets.Secret;
import org.gcube.informationsystem.model.reference.properties.Metadata; import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -24,7 +21,6 @@ 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);
@ -76,11 +72,8 @@ public class ContextTest {
} }
public static void set(Secret secret) throws Exception { public static void set(Secret secret) throws Exception {
SecretManagerProvider.instance.reset(); SecretManagerProvider.reset();
SecretManager secretManager = new SecretManager(); SecretManagerProvider.set(secret);
secretManager.addSecret(secret);
SecretManagerProvider.instance.set(secretManager);
SecretManagerProvider.instance.get().set();
} }
public static void setContextByName(String fullContextName) throws Exception { public static void setContextByName(String fullContextName) throws Exception {
@ -88,33 +81,21 @@ public class ContextTest {
set(secret); set(secret);
} }
private static TokenResponse getJWTAccessToken(String context) throws Exception { private static TokenResponse getJWTAccessToken(String context) throws Exception {
ScopeProvider.instance.set(context);
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null); TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null);
return tr; return tr;
} }
public static Secret getSecretByContextName(String context) throws Exception { public static Secret getSecretByContextName(String context) throws Exception {
TokenResponse tr = getJWTAccessToken(context); TokenResponse tr = getJWTAccessToken(context);
Secret secret = new JWTSecret(tr.getAccessToken()); Secret secret = new AccessTokenSecret(tr.getAccessToken(), context);
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; return secret;
} }
public static String getUser() { public static String getUser() {
String user = Metadata.UNKNOWN_USER; String user = Metadata.UNKNOWN_USER;
try { try {
user = SecretManagerProvider.instance.get().getUser().getUsername(); user = SecretManagerProvider.get().getOwner().getId();
} catch(Exception e) { } catch(Exception e) {
logger.error("Unable to retrieve user. {} will be used", user); logger.error("Unable to retrieve user. {} will be used", user);
} }
@ -128,7 +109,7 @@ public class ContextTest {
@AfterClass @AfterClass
public static void afterClass() throws Exception { public static void afterClass() throws Exception {
SecretManagerProvider.instance.reset(); SecretManagerProvider.reset();
} }
} }