multiple token generation in one call added

This commit is contained in:
lucio 2020-01-23 15:24:19 +01:00
parent b33853f448
commit ef7c65e0fb
3 changed files with 51 additions and 31 deletions

View File

@ -11,7 +11,7 @@
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId> <artifactId>common-smartgears</artifactId>
<version>2.1.9</version> <version>2.1.9-SNAPSHOT</version>
<name>SmartGears</name> <name>SmartGears</name>
<dependencyManagement> <dependencyManagement>

View File

@ -17,6 +17,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.FilterRegistration; import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
@ -71,20 +72,20 @@ public class ApplicationManager {
try { try {
context = provider().contextFor(container, application); context = provider().contextFor(container, application);
for (Entry<String,? extends ServletRegistration> servlet : application.getServletRegistrations().entrySet()) for (Entry<String,? extends ServletRegistration> servlet : application.getServletRegistrations().entrySet())
log.trace("servlet {} : {} {} ", application.getServletContextName(),servlet.getKey(), servlet.getValue().getMappings()); log.trace("servlet {} : {} {} ", application.getServletContextName(),servlet.getKey(), servlet.getValue().getMappings());
context.configuration().validate(); context.configuration().validate();
/* if (context.configuration().secure() && /* if (context.configuration().secure() &&
container.configuration().securePort()==null) container.configuration().securePort()==null)
throw new IllegalStateException( throw new IllegalStateException(
String.format("Application %s cannot be managed because is declared as secure without a secure connector port declared in the container", context.application().getContextPath())); String.format("Application %s cannot be managed because is declared as secure without a secure connector port declared in the container", context.application().getContextPath()));
*/ */
context.configuration().startTokens(generateTokensForApplication(container)); context.configuration().startTokens(generateTokensForApplication(container).stream().collect(Collectors.toSet()));
saveApplicationState(); saveApplicationState();
@ -128,8 +129,8 @@ public class ApplicationManager {
return context; return context;
} catch (RuntimeException e) { } catch (RuntimeException e) {
if (context != null) { if (context != null) {
log.error("error starting application {}",context.name(), e); log.error("error starting application {}",context.name(), e);
@ -142,13 +143,25 @@ public class ApplicationManager {
} }
private Set<String> generateTokensForApplication(ContainerContext container){ private List<String> generateTokensForApplication(ContainerContext container){
log.info("generating token for app {}",context.configuration().name()); log.info("generating token for app {}",context.configuration().name());
Set<String> tokens = new HashSet<String>();
AuthorizationProxy authProxy = provider().authorizationProxy(); SecurityTokenProvider.instance.set(container.configuration().startTokens().get(0));
for (String containerToken :container.configuration().startTokens()) try {
tokens.add(generateApplicationToken(containerToken, authProxy)); AuthorizationProxy authProxy = provider().authorizationProxy();
return tokens; try {
return authProxy.generateServiceToken(Utils.getServiceInfo(context), container.configuration().startTokens());
}catch (Exception e) {
log.error("error generating service token",e);
throw new RuntimeException(e);
}
} catch (Exception e) {
throw new RuntimeException("error contacting authorization service",e);
} finally{
SecurityTokenProvider.instance.reset();
}
} }
private String generateApplicationToken(String containerToken, AuthorizationProxy authProxy){ private String generateApplicationToken(String containerToken, AuthorizationProxy authProxy){
@ -161,7 +174,7 @@ public class ApplicationManager {
} finally{ } finally{
SecurityTokenProvider.instance.reset(); SecurityTokenProvider.instance.reset();
} }
} }
@ -243,12 +256,12 @@ public class ApplicationManager {
extension.init(context); extension.init(context);
if (context.configuration().includes().isEmpty()) { if (context.configuration().includes().isEmpty()) {
//register excludes for extension in case of includes they are excluded by default //register excludes for extension in case of includes they are excluded by default
context.configuration().excludes().addAll(extension.excludes()); context.configuration().excludes().addAll(extension.excludes());
} }
String mapping = extension.mapping(); String mapping = extension.mapping();
application.addServlet(context.configuration().name() + "-" + extension.name(), extension) application.addServlet(context.configuration().name() + "-" + extension.name(), extension)
@ -315,6 +328,7 @@ public class ApplicationManager {
log.trace("app token created : {} ", appToken); log.trace("app token created : {} ", appToken);
context.events().fire(appToken, ProfileEvents.addToContext); context.events().fire(appToken, ProfileEvents.addToContext);
context.events().fire(appToken, Constants.token_registered); context.events().fire(appToken, Constants.token_registered);
saveApplicationState();
} }
@Observes(value = ContextEvents.REMOVE_TOKEN_FROM_APPLICATION, kind = critical) @Observes(value = ContextEvents.REMOVE_TOKEN_FROM_APPLICATION, kind = critical)
@ -325,8 +339,9 @@ public class ApplicationManager {
log.trace("app token removed : {} ", appToken); log.trace("app token removed : {} ", appToken);
context.events().fire(appToken, ProfileEvents.removeFromContext); context.events().fire(appToken, ProfileEvents.removeFromContext);
context.events().fire(appToken, Constants.token_removed); context.events().fire(appToken, Constants.token_removed);
saveApplicationState();
} }
}; };
context.container().events().subscribe(observer); context.container().events().subscribe(observer);

View File

@ -112,24 +112,29 @@ public class ContainerManager {
} }
private void validateContainer(ContainerContext context) { private void validateContainer(ContainerContext context) {
List<String> tokensToRemove = new ArrayList<String>(); //List<String> tokensToRemove = new ArrayList<String>();
Set<String> foundContexts= new HashSet<String>(); Set<String> foundContexts= new HashSet<String>();
for (String token : context.configuration().startTokens()){ try {
String tokenContext = resolveTokenForAdd(foundContexts, token); List<AuthorizationEntry> entries = authProvider.get(context.configuration().startTokens());
if (tokenContext!=null){
log.info("the container will be started in context {}",tokenContext); log.info("requesting auth on {} tokens returned {} entries", context.configuration().startTokens().size(),entries.size());
foundContexts.add(tokenContext);
} else for (AuthorizationEntry entry : entries ) {
tokensToRemove.add(token); log.info("the container will be started in context {}",entry.getContext());
} foundContexts.add(entry.getContext());
}
} catch (Exception e) {
log.error("error contacting auth service on container",e);
}
if (foundContexts.isEmpty()){ if (foundContexts.isEmpty()){
log.error("no valid starting token are specified, moving the container to failed"); log.error("no valid starting token are specified, moving the container to failed");
throw new RuntimeException("no valid starting token are specified"); throw new RuntimeException("no valid starting token are specified");
} }
context.configuration().startTokens().removeAll(tokensToRemove); //context.configuration().startTokens().removeAll(tokensToRemove);
context.configuration().allowedContexts(foundContexts); context.configuration().allowedContexts(foundContexts);
} }