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>
<artifactId>common-smartgears</artifactId>
<version>2.1.9</version>
<version>2.1.9-SNAPSHOT</version>
<name>SmartGears</name>
<dependencyManagement>

View File

@ -17,6 +17,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
@ -71,20 +72,20 @@ public class ApplicationManager {
try {
context = provider().contextFor(container, application);
for (Entry<String,? extends ServletRegistration> servlet : application.getServletRegistrations().entrySet())
log.trace("servlet {} : {} {} ", application.getServletContextName(),servlet.getKey(), servlet.getValue().getMappings());
context.configuration().validate();
/* if (context.configuration().secure() &&
/* if (context.configuration().secure() &&
container.configuration().securePort()==null)
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()));
*/
*/
context.configuration().startTokens(generateTokensForApplication(container));
context.configuration().startTokens(generateTokensForApplication(container).stream().collect(Collectors.toSet()));
saveApplicationState();
@ -128,8 +129,8 @@ public class ApplicationManager {
return context;
} catch (RuntimeException e) {
if (context != null) {
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());
Set<String> tokens = new HashSet<String>();
AuthorizationProxy authProxy = provider().authorizationProxy();
for (String containerToken :container.configuration().startTokens())
tokens.add(generateApplicationToken(containerToken, authProxy));
return tokens;
SecurityTokenProvider.instance.set(container.configuration().startTokens().get(0));
try {
AuthorizationProxy authProxy = provider().authorizationProxy();
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){
@ -161,7 +174,7 @@ public class ApplicationManager {
} finally{
SecurityTokenProvider.instance.reset();
}
}
@ -243,12 +256,12 @@ public class ApplicationManager {
extension.init(context);
if (context.configuration().includes().isEmpty()) {
//register excludes for extension in case of includes they are excluded by default
context.configuration().excludes().addAll(extension.excludes());
}
String mapping = extension.mapping();
application.addServlet(context.configuration().name() + "-" + extension.name(), extension)
@ -315,6 +328,7 @@ public class ApplicationManager {
log.trace("app token created : {} ", appToken);
context.events().fire(appToken, ProfileEvents.addToContext);
context.events().fire(appToken, Constants.token_registered);
saveApplicationState();
}
@Observes(value = ContextEvents.REMOVE_TOKEN_FROM_APPLICATION, kind = critical)
@ -325,8 +339,9 @@ public class ApplicationManager {
log.trace("app token removed : {} ", appToken);
context.events().fire(appToken, ProfileEvents.removeFromContext);
context.events().fire(appToken, Constants.token_removed);
saveApplicationState();
}
};
context.container().events().subscribe(observer);

View File

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