org.jboss.as.ee.security.JaccService Maven / Gradle / Ivy
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.jboss.as.ee.security;
import static org.jboss.as.ee.logging.EeLogger.ROOT_LOGGER;
import static org.wildfly.common.Assert.checkNotNullParam;
import java.security.Policy;
import jakarta.security.jacc.PolicyConfiguration;
import jakarta.security.jacc.PolicyConfigurationFactory;
import jakarta.security.jacc.PolicyContextException;
import org.jboss.modules.ModuleLoadException;
import org.jboss.msc.inject.Injector;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.wildfly.security.manager.WildFlySecurityManager;
/**
* A service for Jakarta Authorization policies
*
* @author Marcus Moyses
*/
public abstract class JaccService implements Service {
private static final String JACC_MODULE = "org.jboss.as.security.jacc-module";
public static final ServiceName SERVICE_NAME = ServiceName.of("jacc");
private final String contextId;
private final T metaData;
private final Boolean standalone;
private volatile PolicyConfiguration policyConfiguration;
private final InjectedValue parentPolicy = new InjectedValue();
public JaccService(final String contextId, T metaData, Boolean standalone) {
checkNotNullParam(contextId, contextId);
this.contextId = contextId;
this.metaData = metaData;
this.standalone = standalone;
}
/** {@inheritDoc} */
@Override
public PolicyConfiguration getValue() throws IllegalStateException, IllegalArgumentException {
return policyConfiguration;
}
/** {@inheritDoc} */
@Override
public void start(StartContext context) throws StartException {
try {
PolicyConfigurationFactory pcf = getPolicyConfigurationFactory();
synchronized (pcf) { // synchronize on the factory
policyConfiguration = pcf.getPolicyConfiguration(contextId, false);
if (metaData != null) {
createPermissions(metaData, policyConfiguration);
} else {
ROOT_LOGGER.debugf("Cannot create permissions with 'null' metaData for id=%s", contextId);
}
if (!standalone) {
PolicyConfiguration parent = parentPolicy.getValue();
if (parent != null) {
parent = pcf.getPolicyConfiguration(parent.getContextID(), false);
parent.linkConfiguration(policyConfiguration);
policyConfiguration.commit();
parent.commit();
} else {
ROOT_LOGGER.debugf("Could not retrieve parent policy for policy %s", contextId);
}
} else {
policyConfiguration.commit();
}
// Allow the policy to incorporate the policy configs
Policy.getPolicy().refresh();
}
} catch (Exception e) {
throw ROOT_LOGGER.unableToStartException("JaccService", e);
}
}
private PolicyConfigurationFactory getPolicyConfigurationFactory() throws ModuleLoadException, ClassNotFoundException, PolicyContextException {
String module = WildFlySecurityManager.getPropertyPrivileged(JACC_MODULE, null);
final ClassLoader originalClassLoader;
final ClassLoader jaccClassLoader;
if (module != null) {
jaccClassLoader = SecurityActions.getModuleClassLoader(module);
originalClassLoader = SecurityActions.setThreadContextClassLoader(jaccClassLoader);
} else {
jaccClassLoader = null;
originalClassLoader = null;
}
try {
return PolicyConfigurationFactory.getPolicyConfigurationFactory();
} finally {
if (originalClassLoader != null) {
SecurityActions.setThreadContextClassLoader(originalClassLoader);
}
}
}
/** {@inheritDoc} */
@Override
public void stop(StopContext context) {
try {
PolicyConfigurationFactory pcf = PolicyConfigurationFactory.getPolicyConfigurationFactory();
synchronized (pcf) { // synchronize on the factory
policyConfiguration = pcf.getPolicyConfiguration(contextId, false);
policyConfiguration.delete();
}
} catch (Exception e) {
ROOT_LOGGER.errorDeletingJACCPolicy(e);
}
policyConfiguration = null;
}
/**
* Target {@code Injector}
*
* @return target
*/
public Injector getParentPolicyInjector() {
return parentPolicy;
}
/**
* Create JACC permissions for the deployment
*
* @param metaData
* @param policyConfiguration
* @throws PolicyContextException
*/
public abstract void createPermissions(T metaData, PolicyConfiguration policyConfiguration) throws PolicyContextException;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy