Compare commits
3 Commits
master
...
feature/sg
Author | SHA1 | Date |
---|---|---|
Luca Frosini | 200abca6f8 | |
Luca Frosini | 560faafab5 | |
Luca Frosini | deabc4a4fc |
|
@ -2,6 +2,11 @@ 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
18
pom.xml
|
@ -5,12 +5,12 @@
|
|||
<parent>
|
||||
<groupId>org.gcube.tools</groupId>
|
||||
<artifactId>maven-parent</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.2.0</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.gcube.information-system</groupId>
|
||||
<artifactId>resource-registry-api</artifactId>
|
||||
<version>5.1.0-SNAPSHOT</version>
|
||||
<version>6.0.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>2.4.0</version>
|
||||
<version>4.0.0-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
@ -44,7 +44,11 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<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>
|
||||
<groupId>org.slf4j</groupId>
|
||||
|
@ -62,11 +66,5 @@
|
|||
<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>
|
|
@ -62,9 +62,12 @@ public class ContextCache {
|
|||
protected Map<String, UUID> contextFullNameToUUID;
|
||||
protected Tree<Context> contextsTree;
|
||||
|
||||
protected boolean initialized;
|
||||
|
||||
public ContextCache() {
|
||||
Calendar now = Calendar.getInstance();
|
||||
cleanCache(now);
|
||||
initialized = false;
|
||||
}
|
||||
|
||||
public void cleanCache() {
|
||||
|
@ -72,7 +75,7 @@ public class ContextCache {
|
|||
}
|
||||
|
||||
protected void cleanCache(Calendar calendar) {
|
||||
this.contexts = null;
|
||||
this.contexts = new ArrayList<>();
|
||||
this.uuidToContext = new LinkedHashMap<>();
|
||||
this.uuidToContextFullName = new LinkedHashMap<>();
|
||||
this.contextFullNameToUUID = new TreeMap<>();
|
||||
|
@ -84,6 +87,7 @@ public class ContextCache {
|
|||
this.expiringTime.setTimeInMillis(calendar.getTimeInMillis());
|
||||
this.expiringTime.add(Calendar.MILLISECOND, -1);
|
||||
this.expiringTime.add(Calendar.MILLISECOND, expiringTimeout);
|
||||
initialized = false;
|
||||
}
|
||||
|
||||
public void renew() throws ResourceRegistryException {
|
||||
|
@ -103,14 +107,17 @@ public class ContextCache {
|
|||
|
||||
public synchronized void refreshContextsIfNeeded() throws ResourceRegistryException {
|
||||
Calendar now = Calendar.getInstance();
|
||||
if((now.after(expiringTime) || (contexts==null)) && contextCacheRenewal!=null) {
|
||||
if((now.after(expiringTime) || (!initialized)) && contextCacheRenewal!=null) {
|
||||
try {
|
||||
List<Context> contexts = contextCacheRenewal.renew();
|
||||
setContexts(now, contexts);
|
||||
initialized = true;
|
||||
} catch (ResourceRegistryException e) {
|
||||
logger.error("Unable to refresh Cache", e);
|
||||
if(contexts==null) {
|
||||
if(!initialized) {
|
||||
logger.error("Unable to initialize Context Cache", 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) {
|
||||
cleanCache(calendar);
|
||||
this.contexts = new ArrayList<>();
|
||||
|
||||
for(Context c : contexts) {
|
||||
UUID uuid = c.getID();
|
||||
|
@ -232,7 +238,8 @@ public class ContextCache {
|
|||
return new TreeMap<>(contextFullNameToUUID);
|
||||
}
|
||||
|
||||
public Tree<Context> getContextsTree() {
|
||||
public Tree<Context> getContextsTree() throws ResourceRegistryException {
|
||||
refreshContextsIfNeeded();
|
||||
return contextsTree;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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)
|
||||
|
@ -12,7 +12,7 @@ public abstract class ServiceInstance {
|
|||
public static final String BASE_URL = "https://url.d4science.org";
|
||||
|
||||
public static String getServiceURL() {
|
||||
String context = ContextUtility.getCurrentContextFullName();
|
||||
String context = SecretManagerProvider.get().getContext();
|
||||
return getServiceURL(context);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,14 +7,11 @@ 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.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.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
@ -24,7 +21,6 @@ import org.slf4j.LoggerFactory;
|
|||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ContextTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
||||
|
@ -76,11 +72,8 @@ public class ContextTest {
|
|||
}
|
||||
|
||||
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();
|
||||
SecretManagerProvider.reset();
|
||||
SecretManagerProvider.set(secret);
|
||||
}
|
||||
|
||||
public static void setContextByName(String fullContextName) throws Exception {
|
||||
|
@ -88,33 +81,21 @@ 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 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);
|
||||
Secret secret = new AccessTokenSecret(tr.getAccessToken(), context);
|
||||
return secret;
|
||||
}
|
||||
|
||||
public static String getUser() {
|
||||
String user = Metadata.UNKNOWN_USER;
|
||||
try {
|
||||
user = SecretManagerProvider.instance.get().getUser().getUsername();
|
||||
user = SecretManagerProvider.get().getOwner().getId();
|
||||
} catch(Exception e) {
|
||||
logger.error("Unable to retrieve user. {} will be used", user);
|
||||
}
|
||||
|
@ -128,7 +109,7 @@ public class ContextTest {
|
|||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
SecretManagerProvider.instance.reset();
|
||||
SecretManagerProvider.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue