Search in sources :

Example 1 with LdapConnectionPool

use of org.apache.directory.ldap.client.api.LdapConnectionPool in project Singularity by HubSpot.

the class SingularityLDAPDatastore method createConnectionPool.

private static LdapConnectionPool createConnectionPool(LDAPConfiguration configuration) throws IOException {
    final LdapConnectionConfig config = new LdapConnectionConfig();
    config.setLdapHost(configuration.getHostname());
    config.setLdapPort(configuration.getPort());
    config.setName(configuration.getBindDn());
    config.setCredentials(configuration.getBindPassword());
    final DefaultPoolableLdapConnectionFactory factory = new DefaultPoolableLdapConnectionFactory(config);
    final LdapConnectionPool pool = new LdapConnectionPool(factory);
    pool.setTestOnBorrow(configuration.isPoolTestOnBorrow());
    pool.setTestOnReturn(configuration.isPoolTestOnReturn());
    pool.setTestWhileIdle(configuration.isPoolTestWhileIdle());
    pool.setMaxActive(configuration.getPoolMaxActive());
    pool.setMaxIdle(configuration.getPoolMaxIdle());
    pool.setMinIdle(configuration.getPoolMinIdle());
    pool.setMaxWait(configuration.getPoolMaxWait());
    switch(configuration.getPoolWhenExhaustedAction()) {
        case BLOCK:
            pool.setWhenExhaustedAction(LdapConnectionPool.WHEN_EXHAUSTED_BLOCK);
            break;
        case FAIL:
            pool.setWhenExhaustedAction(LdapConnectionPool.WHEN_EXHAUSTED_FAIL);
            break;
        case GROW:
            pool.setWhenExhaustedAction(LdapConnectionPool.WHEN_EXHAUSTED_GROW);
            break;
        default:
            pool.setWhenExhaustedAction(LdapConnectionPool.DEFAULT_WHEN_EXHAUSTED_ACTION);
    }
    return pool;
}
Also used : DefaultPoolableLdapConnectionFactory(org.apache.directory.ldap.client.api.DefaultPoolableLdapConnectionFactory) LdapConnectionConfig(org.apache.directory.ldap.client.api.LdapConnectionConfig) LdapConnectionPool(org.apache.directory.ldap.client.api.LdapConnectionPool)

Example 2 with LdapConnectionPool

use of org.apache.directory.ldap.client.api.LdapConnectionPool in project jackrabbit-oak by apache.

the class LdapIdentityProvider method init.

// ------------------------------------------------------------< private >---
/**
 * Initializes the ldap identity provider.
 */
private void init() {
    if (adminConnectionFactory != null) {
        throw new IllegalStateException("Provider already initialized.");
    }
    // make sure the JVM supports the TLSv1.1
    try {
        enabledSSLProtocols = null;
        SSLContext.getInstance("TLSv1.1");
    } catch (NoSuchAlgorithmException e) {
        log.warn("JDK does not support TLSv1.1. Disabling it.");
        enabledSSLProtocols = new String[] { "TLSv1" };
    }
    // setup admin connection pool
    LdapConnectionConfig cc = createConnectionConfig();
    String bindDN = config.getBindDN();
    if (bindDN != null && !bindDN.isEmpty()) {
        cc.setName(bindDN);
        cc.setCredentials(config.getBindPassword());
    }
    adminConnectionFactory = new ValidatingPoolableLdapConnectionFactory(cc);
    if (config.getAdminPoolConfig().lookupOnValidate()) {
        adminConnectionFactory.setValidator(new LookupLdapConnectionValidator());
    } else {
        adminConnectionFactory.setValidator(new DefaultLdapConnectionValidator());
    }
    if (config.getAdminPoolConfig().getMaxActive() != 0) {
        adminPool = new LdapConnectionPool(adminConnectionFactory);
        adminPool.setTestOnBorrow(true);
        adminPool.setMaxActive(config.getAdminPoolConfig().getMaxActive());
        adminPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    }
    // setup unbound connection pool. let's create a new version of the config
    cc = createConnectionConfig();
    userConnectionFactory = new PoolableUnboundConnectionFactory(cc);
    if (config.getUserPoolConfig().lookupOnValidate()) {
        userConnectionFactory.setValidator(new UnboundLookupConnectionValidator());
    } else {
        userConnectionFactory.setValidator(new UnboundConnectionValidator());
    }
    if (config.getUserPoolConfig().getMaxActive() != 0) {
        userPool = new UnboundLdapConnectionPool(userConnectionFactory);
        userPool.setTestOnBorrow(true);
        userPool.setMaxActive(config.getUserPoolConfig().getMaxActive());
        userPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    }
    log.info("LdapIdentityProvider initialized: {}", config);
}
Also used : DefaultLdapConnectionValidator(org.apache.directory.ldap.client.api.DefaultLdapConnectionValidator) ValidatingPoolableLdapConnectionFactory(org.apache.directory.ldap.client.api.ValidatingPoolableLdapConnectionFactory) LdapConnectionConfig(org.apache.directory.ldap.client.api.LdapConnectionConfig) LookupLdapConnectionValidator(org.apache.directory.ldap.client.api.LookupLdapConnectionValidator) LdapConnectionPool(org.apache.directory.ldap.client.api.LdapConnectionPool) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with LdapConnectionPool

