Skip to content

Commit

Permalink
[WFLY-10614] Move to new MSC Service APIs before adding new SecurityD…
Browse files Browse the repository at this point in the history
…omain handling.
  • Loading branch information
darranl committed Jun 25, 2018
1 parent 1bf993b commit 72befa9
Showing 1 changed file with 33 additions and 33 deletions.
Expand Up @@ -56,6 +56,7 @@
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;


import javax.security.jacc.WebResourcePermission; import javax.security.jacc.WebResourcePermission;
Expand All @@ -75,7 +76,6 @@
import org.jboss.as.clustering.controller.SimpleCapabilityServiceConfigurator; import org.jboss.as.clustering.controller.SimpleCapabilityServiceConfigurator;
import org.jboss.as.controller.AbstractAddStepHandler; import org.jboss.as.controller.AbstractAddStepHandler;
import org.jboss.as.controller.AttributeDefinition; import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.CapabilityServiceBuilder;
import org.jboss.as.controller.CapabilityServiceTarget; import org.jboss.as.controller.CapabilityServiceTarget;
import org.jboss.as.controller.OperationContext; import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException; import org.jboss.as.controller.OperationFailedException;
Expand All @@ -96,8 +96,8 @@
import org.jboss.dmr.ModelNode; import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType; import org.jboss.dmr.ModelType;
import org.jboss.metadata.javaee.jboss.RunAsIdentityMetaData; import org.jboss.metadata.javaee.jboss.RunAsIdentityMetaData;
import org.jboss.msc.inject.Injector; import org.jboss.msc.Service;
import org.jboss.msc.service.Service; import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceController; import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceController.Mode; import org.jboss.msc.service.ServiceController.Mode;
import org.jboss.msc.service.ServiceController.State; import org.jboss.msc.service.ServiceController.State;
Expand All @@ -106,7 +106,6 @@
import org.jboss.msc.service.StartContext; import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException; import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext; import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.wildfly.clustering.service.ServiceConfigurator; import org.wildfly.clustering.service.ServiceConfigurator;
import org.wildfly.elytron.web.undertow.server.ElytronContextAssociationHandler; import org.wildfly.elytron.web.undertow.server.ElytronContextAssociationHandler;
import org.wildfly.elytron.web.undertow.server.ElytronHttpExchange; import org.wildfly.elytron.web.undertow.server.ElytronHttpExchange;
Expand Down Expand Up @@ -278,25 +277,26 @@ protected void performRuntime(OperationContext context, ModelNode operation, Res
ModelNode model = resource.getModel(); ModelNode model = resource.getModel();
CapabilityServiceTarget target = context.getCapabilityServiceTarget(); CapabilityServiceTarget target = context.getCapabilityServiceTarget();


String httpServerMechanismFactory = HTTP_AUTHENTICATION_FACTORY.resolveModelAttribute(context, model).asString(); final String httpServerMechanismFactory = HTTP_AUTHENTICATION_FACTORY.resolveModelAttribute(context, model).asStringOrNull();
boolean overrideDeploymentConfig = OVERRIDE_DEPLOYMENT_CONFIG.resolveModelAttribute(context, model).asBoolean(); boolean overrideDeploymentConfig = OVERRIDE_DEPLOYMENT_CONFIG.resolveModelAttribute(context, model).asBoolean();
boolean enableJacc = ENABLE_JACC.resolveModelAttribute(context, model).asBoolean(); boolean enableJacc = ENABLE_JACC.resolveModelAttribute(context, model).asBoolean();


String securityDomainName = context.getCurrentAddressValue(); String securityDomainName = context.getCurrentAddressValue();


ApplicationSecurityDomainService applicationSecurityDomainService = new ApplicationSecurityDomainService(overrideDeploymentConfig, enableJacc); ServiceName applicationSecurityDomainName = APPLICATION_SECURITY_DOMAIN_RUNTIME_CAPABILITY.getCapabilityServiceName(context.getCurrentAddress());


CapabilityServiceBuilder<BiFunction<DeploymentInfo, Function<String, RunAsIdentityMetaData>, Registration>> serviceBuilder = target ServiceBuilder<?> serviceBuilder = target
.addCapability(APPLICATION_SECURITY_DOMAIN_RUNTIME_CAPABILITY, applicationSecurityDomainService) .addService(applicationSecurityDomainName)
.setInitialMode(Mode.LAZY); .setInitialMode(Mode.LAZY);


serviceBuilder.addCapabilityRequirement(REF_HTTP_AUTHENTICATION_FACTORY, HttpAuthenticationFactory.class,
applicationSecurityDomainService.getHttpAuthenticationFactoryInjector(), httpServerMechanismFactory); Supplier<HttpAuthenticationFactory> httpAuthenticationFactorySupplier = serviceBuilder.requires(context.getCapabilityServiceName(REF_HTTP_AUTHENTICATION_FACTORY, HttpAuthenticationFactory.class, httpServerMechanismFactory));


if (enableJacc) { if (enableJacc) {
serviceBuilder.addCapabilityRequirement(REF_JACC_POLICY, Policy.class); serviceBuilder.requires(context.getCapabilityServiceName(REF_JACC_POLICY, Policy.class));
} }


final Supplier<UnaryOperator<HttpServerAuthenticationMechanismFactory>> transformerSupplier;
if (resource.hasChild(UndertowExtension.PATH_SSO)) { if (resource.hasChild(UndertowExtension.PATH_SSO)) {
ModelNode ssoModel = resource.getChild(UndertowExtension.PATH_SSO).getModel(); ModelNode ssoModel = resource.getChild(UndertowExtension.PATH_SSO).getModel();


Expand All @@ -318,12 +318,16 @@ protected void performRuntime(OperationContext context, ModelNode operation, Res
ServiceConfigurator factoryConfigurator = new SingleSignOnSessionFactoryServiceConfigurator(securityDomainName).configure(context, ssoModel); ServiceConfigurator factoryConfigurator = new SingleSignOnSessionFactoryServiceConfigurator(securityDomainName).configure(context, ssoModel);
factoryConfigurator.build(target).setInitialMode(ServiceController.Mode.ON_DEMAND).install(); factoryConfigurator.build(target).setInitialMode(ServiceController.Mode.ON_DEMAND).install();


InjectedValue<SingleSignOnSessionFactory> singleSignOnSessionFactory = new InjectedValue<>(); Supplier<SingleSignOnSessionFactory> singleSignOnSessionFactorySupplier = serviceBuilder.requires(factoryConfigurator.getServiceName());
serviceBuilder.addDependency(factoryConfigurator.getServiceName(), SingleSignOnSessionFactory.class, singleSignOnSessionFactory); UnaryOperator<HttpServerAuthenticationMechanismFactory> transformer = (factory) -> new SingleSignOnServerMechanismFactory(factory, singleSignOnSessionFactorySupplier.get(), singleSignOnConfiguration);
transformerSupplier = () -> transformer;


applicationSecurityDomainService.getSingleSignOnSessionFactoryInjector().inject(factory -> new SingleSignOnServerMechanismFactory(factory, singleSignOnSessionFactory.getValue(), singleSignOnConfiguration)); } else {
transformerSupplier = () -> null;
} }


Consumer<BiFunction<DeploymentInfo, Function<String, RunAsIdentityMetaData>, Registration>> valueConsumer = serviceBuilder.provides(applicationSecurityDomainName);
serviceBuilder.setInstance(new ApplicationSecurityDomainService(overrideDeploymentConfig, enableJacc, httpAuthenticationFactorySupplier, transformerSupplier, valueConsumer));
serviceBuilder.install(); serviceBuilder.install();
} }


