Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SecurityManager #1194

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@
import static io.smallrye.config.ConfigMappings.ConfigClassWithPrefix.configClassWithPrefix;
import static io.smallrye.config.inject.ConfigProducer.isClassHandledByConfigProducer;
import static io.smallrye.config.inject.InjectionMessages.formatInjectionPoint;
import static io.smallrye.config.inject.SecuritySupport.getContextClassLoader;
import static java.util.stream.Collectors.toSet;

import java.lang.reflect.ParameterizedType;
@@ -183,7 +182,8 @@ protected void registerCustomBeans(@Observes AfterBeanDiscovery abd, BeanManager
}

protected void validate(@Observes AfterDeploymentValidation adv) {
SmallRyeConfig config = ConfigProvider.getConfig(getContextClassLoader()).unwrap(SmallRyeConfig.class);
SmallRyeConfig config = ConfigProvider.getConfig(Thread.currentThread().getContextClassLoader())
.unwrap(SmallRyeConfig.class);
Set<String> configNames = StreamSupport.stream(config.getPropertyNames().spliterator(), false).collect(toSet());
for (InjectionPoint injectionPoint : getConfigPropertyInjectionPoints()) {
Type type = injectionPoint.getType();
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.smallrye.config.inject;

import static io.smallrye.config.inject.SecuritySupport.getContextClassLoader;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
@@ -52,7 +50,7 @@ public T create(final CreationalContext<T> creationalContext) {
}
}

SmallRyeConfig config = ConfigProvider.getConfig(getContextClassLoader()).unwrap(SmallRyeConfig.class);
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
return config.getConfigMapping(getBeanClass(), prefix);
}

Original file line number Diff line number Diff line change
@@ -15,8 +15,6 @@
*/
package io.smallrye.config.inject;

import static io.smallrye.config.inject.SecuritySupport.getContextClassLoader;

import java.lang.reflect.Type;
import java.util.*;
import java.util.function.Supplier;
@@ -42,7 +40,7 @@
public class ConfigProducer {
@Produces
protected SmallRyeConfig getConfig() {
return ConfigProvider.getConfig(getContextClassLoader()).unwrap(SmallRyeConfig.class);
return ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
}

@Dependent
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.smallrye.config.inject;

import static io.smallrye.config.inject.SecuritySupport.getContextClassLoader;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
@@ -47,7 +45,7 @@ public T create(final CreationalContext<T> creationalContext) {
}
}

SmallRyeConfig config = ConfigProvider.getConfig(getContextClassLoader()).unwrap(SmallRyeConfig.class);
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
return config.getConfigMapping(getBeanClass(), prefix);
}

22 changes: 2 additions & 20 deletions cdi/src/main/java/io/smallrye/config/inject/SecuritySupport.java
Original file line number Diff line number Diff line change
@@ -13,35 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.smallrye.config.inject;

import java.security.AccessController;
import java.security.PrivilegedAction;

import io.smallrye.config._private.ConfigLogging;

/**
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2018 Red Hat inc.
*/
@Deprecated(forRemoval = true)
class SecuritySupport {
private SecuritySupport() {
}

static ClassLoader getContextClassLoader() {
if (System.getSecurityManager() == null) {
return Thread.currentThread().getContextClassLoader();
} else {
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
ClassLoader tccl = null;
try {
tccl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) {
ConfigLogging.log.failedToRetrieveClassloader(ex);
}
return tccl;
});
}
return Thread.currentThread().getContextClassLoader();
}

}
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ protected List<ConfigSource> loadConfigSources(final String location, final int
}

protected List<ConfigSource> loadConfigSources(final String[] locations, final int ordinal) {
return loadConfigSources(locations, ordinal, SecuritySupport.getContextClassLoader());
return loadConfigSources(locations, ordinal, Thread.currentThread().getContextClassLoader());
}

