private List<Tuple<PluginInfo,Plugin>> loadBundles(Set<Bundle> bundles) { List<Tuple<PluginInfo, Plugin>> plugins = new ArrayList<>(); Map<String, Plugin> loaded = new HashMap<>(); Map<String, Set<URL>> transitiveUrls = new HashMap<>(); List<Bundle> sortedBundles = sortBundles(bundles); for (Bundle bundle : sortedBundles) { checkBundleJarHell(JarHell.parseClassPath(), bundle, transitiveUrls); final Plugin plugin = loadBundle(bundle, loaded); plugins.add(new Tuple<>(bundle.plugin, plugin)); } return Collections.unmodifiableList(plugins); }
/** Adds access to classpath jars/classes for jar hell scan, etc */ @SuppressForbidden(reason = "accesses fully qualified URLs to configure security") static void addClasspathPermissions(Permissions policy) throws IOException { // add permissions to everything in classpath // really it should be covered by lib/, but there could be e.g. agents or similar configured) for (URL url : JarHell.parseClassPath()) { Path path; try { path = PathUtils.get(url.toURI()); } catch (URISyntaxException e) { throw new RuntimeException(e); } // resource itself if (Files.isDirectory(path)) { addDirectoryPath(policy, "class.path", path, "read,readlink"); } else { addSingleFilePath(policy, path, "read,readlink"); } } }
/** * Initializes SecurityManager for the environment * Can only happen once! * @param environment configuration for generating dynamic permissions * @param filterBadDefaults true if we should filter out bad java defaults in the system policy. */ static void configure(Environment environment, boolean filterBadDefaults) throws IOException, NoSuchAlgorithmException { // enable security policy: union of template and environment-based paths, and possibly plugin permissions Map<String, URL> codebases = getCodebaseJarMap(JarHell.parseClassPath()); Policy.setPolicy(new ESPolicy(codebases, createPermissions(environment), getPluginPermissions(environment), filterBadDefaults)); // enable security manager final String[] classesThatCanExit = new String[]{ // SecureSM matches class names as regular expressions so we escape the $ that arises from the nested class name ElasticsearchUncaughtExceptionHandler.PrivilegedHaltAction.class.getName().replace("$", "\\$"), Command.class.getName()}; System.setSecurityManager(new SecureSM(classesThatCanExit)); // do some basic tests selfTest(); }
/** * Parses the classpath into an array of URLs * @return array of URLs * @throws IllegalStateException if the classpath contains empty elements */ public static Set<URL> parseClassPath() { return parseClassPath(System.getProperty("java.class.path")); }
/** * Parses the classpath into an array of URLs * @return array of URLs * @throws IllegalStateException if the classpath contains empty elements */ public static Set<URL> parseClassPath() { return parseClassPath(System.getProperty("java.class.path")); }
/** * Parses the classpath into an array of URLs * @return array of URLs * @throws IllegalStateException if the classpath contains empty elements */ public static URL[] parseClassPath() { return parseClassPath(System.getProperty("java.class.path")); }
/** * Parses the classpath into an array of URLs * @return array of URLs * @throws IllegalStateException if the classpath contains empty elements */ public static Set<URL> parseClassPath() { return parseClassPath(System.getProperty("java.class.path")); }
/** * Checks the current classpath for duplicate classes * @param output A {@link String} {@link Consumer} to which debug output will be sent * @throws IllegalStateException if jar hell was found */ public static void checkJarHell(Consumer<String> output) throws IOException, URISyntaxException { ClassLoader loader = JarHell.class.getClassLoader(); output.accept("java.class.path: " + System.getProperty("java.class.path")); output.accept("sun.boot.class.path: " + System.getProperty("sun.boot.class.path")); if (loader instanceof URLClassLoader) { output.accept("classloader urls: " + Arrays.toString(((URLClassLoader)loader).getURLs())); } checkJarHell(parseClassPath(), output); }
/** * Checks the current classpath for duplicate classes * @throws IllegalStateException if jar hell was found */ public static void checkJarHell() throws IOException, URISyntaxException { ClassLoader loader = JarHell.class.getClassLoader(); Logger logger = Loggers.getLogger(JarHell.class); if (logger.isDebugEnabled()) { logger.debug("java.class.path: {}", System.getProperty("java.class.path")); logger.debug("sun.boot.class.path: {}", System.getProperty("sun.boot.class.path")); if (loader instanceof URLClassLoader ) { logger.debug("classloader urls: {}", Arrays.toString(((URLClassLoader)loader).getURLs())); } } checkJarHell(parseClassPath()); }
/** * Checks the current classpath for duplicate classes * @throws IllegalStateException if jar hell was found */ public static void checkJarHell() throws IOException, URISyntaxException { ClassLoader loader = JarHell.class.getClassLoader(); Logger logger = Loggers.getLogger(JarHell.class); if (logger.isDebugEnabled()) { logger.debug("java.class.path: {}", System.getProperty("java.class.path")); logger.debug("sun.boot.class.path: {}", System.getProperty("sun.boot.class.path")); if (loader instanceof URLClassLoader ) { logger.debug("classloader urls: {}", Arrays.toString(((URLClassLoader)loader).getURLs())); } } checkJarHell(parseClassPath()); }
/** * return parsed classpath, but with symlinks resolved to destination files for matching * this is for matching the toRealPath() in the code where we have a proper plugin structure */ @SuppressForbidden(reason = "does evil stuff with paths and urls because devs and jenkins do evil stuff with paths and urls") static Set<URL> parseClassPathWithSymlinks() throws Exception { Set<URL> raw = JarHell.parseClassPath(); Set<URL> cooked = new HashSet<>(raw.size()); for (URL url : raw) { boolean added = cooked.add(PathUtils.get(url.toURI()).toRealPath().toUri().toURL()); if (added == false) { throw new IllegalStateException("Duplicate in classpath after resolving symlinks: " + url); } } return raw; }
private List<Tuple<PluginInfo,Plugin>> loadBundles(Set<Bundle> bundles) { List<Tuple<PluginInfo, Plugin>> plugins = new ArrayList<>(); Map<String, Plugin> loaded = new HashMap<>(); Map<String, Set<URL>> transitiveUrls = new HashMap<>(); List<Bundle> sortedBundles = sortBundles(bundles); for (Bundle bundle : sortedBundles) { checkBundleJarHell(JarHell.parseClassPath(), bundle, transitiveUrls); final Plugin plugin = loadBundle(bundle, loaded); plugins.add(new Tuple<>(bundle.plugin, plugin)); } return Collections.unmodifiableList(plugins); }
private List<Tuple<PluginInfo,Plugin>> loadBundles(Set<Bundle> bundles) { List<Tuple<PluginInfo, Plugin>> plugins = new ArrayList<>(); Map<String, Plugin> loaded = new HashMap<>(); Map<String, Set<URL>> transitiveUrls = new HashMap<>(); List<Bundle> sortedBundles = sortBundles(bundles); for (Bundle bundle : sortedBundles) { checkBundleJarHell(JarHell.parseClassPath(), bundle, transitiveUrls); final Plugin plugin = loadBundle(bundle, loaded); plugins.add(new Tuple<>(bundle.plugin, plugin)); } return Collections.unmodifiableList(plugins); }
public static Set<URL> parseClassPath() { return parseClassPath(System.getProperty("java.class.path")); }
ESPolicy(PermissionCollection dynamic, Map<String,Policy> plugins, boolean filterBadDefaults) { this.template = Security.readPolicy(getClass().getResource(POLICY_RESOURCE), JarHell.parseClassPath()); this.untrusted = Security.readPolicy(getClass().getResource(UNTRUSTED_RESOURCE), Collections.emptySet()); if (filterBadDefaults) { this.system = new SystemPolicy(Policy.getPolicy()); } else { this.system = Policy.getPolicy(); } this.dynamic = dynamic; this.plugins = plugins; }
public ESPolicy(PermissionCollection dynamic, Map<String,Policy> plugins, boolean filterBadDefaults) { this.template = Security.readPolicy(getClass().getResource(POLICY_RESOURCE), JarHell.parseClassPath()); this.untrusted = Security.readPolicy(getClass().getResource(UNTRUSTED_RESOURCE), new URL[0]); if (filterBadDefaults) { this.system = new SystemPolicy(Policy.getPolicy()); } else { this.system = Policy.getPolicy(); } this.dynamic = dynamic; this.plugins = plugins; }
/** * Checks the current classpath for duplicate classes * @throws IllegalStateException if jar hell was found */ public static void checkJarHell() throws Exception { ClassLoader loader = JarHell.class.getClassLoader(); ESLogger logger = Loggers.getLogger(JarHell.class); if (logger.isDebugEnabled()) { logger.debug("java.class.path: {}", System.getProperty("java.class.path")); logger.debug("sun.boot.class.path: {}", System.getProperty("sun.boot.class.path")); if (loader instanceof URLClassLoader ) { logger.debug("classloader urls: {}", Arrays.toString(((URLClassLoader)loader).getURLs())); } } checkJarHell(parseClassPath()); }
/** Adds access to classpath jars/classes for jar hell scan, etc */ @SuppressForbidden(reason = "accesses fully qualified URLs to configure security") static void addClasspathPermissions(Permissions policy) throws IOException { // add permissions to everything in classpath // really it should be covered by lib/, but there could be e.g. agents or similar configured) for (URL url : JarHell.parseClassPath()) { Path path; try { path = PathUtils.get(url.toURI()); } catch (URISyntaxException e) { throw new RuntimeException(e); } // resource itself policy.add(new FilePermission(path.toString(), "read,readlink")); // classes underneath if (Files.isDirectory(path)) { policy.add(new FilePermission(path.toString() + path.getFileSystem().getSeparator() + "-", "read,readlink")); } } }
/** * Initializes SecurityManager for the environment * Can only happen once! * @param environment configuration for generating dynamic permissions * @param filterBadDefaults true if we should filter out bad java defaults in the system policy. */ static void configure(Environment environment, boolean filterBadDefaults) throws IOException, NoSuchAlgorithmException { // enable security policy: union of template and environment-based paths, and possibly plugin permissions Map<String, URL> codebases = getCodebaseJarMap(JarHell.parseClassPath()); Policy.setPolicy(new ESPolicy(codebases, createPermissions(environment), getPluginPermissions(environment), filterBadDefaults)); // enable security manager final String[] classesThatCanExit = new String[]{ // SecureSM matches class names as regular expressions so we escape the $ that arises from the nested class name ElasticsearchUncaughtExceptionHandler.PrivilegedHaltAction.class.getName().replace("$", "\\$"), Command.class.getName()}; System.setSecurityManager(new SecureSM(classesThatCanExit)); // do some basic tests selfTest(); }
/** * Initializes SecurityManager for the environment * Can only happen once! * @param environment configuration for generating dynamic permissions * @param filterBadDefaults true if we should filter out bad java defaults in the system policy. */ static void configure(Environment environment, boolean filterBadDefaults) throws IOException, NoSuchAlgorithmException { // enable security policy: union of template and environment-based paths, and possibly plugin permissions Map<String, URL> codebases = getCodebaseJarMap(JarHell.parseClassPath()); Policy.setPolicy(new ESPolicy(codebases, createPermissions(environment), getPluginPermissions(environment), filterBadDefaults)); // enable security manager final String[] classesThatCanExit = new String[]{ // SecureSM matches class names as regular expressions so we escape the $ that arises from the nested class name ElasticsearchUncaughtExceptionHandler.PrivilegedHaltAction.class.getName().replace("$", "\\$"), Command.class.getName()}; System.setSecurityManager(new SecureSM(classesThatCanExit)); // do some basic tests selfTest(); }