Reorganizing code

This commit is contained in:
luca.frosini 2023-07-13 10:00:16 +02:00
parent 0b4831ef28
commit 8d8100276a
13 changed files with 124 additions and 52 deletions

View File

@ -68,6 +68,11 @@
<artifactId>authorization-utils</artifactId>
<version>[2.1.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>keycloak-client</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.data-catalogue</groupId>
<artifactId>gcat-api</artifactId>

View File

@ -1,4 +1,4 @@
package org.gcube.grsf.publisher.record;
package org.gcube.grsf.publisher.ckan.record;
/**
* @author Luca Frosini (ISTI - CNR)

View File

@ -1,4 +1,4 @@
package org.gcube.grsf.publisher.record;
package org.gcube.grsf.publisher.ckan.record;
import java.net.URL;

View File

@ -1,4 +1,4 @@
package org.gcube.grsf.publisher.record;
package org.gcube.grsf.publisher.ckan.record;
import java.io.File;
import java.io.FileInputStream;
@ -84,26 +84,15 @@ public abstract class Record extends CKANPackage {
protected ObjectMapper objectMapper;
protected JsonNode jsonNode;
protected Set<String> groups;
protected Set<String> tags;
protected Set<FileContainer> wsUploadedFiles;
public Record() {
this.objectMapper = new ObjectMapper();
this.groups = new HashSet<>();
this.tags = new HashSet<>();
}
public abstract String getType();
protected void addGroup(String group) {
this.groups.add(group);
}
protected void addTag(String tag) {
this.tags.add(tag);
}
public void setGRSFUUID(String grsfUUID) {
this.grsfUUID = grsfUUID;
}

View File

@ -1,4 +1,4 @@
package org.gcube.grsf.publisher.record;
package org.gcube.grsf.publisher.ckan.record;
/**
* @author Luca Frosini (ISTI - CNR)

View File

@ -1,4 +1,4 @@
package org.gcube.grsf.publisher.record;
package org.gcube.grsf.publisher.ckan.record;
/**
* @author Luca Frosini (ISTI - CNR)

View File

@ -12,7 +12,7 @@ import javax.ws.rs.core.UriInfo;
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.grsf.publisher.record.Record;
import org.gcube.grsf.publisher.ckan.record.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -15,7 +15,7 @@ import javax.ws.rs.core.Response;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.grsf.publisher.annotation.PATCH;
import org.gcube.grsf.publisher.annotation.PURGE;
import org.gcube.grsf.publisher.record.Fishery;
import org.gcube.grsf.publisher.ckan.record.Fishery;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;

View File

@ -15,7 +15,7 @@ import javax.ws.rs.core.Response;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.grsf.publisher.annotation.PATCH;
import org.gcube.grsf.publisher.annotation.PURGE;
import org.gcube.grsf.publisher.record.Stock;
import org.gcube.grsf.publisher.ckan.record.Stock;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;

View File

@ -15,7 +15,7 @@ import javax.ws.rs.core.Response;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.grsf.publisher.annotation.PATCH;
import org.gcube.grsf.publisher.annotation.PURGE;
import org.gcube.grsf.publisher.record.TraceabilityUnit;
import org.gcube.grsf.publisher.ckan.record.TraceabilityUnit;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;

View File

@ -9,77 +9,151 @@ 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.gcat.configuration.CatalogueConfigurationFactory;
import org.gcube.grsf.publisher.configuration.isproxies.impl.GRSFFacetBasedISConfigurationProxyFactory;
import org.gcube.grsf.publisher.configuration.isproxies.impl.GRSFGCoreISConfigurationProxyFactory;
import org.gcube.common.keycloak.KeycloakClient;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ContextTest {
protected static Properties properties;
protected static final String PROPERTIES_FILENAME = "token.properties";
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
public static final String ROOT;
protected static final String CONFIG_INI_FILENAME = "config.properties";
public static final String GCUBE;
public static final String GCUBE_DEVNEXT;
public static final String GCUBE_DEVNEXT_NEXTNEXT;
public static final String GCUBE_DEVSEC;
public static final String GCUBE_DEVSEC_DEVVRE;
public static final String ROOT;
public static final String VO;
public static final String VRE;
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 {
properties = new Properties();
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
GCUBE = "/gcube";
GCUBE_DEVNEXT = GCUBE + "/devNext";
GCUBE_DEVNEXT_NEXTNEXT = GCUBE_DEVNEXT + "/NextNext";
GCUBE_DEVSEC = GCUBE + "/devsec";
GCUBE_DEVSEC_DEVVRE = GCUBE_DEVSEC + "/devVRE";
ROOT = GCUBE;
VO = GCUBE_DEVNEXT;
VRE = GCUBE_DEVNEXT_NEXTNEXT;
properties = new Properties();
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);
}
ROOT = "/gcube";
VO = ROOT + "/devsec";
VRE = VO + "/devVRE";
}
public enum Type{
USER, CLIENT_ID
};
public static void set(Secret secret) throws Exception {
SecretManagerProvider.instance.reset();
SecretManager secretManager = new SecretManager();
SecretManagerProvider.instance.set(secretManager);
SecretManager secretManager = new SecretManager();
secretManager.addSecret(secret);
secretManager.set();
SecretManagerProvider.instance.set(secretManager);
SecretManagerProvider.instance.get().set();
}
public static void setContextByName(String fullContextName) throws Exception {
Secret secret = getSecretByContextName(fullContextName);
set(secret);
}
private static TokenResponse getJWTAccessToken(String context) throws Exception {
Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
KeycloakClient keycloakClient = KeycloakClientFactory.newInstance();
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 = keycloakClient.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 = keycloakClient.queryOIDCTokenOfUserWithContext(context, clientId, clientSecret, username, password, context);
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);
}
public static void setContextByName(String fullContextName) throws Exception {
Secret secret = getSecretByContextName(fullContextName);
set(secret);
}
private static Secret getSecret(String token) throws Exception {
Secret secret = SecretUtility.getSecretByTokenString(token);
return secret;
}
private static Secret getSecretByContextName(String fullContextName) throws Exception {
String token = ContextTest.properties.getProperty(fullContextName);
return getSecret(token);
}
@BeforeClass
public static void beforeClass() throws Exception {
setContextByName(VRE);
GRSFFacetBasedISConfigurationProxyFactory fbigcpf = new GRSFFacetBasedISConfigurationProxyFactory();
CatalogueConfigurationFactory.addISConfigurationProxyFactory(fbigcpf);
GRSFGCoreISConfigurationProxyFactory gcigcpf = new GRSFGCoreISConfigurationProxyFactory();
CatalogueConfigurationFactory.addISConfigurationProxyFactory(gcigcpf);
}
@AfterClass

View File

@ -19,8 +19,8 @@ import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.gcat.configuration.CatalogueConfigurationFactory;
import org.gcube.gcat.utils.URIResolver;
import org.gcube.grsf.publisher.ContextTest;
import org.gcube.grsf.publisher.ckan.record.Record;
import org.gcube.grsf.publisher.configuration.GRSFCatalogueConfiguration;
import org.gcube.grsf.publisher.record.Record;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -74,7 +74,7 @@ public class FreeMarkerTest {
}
public void testAll(boolean patch) throws Exception {
int maxTestRecords = 30;
int maxTestRecords = 1;
int maxTestRecordsPerSource = 1;
FreeMarker freeMarker = new FreeMarker();

View File

@ -18,6 +18,10 @@ import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.grsf.publisher.ContextTest;
import org.gcube.grsf.publisher.ckan.record.Fishery;
import org.gcube.grsf.publisher.ckan.record.Record;
import org.gcube.grsf.publisher.ckan.record.Stock;
import org.gcube.grsf.publisher.ckan.record.TraceabilityUnit;
import org.gcube.grsf.publisher.freemarker.FreeMarker;
import org.junit.Ignore;
import org.junit.Test;