Migrated to SecretManagerProvider

This commit is contained in:
Luca Frosini 2022-03-18 12:19:20 +01:00
parent 4839ac5b4e
commit 9492350a61
5 changed files with 170 additions and 172 deletions

View File

@ -6,8 +6,6 @@ import java.util.TreeSet;
import java.util.UUID; import java.util.UUID;
import org.gcube.common.authorization.client.proxy.AuthorizationProxy; import org.gcube.common.authorization.client.proxy.AuthorizationProxy;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.smartgears.provider.ProviderFactory; import org.gcube.smartgears.provider.ProviderFactory;
@ -27,27 +25,6 @@ public class ContextUtility {
authorizationProxy = ProviderFactory.provider().authorizationProxy(); authorizationProxy = ProviderFactory.provider().authorizationProxy();
} }
public static void resetContex() {
SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset();
}
public static void setContextFromToken(String token) {
if (token == null || token.compareTo("") == 0) {
resetContex();
} else {
SecurityTokenProvider.instance.set(token);
String scope = getContextName(token);
ScopeProvider.instance.set(scope);
}
}
public static String getCurrentContextName() {
String token = SecurityTokenProvider.instance.get();
return getContextName(token);
}
public static String getContextName(String token) { public static String getContextName(String token) {
try { try {
return authorizationProxy.get(token).getContext(); return authorizationProxy.get(token).getContext();

View File

@ -10,13 +10,17 @@ import static org.gcube.smartgears.utils.Utils.rethrowUnchecked;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.SortedSet;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.events.Observes; import org.gcube.common.events.Observes;
import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
@ -96,7 +100,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
private void init() { private void init() {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
try { try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
boolean create = true; boolean create = true;
@ -105,11 +109,12 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
Set<UUID> startContextsUUID = new HashSet<>(); Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = ContextCache.getInstance(); ContextCache contextCache = ContextCache.getInstance();
for (String token : startTokens) { for (String token : startTokens) {
ContextUtility.setContextFromToken(token); Secret secret = SecretUtility.getSecretByTokenString(token);
String contextFullName = ContextUtility.getContextName(token); secretManager.startSession(secret);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
try { try {
String contextFullName = ContextUtility.getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
if (create) { if (create) {
eServiceManager = new EServiceManager(applicationContext); eServiceManager = new EServiceManager(applicationContext);
eServiceManager.createEService(); eServiceManager.createEService();
@ -120,8 +125,11 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
eServiceManager.addToContext(); eServiceManager.addToContext();
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to add {} to current context ({})", eServiceManager.getEService(), UUID uuid = UUID.fromString(applicationContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid,
ContextUtility.getContextName(token), e); ContextUtility.getContextName(token), e);
}finally {
secretManager.endSession();
} }
} }
@ -131,7 +139,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
} catch (Throwable e) { } catch (Throwable e) {
rethrowUnchecked(e); rethrowUnchecked(e);
} finally { } finally {
ContextUtility.setContextFromToken(previousToken); secretManager.endSession();
Thread.currentThread().setContextClassLoader(contextCL); Thread.currentThread().setContextClassLoader(contextCL);
} }
logger.info("{} init() terminated", this.getClass().getSimpleName()); logger.info("{} init() terminated", this.getClass().getSimpleName());
@ -144,61 +152,38 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
@Observes({ activation, stop, failure }) @Observes({ activation, stop, failure })
void onChanged(ApplicationLifecycle lc) { void onChanged(ApplicationLifecycle lc) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(applicationContext.configuration().startTokens().iterator().next());
}
try { try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
if (secrets==null || secrets.size()==0) {
String token = applicationContext.configuration().startTokens().iterator().next();
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
}
eServiceManager.updateServiceStateFacet(); eServiceManager.updateServiceStateFacet();
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to update {} State", EService.NAME, e); logger.error("Failed to update {} State", EService.NAME, e);
} finally { } finally {
ContextUtility.setContextFromToken(previousToken); secretManager.endSession();
Thread.currentThread().setContextClassLoader(contextCL); Thread.currentThread().setContextClassLoader(contextCL);
} }
} }
/*
@Observes({ stop, failure })
void onStop(ApplicationLifecycle lc) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(applicationContext.configuration().startTokens().iterator().next());
}
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
eServiceManager.updateServiceStateFacet();
} catch (Exception e) {
logger.error("Failed to update {} State", EService.NAME, e);
} finally {
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
*/
@Observes(value = addToContext) @Observes(value = addToContext)
void addTo(String token) { void addTo(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(applicationContext.configuration().startTokens().iterator().next());
}
try { try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
ContextUtility.setContextFromToken(token); Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
eServiceManager.addToContext(); eServiceManager.addToContext();
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to add {} to current context ({})", EService.NAME, logger.error("Failed to add {} to current context ({})", EService.NAME,
ContextUtility.getCurrentContextName(), e); secretManager.getContext(), e);
} finally { } finally {
ContextUtility.setContextFromToken(previousToken); secretManager.endSession();
Thread.currentThread().setContextClassLoader(contextCL); Thread.currentThread().setContextClassLoader(contextCL);
} }
} }
@ -206,19 +191,17 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
@Observes(value = removeFromContext) @Observes(value = removeFromContext)
void removeFrom(String token) { void removeFrom(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(applicationContext.configuration().startTokens().iterator().next());
}
try { try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
eServiceManager.removeFromContext(); eServiceManager.removeFromContext();
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to remove {} from current context ({})", logger.error("Failed to remove {} from current context ({})",
EService.NAME, ContextUtility.getCurrentContextName(), e); EService.NAME, secretManager.getContext(), e);
} finally { } finally {
ContextUtility.setContextFromToken(previousToken); secretManager.endSession();
Thread.currentThread().setContextClassLoader(contextCL); Thread.currentThread().setContextClassLoader(contextCL);
} }
@ -245,26 +228,28 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
} }
if (lc.state() == ApplicationState.active) { if (lc.state() == ApplicationState.active) {
logger.info("scheduling periodic updates of application {} EService", logger.info("Scheduling periodic updates of {} from application {}",
applicationContext.name()); EService.NAME, applicationContext.name());
} else { } else {
logger.info("resuming periodic updates of application {} EService", logger.info("Resuming periodic updates of {} for application {}",
applicationContext.name()); EService.NAME, applicationContext.name());
} }
final Runnable updateTask = new Runnable() { final Runnable updateTask = new Runnable() {
public void run() { public void run() {
String previousToken = SecurityTokenProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
if (previousToken == null) {
ContextUtility.setContextFromToken(
applicationContext.configuration().startTokens().iterator().next());
}
try { try {
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
if (secrets==null || secrets.size()==0) {
String token = applicationContext.configuration().startTokens().iterator().next();
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
}
eServiceManager.updateServiceStateFacet(); eServiceManager.updateServiceStateFacet();
} catch (Exception e) { } catch (Exception e) {
logger.error("Cannot complete periodic update of EService", e); logger.error("Cannot complete periodic update of {}", EService.NAME, e);
} finally { } finally {
ContextUtility.setContextFromToken(previousToken); secretManager.endSession();
} }
} }
}; };
@ -279,15 +264,15 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) { synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) {
if (periodicUpdates != null) { if (periodicUpdates != null) {
logger.trace("stopping periodic updates of EService for application {} ", logger.trace("Stopping periodic updates of {} for application {} ",
applicationContext.name()); EService.NAME, applicationContext.name());
try { try {
periodicUpdates.cancel(true); periodicUpdates.cancel(true);
periodicUpdates = null; periodicUpdates = null;
} catch (Exception e) { } catch (Exception e) {
logger.warn("could not stop periodic updates of EService for application {}", logger.warn("Could not stop periodic updates of {} for application {}",
applicationContext.name(), e); EService.NAME, applicationContext.name(), e);
} }
} }
} }

