Search in sources :

Example 81 with CodeSource

use of java.security.CodeSource in project jdk8u_jdk by JetBrains.

the class TokenStore method init.

private static void init() throws Exception {
    // first write policy files
    PolicyParser pp = new PolicyParser();
    pp.read(new StringReader(POLICY_NO_STORE));
    pp.write(new FileWriter(NO_STORE_FILE, false));
    pp = new PolicyParser();
    pp.read(new StringReader(POLICY_URL));
    pp.write(new FileWriter(URL_FILE, false));
    pp = new PolicyParser();
    pp.read(new StringReader(POLICY_URL_T));
    pp.write(new FileWriter(URL_T_FILE, false));
    pp = new PolicyParser();
    pp.read(new StringReader(POLICY_URL_T_P));
    pp.write(new FileWriter(URL_T_P_FILE, false));
    pp = new PolicyParser();
    pp.read(new StringReader(POLICY_URL_PWD));
    pp.write(new FileWriter(URL_PWD_FILE, false));
    pp = new PolicyParser();
    pp.read(new StringReader(POLICY_URL_T_P_PWD));
    pp.write(new FileWriter(URL_T_P_PWD_FILE, false));
    pp = new PolicyParser();
    pp.read(new StringReader(POLICY_BADPASS));
    pp.write(new FileWriter(BADPASS_FILE, false));
    // next load keystore data to build PD's
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(new FileInputStream(System.getProperty("test.src", ".") + File.separatorChar + "TokenStore.keystore"), storePassword);
    NO_STORE_DOMAIN = new ProtectionDomain(new CodeSource(new URL("file:/foo"), (java.security.cert.Certificate[]) null), // perms
    null, // class loader
    null, // principals
    null);
    Certificate[] chain = (Certificate[]) ks.getCertificateChain("POLICY_URL");
    URL_DOMAIN = new ProtectionDomain(new CodeSource(new URL("file:/foo"), chain), // perms
    null, // class loader
    null, // principals
    null);
    chain = (Certificate[]) ks.getCertificateChain("POLICY_URL_T");
    URL_T_DOMAIN = new ProtectionDomain(new CodeSource(new URL("file:/foo"), chain), // perms
    null, // class loader
    null, // principals
    null);
    chain = (Certificate[]) ks.getCertificateChain("POLICY_URL_T_P");
    URL_T_P_DOMAIN = new ProtectionDomain(new CodeSource(new URL("file:/foo"), chain), // perms
    null, // class loader
    null, // principals
    null);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) CodeSource(java.security.CodeSource) KeyStore(java.security.KeyStore)

Example 82 with CodeSource

use of java.security.CodeSource in project tdme by andreasdr.

the class StandardFileSystem method list.

/*
	 * (non-Javadoc)
	 * @see net.drewke.tdme.os.FileSystemInterface#listFiles(java.lang.String, java.io.FilenameFilter)
	 */
