Java Code Examples for java.security.ProtectionDomain#getPermissions()

The following examples show how to use java.security.ProtectionDomain#getPermissions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PluginClassLoaderTest.java    From kafka-webview with MIT License 5 votes vote down vote up
/**
 * Tests loading a class from a jar.
 */
@Test
public void testLoadingFilterPlugin() throws ClassNotFoundException, IllegalAccessException,
    InstantiationException, NoSuchMethodException, InvocationTargetException {
    // Get URL to our jar
    final URL jar = getClass().getClassLoader().getResource("testDeserializer/testPlugins.jar");
    final String classPath = "examples.filter.LowOffsetFilter";

    // Create class loader
    final PluginClassLoader pluginClassLoader = new PluginClassLoader(jar, getClass().getClassLoader());

    final Class<? extends RecordFilter> filterPlugin = (Class<? extends RecordFilter>) pluginClassLoader.loadClass(classPath);
    assertNotNull("Should not be null", filterPlugin);

    // Create an instance of it and validate.
    final RecordFilter filter = filterPlugin.getDeclaredConstructor().newInstance();
    final String topic = "MyTopic";
    final int partition = 2;
    final long offset = 2423L;
    final Object key = "key";
    final Object value = "{name='Bob', value='value'}";
    filter.includeRecord(topic, partition, offset, key, value);

    // Validate it came from our classloader, more of a sanity test.
    assertTrue("Should have our parent class loader", filter.getClass().getClassLoader() instanceof PluginClassLoader);

    // Validate permission set defined
    final ProtectionDomain protectionDomain = filter.getClass().getProtectionDomain();
    final PermissionCollection permissionCollection = protectionDomain.getPermissions();
    assertTrue("Should have read only permissions", permissionCollection.isReadOnly());
}
 
Example 2
Source File: JarClassLoader.java    From hccd with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Loads specified JAR.
 *
 * @param jarFileInfo
 * @throws IOException
 */
private void loadJar(JarFileInfo jarFileInfo) throws IOException {
    lstJarFile.add(jarFileInfo);
    try {
        Enumeration<JarEntry> en = jarFileInfo.jarFile.entries();
        final String EXT_JAR = ".jar";
        while (en.hasMoreElements()) {
            JarEntry je = en.nextElement();
            if (je.isDirectory()) {
                continue;
            }
            String s = je.getName().toLowerCase(); // JarEntry name
            if (s.lastIndexOf(EXT_JAR) == s.length() - EXT_JAR.length()) {
                JarEntryInfo inf = new JarEntryInfo(jarFileInfo, je);
                File fileTemp = createTempFile(inf);
                logInfo(LogArea.JAR, "Loading inner JAR %s from temp file %s",
                        inf.jarEntry, getFilename4Log(fileTemp));
                // Construct ProtectionDomain for this inner JAR:
                URL url = fileTemp.toURI().toURL();
                ProtectionDomain pdParent = jarFileInfo.pd;
                // 'csParent' is never null: top JAR has it, JCL creates it for child JAR:
                CodeSource csParent = pdParent.getCodeSource();
                Certificate[] certParent = csParent.getCertificates();
                CodeSource csChild = (certParent == null ? new CodeSource(url, csParent.getCodeSigners())
                        : new CodeSource(url, certParent));
                ProtectionDomain pdChild = new ProtectionDomain(csChild,
                        pdParent.getPermissions(), pdParent.getClassLoader(), pdParent.getPrincipals());
                loadJar(new JarFileInfo(
                        new JarFile(fileTemp), inf.getName(), jarFileInfo, pdChild, fileTemp));
            }
        }
    } catch (JarClassLoaderException e) {
        throw new RuntimeException(
                "ERROR on loading inner JAR: " + e.getMessageAll());
    }
}
 
Example 3
Source File: SecuritySubject.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @return Permissions for SecurityManager checks.
 */
public default PermissionCollection sandboxPermissions() {
    ProtectionDomain pd = SecurityUtils.doPrivileged(() -> getClass().getProtectionDomain());

    return pd != null ? pd.getPermissions() : SecurityUtils.ALL_PERMISSIONS;
}