Search in sources :

Example 46 with CodeSource

use of java.security.CodeSource in project Lucee by lucee.

the class CFMLEngineFactory method deployBundledBundle.

private File deployBundledBundle(File bundleDirectory, String symbolicName, String symbolicVersion) {
    String sub = "bundles/";
    String nameAndVersion = symbolicName + "|" + symbolicVersion;
    String osgiFileName = symbolicName + "-" + symbolicVersion + ".jar";
    String pack20Ext = ".pack.gz";
    boolean isPack200 = false;
    // first we look for a exact match
    InputStream is = getClass().getResourceAsStream("bundles/" + osgiFileName);
    if (is == null)
        is = getClass().getResourceAsStream("/bundles/" + osgiFileName);
    if (is != null)
        printDate("found /bundles/" + osgiFileName + " in lucee.jar");
    else
        printDate("could not find /bundles/" + osgiFileName + " in lucee.jar");
    if (is == null) {
        is = getClass().getResourceAsStream("bundles/" + osgiFileName + pack20Ext);
        if (is == null)
            is = getClass().getResourceAsStream("/bundles/" + osgiFileName + pack20Ext);
        isPack200 = true;
        if (is != null)
            printDate("found /bundles/" + osgiFileName + pack20Ext + " in lucee.jar");
        else
            printDate("could not find /bundles/" + osgiFileName + pack20Ext + " in lucee.jar");
    }
    if (is != null) {
        File temp = null;
        try {
            // copy to temp file
            temp = File.createTempFile("bundle", ".tmp");
            printDate("copy lucee.jar!/bundles/" + osgiFileName + pack20Ext + " to " + temp);
            Util.copy(new BufferedInputStream(is), new FileOutputStream(temp), true, true);
            if (isPack200) {
                File temp2 = File.createTempFile("bundle", ".tmp2");
                Pack200Util.pack2Jar(temp, temp2);
                printDate("unpack " + temp + " to " + temp2);
                temp.delete();
                temp = temp2;
            }
            // adding bundle
            File trg = new File(bundleDirectory, osgiFileName);
            temp.renameTo(trg);
            printDate("adding bundle [" + symbolicName + "] in version [" + symbolicVersion + "] to [" + trg + "]");
            log(Logger.LOG_DEBUG, "adding bundle [" + symbolicName + "] in version [" + symbolicVersion + "] to [" + trg + "]");
            return trg;
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            if (temp != null && temp.exists())
                temp.delete();
        }
    }
    // this also not works with windows
    if (isWindows())
        return null;
    ZipEntry entry;
    File temp;
    ZipInputStream zis = null;
    try {
        CodeSource src = CFMLEngineFactory.class.getProtectionDomain().getCodeSource();
        if (src == null)
            return null;
        URL loc = src.getLocation();
        zis = new ZipInputStream(loc.openStream());
        String path, name, bundleInfo;
        int index;
        while ((entry = zis.getNextEntry()) != null) {
            temp = null;
            path = entry.getName().replace('\\', '/');
            // some zip path start with "/" some not
            if (path.startsWith("/"))
                path = path.substring(1);
            isPack200 = false;
            if (path.startsWith(sub) && (path.endsWith(".jar"))) {
                // ignore non jar files or file from elsewhere
                index = path.lastIndexOf('/') + 1;
                if (index == sub.length()) {
                    // ignore sub directories
                    name = path.substring(index);
                    temp = null;
                    try {
                        temp = File.createTempFile("bundle", ".tmp");
                        Util.copy(zis, new FileOutputStream(temp), false, true);
                        /*if(isPack200) {
								File temp2 = File.createTempFile("bundle", ".tmp2");
								Pack200Util.pack2Jar(temp, temp2);
								temp.delete();
								temp=temp2;
								name=name.substring(0,name.length()-".pack.gz".length());
							}*/
                        bundleInfo = BundleLoader.loadBundleInfo(temp);
                        if (bundleInfo != null && nameAndVersion.equals(bundleInfo)) {
                            File trg = new File(bundleDirectory, name);
                            temp.renameTo(trg);
                            printDate("adding bundle [ " + symbolicName + " ] in version [ " + symbolicVersion + " ] to [ " + trg + " ]");
                            log(Logger.LOG_DEBUG, "adding bundle [" + symbolicName + "] in version [" + symbolicVersion + "] to [" + trg + "]");
                            return trg;
                        }
                    } finally {
                        if (temp != null && temp.exists())
                            temp.delete();
                    }
                }
            }
            zis.closeEntry();
        }
    } catch (Throwable t) {
        if (t instanceof ThreadDeath)
            throw (ThreadDeath) t;
    } finally {
        Util.closeEL(zis);
    }
    return null;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) CodeSource(java.security.CodeSource) URL(java.net.URL) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 47 with CodeSource

