Search in sources :

Example 1 with LdapNetworkConnection

use of org.apache.directory.ldap.client.api.LdapNetworkConnection in project graylog2-server by Graylog2.

the class LdapResource method testLdapConfiguration.

@POST
@Timed
@RequiresPermissions(RestPermissions.LDAP_EDIT)
@ApiOperation("Test LDAP Configuration")
@Path("/test")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@NoAuditEvent("only used to test LDAP configuration")
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());
    if (request.trustAllCertificates()) {
        config.setTrustManagers(new TrustAllX509TrustManager());
    }
    if (!isNullOrEmpty(request.systemUsername()) && !isNullOrEmpty(request.systemPassword())) {
        config.setName(request.systemUsername());
        config.setCredentials(request.systemPassword());
    }
    LdapNetworkConnection connection = null;
    try {
        try {
            connection = ldapConnector.connect(config);
        } catch (LdapException e) {
            return LdapTestConfigResponse.create(false, false, false, Collections.<String, String>emptyMap(), Collections.<String>emptySet(), e.getMessage());
        }
        if (null == connection) {
            return LdapTestConfigResponse.create(false, false, false, Collections.<String, String>emptyMap(), Collections.<String>emptySet(), "Could not connect to LDAP server");
        }
        boolean connected = connection.isConnected();
        boolean systemAuthenticated = connection.isAuthenticated();
        // the web interface allows testing the connection only, in that case we can bail out early.
        if (request.testConnectOnly()) {
            return LdapTestConfigResponse.create(connected, systemAuthenticated, false, Collections.<String, String>emptyMap(), Collections.<String>emptySet());
        }
        String userPrincipalName = null;
        boolean loginAuthenticated = false;
        Map<String, String> entryMap = Collections.emptyMap();
        String exception = null;
        Set<String> groups = Collections.emptySet();
        try {
            final LdapEntry entry = ldapConnector.search(connection, request.searchBase(), request.searchPattern(), "*", request.principal(), request.activeDirectory(), request.groupSearchBase(), request.groupIdAttribute(), request.groupSearchPattern());
            if (entry != null) {
                userPrincipalName = entry.getBindPrincipal();
                entryMap = entry.getAttributes();
                groups = entry.getGroups();
            }
        } catch (CursorException | LdapException e) {
            exception = e.getMessage();
        }
        try {
            loginAuthenticated = ldapConnector.authenticate(connection, userPrincipalName, request.password());
        } catch (Exception e) {
            exception = e.getMessage();
        }
        return LdapTestConfigResponse.create(connected, systemAuthenticated, loginAuthenticated, entryMap, groups, exception);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                LOG.warn("Unable to close LDAP connection.", e);
            }
        }
    }
}
Also used : LdapConnectionConfig(org.apache.directory.ldap.client.api.LdapConnectionConfig) LdapEntry(org.graylog2.shared.security.ldap.LdapEntry) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) IOException(java.io.IOException) TrustAllX509TrustManager(org.graylog2.security.TrustAllX509TrustManager) URI(java.net.URI) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) IOException(java.io.IOException) ValidationException(org.graylog2.plugin.database.ValidationException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 2 with LdapNetworkConnection

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

the class PoolableUnboundConnectionFactory method makeObject.

/**
     * {@inheritDoc}
     */
public LdapConnection makeObject() throws LdapException {
    LdapNetworkConnection connection = config.isUseTls() ? new TlsGuardingConnection(config) : new LdapNetworkConnection(config);
    connection.connect();
    log.debug("creating new connection: {}", connection);
    return connection;
}
Also used : LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection)

Example 3 with LdapNetworkConnection

use of org.apache.directory.ldap.client.api.LdapNetworkConnection in project karaf by apache.

the class LdapSpecialCharsInPasswordTest method changeAdminPassword.

@Before
public void changeAdminPassword() throws Exception {
    LdapConnection connection = new LdapNetworkConnection("localhost", getLdapServer().getPort());
    connection.bind("uid=admin,ou=system", "secret");
    Dn adminDn = new Dn("uid=admin,ou=system");
    ModifyRequest modReq = new ModifyRequestImpl();
    modReq.setName(adminDn);
    modReq.replace(SchemaConstants.USER_PASSWORD_AT, NEW_CONNECTION_PASSWORD);
    connection.modify(modReq);
    connection.close();
    // check that we actually changed the admin connection password
    connection = new LdapNetworkConnection("localhost", getLdapServer().getPort());
    connection.bind("uid=admin,ou=system", NEW_CONNECTION_PASSWORD);
    connection.close();
}
Also used : ModifyRequestImpl(org.apache.directory.api.ldap.model.message.ModifyRequestImpl) Dn(org.apache.directory.api.ldap.model.name.Dn) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection) Before(org.junit.Before)

Example 4 with LdapNetworkConnection

use of org.apache.directory.ldap.client.api.LdapNetworkConnection in project midpoint by Evolveum.

the class AbstractAdLdapTest method assertNoLdapAccount.

protected void assertNoLdapAccount(String uid, String cn) throws LdapException, IOException, CursorException {
    LdapNetworkConnection connection = ldapConnect();
    List<Entry> entriesCn = ldapSearch(connection, "(cn=" + cn + ")");
    List<Entry> entriesSamAccountName = ldapSearch(connection, "(sAMAccountName=" + uid + ")");
    ldapDisconnect(connection);
    assertEquals("Unexpected number of entries for cn=" + cn + ": " + entriesCn, 0, entriesCn.size());
    assertEquals("Unexpected number of entries for sAMAccountName=" + uid + ": " + entriesSamAccountName, 0, entriesSamAccountName.size());
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection)

Example 5 with LdapNetworkConnection

use of org.apache.directory.ldap.client.api.LdapNetworkConnection in project midpoint by Evolveum.

the class AbstractAdLdapMultidomainTest method assertNoLdapAccount.

protected void assertNoLdapAccount(UserLdapConnectionConfig config, String uid, String cn) throws LdapException, IOException, CursorException {
    LdapNetworkConnection connection = ldapConnect(config);
    List<Entry> entriesCn = ldapSearch(config, connection, "(cn=" + cn + ")");
    List<Entry> entriesSamAccountName = ldapSearch(config, connection, "(sAMAccountName=" + uid + ")");
    ldapDisconnect(connection);
    assertEquals("Unexpected number of entries for cn=" + cn + ": " + entriesCn, 0, entriesCn.size());
    assertEquals("Unexpected number of entries for sAMAccountName=" + uid + ": " + entriesSamAccountName, 0, entriesSamAccountName.size());
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection)

Aggregations

LdapNetworkConnection (org.apache.directory.ldap.client.api.LdapNetworkConnection)24 Entry (org.apache.directory.api.ldap.model.entry.Entry)11 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)10 SearchResultEntry (org.apache.directory.api.ldap.model.message.SearchResultEntry)8 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)5 Test (org.testng.annotations.Test)5 AbstractModelIntegrationTest (com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 Task (com.evolveum.midpoint.task.api.Task)4 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)4 IOException (java.io.IOException)4 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)4 Dn (org.apache.directory.api.ldap.model.name.Dn)3 LdapConnectionConfig (org.apache.directory.ldap.client.api.LdapConnectionConfig)3 ApiOperation (io.swagger.annotations.ApiOperation)2 URI (java.net.URI)2 BadRequestException (javax.ws.rs.BadRequestException)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2