Search in sources :

Example 41 with NameClassPair

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

the class LegacyLDAPSecuritySettingPluginTest 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());
    }
    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 : Hashtable(java.util.Hashtable) NameClassPair(javax.naming.NameClassPair) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 42 with NameClassPair

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

the class LDAPModuleRoleExpansionTest 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 43 with NameClassPair

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

the class AbstractCachedLDAPAuthorizationMapLegacyTest method cleanAndLoad.

public static void cleanAndLoad(String deleteFromDn, String ldifResourcePath, String ldapHost, int ldapPort, String ldapUser, String ldapPass, DirContext context) throws Exception {
    // Cleanup everything used for testing.
    List<String> dns = new LinkedList<>();
    dns.add(deleteFromDn);
    while (!dns.isEmpty()) {
        String name = dns.get(dns.size() - 1);
        Context currentContext = (Context) context.lookup(name);
        NamingEnumeration<NameClassPair> namingEnum = currentContext.list("");
        if (namingEnum.hasMore()) {
            while (namingEnum.hasMore()) {
                dns.add(namingEnum.next().getNameInNamespace());
            }
        } else {
            context.unbind(name);
            dns.remove(dns.size() - 1);
        }
    }
    // A bit of a hacked approach to loading an LDIF into OpenLDAP since there isn't an easy way to do it
    // otherwise.  This approach invokes the command line tool programmatically but has
    // to short-circuit the call to System.exit that the command line tool makes when it finishes.
    // We are assuming that there isn't already a security manager in place.
    final SecurityManager securityManager = new SecurityManager() {

        @Override
        public void checkPermission(java.security.Permission permission) {
            if (permission.getName().contains("exitVM")) {
                throw new SecurityException("System.exit calls disabled for the moment.");
            }
        }
    };
    System.setSecurityManager(securityManager);
    File file = new File(AbstractCachedLDAPAuthorizationMapLegacyTest.class.getClassLoader().getResource(ldifResourcePath).toURI());
    Class<?> clazz = Class.forName("LDAPModify");
    Method mainMethod = clazz.getMethod("main", String[].class);
    try {
        mainMethod.invoke(null, new Object[] { new String[] { "-v", "-h", ldapHost, "-p", String.valueOf(ldapPort), "-D", ldapUser, "-w", ldapPass, "-a", "-f", file.toString() } });
    } catch (InvocationTargetException e) {
        if (!(e.getTargetException() instanceof SecurityException)) {
            throw e;
        }
    }
    System.setSecurityManager(null);
}
Also used : Context(javax.naming.Context) DirContext(javax.naming.directory.DirContext) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) InvocationTargetException(java.lang.reflect.InvocationTargetException) NameClassPair(javax.naming.NameClassPair) File(java.io.File)

Example 44 with NameClassPair

use of javax.naming.NameClassPair in project geronimo-xbean by apache.

the class ContextUtil method listToMap.

public static Map<String, String> listToMap(NamingEnumeration enumeration) {
    Map<String, String> result = new HashMap<String, String>();
    while (enumeration.hasMoreElements()) {
        NameClassPair nameClassPair = (NameClassPair) enumeration.nextElement();
        String name = nameClassPair.getName();
        result.put(name, nameClassPair.getClassName());
    }
    return result;
}
Also used : HashMap(java.util.HashMap) NameClassPair(javax.naming.NameClassPair)

Example 45 with NameClassPair

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

the class ServiceRegistryContextTest method checkServiceListList.

@Test
public void checkServiceListList() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    String className = Runnable.class.getName();
    Runnable t = Skeleton.newMock(Runnable.class);
    // we don't want the default service
    reg.unregister();
    ServiceRegistration reg = bc.registerService(className, t, null);
    ServiceRegistration reg2 = bc.registerService(className, new Thread(), null);
    NamingEnumeration<NameClassPair> ne = ctx.list("osgi:servicelist/" + className);
    assertTrue(ne.hasMoreElements());
    NameClassPair ncp = ne.nextElement();
    assertEquals(String.valueOf(reg.getReference().getProperty(Constants.SERVICE_ID)), ncp.getName());
    assertTrue("Class name not correct. Was: " + ncp.getClassName(), ncp.getClassName().contains("Proxy"));
    assertTrue(ne.hasMoreElements());
    ncp = ne.nextElement();
    assertEquals(String.valueOf(reg2.getReference().getProperty(Constants.SERVICE_ID)), ncp.getName());
    assertEquals("Class name not correct.", Thread.class.getName(), ncp.getClassName());
    assertFalse(ne.hasMoreElements());
}
Also used : NameClassPair(javax.naming.NameClassPair) BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

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