use of java.security.CodeSource in project rt.equinox.framework by eclipse.

the class EclipseStarter method getSysPathFromCodeSource.

private static String getSysPathFromCodeSource() {
    ProtectionDomain pd = EclipseStarter.class.getProtectionDomain();
    if (pd == null)
        return null;
    CodeSource cs = pd.getCodeSource();
    if (cs == null)
        return null;
    URL url = cs.getLocation();
    if (url == null)
        return null;
    String result = url.getPath();
    if (File.separatorChar == '\\') {
        // in case on windows the \ is used
        result = result.replace('\\', '/');
    }
    if (result.endsWith(".jar")) {
        // $NON-NLS-1$
        result = result.substring(0, result.lastIndexOf('/'));
        if (// $NON-NLS-1$
        "folder".equals(getProperty(PROP_FRAMEWORK_SHAPE)))
            result = result.substring(0, result.lastIndexOf('/'));
    } else {
        if (// $NON-NLS-1$
        result.endsWith("/"))
            result = result.substring(0, result.length() - 1);
        result = result.substring(0, result.lastIndexOf('/'));
        result = result.substring(0, result.lastIndexOf('/'));
    }
    return result;
}
Also used : ProtectionDomain(java.security.ProtectionDomain) CodeSource(java.security.CodeSource)

Example 48 with CodeSource

use of java.security.CodeSource in project drools by kiegroup.

the class PackageCompilationDataTest method testCodeSourceUrl.

@Test
public void testCodeSourceUrl() throws Exception {
    final String className = TestEvalExpression.class.getName();
    KnowledgeBaseImpl kBase = new KnowledgeBaseImpl("xxx", null);
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
    pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    JavaDialectRuntimeData data = new JavaDialectRuntimeData();
    data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pkg.getDialectRuntimeRegistry().setDialectData("java", data);
    kBase.addPackage(pkg);
    final JavaDialectRuntimeData pcData = (JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java");
    final EvalCondition invoker = new EvalCondition(null);
    pcData.putInvoker(className, invoker);
    final InputStream is = getClass().getClassLoader().getResourceAsStream(className.replace('.', '/') + ".class");
    try {
        pcData.write(className.replace('.', '/') + ".class", read(is));
    } finally {
        is.close();
    }
    pcData.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pcData.onBeforeExecute();
    Class cls = kBase.getRootClassLoader().loadClass("org.drools.core.rule.PackageCompilationDataTest$TestEvalExpression");
    final CodeSource codeSource = invoker.getEvalExpression().getClass().getProtectionDomain().getCodeSource();
    assertNotNull(codeSource.getLocation());
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) InputStream(java.io.InputStream) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) CodeSource(java.security.CodeSource) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 49 with CodeSource

use of java.security.CodeSource in project Payara by payara.

the class SecurityContextUtil method authorizeCORBA.

// return true if authorization succeeds, false otherwise.
private boolean authorizeCORBA(byte[] object_id, String method) throws Exception {
    // Check if target is an EJB
    ProtocolManager protocolMgr = orbHelper.getProtocolManager();
    // is on a callback object in the client VM.
    if (protocolMgr == null) {
        return true;
    }
    if (protocolMgr.getEjbDescriptor(object_id) != null) {
        // an EJB object
        return true;
    }
    CORBAObjectPermission perm = new CORBAObjectPermission("*", method);
    // Create a ProtectionDomain for principal on current thread.
    com.sun.enterprise.security.SecurityContext sc = com.sun.enterprise.security.SecurityContext.getCurrent();
    Set principalSet = sc.getPrincipalSet();
    Principal[] principals = (principalSet == null ? null : (Principal[]) principalSet.toArray(new Principal[principalSet.size()]));
    CodeSource cs = new CodeSource(new java.net.URL("file://"), (java.security.cert.Certificate[]) null);
    ProtectionDomain prdm = new ProtectionDomain(cs, null, null, principals);
    // Check if policy gives principal the permissions
    boolean result = policy.implies(prdm, perm);
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "CORBA Object permission evaluation result=" + result + " for method=" + method);
    }
    return result;
}
Also used : ProtectionDomain(java.security.ProtectionDomain) Set(java.util.Set) CORBAObjectPermission(com.sun.enterprise.security.CORBAObjectPermission) CodeSource(java.security.CodeSource) ProtocolManager(org.glassfish.enterprise.iiop.api.ProtocolManager) Principal(java.security.Principal)

