Search in sources :

Example 16 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project karaf by apache.

the class LdapPoolingTest method testSSLConnectionWithoutPool.

@Test
public void testSSLConnectionWithoutPool() throws Exception {
    System.setProperty("com.sun.jndi.ldap.connect.pool.maxsize", "2");
    System.setProperty("com.sun.jndi.ldap.connect.pool.protocol", "ssl");
    System.setProperty("com.sun.jndi.ldap.connect.pool.debug", "all");
    Hashtable<String, String> env = new Hashtable<>();
    env.put("com.sun.jndi.ldap.connect.pool", "false");
    env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
    env.put("java.naming.provider.url", "ldaps://localhost:" + getLdapServer().getPortSSL() + "/ou=system");
    env.put("java.naming.ldap.factory.socket", ManagedSSLSocketFactory.class.getName());
    env.put("java.naming.security.protocol", "ssl");
    env.put("java.naming.security.principal", "uid=admin,ou=system");
    env.put("java.naming.security.credentials", "secret");
    env.put("java.naming.security.authentication", "simple");
    final int[] socketsCreated = new int[] { 0 };
    ManagedSSLSocketFactory.setSocketFactory(new ManagedSSLSocketFactory(sslContext.getSocketFactory()) {

        @Override
        public Socket createSocket(String host, int port) throws IOException {
            socketsCreated[0]++;
            return super.createSocket(host, port);
        }
    });
    InitialDirContext context = new InitialDirContext(env);
    context.close();
    new InitialDirContext(env);
    context.close();
    ManagedSSLSocketFactory.setSocketFactory(null);
    assertThat(socketsCreated[0], equalTo(2));
}
Also used : Hashtable(java.util.Hashtable) IOException(java.io.IOException) InitialDirContext(javax.naming.directory.InitialDirContext) Socket(java.net.Socket) Test(org.junit.Test)

Example 17 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project wildfly by wildfly.

the class LdapUrlTestServlet method runSearch.

/**
     * Try to search in LDAP with search base containing URL. Also try to retrieve RequestControls from LdapContext.
     *
     * @param hostname
     * @return
     * @throws Exception
     */
public static String runSearch(final String hostname, boolean testLdapCtx) throws Exception {
    final StringBuilder result = new StringBuilder();
    final String ldapUrl = "ldap://" + (hostname == null ? "localhost" : hostname) + ":10389";
    final Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.PROVIDER_URL, ldapUrl);
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    final SearchControls ctl = new SearchControls();
    ctl.setReturningAttributes(new String[] { "cn" });
    DirContext dirCtx = null;
    if (testLdapCtx) {
        // LdapContext must also work
        LdapContext ldapCtx = new InitialLdapContext(env, null);
        // next line tests if the LdapContext works
        ldapCtx.getRequestControls();
        dirCtx = ldapCtx;
    } else {
        dirCtx = new InitialDirContext(env);
    }
    final NamingEnumeration<SearchResult> nenum = dirCtx.search(ldapUrl + "/dc=jboss,dc=org", "(uid=jduke)", ctl);
    while (nenum.hasMore()) {
        SearchResult sr = nenum.next();
        Attributes attrs = sr.getAttributes();
        result.append("cn=").append(attrs.get("cn").get());
    }
    dirCtx.close();
    return result.toString();
}
Also used : Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 18 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project gerrit by GerritCodeReview.

the class Helper method kerberosOpen.

private DirContext kerberosOpen(final Properties env) throws LoginException, NamingException {
    LoginContext ctx = new LoginContext("KerberosLogin");
    ctx.login();
    Subject subject = ctx.getSubject();
    try {
        return Subject.doAs(subject, new PrivilegedExceptionAction<DirContext>() {

            @Override
            public DirContext run() throws NamingException {
                return new InitialDirContext(env);
            }
        });
    } catch (PrivilegedActionException e) {
        Throwables.throwIfInstanceOf(e.getException(), NamingException.class);
        Throwables.throwIfInstanceOf(e.getException(), RuntimeException.class);
        LdapRealm.log.warn("Internal error", e.getException());
        return null;
    } finally {
        ctx.logout();
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) PrivilegedActionException(java.security.PrivilegedActionException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) Subject(javax.security.auth.Subject)

Example 19 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project gerrit by GerritCodeReview.

the class Helper method open.

DirContext open() throws NamingException, LoginException {
    final Properties env = createContextProperties();
    env.put(Context.SECURITY_AUTHENTICATION, authentication);
    env.put(Context.REFERRAL, referral);
    if ("GSSAPI".equals(authentication)) {
        return kerberosOpen(env);
    }
    if (username != null) {
        env.put(Context.SECURITY_PRINCIPAL, username);
        env.put(Context.SECURITY_CREDENTIALS, password);
    }
    return new InitialDirContext(env);
}
Also used : InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties)

Example 20 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project jdk8u_jdk by JetBrains.

the class LDAPCertStore method createInitialDirContext.

/**
     * Create InitialDirContext.
     *
     * @param server Server DNS name hosting LDAP service
     * @param port   Port at which server listens for requests
     * @throws InvalidAlgorithmParameterException if creation fails
     */
private void createInitialDirContext(String server, int port) throws InvalidAlgorithmParameterException {
    String url = "ldap://" + server + ":" + port;
    Hashtable<String, Object> env = new Hashtable<>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    // If property is set to true, disable application resource file lookup.
    boolean disableAppResourceFiles = AccessController.doPrivileged(new GetBooleanAction(PROP_DISABLE_APP_RESOURCE_FILES));
    if (disableAppResourceFiles) {
        if (debug != null) {
            debug.println("LDAPCertStore disabling app resource files");
        }
        env.put("com.sun.naming.disable.app.resource.files", "true");
    }
    try {
        ctx = new InitialDirContext(env);
        /*
             * By default, follow referrals unless application has
             * overridden property in an application resource file.
             */
        Hashtable<?, ?> currentEnv = ctx.getEnvironment();
        if (currentEnv.get(Context.REFERRAL) == null) {
            ctx.addToEnvironment(Context.REFERRAL, "follow");
        }
    } catch (NamingException e) {
        if (debug != null) {
            debug.println("LDAPCertStore.engineInit about to throw " + "InvalidAlgorithmParameterException");
            e.printStackTrace();
        }
        Exception ee = new InvalidAlgorithmParameterException("unable to create InitialDirContext using supplied parameters");
        ee.initCause(e);
        throw (InvalidAlgorithmParameterException) ee;
    }
}
Also used : GetBooleanAction(sun.security.action.GetBooleanAction) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) IOException(java.io.IOException)

Aggregations

InitialDirContext (javax.naming.directory.InitialDirContext)37 NamingException (javax.naming.NamingException)18 DirContext (javax.naming.directory.DirContext)18 Hashtable (java.util.Hashtable)17 Attributes (javax.naming.directory.Attributes)9 IOException (java.io.IOException)8 Attribute (javax.naming.directory.Attribute)7 Properties (java.util.Properties)5 Socket (java.net.Socket)4 SearchResult (javax.naming.directory.SearchResult)4 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 NamingEnumeration (javax.naming.NamingEnumeration)2 SearchControls (javax.naming.directory.SearchControls)2 InitialLdapContext (javax.naming.ldap.InitialLdapContext)2 LdapContext (javax.naming.ldap.LdapContext)2 JndiUtils.getInitialDirContext (com.facebook.presto.server.security.util.jndi.JndiUtils.getInitialDirContext)1 AuthenticationFailedException (com.google.gerrit.server.account.AuthenticationFailedException)1 LdapCtx (com.sun.jndi.ldap.LdapCtx)1