2020-01-27 17:07:37 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.contexts;
|
2016-07-18 11:57:12 +02:00
|
|
|
|
2017-05-04 17:24:36 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2017-11-28 17:34:43 +01:00
|
|
|
import java.util.NoSuchElementException;
|
2016-10-27 17:46:09 +02:00
|
|
|
import java.util.UUID;
|
2016-07-18 11:57:12 +02:00
|
|
|
|
2022-03-07 14:16:51 +01:00
|
|
|
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
2016-07-18 11:57:12 +02:00
|
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
2021-10-25 11:40:47 +02:00
|
|
|
import org.gcube.informationsystem.contexts.reference.entities.Context;
|
|
|
|
import org.gcube.informationsystem.contexts.reference.relations.IsParentOf;
|
2016-09-12 14:45:40 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2022-02-01 16:49:12 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
|
2020-01-27 17:07:37 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
|
2021-02-10 15:54:52 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
|
2023-05-11 18:35:56 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.utils.DBUtility;
|
2023-04-20 08:27:17 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.utils.UUIDUtility;
|
2016-07-18 11:57:12 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
import com.arcadedb.graph.MutableVertex;
|
|
|
|
import com.arcadedb.graph.Vertex;
|
|
|
|
import com.arcadedb.graph.Vertex.DIRECTION;
|
|
|
|
import com.arcadedb.query.sql.executor.Result;
|
|
|
|
import com.arcadedb.query.sql.executor.ResultSet;
|
|
|
|
import com.arcadedb.remote.RemoteDatabase;
|
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
/**
|
2016-10-12 14:33:23 +02:00
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
2016-07-18 11:57:12 +02:00
|
|
|
*/
|
2016-11-11 12:01:12 +01:00
|
|
|
public class ContextUtility {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ContextUtility.class);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
|
|
|
private Map<UUID,SecurityContext> contexts;
|
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
private static ContextUtility contextUtility;
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-12-05 10:10:34 +01:00
|
|
|
public static ContextUtility getInstance() {
|
2017-11-30 18:06:08 +01:00
|
|
|
if(contextUtility == null) {
|
2017-11-28 17:34:43 +01:00
|
|
|
contextUtility = new ContextUtility();
|
|
|
|
}
|
|
|
|
return contextUtility;
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
private ContextUtility() {
|
|
|
|
contexts = new HashMap<>();
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2021-01-26 16:54:20 +01:00
|
|
|
public static String getCurrentContextFullName() {
|
2022-03-07 14:16:51 +01:00
|
|
|
return SecretManagerProvider.instance.get().getContext();
|
2016-11-03 12:15:18 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
public static SecurityContext getCurrentSecurityContext() throws ResourceRegistryException {
|
|
|
|
String fullName = getCurrentContextFullName();
|
2017-11-30 18:06:08 +01:00
|
|
|
if(fullName == null) {
|
2017-11-28 17:34:43 +01:00
|
|
|
throw new ContextException("Null Token and Scope. Please set your token first.");
|
|
|
|
}
|
2017-12-05 10:10:34 +01:00
|
|
|
return ContextUtility.getInstance().getSecurityContextByFullName(fullName);
|
2017-11-28 17:34:43 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
public static AdminSecurityContext getAdminSecurityContext() throws ResourceRegistryException {
|
2021-10-22 19:26:36 +02:00
|
|
|
AdminSecurityContext adminSecurityContext = AdminSecurityContext.getInstance();
|
2017-11-28 17:34:43 +01:00
|
|
|
return adminSecurityContext;
|
|
|
|
}
|
|
|
|
|
2017-12-05 10:10:34 +01:00
|
|
|
public synchronized void addSecurityContext(SecurityContext securityContext) {
|
|
|
|
contexts.put(securityContext.getUUID(), securityContext);
|
|
|
|
}
|
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
public synchronized void addSecurityContext(String fullname, SecurityContext securityContext) {
|
|
|
|
contexts.put(securityContext.getUUID(), securityContext);
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2021-01-28 22:53:10 +01:00
|
|
|
public synchronized SecurityContext getSecurityContextByFullName(String fullName) throws ContextException {
|
2023-05-11 18:35:56 +02:00
|
|
|
// ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
|
|
|
RemoteDatabase database = null;
|
2016-07-18 11:57:12 +02:00
|
|
|
try {
|
2017-11-28 17:34:43 +01:00
|
|
|
SecurityContext securityContext = null;
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
logger.trace("Trying to get {} for {}", SecurityContext.class.getSimpleName(), fullName);
|
2023-05-10 17:21:00 +02:00
|
|
|
UUID uuid = ServerContextCache.getInstance().getUUIDByFullName(fullName);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2020-11-05 12:12:19 +01:00
|
|
|
if(uuid != null) {
|
|
|
|
securityContext = contexts.get(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(securityContext==null) {
|
2017-11-28 17:34:43 +01:00
|
|
|
logger.trace("{} for {} is not in cache. Going to get it", SecurityContext.class.getSimpleName(),
|
|
|
|
fullName);
|
2023-05-11 18:35:56 +02:00
|
|
|
database = getAdminSecurityContext().getRemoteDatabase(PermissionMode.READER);
|
2021-09-16 15:23:04 +02:00
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
MutableVertex contextVertex = getContextVertexByFullName(database, fullName);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2023-04-21 17:04:14 +02:00
|
|
|
uuid = UUIDUtility.getUUID(contextVertex);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
securityContext = getSecurityContextByUUID(uuid, contextVertex);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
addSecurityContext(fullName, securityContext);
|
2021-09-16 15:23:04 +02:00
|
|
|
|
2020-11-05 12:12:19 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
return securityContext;
|
2017-11-30 18:06:08 +01:00
|
|
|
|
|
|
|
} catch(ContextException e) {
|
2016-11-03 12:15:18 +01:00
|
|
|
throw e;
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(Exception e) {
|
2020-11-09 15:45:07 +01:00
|
|
|
throw new ContextException("Unable to retrieve Context UUID from current Context", e);
|
2021-09-16 15:23:04 +02:00
|
|
|
} finally {
|
2023-05-11 18:35:56 +02:00
|
|
|
if(database!=null) {
|
|
|
|
database.close();
|
2021-09-16 15:23:04 +02:00
|
|
|
}
|
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
// if(current!=null) {
|
|
|
|
// current.activateOnCurrentThread();
|
|
|
|
// }
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2018-06-06 19:26:19 +02:00
|
|
|
public SecurityContext getSecurityContextByUUID(UUID uuid) throws ResourceRegistryException {
|
2017-11-28 17:34:43 +01:00
|
|
|
return getSecurityContextByUUID(uuid, null);
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
public static RemoteDatabase getCurrentODatabaseDocumentFromThreadLocal() {
|
|
|
|
RemoteDatabase current = null;
|
2019-11-05 18:36:44 +01:00
|
|
|
try {
|
2023-05-11 18:35:56 +02:00
|
|
|
// current = (RemoteDatabase) ODatabaseRecordThreadLocal.instance().get();
|
2019-11-05 18:36:44 +01:00
|
|
|
}catch (Exception e) {
|
2021-02-16 13:52:23 +01:00
|
|
|
// It is possible that there is no current ODatabaseDocument
|
2019-11-05 18:36:44 +01:00
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
private Vertex getContextVertexByUUID(UUID uuid) throws ResourceRegistryException {
|
|
|
|
RemoteDatabase current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
|
|
|
RemoteDatabase database = null;
|
2021-09-16 15:23:04 +02:00
|
|
|
try {
|
2023-05-11 18:35:56 +02:00
|
|
|
database = getAdminSecurityContext().getRemoteDatabase(PermissionMode.READER);
|
|
|
|
Vertex vertex = DBUtility.getElementByUUID(database, Context.NAME, uuid,
|
|
|
|
Vertex.class);
|
|
|
|
return vertex;
|
2021-09-16 15:23:04 +02:00
|
|
|
} finally {
|
2023-05-11 18:35:56 +02:00
|
|
|
if(database!=null) {
|
|
|
|
database.close();
|
2021-09-16 15:23:04 +02:00
|
|
|
}
|
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
// if(current!=null) {
|
|
|
|
// current.activateOnCurrentThread();
|
|
|
|
// }
|
2019-11-05 18:36:44 +01:00
|
|
|
}
|
2016-10-14 15:35:20 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
private SecurityContext getSecurityContextByUUID(UUID uuid, Vertex contextVertex) throws ResourceRegistryException {
|
2017-11-28 17:34:43 +01:00
|
|
|
SecurityContext securityContext = contexts.get(uuid);
|
2017-11-30 18:06:08 +01:00
|
|
|
if(securityContext == null) {
|
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
securityContext = new SecurityContext(uuid);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
try {
|
2017-11-30 18:06:08 +01:00
|
|
|
if(contextVertex == null) {
|
2017-11-28 17:34:43 +01:00
|
|
|
contextVertex = getContextVertexByUUID(uuid);
|
|
|
|
}
|
2023-05-11 18:35:56 +02:00
|
|
|
Vertex parentVertex = contextVertex.getVertices(DIRECTION.IN, IsParentOf.NAME).iterator().next();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
|
|
|
if(parentVertex != null) {
|
2023-04-20 08:27:17 +02:00
|
|
|
UUID parentUUID = UUIDUtility.getUUID(parentVertex);
|
2017-11-28 17:34:43 +01:00
|
|
|
securityContext.setParentSecurityContext(getSecurityContextByUUID(parentUUID, parentVertex));
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
|
|
|
} catch(NoSuchElementException e) {
|
2017-11-28 17:34:43 +01:00
|
|
|
// No parent
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
contexts.put(uuid, securityContext);
|
2016-10-14 15:35:20 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
return securityContext;
|
2016-10-14 15:35:20 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2021-09-16 15:23:04 +02:00
|
|
|
/*
|
2018-04-12 14:46:24 +02:00
|
|
|
protected UUID getContextUUIDFromFullName(String fullName) throws ResourceRegistryException {
|
2019-11-05 18:36:44 +01:00
|
|
|
OVertex contextVertex = getContextVertexByFullName(fullName);
|
2018-04-12 14:46:24 +02:00
|
|
|
return Utility.getUUID(contextVertex);
|
|
|
|
}
|
2021-09-16 15:23:04 +02:00
|
|
|
*/
|
2018-04-12 14:46:24 +02:00
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
private MutableVertex getContextVertexByFullName(RemoteDatabase database , String fullName) throws ResourceRegistryException {
|
|
|
|
logger.trace("Going to get {} {} with full name '{}'", Context.NAME, Vertex.class.getSimpleName(), fullName);
|
2021-09-16 15:23:04 +02:00
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
ScopeBean scopeBean = new ScopeBean(fullName);
|
|
|
|
String name = scopeBean.name();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2019-11-06 12:13:19 +01:00
|
|
|
// TODO Rewrite better query. This query works because all the scope parts has a different name
|
|
|
|
String select = "SELECT FROM " + Context.class.getSimpleName() + " WHERE " + Context.NAME_PROPERTY + " = :name";
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
map.put("name", name);
|
2019-11-05 18:36:44 +01:00
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
ResultSet resultSet = database.command("sql", select, map);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2019-11-06 12:13:19 +01:00
|
|
|
if(resultSet == null || !resultSet.hasNext()) {
|
2017-11-28 17:34:43 +01:00
|
|
|
throw new ContextNotFoundException("Error retrieving context with name " + fullName);
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
Result result = resultSet.next();
|
|
|
|
MutableVertex context = (MutableVertex) ElementManagementUtility.getElementFromOptional(result.getVertex());
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
logger.trace("Context Representing Vertex : {}", DBUtility.getAsStringForLogging(context));
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2019-11-06 12:13:19 +01:00
|
|
|
if(resultSet.hasNext()) {
|
2017-11-28 17:34:43 +01:00
|
|
|
throw new ContextNotFoundException("Found more than one context with name " + name
|
|
|
|
+ "but required the one with path" + fullName + ". Please Reimplement the query");
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
return context;
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|