View File

@ -15,6 +15,7 @@ import static org.gcube.smartgears.utils.Utils.rethrowUnchecked;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.SortedSet;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -22,7 +23,10 @@ import java.util.concurrent.ScheduledFuture;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.events.Observes; import org.gcube.common.events.Observes;
import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.entities.Resource;
@ -95,7 +99,7 @@ public class HostingNodeHandler extends ContainerHandler {
private void init() { private void init() {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
try { try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
boolean create = true; boolean create = true;
@ -105,7 +109,8 @@ public class HostingNodeHandler extends ContainerHandler {
Set<UUID> startContextsUUID = new HashSet<>(); Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = ContextCache.getInstance(); ContextCache contextCache = ContextCache.getInstance();
for (String token : startTokens) { for (String token : startTokens) {
ContextUtility.setContextFromToken(token); Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
String contextFullName = ContextUtility.getContextName(token); String contextFullName = ContextUtility.getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName); UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID); startContextsUUID.add(contextUUID);
@ -122,7 +127,8 @@ public class HostingNodeHandler extends ContainerHandler {
hostingNodeManager.addToContext(); hostingNodeManager.addToContext();
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to add {} to current context ({})", hostingNodeManager.getHostingNode(), UUID uuid = UUID.fromString(containerContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid,
ContextUtility.getContextName(token), e); ContextUtility.getContextName(token), e);
} }
} }
@ -133,7 +139,7 @@ public class HostingNodeHandler extends ContainerHandler {
} catch (Throwable e) { } catch (Throwable e) {
rethrowUnchecked(e); rethrowUnchecked(e);
} finally { } finally {
ContextUtility.setContextFromToken(previousToken); secretManager.endSession();
Thread.currentThread().setContextClassLoader(contextCL); Thread.currentThread().setContextClassLoader(contextCL);
} }
logger.info("{} init() terminated", this.getClass().getSimpleName()); logger.info("{} init() terminated", this.getClass().getSimpleName());
@ -143,16 +149,18 @@ public class HostingNodeHandler extends ContainerHandler {
containerContext.events().subscribe(new Object() { containerContext.events().subscribe(new Object() {
//@Observes({ activation, part_activation, shutdown, stop, failure }) @Observes({ activation, part_activation, shutdown, stop, failure})
@Observes({ activation, part_activation})
void onChanged(ContainerLifecycle cl) { void onChanged(ContainerLifecycle cl) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
if (previousToken == null) {
ContextUtility.setContextFromToken(containerContext.configuration().startTokens().iterator().next());
}
try { try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
if (secrets==null || secrets.size()==0) {
String token = containerContext.configuration().startTokens().iterator().next();
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
}
ContainerState containerState = cl.state(); ContainerState containerState = cl.state();
switch (containerState) { switch (containerState) {
case active: case active:
@ -166,26 +174,7 @@ public class HostingNodeHandler extends ContainerHandler {
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to update {} State", HostingNode.NAME, e); logger.error("Failed to update {} State", HostingNode.NAME, e);
} finally { } finally {
ContextUtility.setContextFromToken(previousToken); secretManager.endSession();
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@Observes({ shutdown, stop, failure })
void onShutdown(ContainerLifecycle cl) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(containerContext.configuration().startTokens().iterator().next());
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
hostingNodeManager.updateStatus();
} catch (Exception e) {
logger.error("cannot complete periodic update of {}", HostingNode.NAME, e);
} finally {
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL); Thread.currentThread().setContextClassLoader(contextCL);
} }
} }
@ -193,18 +182,16 @@ public class HostingNodeHandler extends ContainerHandler {
@Observes(value = addToContext) @Observes(value = addToContext)
void addTo(String token) { void addTo(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(containerContext.configuration().startTokens().iterator().next());
}
try { try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
hostingNodeManager.addToContext(); hostingNodeManager.addToContext();
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to update Service State", e); logger.error("Failed to update Service State", e);
} finally { } finally {
ContextUtility.setContextFromToken(previousToken); secretManager.endSession();
Thread.currentThread().setContextClassLoader(contextCL); Thread.currentThread().setContextClassLoader(contextCL);
} }
} }
@ -212,17 +199,16 @@ public class HostingNodeHandler extends ContainerHandler {
@Observes(value = removeFromContext) @Observes(value = removeFromContext)
void removeFrom(String token) { void removeFrom(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(containerContext.configuration().startTokens().iterator().next());
}
try { try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
hostingNodeManager.removeFromContext(); hostingNodeManager.removeFromContext();
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to update Service State", e); logger.error("Failed to update Service State", e);
} finally { } finally {
secretManager.endSession();
Thread.currentThread().setContextClassLoader(contextCL); Thread.currentThread().setContextClassLoader(contextCL);
} }
} }
@ -249,22 +235,26 @@ public class HostingNodeHandler extends ContainerHandler {
} }
if (cl.state() == active) { if (cl.state() == active) {
logger.info("scheduling periodic updates of {}", HostingNode.NAME); logger.info("Scheduling periodic updates of {}", HostingNode.NAME);
} else { } else {
logger.info("resuming periodic updates of {}", HostingNode.NAME); logger.info("Resuming periodic updates of {}", HostingNode.NAME);
} }
final Runnable updateTask = new Runnable() { final Runnable updateTask = new Runnable() {
public void run() { public void run() {
String currentToken = SecurityTokenProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
if (currentToken == null)
currentToken = containerContext.configuration().startTokens().iterator().next();
ContextUtility.setContextFromToken(currentToken);
try { try {
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
if (secrets==null || secrets.size()==0) {
String token = containerContext.configuration().startTokens().iterator().next();
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
}
hostingNodeManager.updateFacets(); hostingNodeManager.updateFacets();
} catch (Exception e) { } catch (Exception e) {
logger.error("cannot complete periodic update of {}", HostingNode.NAME, e); logger.error("Cannot complete periodic update of {}", HostingNode.NAME, e);
}finally {
secretManager.endSession();
} }
} }
}; };
@ -278,13 +268,13 @@ public class HostingNodeHandler extends ContainerHandler {
synchronized void cancelPeriodicUpdates(ContainerLifecycle cl) { synchronized void cancelPeriodicUpdates(ContainerLifecycle cl) {
if (periodicUpdates != null) { if (periodicUpdates != null) {
logger.trace("stopping periodic updates of {}", HostingNode.NAME); logger.trace("Stopping periodic updates of {}", HostingNode.NAME);
try { try {
periodicUpdates.cancel(true); periodicUpdates.cancel(true);
service.shutdownNow(); service.shutdownNow();
periodicUpdates = null; periodicUpdates = null;
} catch (Exception e) { } catch (Exception e) {
logger.warn("could not stop periodic updates of {}", HostingNode.NAME, e); logger.warn("Could not stop periodic updates of {}", HostingNode.NAME, e);
} }
} }
} }

View File

@ -2,12 +2,16 @@ package org.gcube.smartgears.handler.resourceregistry.resourcemanager;
import java.net.URI; import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import javax.servlet.ServletRegistration; import javax.servlet.ServletRegistration;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.properties.Header;
@ -25,13 +29,16 @@ import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClien
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher; import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory; import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.EventFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.StateFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.StateFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.EServiceImpl; import org.gcube.resourcemanagement.model.impl.entities.resources.EServiceImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.HostingNodeImpl;
import org.gcube.resourcemanagement.model.impl.properties.ValueSchemaImpl; import org.gcube.resourcemanagement.model.impl.properties.ValueSchemaImpl;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl; import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl;
import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.ActivatesImpl; import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.ActivatesImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.AccessPointFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.AccessPointFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.EventFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService; import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
@ -42,8 +49,6 @@ import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activa
import org.gcube.smartgears.configuration.application.ApplicationConfiguration; import org.gcube.smartgears.configuration.application.ApplicationConfiguration;
import org.gcube.smartgears.configuration.container.ContainerConfiguration; import org.gcube.smartgears.configuration.container.ContainerConfiguration;
import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.handler.resourceregistry.Constants;
import org.gcube.smartgears.handler.resourceregistry.ContextUtility;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -74,36 +79,38 @@ public class EServiceManager {
} }
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
HostingNode hostingNode = applicationContext.container().properties() UUID uuid = UUID.fromString(applicationContext.container().id());
.lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode(); SecretManager secretManager = SecretManagerProvider.instance.get();
try { try {
resourceRegistryPublisher.addResourceToCurrentContext(hostingNode, false); resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} successfully added to current context ({})", eService, ContextUtility.getCurrentContextName()); logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, secretManager.getContext());
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to add {} to current context ({})", eService, ContextUtility.getCurrentContextName(), e); logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e);
} }
} }
public void removeFromContext() public void removeFromContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.container().id());
SecretManager secretManager = SecretManagerProvider.instance.get();
try { try {
resourceRegistryPublisher.removeResourceFromCurrentContext(eService, false); resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} successfully removed from current context ({})", eService, logger.info("{} with UUID {} successfully removed from current context ({})", HostingNode.NAME, uuid,
ContextUtility.getCurrentContextName()); secretManager.getContext());
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to remove {} from current context ({})", eService, logger.error("Unable to remove {} with UUID {} from current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e);
ContextUtility.getCurrentContextName(), e);
} }
} }
public void removeFromContext(UUID contextUUID) public void removeFromContext(UUID contextUUID)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID); String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID);
UUID uuid = UUID.fromString(applicationContext.container().id());
try { try {
resourceRegistryPublisher.removeResourceFromContext(eService, contextUUID, false); resourceRegistryPublisher.removeResourceFromContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} successfully removed from context ({})", eService, contextFullName); logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid, contextFullName);
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to remove {} from current context ({})", eService, contextFullName, e); logger.error("Unable to remove {} from current context ({})", HostingNode.NAME, uuid, contextFullName, e);
} }
} }
@ -173,11 +180,18 @@ public class EServiceManager {
} }
} }
Date date = Calendar.getInstance().getTime();
stateFacet = new StateFacetImpl(); stateFacet = new StateFacetImpl();
String state = getState(); String state = getState();
stateFacet.setValue(state); stateFacet.setValue(state);
eService.addFacet(stateFacet); eService.addFacet(stateFacet);
stateFacet.setAdditionalProperty("date", date);
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setEvent(state);
eventFacet.setDate(date);
return eService; return eService;
} }
@ -192,6 +206,8 @@ public class EServiceManager {
stateFacet = new StateFacetImpl(); stateFacet = new StateFacetImpl();
String state = getState(); String state = getState();
stateFacet.setValue(state); stateFacet.setValue(state);
Date date = Calendar.getInstance().getTime();
stateFacet.setAdditionalProperty("date", date);
eService.addFacet(stateFacet); eService.addFacet(stateFacet);
resourceRegistryPublisher.update(eService); resourceRegistryPublisher.update(eService);
} else { } else {
@ -218,6 +234,8 @@ public class EServiceManager {
stateFacet = new StateFacetImpl(); stateFacet = new StateFacetImpl();
String state = getState(); String state = getState();
stateFacet.setValue(state); stateFacet.setValue(state);
Date date = Calendar.getInstance().getTime();
stateFacet.setAdditionalProperty("date", date);
eService.addFacet(stateFacet); eService.addFacet(stateFacet);
resourceRegistryPublisher.update(eService); resourceRegistryPublisher.update(eService);
} else { } else {
@ -230,16 +248,38 @@ public class EServiceManager {
} }
public void updateServiceStateFacet() throws ResourceRegistryException { public void updateServiceStateFacet() throws ResourceRegistryException {
Date date = Calendar.getInstance().getTime();
String state = getState(); String state = getState();
stateFacet.setValue(state); stateFacet.setValue(state);
stateFacet = resourceRegistryPublisher.updateFacet(stateFacet);
boolean found = false;
List<EventFacet> eventFacets = eService.getFacets(EventFacet.class);
for(EventFacet eventFacet : eventFacets) {
if(eventFacet.getEvent().compareTo(state)==0) {
found = true;
eventFacet.setDate(date);
break;
}
}
if(!found) {
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setDate(date);
eventFacet.setEvent(state);
eService.addFacet(eventFacet);
}
eService = resourceRegistryPublisher.updateResource(eService);
} }
private Activates<HostingNode, EService> createActivatesRelation(EService eService) private Activates<HostingNode, EService> createActivatesRelation(EService eService)
throws ResourceRegistryException { throws ResourceRegistryException {
HostingNode hostingNode = applicationContext.container().properties() UUID hostingNodeUUID = UUID.fromString(applicationContext.container().id());
.lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode(); HostingNode hostingNode = new HostingNodeImpl();
Header header = new HeaderImpl(hostingNodeUUID);
hostingNode.setHeader(header);
PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);

View File

@ -32,6 +32,8 @@ import javax.management.ObjectName;
import javax.management.ReflectionException; import javax.management.ReflectionException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.entities.Resource;
@ -77,7 +79,6 @@ import org.gcube.smartgears.configuration.container.ContainerConfiguration;
import org.gcube.smartgears.configuration.container.Site; import org.gcube.smartgears.configuration.container.Site;
import org.gcube.smartgears.configuration.library.SmartGearsConfiguration; import org.gcube.smartgears.configuration.library.SmartGearsConfiguration;
import org.gcube.smartgears.context.container.ContainerContext; import org.gcube.smartgears.context.container.ContainerContext;
import org.gcube.smartgears.handler.resourceregistry.ContextUtility;
import org.gcube.smartgears.lifecycle.container.ContainerState; import org.gcube.smartgears.lifecycle.container.ContainerState;
import org.gcube.smartgears.provider.ProviderFactory; import org.gcube.smartgears.provider.ProviderFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -111,33 +112,38 @@ public class HostingNodeManager {
} }
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id());
SecretManager secretManager = SecretManagerProvider.instance.get();
try { try {
resourceRegistryPublisher.addResourceToCurrentContext(hostingNode, false); resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} successfully added to current context ({})", hostingNode, ContextUtility.getCurrentContextName()); logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, secretManager.getContext());
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to add {} to current context ({})", hostingNode, ContextUtility.getCurrentContextName(), e); logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e);
} }
} }
public void removeFromContext() public void removeFromContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id());
SecretManager secretManager = SecretManagerProvider.instance.get();
try { try {
resourceRegistryPublisher.removeResourceFromCurrentContext(hostingNode, false); resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} successfully removed from current context ({})", hostingNode, logger.info("{} with UUID {} successfully removed from current context ({})", HostingNode.NAME, uuid,
ContextUtility.getCurrentContextName()); secretManager.getContext());
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to remove {} from current context ({})", hostingNode, ContextUtility.getCurrentContextName(), e); logger.error("Unable to remove {} with UUID {} from current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e);
} }
} }
public void removeFromContext(UUID contextUUID) public void removeFromContext(UUID contextUUID)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID); String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID);
UUID uuid = UUID.fromString(containerContext.id());
try { try {
resourceRegistryPublisher.removeResourceFromContext(hostingNode, contextUUID, false); resourceRegistryPublisher.removeResourceFromContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} successfully removed from context ({})", hostingNode, contextFullName); logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid, contextFullName);
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to remove {} from current context ({})", hostingNode, contextFullName, e); logger.error("Unable to remove {} from current context ({})", HostingNode.NAME, uuid, contextFullName, e);
} }
} }
@ -393,7 +399,7 @@ public class HostingNodeManager {
locationFacet.setLongitude(site.longitude()); locationFacet.setLongitude(site.longitude());
hostingNode.addFacet(locationFacet); hostingNode.addFacet(locationFacet);
logger.info("hostingNode instanciated"); logger.info("{} instantiated", HostingNode.NAME);
return hostingNode; return hostingNode;
} }