Example 50 with CodeSource

use of java.security.CodeSource in project Payara by payara.

the class SMGlobalPolicyUtil method loadServerPolicy.

private static void loadServerPolicy(PolicyType policyType) throws IOException {
    if (policyType == null)
        return;
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("PolicyType= " + policyType);
    }
    String policyFilename = null;
    Map<CommponentType, PermissionCollection> policyMap = null;
    switch(policyType) {
        case EEGranted:
            policyFilename = domainCfgFolder + EE_GRANT_FILE;
            policyMap = compTypeToEEGarntsMap;
            break;
        case EERestricted:
            policyFilename = domainCfgFolder + EE_RESTRICTED_FILE;
            policyMap = compTypeToEERestrictedMap;
            break;
        case ServerAllowed:
            policyFilename = domainCfgFolder + SERVER_ALLOWED_FILE;
            policyMap = compTypeToServAllowedMap;
            break;
    }
    if (policyFilename == null || policyMap == null)
        throw new IllegalArgumentException("Unrecognized policy type: " + policyType);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("policyFilename= " + policyFilename);
    }
    File f = new File(policyFilename);
    if (!f.exists())
        return;
    URL furl = new URL("file:" + policyFilename);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("Loading policy from " + furl);
    }
    PolicyFile pf = new PolicyFile(furl);
    CodeSource cs = new CodeSource(new URL(EJB_TYPE_CODESOURCE), (Certificate[]) null);
    PermissionCollection pc = pf.getPermissions(cs);
    policyMap.put(CommponentType.ejb, pc);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("Loaded EJB policy = " + pc);
    }
    cs = new CodeSource(new URL(WEB_TYPE_CODESOURCE), (Certificate[]) null);
    pc = pf.getPermissions(cs);
    policyMap.put(CommponentType.war, pc);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("Loaded WEB policy =" + pc);
    }
    cs = new CodeSource(new URL(RAR_TYPE_CODESOURCE), (Certificate[]) null);
    pc = pf.getPermissions(cs);
    policyMap.put(CommponentType.rar, pc);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("Loaded rar policy =" + pc);
    }
    cs = new CodeSource(new URL(CLIENT_TYPE_CODESOURCE), (Certificate[]) null);
    pc = pf.getPermissions(cs);
    policyMap.put(CommponentType.car, pc);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("Loaded car policy =" + pc);
    }
    cs = new CodeSource(new URL(EAR_TYPE_CODESOURCE), (Certificate[]) null);
    pc = pf.getPermissions(cs);
    policyMap.put(CommponentType.ear, pc);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("Loaded ear policy =" + pc);
    }
}
Also used : PermissionCollection(java.security.PermissionCollection) PolicyFile(sun.security.provider.PolicyFile) CodeSource(java.security.CodeSource) PolicyFile(sun.security.provider.PolicyFile) File(java.io.File) URL(java.net.URL) Certificate(java.security.cert.Certificate)

Aggregations

CodeSource (java.security.CodeSource)104 URL (java.net.URL)49 ProtectionDomain (java.security.ProtectionDomain)39 File (java.io.File)30 IOException (java.io.IOException)20 Certificate (java.security.cert.Certificate)17 JarFile (java.util.jar.JarFile)13 PermissionCollection (java.security.PermissionCollection)12 URI (java.net.URI)11 URISyntaxException (java.net.URISyntaxException)11 Permissions (java.security.Permissions)11 Policy (java.security.Policy)10 FilePermission (java.io.FilePermission)7 InputStream (java.io.InputStream)6 AccessControlContext (java.security.AccessControlContext)6 MalformedURLException (java.net.MalformedURLException)5 Permission (java.security.Permission)4 JarEntry (java.util.jar.JarEntry)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)3 URLClassLoader (java.net.URLClassLoader)3