Search in sources :

Example 1 with StandaloneLdapApiService

use of org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService in project wildfly-camel by wildfly-extras.

the class LdapIntegrationTest method getWiredContext.

private LdapContext getWiredContext(int port) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://" + InetAddress.getLocalHost().getHostName() + ":" + port);
    env.put(Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN);
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    LdapApiService ldapApiService = new StandaloneLdapApiService();
    return new InitialLdapContext(env, JndiUtils.toJndiControls(ldapApiService));
}
Also used : LdapApiService(org.apache.directory.api.ldap.codec.api.LdapApiService) StandaloneLdapApiService(org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService) Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) StandaloneLdapApiService(org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService)

Example 2 with StandaloneLdapApiService

use of org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService 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)

Aggregations

LdapApiService (org.apache.directory.api.ldap.codec.api.LdapApiService)2 StandaloneLdapApiService (org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService)2 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 InitialLdapContext (javax.naming.ldap.InitialLdapContext)1 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)1 CfgRuntimeException (org.apache.directory.fortress.core.CfgRuntimeException)1 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)1 LdapConnectionConfig (org.apache.directory.ldap.client.api.LdapConnectionConfig)1 LdapConnectionPool (org.apache.directory.ldap.client.api.LdapConnectionPool)1 ValidatingPoolableLdapConnectionFactory (org.apache.directory.ldap.client.api.ValidatingPoolableLdapConnectionFactory)1