solved a bug with scope maps

This commit is contained in:
lucio 2020-04-07 19:14:43 +02:00
parent f62a477b84
commit d0a7c0520a
4 changed files with 49 additions and 36 deletions

View File

@ -27,7 +27,6 @@
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">

View File

@ -3,7 +3,9 @@ org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -61,7 +61,6 @@
<groupId>org.gcube.core</groupId>
<artifactId>common-scope-maps</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope</artifactId>

View File

@ -36,6 +36,7 @@ import org.gcube.common.authorizationservice.util.Constants;
import org.gcube.common.authorizationservice.util.TokenPersistence;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.api.ServiceMap;
import org.gcube.common.scope.impl.ScopedServiceMap;
import lombok.extern.slf4j.Slf4j;
@ -62,8 +63,15 @@ public class TokenManager {
CalledMethodProvider.instance.set("retrieve");
log.info("token retreiver called with token {}",token);
AuthorizationEntry info = persistence.getAuthorizationEntry(token);
AuthorizationEntry info = null;
try {
info = persistence.getAuthorizationEntry(token);
} catch ( Throwable t) {
log.error("erorr on authorization", t);
throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("error on authorization").type(MediaType.TEXT_PLAIN).build());
}
log.info("info retrieved {}",info);
if (info == null){
@ -71,21 +79,23 @@ public class TokenManager {
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
.entity("token "+token+" not found").type(MediaType.TEXT_PLAIN).build());
}
try{
ScopeProvider.instance.set(info.getContext());
ServiceMap map = ServiceMap.instance;
log.info("current context is {}", info.getContext());
ServiceMap map = ((ScopedServiceMap)ServiceMap.instance).currentMap();
log.info("map is {}", map);
ScopeProvider.instance.reset();
info.setMap(map);
}catch(Exception e){
log.info("map set");
}catch(Throwable e){
log.error("error retrieving map for {}", info.getContext(), e);
}
log.debug("returning info {}", info);
return info;
}
/**
*
* retrieves the AuthorzationEntry connected to the specified token
@ -104,16 +114,19 @@ public class TokenManager {
for (String token : tokens ) {
try {
AuthorizationEntry info = persistence.getAuthorizationEntry(token);
try{
ScopeProvider.instance.set(info.getContext());
ServiceMap map = ServiceMap.instance;
ServiceMap map = ((ScopedServiceMap)ServiceMap.instance).currentMap();
ScopeProvider.instance.reset();
info.setMap(map);
}catch(Exception e){
}catch(Throwable e){
log.error("error retrieving map for {}", info.getContext(), e);
}
toReturn.add(info);
}catch(Exception e) {}
}catch(Exception t) {
log.error("erorr on authorization", t);
}
}
log.info("info retrieved {}",toReturn);
@ -122,13 +135,13 @@ public class TokenManager {
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
.entity("no tokens poassed have been found ").type(MediaType.TEXT_PLAIN).build());
}
return new AuthorizationEntryList(toReturn);
}
/**
*
* retrieves the AuthorzationEntry connected to the specified token
@ -141,16 +154,16 @@ public class TokenManager {
public String getTokenByUserAndContext(@NotNull @PathParam("user") String user, @QueryParam("context") String context ) {
CalledMethodProvider.instance.set("retrieve");
log.info("resolving token for user {} in context {}",user, context);
if (context==null){
log.error("null context found");
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
.entity("null context found").type(MediaType.TEXT_PLAIN).build());
}
String token = persistence.getExistingToken(user, context, Constants.DEFAULT_TOKEN_QUALIFIER);
if (token == null){
log.error("token {} not found ", token);
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
@ -171,7 +184,7 @@ public class TokenManager {
return token;
}
/**
*
* Generates a token for a user (saving the passed roles) if it doesn't exist yet.
@ -206,7 +219,7 @@ public class TokenManager {
.entity("Error Generating Token: "+e.getMessage()).type(MediaType.TEXT_PLAIN).build());
}
}
/**
*
* Generates a token for a user (saving the passed roles) if it doesn't exist yet.
@ -234,8 +247,8 @@ public class TokenManager {
.entity("Error setting roles to token: "+e.getMessage()).type(MediaType.TEXT_PLAIN).build());
}
}
/**
*
* REmoves a token for a user.
@ -257,14 +270,14 @@ public class TokenManager {
if (clientId.split(":").length>1) throw new Exception("invalid user id: "+clientId);
persistence.removeAllAuthorizationsEntryForClientId(context, clientId);
}catch(Exception e){
log.error("error generating token ",e);
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
.entity("Error removing Token: "+e.getMessage()).type(MediaType.TEXT_PLAIN).build());
}
}
/**
*
* Generates a token for a service if it doesn't exist yet.
@ -308,25 +321,25 @@ public class TokenManager {
CalledMethodProvider.instance.set("generate");
log.info("calling generate service token bunch");
try{
AuthorizationEntry callerInfo = (AuthorizationEntry)req.getAttribute(AuthorizedCallFilter.AUTH_ATTRIBUTE);
List<String> tokensToReturn = new ArrayList<String>();
for (String token: entity.getContainerTokens()) {
AuthorizationEntry authInfo = this.retrieveToken(token);
if (authInfo==null) continue;
if (!authInfo.getClientInfo().getId().equals(callerInfo.getClientInfo().getId()))
log.warn("a token with a different ContainerInfo of the caller used, skipping it");
else {
String genToken = generateTokenForServiceInfo(entity.getInfo(), authInfo);
tokensToReturn.add(genToken);
}
}
return new ListMapper(tokensToReturn);
}catch(Exception e){
log.error("error generating tokens ",e);
@ -391,7 +404,7 @@ public class TokenManager {
.entity("Error Generating Token: "+e.getMessage()).type(MediaType.TEXT_PLAIN).build());
}
}
@Path("node")
@PUT
@Consumes(MediaType.APPLICATION_XML)
@ -399,16 +412,16 @@ public class TokenManager {
@Context HttpServletRequest req) {
CalledMethodProvider.instance.set("generate");
try{
AuthorizationEntry info = (AuthorizationEntry)req.getAttribute(AuthorizedCallFilter.AUTH_ATTRIBUTE);
if (context!=null)
return generateTokenForContainerInfo(containerInfo, context);
else if (info!=null){
log.info("generator called for node {} in context {} ",containerInfo.getId(), info.getContext());
return generateTokenForContainerInfo(containerInfo, info);
}
throw new Exception("error trying to activate node (token and context are empty)");
}catch(Exception e){
log.error("error generating token ",e);
@ -429,7 +442,7 @@ public class TokenManager {
}
return token;
}
private String generateTokenForContainerInfo(ContainerInfo containerInfo, AuthorizationEntry authInfo) throws Exception{
if (containerInfo.getId().split(":").length!=2) throw new Exception("invalid container id: "+containerInfo.getId());
@ -442,7 +455,7 @@ public class TokenManager {
}
return token;
}
private String generateTokenForServiceInfo(ServiceInfo serviceInfo, AuthorizationEntry authInfo) throws Exception{
if (serviceInfo.getId().split(":").length!=3) throw new Exception("invalid service id: "+serviceInfo.getId());