public String[] list(String path, FilenameFilter filter) throws IOException {
    ArrayList<String> files = new ArrayList<String>();
    // list files in file system
    String[] fileSystemFiles = new File(path).list(filter);
    if (fileSystemFiles != null) {
        for (String fileName : fileSystemFiles) {
            files.add(fileName);
        }
    }
    // list file in associated jar from calling class
    try {
        // we only support unix style path names in jar files
        path = path.replace('\\', '/');
        //
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        CodeSource src = Class.forName(stackTraceElements[2].getClassName()).getProtectionDomain().getCodeSource();
        if (src != null) {
            URL jar = src.getLocation();
            ZipInputStream zip = new ZipInputStream(jar.openStream());
            while (true) {
                ZipEntry e = zip.getNextEntry();
                if (e == null)
                    break;
                String name = e.getName();
                if (name.startsWith(path)) {
                    String fileName = name.substring(path.length() + 1);
                    if (filter.accept(new File(path), fileName))
                        files.add(fileName);
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
    //
    }
    // remove duplicate entries
    ArrayList<String> filesNoDuplicates = new ArrayList<String>();
    for (String file : files) {
        boolean duplicate = false;
        for (String _file : filesNoDuplicates) {
            if (file.equals(_file)) {
                duplicate = true;
                break;
            }
        }
        if (duplicate == false)
            filesNoDuplicates.add(file);
    }
    // 
    String[] _files = new String[filesNoDuplicates.size()];
    filesNoDuplicates.toArray(_files);
    return _files;
}
Also used : ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) CodeSource(java.security.CodeSource) URL(java.net.URL) ZipInputStream(java.util.zip.ZipInputStream) File(java.io.File)

Example 83 with CodeSource

use of java.security.CodeSource in project apex-core by apache.

the class JarHelper method getJar.

public String getJar(Class<?> jarClass) {
    String jar = null;
    final CodeSource codeSource = jarClass.getProtectionDomain().getCodeSource();
    if (codeSource != null) {
        URL location = codeSource.getLocation();
        jar = sourceToJar.get(location);
        if (jar == null) {
            // don't create jar file from folders multiple times
            if ("jar".equals(location.getProtocol())) {
                try {
                    location = ((JarURLConnection) location.openConnection()).getJarFileURL();
                } catch (IOException e) {
                    throw new AssertionError("Cannot resolve jar file for " + jarClass, e);
                }
            }
            if ("file".equals(location.getProtocol())) {
                jar = location.getFile();
                final File dir = new File(jar);
                if (dir.isDirectory()) {
                    try {
                        jar = createJar("apex-", dir, false);
                    } catch (IOException e) {
                        throw new AssertionError("Cannot resolve jar file for " + jarClass + ". URL " + location, e);
                    }
                }
            } else {
                throw new AssertionError("Cannot resolve jar file for " + jarClass + ". URL " + location);
            }
            sourceToJar.put(location, jar);
            logger.debug("added sourceLocation {} as {}", location, jar);
        }
        if (jar == null) {
            throw new AssertionError("Cannot resolve jar file for " + jarClass);
        }
    }
    return jar;
}
Also used : IOException(java.io.IOException) CodeSource(java.security.CodeSource) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL)

Example 84 with CodeSource

use of java.security.CodeSource in project poi by apache.

the class OOXMLLite method getLoadedClasses.

/**
     *
     * @param ptrn the pattern to filter output
     * @return the classes loaded by the system class loader keyed by class name
     */
@SuppressWarnings("unchecked")
private static Map<String, Class<?>> getLoadedClasses(String ptrn) {
    // make the field accessible, we defer this from static initialization to here to 
    // allow JDKs which do not have this field (e.g. IBM JDK) to at least load the class
    // without failing, see https://issues.apache.org/bugzilla/show_bug.cgi?id=56550
    final Field _classes = AccessController.doPrivileged(new PrivilegedAction<Field>() {

        @SuppressForbidden("TODO: Reflection works until Java 8 on Oracle/Sun JDKs, but breaks afterwards (different classloader types, access checks)")
        public Field run() {
            try {
                Field fld = ClassLoader.class.getDeclaredField("classes");
                fld.setAccessible(true);
                return fld;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    ClassLoader appLoader = ClassLoader.getSystemClassLoader();
    try {
        Vector<Class<?>> classes = (Vector<Class<?>>) _classes.get(appLoader);
        Map<String, Class<?>> map = new HashMap<String, Class<?>>();
        for (Class<?> cls : classes) {
            // e.g. proxy-classes, ...
            ProtectionDomain pd = cls.getProtectionDomain();
            if (pd == null)
                continue;
            CodeSource cs = pd.getCodeSource();
            if (cs == null)
                continue;
            URL loc = cs.getLocation();
            if (loc == null)
                continue;
            String jar = loc.toString();
            if (jar.contains(ptrn)) {
                map.put(cls.getName(), cls);
            }
        }
        return map;
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) HashMap(java.util.HashMap) CodeSource(java.security.CodeSource) IOException(java.io.IOException) URL(java.net.URL) Field(java.lang.reflect.Field) Vector(java.util.Vector)

Example 85 with CodeSource

use of java.security.CodeSource in project sling by apache.

the class JspRuntimeContext method initSecurity.

// -------------------------------------------------------- Private Methods
/**
     * Method used to initialize SecurityManager data.
     */
private void initSecurity() {
    // Setup the PermissionCollection for this web app context
    // based on the permissions configured for the root of the
    // web app context directory, then add a file read permission
    // for that directory.
    Policy policy = Policy.getPolicy();
    if (policy != null) {
        try {
            // Get the permissions for the web app context
            String docBase = context.getRealPath("/");
            if (docBase == null) {
                docBase = options.getScratchDir().toString();
            }
            String codeBase = docBase;
            if (!codeBase.endsWith(File.separator)) {
                codeBase = codeBase + File.separator;
            }
            File contextDir = new File(codeBase);
            URL url = contextDir.getCanonicalFile().toURL();
            final CodeSource codeSource = new CodeSource(url, (Certificate[]) null);
            permissionCollection = policy.getPermissions(codeSource);
            // Create a file read permission for web app context directory
            if (!docBase.endsWith(File.separator)) {
                permissionCollection.add(new FilePermission(docBase, "read"));
                docBase = docBase + File.separator;
            } else {
                permissionCollection.add(new FilePermission(docBase.substring(0, docBase.length() - 1), "read"));
            }
            docBase = docBase + "-";
            permissionCollection.add(new FilePermission(docBase, "read"));
            // Create a file read permission for web app tempdir (work)
            // directory
            String workDir = options.getScratchDir().toString();
            if (!workDir.endsWith(File.separator)) {
                permissionCollection.add(new FilePermission(workDir, "read"));
                workDir = workDir + File.separator;
            }
            workDir = workDir + "-";
            permissionCollection.add(new FilePermission(workDir, "read"));
            // Allow the JSP to access org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase
            permissionCollection.add(new RuntimePermission("accessClassInPackage.org.apache.jasper.runtime"));
        } catch (final Exception e) {
            context.log("Security Init for context failed", e);
        }
    }
}
Also used : Policy(java.security.Policy) CodeSource(java.security.CodeSource) File(java.io.File) FilePermission(java.io.FilePermission) URL(java.net.URL) Certificate(java.security.cert.Certificate)

Aggregations

CodeSource (java.security.CodeSource)85 URL (java.net.URL)43 ProtectionDomain (java.security.ProtectionDomain)29 File (java.io.File)24 IOException (java.io.IOException)16 Certificate (java.security.cert.Certificate)14 URISyntaxException (java.net.URISyntaxException)10 Permissions (java.security.Permissions)10 Policy (java.security.Policy)10 JarFile (java.util.jar.JarFile)10 PermissionCollection (java.security.PermissionCollection)9 URI (java.net.URI)8 FilePermission (java.io.FilePermission)7 MalformedURLException (java.net.MalformedURLException)5 AccessControlContext (java.security.AccessControlContext)5 URLClassLoader (java.net.URLClassLoader)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)3 JarURLConnection (java.net.JarURLConnection)3 SocketPermission (java.net.SocketPermission)3 Path (java.nio.file.Path)3