Search in sources :

Example 6 with NameClassPair

use of javax.naming.NameClassPair in project activemq-artemis by apache.

the class SaslKrb5LDAPSecurityTest method testSaslGssapiLdapAuth.

@Test
public void testSaslGssapiLdapAuth() throws Exception {
    final Hashtable<String, String> env = new Hashtable<>();
    env.put(Context.PROVIDER_URL, "ldap://localhost:1024");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI");
    LoginContext loginContext = new LoginContext("broker-sasl-gssapi");
    loginContext.login();
    try {
        Subject.doAs(loginContext.getSubject(), (PrivilegedExceptionAction<Object>) () -> {
            HashSet<String> set = new HashSet<>();
            DirContext ctx = new InitialDirContext(env);
            NamingEnumeration<NameClassPair> list = ctx.list("ou=system");
            while (list.hasMore()) {
                NameClassPair ncp = list.next();
                set.add(ncp.getName());
            }
            Assert.assertTrue(set.contains("uid=first"));
            Assert.assertTrue(set.contains("cn=users"));
            Assert.assertTrue(set.contains("ou=configuration"));
            Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot"));
            ctx.close();
            return null;
        });
    } catch (PrivilegedActionException e) {
        throw e.getException();
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) PrivilegedActionException(java.security.PrivilegedActionException) Hashtable(java.util.Hashtable) NameClassPair(javax.naming.NameClassPair) NamingEnumeration(javax.naming.NamingEnumeration) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with NameClassPair

use of javax.naming.NameClassPair in project activemq-artemis by apache.

the class LDAPAuthorizationMapTest method testOpen.

@Test
public void testOpen() throws Exception {
    DirContext ctx = authMap.open();
    HashSet<String> set = new HashSet<>();
    NamingEnumeration<NameClassPair> list = ctx.list("ou=destinations,o=ActiveMQ,ou=system");
    while (list.hasMore()) {
        NameClassPair ncp = list.next();
        set.add(ncp.getName());
    }
    assertTrue(set.contains("ou=topics"));
    assertTrue(set.contains("ou=queues"));
}
Also used : NameClassPair(javax.naming.NameClassPair) DirContext(javax.naming.directory.DirContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with NameClassPair

use of javax.naming.NameClassPair in project activemq-artemis by apache.

the class LegacyLDAPSecuritySettingPluginListenerTest method testRunning.

@Test
public void testRunning() throws Exception {
    DirContext ctx = getContext();
    HashSet<String> set = new HashSet<>();
    NamingEnumeration<NameClassPair> list = ctx.list("ou=system");
    while (list.hasMore()) {
        NameClassPair ncp = list.next();
        set.add(ncp.getName());
    }
    Assert.assertTrue(set.contains("uid=admin"));
    Assert.assertTrue(set.contains("ou=users"));
    Assert.assertTrue(set.contains("ou=groups"));
    Assert.assertTrue(set.contains("ou=configuration"));
    Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot"));
}
Also used : NameClassPair(javax.naming.NameClassPair) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with NameClassPair

use of javax.naming.NameClassPair in project activemq-artemis by apache.

the class LDAPLoginModuleTest method testRunning.

@Test
public void testRunning() throws Exception {
    Hashtable<String, String> env = new Hashtable<>();
    env.put(Context.PROVIDER_URL, "ldap://localhost:1024");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
    env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
    DirContext ctx = new InitialDirContext(env);
    HashSet<String> set = new HashSet<>();
    NamingEnumeration<NameClassPair> list = ctx.list("ou=system");
    while (list.hasMore()) {
        NameClassPair ncp = list.next();
        set.add(ncp.getName());
    }
    assertTrue(set.contains("uid=admin"));
    assertTrue(set.contains("ou=users"));
    assertTrue(set.contains("ou=groups"));
    assertTrue(set.contains("ou=configuration"));
    assertTrue(set.contains("prefNodeName=sysPrefRoot"));
}
Also used : Hashtable(java.util.Hashtable) NameClassPair(javax.naming.NameClassPair) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with NameClassPair

use of javax.naming.NameClassPair in project tomcat70 by apache.

the class WebappLoader method setRepositories.

/**
 * Configure the repositories for our class loader, based on the
 * associated Context.
 * @throws IOException
 */
private void setRepositories() throws IOException {
    if (!(container instanceof Context))
        return;
    ServletContext servletContext = ((Context) container).getServletContext();
    if (servletContext == null)
        return;
    loaderRepositories = new ArrayList<String>();
    // Loading the work directory
    File workDir = (File) servletContext.getAttribute(ServletContext.TEMPDIR);
    if (workDir == null) {
        log.info("No work dir for " + servletContext);
    }
    if (log.isDebugEnabled() && workDir != null)
        log.debug(sm.getString("webappLoader.deploy", workDir.getAbsolutePath()));
    classLoader.setWorkDir(workDir);
    DirContext resources = container.getResources();
    // Setting up the class repository (/WEB-INF/classes), if it exists
    String classesPath = "/WEB-INF/classes";
    DirContext classes = null;
    try {
        Object object = resources.lookup(classesPath);
        if (object instanceof DirContext) {
            classes = (DirContext) object;
        }
    } catch (NamingException e) {
    // Silent catch: it's valid that no /WEB-INF/classes collection
    // exists
    }
    if (classes != null) {
        File classRepository = null;
        String absoluteClassesPath = servletContext.getRealPath(classesPath);
        if (absoluteClassesPath != null) {
            classRepository = new File(absoluteClassesPath);
        } else {
            classRepository = new File(workDir, classesPath);
            if (!classRepository.mkdirs() && !classRepository.isDirectory()) {
                throw new IOException(sm.getString("webappLoader.mkdirFailure"));
            }
            if (!copyDir(classes, classRepository)) {
                throw new IOException(sm.getString("webappLoader.copyFailure"));
            }
        }
        if (log.isDebugEnabled())
            log.debug(sm.getString("webappLoader.classDeploy", classesPath, classRepository.getAbsolutePath()));
        // Adding the repository to the class loader
        classLoader.addRepository(classesPath + "/", classRepository);
        loaderRepositories.add(classesPath + "/");
    }
    // Setting up the JAR repository (/WEB-INF/lib), if it exists
    String libPath = "/WEB-INF/lib";
    classLoader.setJarPath(libPath);
    DirContext libDir = null;
    // Looking up directory /WEB-INF/lib in the context
    try {
        Object object = resources.lookup(libPath);
        if (object instanceof DirContext)
            libDir = (DirContext) object;
    } catch (NamingException e) {
    // Silent catch: it's valid that no /WEB-INF/lib collection
    // exists
    }
    if (libDir != null) {
        boolean copyJars = false;
        String absoluteLibPath = servletContext.getRealPath(libPath);
        File destDir = null;
        if (absoluteLibPath != null) {
            destDir = new File(absoluteLibPath);
        } else {
            copyJars = true;
            destDir = new File(workDir, libPath);
            if (!destDir.mkdirs() && !destDir.isDirectory()) {
                throw new IOException(sm.getString("webappLoader.mkdirFailure"));
            }
        }
        // Looking up directory /WEB-INF/lib in the context
        NamingEnumeration<NameClassPair> enumeration = null;
        try {
            enumeration = libDir.list("");
        } catch (NamingException e) {
            IOException ioe = new IOException(sm.getString("webappLoader.namingFailure", libPath));
            ioe.initCause(e);
            throw ioe;
        }
        while (enumeration.hasMoreElements()) {
            NameClassPair ncPair = enumeration.nextElement();
            String filename = libPath + "/" + ncPair.getName();
            if (!filename.endsWith(".jar"))
                continue;
            // Copy JAR in the work directory, always (the JAR file
            // would get locked otherwise, which would make it
            // impossible to update it or remove it at runtime)
            File destFile = new File(destDir, ncPair.getName());
            if (log.isDebugEnabled())
                log.debug(sm.getString("webappLoader.jarDeploy", filename, destFile.getAbsolutePath()));
            // Bug 45403 - Explicitly call lookup() on the name to check
            // that the resource is readable. We cannot use resources
            // returned by listBindings(), because that lists all of them,
            // but does not perform the necessary checks on each.
            Object obj = null;
            try {
                obj = libDir.lookup(ncPair.getName());
            } catch (NamingException e) {
                IOException ioe = new IOException(sm.getString("webappLoader.namingFailure", filename));
                ioe.initCause(e);
                throw ioe;
            }
            if (!(obj instanceof Resource))
                continue;
            Resource jarResource = (Resource) obj;
            if (copyJars) {
                if (!copy(jarResource.streamContent(), new FileOutputStream(destFile))) {
                    throw new IOException(sm.getString("webappLoader.copyFailure"));
                }
            }
            try {
                JarFile jarFile = JreCompat.getInstance().jarFileNewInstance(destFile);
                classLoader.addJar(filename, jarFile, destFile);
            } catch (Exception ex) {
            // Catch the exception if there is an empty jar file
            // Should ignore and continue loading other jar files
            // in the dir
            }
            loaderRepositories.add(filename);
        }
    }
}
Also used : DirContext(javax.naming.directory.DirContext) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) StandardContext(org.apache.catalina.core.StandardContext) Resource(org.apache.naming.resources.Resource) DirContext(javax.naming.directory.DirContext) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) NamingException(javax.naming.NamingException) LifecycleException(org.apache.catalina.LifecycleException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NameClassPair(javax.naming.NameClassPair) FileOutputStream(java.io.FileOutputStream) ServletContext(javax.servlet.ServletContext) NamingException(javax.naming.NamingException) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

NameClassPair (javax.naming.NameClassPair)59 NamingException (javax.naming.NamingException)27 DirContext (javax.naming.directory.DirContext)19 Test (org.junit.Test)17 HashSet (java.util.HashSet)13 Context (javax.naming.Context)11 InitialContext (javax.naming.InitialContext)10 Hashtable (java.util.Hashtable)9 NamingEnumeration (javax.naming.NamingEnumeration)8 InitialDirContext (javax.naming.directory.InitialDirContext)8 IOException (java.io.IOException)7 FileNotFoundException (java.io.FileNotFoundException)5 HashMap (java.util.HashMap)5 Resource (org.apache.naming.resources.Resource)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 Vector (java.util.Vector)4 CompositeName (javax.naming.CompositeName)4 File (java.io.File)3