Expand Down Expand Up @@ -371,46 +375,42 @@ Predicate<String> getKnownSecurityDomainPredicate() {
return knownApplicationSecurityDomains::contains; return knownApplicationSecurityDomains::contains;
} }


private static class ApplicationSecurityDomainService implements Service<BiFunction<DeploymentInfo, Function<String, RunAsIdentityMetaData>, Registration>>, BiFunction<DeploymentInfo, Function<String, RunAsIdentityMetaData>, Registration> { private static class ApplicationSecurityDomainService implements Service, BiFunction<DeploymentInfo, Function<String, RunAsIdentityMetaData>, Registration> {


private final Supplier<HttpAuthenticationFactory> httpAuthenticationFactorySupplier;
private final Supplier<UnaryOperator<HttpServerAuthenticationMechanismFactory>> singleSignOnTransformerSupplier;

private final Consumer<BiFunction<DeploymentInfo, Function<String, RunAsIdentityMetaData>, Registration>> valueConsumer;


private final boolean overrideDeploymentConfig; private final boolean overrideDeploymentConfig;
private final InjectedValue<HttpAuthenticationFactory> httpAuthenticationFactoryInjector = new InjectedValue<>();
private final InjectedValue<UnaryOperator<HttpServerAuthenticationMechanismFactory>> singleSignOnTransformer = new InjectedValue<>();
private final Set<RegistrationImpl> registrations = new HashSet<>(); private final Set<RegistrationImpl> registrations = new HashSet<>();
private final boolean enableJacc; private final boolean enableJacc;
private SecurityDomain securityDomain; private SecurityDomain securityDomain;


private HttpAuthenticationFactory httpAuthenticationFactory; private HttpAuthenticationFactory httpAuthenticationFactory;


private ApplicationSecurityDomainService(final boolean overrideDeploymentConfig, boolean enableJacc) { private ApplicationSecurityDomainService(final boolean overrideDeploymentConfig, boolean enableJacc, Supplier<HttpAuthenticationFactory> httpAuthenticationFactorySupplier, Supplier<UnaryOperator<HttpServerAuthenticationMechanismFactory>> singleSignOnTransformerSupplier, Consumer<BiFunction<DeploymentInfo, Function<String, RunAsIdentityMetaData>, Registration>> valueConsumer) {
this.overrideDeploymentConfig = overrideDeploymentConfig; this.overrideDeploymentConfig = overrideDeploymentConfig;
this.enableJacc = enableJacc; this.enableJacc = enableJacc;

this.httpAuthenticationFactorySupplier = httpAuthenticationFactorySupplier;
this.singleSignOnTransformerSupplier = singleSignOnTransformerSupplier;
this.valueConsumer = valueConsumer;
} }


@Override @Override
public void start(StartContext context) throws StartException { public void start(StartContext context) throws StartException {
httpAuthenticationFactory = httpAuthenticationFactoryInjector.getValue(); httpAuthenticationFactory = httpAuthenticationFactorySupplier.get();
securityDomain = httpAuthenticationFactory.getSecurityDomain(); securityDomain = httpAuthenticationFactory.getSecurityDomain();
valueConsumer.accept(this);
} }


@Override @Override
public void stop(StopContext context) { public void stop(StopContext context) {
httpAuthenticationFactory = null; httpAuthenticationFactory = null;
} }


@Override
public BiFunction<DeploymentInfo, Function<String, RunAsIdentityMetaData>, Registration> getValue() throws IllegalStateException, IllegalArgumentException {
return this;
}

private Injector<HttpAuthenticationFactory> getHttpAuthenticationFactoryInjector() {
return httpAuthenticationFactoryInjector;
}

Injector<UnaryOperator<HttpServerAuthenticationMechanismFactory>> getSingleSignOnSessionFactoryInjector() {
return this.singleSignOnTransformer;
}

@Override @Override
public Registration apply(DeploymentInfo deploymentInfo, Function<String, RunAsIdentityMetaData> runAsMapper) { public Registration apply(DeploymentInfo deploymentInfo, Function<String, RunAsIdentityMetaData> runAsMapper) {
final ScopeSessionListener scopeSessionListener = ScopeSessionListener.builder() final ScopeSessionListener scopeSessionListener = ScopeSessionListener.builder()
Expand Down Expand Up @@ -446,7 +446,7 @@ public Registration apply(DeploymentInfo deploymentInfo, Function<String, RunAsI


private List<HttpServerAuthenticationMechanism> getAuthenticationMechanisms(Map<String, Map<String, String>> selectedMechanisms) { private List<HttpServerAuthenticationMechanism> getAuthenticationMechanisms(Map<String, Map<String, String>> selectedMechanisms) {
List<HttpServerAuthenticationMechanism> mechanisms = new ArrayList<>(selectedMechanisms.size()); List<HttpServerAuthenticationMechanism> mechanisms = new ArrayList<>(selectedMechanisms.size());
UnaryOperator<HttpServerAuthenticationMechanismFactory> singleSignOnTransformer = this.singleSignOnTransformer.getOptionalValue(); UnaryOperator<HttpServerAuthenticationMechanismFactory> singleSignOnTransformer = this.singleSignOnTransformerSupplier.get();
for (Entry<String, Map<String, String>> entry : selectedMechanisms.entrySet()) { for (Entry<String, Map<String, String>> entry : selectedMechanisms.entrySet()) {
try { try {
UnaryOperator<HttpServerAuthenticationMechanismFactory> factoryTransformation = f -> { UnaryOperator<HttpServerAuthenticationMechanismFactory> factoryTransformation = f -> {
Expand Down

0 comments on commit 72befa9

Please sign in to comment.