@Override public Boolean call() throws Exception { return connection.connect(); } }, Callable.class,
protected LdapNetworkConnection openLdapConnection(LdapSettings ldapSettings) throws LdapException { final LdapConnectionConfig config = new LdapConnectionConfig(); config.setLdapHost(ldapSettings.getUri().getHost()); config.setLdapPort(ldapSettings.getUri().getPort()); config.setUseSsl(ldapSettings.getUri().getScheme().startsWith("ldaps")); config.setUseTls(ldapSettings.isUseStartTls()); if (ldapSettings.isTrustAllCertificates()) { config.setTrustManagers(new TrustAllX509TrustManager()); } config.setName(ldapSettings.getSystemUserName()); config.setCredentials(ldapSettings.getSystemPassword()); return ldapConnector.connect(config); }
public LdapNetworkConnection connect(LdapConnectionConfig config) throws LdapException { final LdapNetworkConnection connection = new LdapNetworkConnection(config); connection.setTimeOut(connectionTimeout); config.getLdapHost(), config.getLdapPort(), config.getName()); connection.bind();
public LdapTestConfigResponse testLdapConfiguration(@ApiParam(name = "Configuration to test", required = true) @Valid @NotNull LdapTestConfigRequest request) { final LdapConnectionConfig config = new LdapConnectionConfig(); final URI ldapUri = request.ldapUri(); config.setLdapHost(ldapUri.getHost()); config.setLdapPort(ldapUri.getPort()); config.setUseSsl(ldapUri.getScheme().startsWith("ldaps")); config.setUseTls(request.useStartTls()); config.setTrustManagers(new TrustAllX509TrustManager()); config.setName(request.systemUsername()); config.setCredentials(request.systemPassword()); } else { config.setCredentials(ldapSettings.getSystemPassword()); boolean connected = connection.isConnected(); boolean systemAuthenticated = connection.isAuthenticated(); if (connection != null) { try { connection.close(); } catch (IOException e) { LOG.warn("Unable to close LDAP connection.", e);
@Override public com.google.common.base.Optional<Boolean> isHealthy() { try { final LdapConnection connection = connectionPool.getConnection(); try { if (connection.isConnected() && connection.isAuthenticated()) { connection.bind(); try { return com.google.common.base.Optional.of(true); } finally { connection.unBind(); } } } finally { connectionPool.releaseConnection(connection); } } catch (LdapException e) { LOG.warn("LdapException caught when checking health", e); exceptionNotifier.notify(String.format("LdapException caught when checking health (%s)", e.getMessage()), e); } return com.google.common.base.Optional.of(false); }
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; }
@Override public void start(BundleContext context) throws Exception { connection = new LdapNetworkConnection("localhost", 10389); connection.connect(); }
private static ValidatingPoolableLdapConnectionFactory newPoolableConnectionFactory( LdapConnectionConfig connectionConfig, LdapApiService apiService, long timeout ) { DefaultLdapConnectionFactory connectionFactory = new DefaultLdapConnectionFactory( connectionConfig ); connectionFactory.setLdapApiService( apiService ); connectionFactory.setTimeOut( timeout ); return new ValidatingPoolableLdapConnectionFactory( connectionFactory ); }
/** * {@inheritDoc} */ @Override public LdapConnection configureConnection( LdapConnection connection ) { connection.setTimeOut( timeout ); connection.setBinaryAttributeDetector( connectionConfig.getBinaryAttributeDetector() ); return connection; }
/** * {@inheritDoc} */ @Override public void loadSchemaRelaxed() throws LdapException { loadSchema( new DefaultSchemaLoader( this, true ) ); }
public boolean authenticate(LdapNetworkConnection connection, String principal, String credentials) throws LdapException { checkArgument(!isNullOrEmpty(principal), "Binding with empty principal is forbidden."); checkArgument(!isNullOrEmpty(credentials), "Binding with empty credentials is forbidden."); final BindRequestImpl bindRequest = new BindRequestImpl(); bindRequest.setName(principal); bindRequest.setCredentials(credentials); LOG.trace("Re-binding with DN {} using password", principal); final BindResponse bind = connection.bind(bindRequest); if (!bind.getLdapResult().getResultCode().equals(ResultCodeEnum.SUCCESS)) { LOG.trace("Re-binding DN {} failed", principal); throw new RuntimeException(bind.toString()); } LOG.trace("Binding DN {} did not throw, connection authenticated: {}", principal, connection.isAuthenticated()); return connection.isAuthenticated(); } }
final Set<String> groups = Sets.newHashSet(); try (final EntryCursor groupSearch = connection.search( groupSearchBase, groupSearchPattern,
final LdapConnectionConfig config = new LdapConnectionConfig(); final URI ldapUri = ldapSettings.getUri(); config.setLdapHost(ldapUri.getHost()); config.setLdapPort(ldapUri.getPort()); config.setUseSsl(ldapUri.getScheme().startsWith("ldaps")); config.setUseTls(ldapSettings.isUseStartTls()); config.setTrustManagers(new TrustAllX509TrustManager()); config.setName(ldapSettings.getSystemUserName()); config.setCredentials(ldapSettings.getSystemPassword());
final LdapConnection connection = connectionPool.getConnection(); checkState(connection.isConnected(), "not connected"); checkState(connection.isAuthenticated(), "not authenticated"); connection.bind(); final EntryCursor userCursor = connection.search(configuration.getUserBaseDN(), String.format(configuration.getUserFilter(), user), SearchScope.ONELEVEL, configuration.getUserNameAttribute(), configuration.getUserEmailAttribute()); final EntryCursor cursor = connection.search(configuration.getGroupBaseDN(), String.format(configuration.getGroupFilter(), user), configuration.getGroupSearchScope(), configuration.getGroupNameAttribute()); } finally { LOG.trace("Loaded {}'s user data in {}", user, JavaUtils.duration(startTime)); connection.unBind(); connectionPool.releaseConnection(connection);
private static ValidatingPoolableLdapConnectionFactory newPoolableConnectionFactory( LdapConnectionConfig connectionConfig, LdapApiService apiService, long timeout ) { DefaultLdapConnectionFactory connectionFactory = new DefaultLdapConnectionFactory( connectionConfig ); connectionFactory.setLdapApiService( apiService ); connectionFactory.setTimeOut( timeout ); return new ValidatingPoolableLdapConnectionFactory( connectionFactory ); }
/** * {@inheritDoc} */ @Override public LdapConnection configureConnection( LdapConnection connection ) { connection.setTimeOut( timeout ); connection.setBinaryAttributeDetector( connectionConfig.getBinaryAttributeDetector() ); return connection; }
/** * {@inheritDoc} */ @Override public void loadSchemaRelaxed() throws LdapException { loadSchema( new DefaultSchemaLoader( this, true ) ); }
/** * {@inheritDoc} */ @Override public void loadSchema() throws LdapException { loadSchema( new DefaultSchemaLoader( this ) ); }
/** * {@inheritDoc} */ @Override public void loadSchema() throws LdapException { loadSchema( new DefaultSchemaLoader( this ) ); }