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;
@ -78,13 +79,13 @@ public class ApplicationManager {
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();
@ -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){
@ -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,6 +339,7 @@ public class ApplicationManager {
log.trace("app token removed : {} ", appToken);
context.events().fire(appToken, ProfileEvents.removeFromContext);
context.events().fire(appToken, Constants.token_removed);
saveApplicationState();
}
};

View File

@ -112,16 +112,21 @@ 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()){
@ -129,7 +134,7 @@ public class ContainerManager {
throw new RuntimeException("no valid starting token are specified");
}
context.configuration().startTokens().removeAll(tokensToRemove);
//context.configuration().startTokens().removeAll(tokensToRemove);
context.configuration().allowedContexts(foundContexts);
}