protected List<ConfigSource> loadConfigSources(final String[] locations, final int ordinal, final ClassLoader classLoader) {
@@ -136,7 +136,7 @@ protected List<ConfigSource> tryFileSystem(final URI uri, final int ordinal) {

protected List<ConfigSource> tryClassPath(final URI uri, final int ordinal, final ClassLoader classLoader) {
final List<ConfigSource> configSources = new ArrayList<>();
final ClassLoader useClassloader = classLoader != null ? classLoader : SecuritySupport.getContextClassLoader();
final ClassLoader useClassloader = classLoader != null ? classLoader : Thread.currentThread().getContextClassLoader();
try {
consumeAsPaths(useClassloader, uri.getPath(), new ConfigSourcePathConsumer(ordinal, configSources));
} catch (IOException e) {
Original file line number Diff line number Diff line change
@@ -56,8 +56,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@@ -89,8 +87,7 @@ public class ConfigMappingGenerator {
private static final Pattern ARRAY_FORMAT_REGEX = Pattern.compile("([<;])L(.*)\\[];");

static {
usefulDebugInfo = Boolean.parseBoolean(AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty("io.smallrye.config.mapper.useful-debug-info")));
usefulDebugInfo = Boolean.parseBoolean(System.getProperty("io.smallrye.config.mapper.useful-debug-info"));
}

private static final String I_CLASS = getInternalName(Class.class);
Original file line number Diff line number Diff line change
@@ -116,7 +116,7 @@ private Converters() {
static final Converter<Class<?>> CLASS_CONVERTER = BuiltInConverter.of(6,
newTrimmingConverter(newEmptyValueConverter(value -> {
try {
return Class.forName(value, true, SecuritySupport.getContextClassLoader());
return Class.forName(value, true, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw ConfigMessages.msg.classConverterNotFound(e, value);
}
Original file line number Diff line number Diff line change
@@ -21,10 +21,8 @@
import static io.smallrye.config.common.utils.StringUtil.replaceNonAlphanumericByUnderscores;
import static io.smallrye.config.common.utils.StringUtil.toLowerCaseAndDotted;
import static java.lang.Character.toLowerCase;
import static java.security.AccessController.doPrivileged;

import java.io.Serializable;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -109,12 +107,7 @@ boolean hasPropertyName(final String propertyName) {
* instantiated in the heap.
*/
private static Map<String, String> getEnvProperties() {
return doPrivileged(new PrivilegedAction<Map<String, String>>() {
@Override
public Map<String, String> run() {
return new HashMap<>(System.getenv());
}
});
return new HashMap<>(System.getenv());
}

private static int getEnvOrdinal(final Map<String, String> properties, final int ordinal) {
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.InvocationTargetException;
@@ -74,9 +75,9 @@ static <T> Converter<T> getConverter(Class<? extends T> clazz) {

private static <T> Converter<T> getConverterFromConstructor(Class<? extends T> clazz, Class<? super String> paramType) {
try {
final Constructor<? extends T> declaredConstructor = SecuritySupport.getDeclaredConstructor(clazz, paramType);
final Constructor<? extends T> declaredConstructor = clazz.getDeclaredConstructor(paramType);
if (!isAccessible(declaredConstructor)) {
SecuritySupport.setAccessible(declaredConstructor, true);
((AccessibleObject) declaredConstructor).setAccessible(true);
}
return new ConstructorConverter<>(declaredConstructor);
} catch (NoSuchMethodException e) {
@@ -96,7 +97,7 @@ private static <T> Converter<T> getConverterFromStaticMethod(Class<? extends T>
return null;
}
if (!isAccessible(method)) {
SecuritySupport.setAccessible(method, true);
((AccessibleObject) method).setAccessible(true);
}
return new StaticMethodConverter<>(clazz, method);
} catch (NoSuchMethodException e) {
Original file line number Diff line number Diff line change
@@ -13,84 +13,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.smallrye.config;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;

import io.smallrye.config._private.ConfigLogging;

/**
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2018 Red Hat inc.
*/
@Deprecated(forRemoval = true)
class SecuritySupport {
private SecuritySupport() {
}

static ClassLoader getContextClassLoader() {
if (System.getSecurityManager() == null) {
return Thread.currentThread().getContextClassLoader();
} else {
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
ClassLoader tccl = null;
try {
tccl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) {
ConfigLogging.log.failedToRetrieveClassloader(ex);
}
return tccl;
});
}
return Thread.currentThread().getContextClassLoader();
}

static void setAccessible(AccessibleObject object, boolean flag) {
if (System.getSecurityManager() == null) {
object.setAccessible(flag);
} else {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {

try {
object.setAccessible(flag);
} catch (SecurityException ex) {
ConfigLogging.log.failedToSetAccessible(ex, object.toString());
}
return null;
});
}
object.setAccessible(flag);
}

static <T> Constructor<? extends T> getDeclaredConstructor(Class<T> clazz, Class<?>... paramTypes)
throws NoSuchMethodException {
if (System.getSecurityManager() == null) {
return clazz.getDeclaredConstructor(paramTypes);
} else {
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<Constructor<? extends T>>) () -> {
Constructor<? extends T> constructor = null;
try {
constructor = clazz.getDeclaredConstructor(paramTypes);

} catch (SecurityException ex) {
ConfigLogging.log.failedToRetrieveDeclaredConstructor(ex, clazz.toString(),
Arrays.toString(paramTypes));
}
return constructor;
});
} catch (PrivilegedActionException e) {
Exception e2 = e.getException();
if (e2 instanceof NoSuchMethodException) {
throw (NoSuchMethodException) e2;
} else {
throw new RuntimeException(e2);
}
}
}
return clazz.getDeclaredConstructor(paramTypes);
}

}
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ public class SmallRyeConfigBuilder implements ConfigBuilder {
private ConfigValidator validator = ConfigValidator.EMPTY;
private final Map<String, String> defaultValues = new HashMap<>();
private final MappingBuilder mappingsBuilder = new MappingBuilder();
private ClassLoader classLoader = SecuritySupport.getContextClassLoader();
private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
private boolean addDiscoveredCustomizers = false;
private boolean addDefaultSources = false;
private boolean addSystemSources = false;
Original file line number Diff line number Diff line change
@@ -14,14 +14,9 @@
*/
public abstract class SmallRyeConfigFactory {
/**
* Construct a new instance. Callers will be checked for the {@code getClassLoader}
* {@link RuntimePermission}.
* Construct a new instance.
*/
protected SmallRyeConfigFactory() {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("getClassLoader"));
}
}

/**
Original file line number Diff line number Diff line change
@@ -15,10 +15,6 @@
*/
package io.smallrye.config;

import static io.smallrye.config.SecuritySupport.getContextClassLoader;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;
@@ -36,17 +32,7 @@
public class SmallRyeConfigProviderResolver extends ConfigProviderResolver {
private final Map<ClassLoader, Config> configsForClassLoader = new ConcurrentHashMap<>();

static final ClassLoader SYSTEM_CL;

static {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
SYSTEM_CL = AccessController
.doPrivileged((PrivilegedAction<ClassLoader>) SmallRyeConfigProviderResolver::calculateSystemClassLoader);
} else {
SYSTEM_CL = calculateSystemClassLoader();
}
}
private static final ClassLoader SYSTEM_CL = calculateSystemClassLoader();

public SmallRyeConfigProviderResolver() {
}
@@ -63,19 +49,19 @@ private static ClassLoader calculateSystemClassLoader() {

@Override
public Config getConfig() {
return getConfig(getContextClassLoader());
return getConfig(Thread.currentThread().getContextClassLoader());
}

@Override
public Config getConfig(ClassLoader classLoader) {
final ClassLoader realClassLoader = getRealClassLoader(classLoader);
final Map<ClassLoader, Config> configsForClassLoader = this.configsForClassLoader;
ClassLoader realClassLoader = getRealClassLoader(classLoader);
Map<ClassLoader, Config> configsForClassLoader = this.configsForClassLoader;
Config config = configsForClassLoader.get(realClassLoader);
if (config == null) {
synchronized (configsForClassLoader) {
config = configsForClassLoader.get(realClassLoader);
if (config == null) {
config = getFactoryFor(realClassLoader, false).getConfigFor(this, classLoader);
config = getFactoryFor(realClassLoader).getConfigFor(this, classLoader);
// don't cache null, as that would leak class loaders
if (config == null) {
throw ConfigMessages.msg.noConfigForClassloader();
@@ -87,18 +73,9 @@ public Config getConfig(ClassLoader classLoader) {
return config;
}

SmallRyeConfigFactory getFactoryFor(final ClassLoader classLoader, final boolean privileged) {
final SecurityManager sm = System.getSecurityManager();
if (sm != null && !privileged) {
// run privileged so that the only things on the access control stack are us and the provider
return AccessController.doPrivileged(new PrivilegedAction<SmallRyeConfigFactory>() {
public SmallRyeConfigFactory run() {
return getFactoryFor(classLoader, true);
}
});
}
final ServiceLoader<SmallRyeConfigFactory> serviceLoader = ServiceLoader.load(SmallRyeConfigFactory.class, classLoader);
final Iterator<SmallRyeConfigFactory> iterator = serviceLoader.iterator();
SmallRyeConfigFactory getFactoryFor(final ClassLoader classLoader) {
ServiceLoader<SmallRyeConfigFactory> serviceLoader = ServiceLoader.load(SmallRyeConfigFactory.class, classLoader);
Iterator<SmallRyeConfigFactory> iterator = serviceLoader.iterator();
return iterator.hasNext() ? iterator.next() : SmallRyeConfigFactory.Default.INSTANCE;
}

@@ -112,8 +89,8 @@ public void registerConfig(Config config, ClassLoader classLoader) {
if (config == null) {
throw ConfigMessages.msg.configIsNull();
}
final ClassLoader realClassLoader = getRealClassLoader(classLoader);
final Map<ClassLoader, Config> configsForClassLoader = this.configsForClassLoader;
ClassLoader realClassLoader = getRealClassLoader(classLoader);
Map<ClassLoader, Config> configsForClassLoader = this.configsForClassLoader;
synchronized (configsForClassLoader) {
final Config existing = configsForClassLoader.putIfAbsent(realClassLoader, config);
if (existing != null) {
@@ -126,23 +103,23 @@ public void registerConfig(Config config, ClassLoader classLoader) {
public void releaseConfig(Config config) {
// todo: see https://github.com/eclipse/microprofile-config/issues/136#issuecomment-535962313
// todo: see https://github.com/eclipse/microprofile-config/issues/471
final Map<ClassLoader, Config> configsForClassLoader = this.configsForClassLoader;
Map<ClassLoader, Config> configsForClassLoader = this.configsForClassLoader;
synchronized (configsForClassLoader) {
configsForClassLoader.values().removeIf(v -> v == config);
}
}

public void releaseConfig(ClassLoader classLoader) {
final ClassLoader realClassLoader = getRealClassLoader(classLoader);
final Map<ClassLoader, Config> configsForClassLoader = this.configsForClassLoader;
ClassLoader realClassLoader = getRealClassLoader(classLoader);
Map<ClassLoader, Config> configsForClassLoader = this.configsForClassLoader;
synchronized (configsForClassLoader) {
configsForClassLoader.remove(realClassLoader);
}
}

static ClassLoader getRealClassLoader(ClassLoader classLoader) {
if (classLoader == null) {
classLoader = getContextClassLoader();
classLoader = Thread.currentThread().getContextClassLoader();
}
if (classLoader == null) {
classLoader = SYSTEM_CL;
Original file line number Diff line number Diff line change
@@ -16,12 +16,9 @@
package io.smallrye.config;

import static io.smallrye.config.common.utils.ConfigSourceUtil.propertiesToMap;
import static java.security.AccessController.doPrivileged;
import static java.util.Collections.unmodifiableMap;

import java.security.PrivilegedAction;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import io.smallrye.config.common.AbstractConfigSource;
@@ -49,11 +46,11 @@ public Set<String> getPropertyNames() {
}

@Override
public String getValue(String s) {
return doPrivileged((PrivilegedAction<String>) () -> System.getProperty(s));
public String getValue(String propertyName) {
return System.getProperty(propertyName);
}

private static Map<String, String> getSystemProperties() {
return unmodifiableMap(propertiesToMap(doPrivileged((PrivilegedAction<Properties>) System::getProperties)));
return unmodifiableMap(propertiesToMap(System.getProperties()));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.smallrye.config;

import static io.smallrye.config.DotEnvConfigSourceProvider.dotEnvSources;
import static io.smallrye.config.SecuritySupport.getContextClassLoader;
import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.StreamSupport.stream;
@@ -29,7 +28,8 @@ void dotEnvSource(@TempDir Path tempDir) throws Exception {

SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(), getContextClassLoader()))
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(),
Thread.currentThread().getContextClassLoader()))
.build();

assertEquals("1234", config.getRawValue("my.prop"));
@@ -69,7 +69,8 @@ void dotEnvSourceProfiles(@TempDir Path tempDir) throws Exception {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withProfile("common,dev")
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(), getContextClassLoader()))
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(),
Thread.currentThread().getContextClassLoader()))
.build();

assertEquals("main", config.getRawValue("my.prop.main"));
@@ -91,7 +92,8 @@ void dotEnvSourceConvertNames(@TempDir Path tempDir) throws Exception {

SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(), getContextClassLoader()))
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(),
Thread.currentThread().getContextClassLoader()))
.build();

assertEquals("1234", config.getRawValue("my.prop"));
@@ -124,7 +126,8 @@ void dottedDashedEnvNames(@TempDir Path tempDir) throws Exception {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(DashedEnvNames.class)
.withSources(new EnvConfigSource(emptyMap(), 300))
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(), getContextClassLoader()))
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(),
Thread.currentThread().getContextClassLoader()))
.withProfile("dev")
.build();