use of org.apache.directory.ldap.client.api.LdapConnectionPool in project directory-fortress-core by apache.

the class LdapConnectionProvider method init.

/**
 * Initialize the three connection pools using settings and coordinates contained in the config.
 */
private void init() {
    IS_SSL = (Config.getInstance().getProperty(GlobalIds.ENABLE_LDAP_SSL) != null && Config.getInstance().getProperty(GlobalIds.ENABLE_LDAP_SSL).equalsIgnoreCase("true") && Config.getInstance().getProperty(GlobalIds.TRUST_STORE) != null && Config.getInstance().getProperty(GlobalIds.TRUST_STORE_PW, true) != null);
    String host = Config.getInstance().getProperty(GlobalIds.LDAP_HOST, "localhost");
    int port = Config.getInstance().getInt(GlobalIds.LDAP_PORT, 389);
    int min = Config.getInstance().getInt(GlobalIds.LDAP_ADMIN_POOL_MIN, 1);
    int max = Config.getInstance().getInt(GlobalIds.LDAP_ADMIN_POOL_MAX, 10);
    int logmin = Config.getInstance().getInt(LDAP_LOG_POOL_MIN, 1);
    int logmax = Config.getInstance().getInt(LDAP_LOG_POOL_MAX, 10);
    LOG.info("LDAP POOL:  host=[{}], port=[{}], min=[{}], max=[{}]", host, port, min, max);
    LdapConnectionConfig config = new LdapConnectionConfig();
    config.setLdapHost(host);
    config.setLdapPort(port);
    config.setName(Config.getInstance().getProperty(GlobalIds.LDAP_ADMIN_POOL_UID, ""));
    config.setUseSsl(IS_SSL);
    if (Config.getInstance().getBoolean(ENABLE_LDAP_STARTTLS, false)) {
        config.setUseTls(true);
    }
    if (IS_SSL && StringUtils.isNotEmpty(Config.getInstance().getProperty(GlobalIds.TRUST_STORE)) && StringUtils.isNotEmpty(Config.getInstance().getProperty(GlobalIds.TRUST_STORE_PW))) {
        // validate certificates but allow self-signed certs if within this truststore:
        config.setTrustManagers(new LdapClientTrustStoreManager(Config.getInstance().getProperty(GlobalIds.TRUST_STORE), Config.getInstance().getProperty(GlobalIds.TRUST_STORE_PW).toCharArray(), null, true));
    }
    String adminPw;
    if (EncryptUtil.isEnabled()) {
        adminPw = EncryptUtil.getInstance().decrypt(Config.getInstance().getProperty(GlobalIds.LDAP_ADMIN_POOL_PW, true));
    } else {
        adminPw = Config.getInstance().getProperty(GlobalIds.LDAP_ADMIN_POOL_PW, true);
    }
    config.setCredentials(adminPw);
    try {
        List<String> listExOps = new ArrayList<>();
        listExOps.add("org.openldap.accelerator.impl.createSession.RbacCreateSessionFactory");
        listExOps.add("org.openldap.accelerator.impl.checkAccess.RbacCheckAccessFactory");
        listExOps.add("org.openldap.accelerator.impl.addRole.RbacAddRoleFactory");
        listExOps.add("org.openldap.accelerator.impl.dropRole.RbacDropRoleFactory");
        listExOps.add("org.openldap.accelerator.impl.deleteSession.RbacDeleteSessionFactory");
        listExOps.add("org.openldap.accelerator.impl.sessionRoles.RbacSessionRolesFactory");
        LdapApiService ldapApiService = new StandaloneLdapApiService(new ArrayList<String>(), listExOps);
        if (!LdapApiServiceFactory.isInitialized()) {
            LdapApiServiceFactory.initialize(ldapApiService);
        }
        config.setLdapApiService(ldapApiService);
    } catch (Exception ex) {
        String error = "Exception caught initializing Admin Pool: " + ex;
        throw new CfgRuntimeException(GlobalErrIds.FT_APACHE_LDAP_POOL_INIT_FAILED, error, ex);
    }
    PoolableObjectFactory<LdapConnection> poolFactory = new ValidatingPoolableLdapConnectionFactory(config);
    // Create the Admin pool
    adminPool = new LdapConnectionPool(poolFactory);
    adminPool.setTestOnBorrow(true);
    adminPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    adminPool.setMaxActive(max);
    adminPool.setMinIdle(min);
    adminPool.setMaxIdle(-1);
    // adminPool.setMaxWait( 0 );
    // Create the User pool
    userPool = new LdapConnectionPool(poolFactory);
    userPool.setTestOnBorrow(true);
    userPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    userPool.setMaxActive(max);
    userPool.setMinIdle(min);
    userPool.setMaxIdle(-1);
    // To enable, set {@code log.admin.user} && {@code log.admin.pw} inside fortress.properties file:
    if (StringUtils.isNotEmpty(LDAP_LOG_POOL_UID) && StringUtils.isNotEmpty(LDAP_LOG_POOL_PW)) {
        // Initializing the log pool in static block requires static props set within fortress.properties.
        // To make this dynamic requires moving this code outside of static block AND storing the connection
        // metadata inside fortress config node (in ldap).
        LdapConnectionConfig logConfig = new LdapConnectionConfig();
        logConfig.setLdapHost(host);
        logConfig.setLdapPort(port);
        logConfig.setName(Config.getInstance().getProperty(GlobalIds.LDAP_ADMIN_POOL_UID, ""));
        logConfig.setUseSsl(IS_SSL);
        if (IS_SSL && StringUtils.isNotEmpty(Config.getInstance().getProperty(GlobalIds.TRUST_STORE)) && StringUtils.isNotEmpty(Config.getInstance().getProperty(GlobalIds.TRUST_STORE_PW, true))) {
            // validate certificates but allow self-signed certs if within this truststore:
            logConfig.setTrustManagers(new LdapClientTrustStoreManager(Config.getInstance().getProperty(GlobalIds.TRUST_STORE), Config.getInstance().getProperty(GlobalIds.TRUST_STORE_PW, true).toCharArray(), null, true));
        }
        logConfig.setName(Config.getInstance().getProperty(LDAP_LOG_POOL_UID, ""));
        String logPw;
        if (EncryptUtil.isEnabled()) {
            logPw = EncryptUtil.getInstance().decrypt(Config.getInstance().getProperty(LDAP_LOG_POOL_PW, true));
        } else {
            logPw = Config.getInstance().getProperty(LDAP_LOG_POOL_PW, true);
        }
        logConfig.setCredentials(logPw);
        poolFactory = new ValidatingPoolableLdapConnectionFactory(logConfig);
        logPool = new LdapConnectionPool(poolFactory);
        logPool.setTestOnBorrow(true);
        logPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
        logPool.setMaxActive(logmax);
        logPool.setMinIdle(logmin);
    }
}
Also used : ValidatingPoolableLdapConnectionFactory(org.apache.directory.ldap.client.api.ValidatingPoolableLdapConnectionFactory) LdapConnectionConfig(org.apache.directory.ldap.client.api.LdapConnectionConfig) ArrayList(java.util.ArrayList) LdapConnectionPool(org.apache.directory.ldap.client.api.LdapConnectionPool) StandaloneLdapApiService(org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService) CfgRuntimeException(org.apache.directory.fortress.core.CfgRuntimeException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CfgRuntimeException(org.apache.directory.fortress.core.CfgRuntimeException) LdapApiService(org.apache.directory.api.ldap.codec.api.LdapApiService) StandaloneLdapApiService(org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 4 with LdapConnectionPool

use of org.apache.directory.ldap.client.api.LdapConnectionPool in project directory-ldap-api by apache.

the class ApiLdapClientApiOsgiTest method useBundleClasses.

@Override
protected void useBundleClasses() throws Exception {
    new LdapNetworkConnection().close();
    new SaslGssApiRequest();
    new Krb5LoginConfiguration();
    new AddFuture(new LdapNetworkConnection(), 2);
    new LdapConnectionTemplate(new LdapConnectionPool(new DefaultPoolableLdapConnectionFactory(new LdapConnectionConfig())));
    FilterBuilder.and(FilterBuilder.not(FilterBuilder.contains("cn", "a", "b"))).toString();
    // Test for DIRAPI-239
    PoolableObjectFactory<LdapConnection> factory = new DefaultPoolableLdapConnectionFactory(new LdapConnectionConfig());
    Config config = new Config();
    LdapConnectionPool ldapConnectionPool = new LdapConnectionPool(factory, config);
    ldapConnectionPool.getLdapApiService();
    ldapConnectionPool.getTestOnBorrow();
}
Also used : SaslGssApiRequest(org.apache.directory.ldap.client.api.SaslGssApiRequest) Config(org.apache.commons.pool.impl.GenericObjectPool.Config) LdapConnectionConfig(org.apache.directory.ldap.client.api.LdapConnectionConfig) DefaultPoolableLdapConnectionFactory(org.apache.directory.ldap.client.api.DefaultPoolableLdapConnectionFactory) LdapConnectionTemplate(org.apache.directory.ldap.client.template.LdapConnectionTemplate) LdapConnectionConfig(org.apache.directory.ldap.client.api.LdapConnectionConfig) LdapConnectionPool(org.apache.directory.ldap.client.api.LdapConnectionPool) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) AddFuture(org.apache.directory.ldap.client.api.future.AddFuture) Krb5LoginConfiguration(org.apache.directory.ldap.client.api.Krb5LoginConfiguration) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Aggregations

LdapConnectionConfig (org.apache.directory.ldap.client.api.LdapConnectionConfig)4 LdapConnectionPool (org.apache.directory.ldap.client.api.LdapConnectionPool)4 DefaultPoolableLdapConnectionFactory (org.apache.directory.ldap.client.api.DefaultPoolableLdapConnectionFactory)2 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)2 ValidatingPoolableLdapConnectionFactory (org.apache.directory.ldap.client.api.ValidatingPoolableLdapConnectionFactory)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Config (org.apache.commons.pool.impl.GenericObjectPool.Config)1 LdapApiService (org.apache.directory.api.ldap.codec.api.LdapApiService)1 StandaloneLdapApiService (org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService)1 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)1 CfgRuntimeException (org.apache.directory.fortress.core.CfgRuntimeException)1 DefaultLdapConnectionValidator (org.apache.directory.ldap.client.api.DefaultLdapConnectionValidator)1 Krb5LoginConfiguration (org.apache.directory.ldap.client.api.Krb5LoginConfiguration)1 LdapNetworkConnection (org.apache.directory.ldap.client.api.LdapNetworkConnection)1 LookupLdapConnectionValidator (org.apache.directory.ldap.client.api.LookupLdapConnectionValidator)1 SaslGssApiRequest (org.apache.directory.ldap.client.api.SaslGssApiRequest)1 AddFuture (org.apache.directory.ldap.client.api.future.AddFuture)1 LdapConnectionTemplate (org.apache.directory.ldap.client.template.LdapConnectionTemplate)1