Search in sources :

Example 6 with LdapNetworkConnection

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

the class AbstractLdapTest method ldapConnect.

protected LdapNetworkConnection ldapConnect(UserLdapConnectionConfig config) throws LdapException, IOException {
    if (config == null) {
        config = new UserLdapConnectionConfig();
        config.setLdapHost(getLdapServerHost());
        config.setLdapPort(getLdapServerPort());
        config.setBindDn(getLdapBindDn());
        config.setBindPassword(getLdapBindPassword());
    }
    LOGGER.trace("LDAP connect to {}:{} as {}", config.getLdapHost(), config.getLdapPort(), config.getBindDn());
    if (useSsl()) {
        config.setUseSsl(true);
        TrustManager trustManager = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        };
        config.setTrustManagers(trustManager);
    }
    config.setBinaryAttributeDetector(binaryAttributeDetector);
    LdapNetworkConnection connection = new LdapNetworkConnection(config);
    boolean connected = connection.connect();
    if (!connected) {
        AssertJUnit.fail("Cannot connect to LDAP server " + config.getLdapHost() + ":" + config.getLdapPort());
    }
    LOGGER.trace("LDAP connected to {}:{}, executing bind as {}", config.getLdapHost(), config.getLdapPort(), config.getBindDn());
    BindRequest bindRequest = new BindRequestImpl();
    bindRequest.setDn(new Dn(config.getBindDn()));
    bindRequest.setCredentials(config.getBindPassword());
    bindRequest.setSimple(true);
    BindResponse bindResponse = connection.bind(bindRequest);
    if (bindResponse.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS) {
        ldapDisconnect(connection);
        throw new SecurityException("Bind as " + config.getBindDn() + " failed: " + bindResponse.getLdapResult().getDiagnosticMessage() + " (" + bindResponse.getLdapResult().getResultCode() + ")");
    }
    LOGGER.trace("LDAP connected to {}:{}, bound as {}", config.getLdapHost(), config.getLdapPort(), config.getBindDn());
    return connection;
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) Dn(org.apache.directory.api.ldap.model.name.Dn) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) X509Certificate(java.security.cert.X509Certificate) BindRequestImpl(org.apache.directory.api.ldap.model.message.BindRequestImpl) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManager(javax.net.ssl.TrustManager)

Example 7 with LdapNetworkConnection

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

the class AbstractLdapTest method getLdapEntry.

protected Entry getLdapEntry(String dn) throws LdapException, IOException, CursorException {
    LdapNetworkConnection connection = ldapConnect();
    Entry entry = getLdapEntry(connection, dn);
    ldapDisconnect(connection);
    return entry;
}
Also used : Entry(org.apache.directory.api.ldap.model.entry.Entry) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection)

Example 8 with LdapNetworkConnection

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

the class AbstractLdapTest method searchLdapAccount.

protected Entry searchLdapAccount(UserLdapConnectionConfig config, String filter) throws LdapException, IOException, CursorException {
    LdapNetworkConnection connection = ldapConnect(config);
    List<Entry> entries = ldapSearch(config, connection, filter);
    ldapDisconnect(connection);
    assertEquals("Unexpected number of entries for " + filter + ": " + entries, 1, entries.size());
    Entry entry = entries.get(0);
    return entry;
}
Also used : Entry(org.apache.directory.api.ldap.model.entry.Entry) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection)

Example 9 with LdapNetworkConnection

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

the class AbstractLdapTest method getLdapGroupByName.

protected Entry getLdapGroupByName(String name) throws LdapException, IOException, CursorException {
    LdapNetworkConnection connection = ldapConnect();
    List<Entry> entries = ldapSearch(connection, "(&(cn=" + name + ")(objectClass=" + getLdapGroupObjectClass() + "))");
    ldapDisconnect(connection);
    assertEquals("Unexpected number of entries for group cn=" + name + ": " + entries, 1, entries.size());
    Entry entry = entries.get(0);
    return entry;
}
Also used : Entry(org.apache.directory.api.ldap.model.entry.Entry) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection)

Example 10 with LdapNetworkConnection

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

the class AbstractLdapSynchronizationTest method test822ModifyAccountHt.

@Test
public void test822ModifyAccountHt() throws Exception {
    final String TEST_NAME = "test822ModifyAccountHt";
    TestUtil.displayTestTile(this, TEST_NAME);
    // GIVEN
    Task task = taskManager.createTaskInstance(this.getClass().getName() + "." + TEST_NAME);
    OperationResult result = task.getResult();
    long tsStart = System.currentTimeMillis();
    // WHEN
    TestUtil.displayWhen(TEST_NAME);
    LdapNetworkConnection connection = ldapConnect();
    Modification modCn = new DefaultModification(ModificationOperation.REPLACE_ATTRIBUTE, "sn", ACCOUNT_HT_SN_MODIFIED);
    connection.modify(toAccountDn(ACCOUNT_HT_UID, ACCOUNT_HT_CN), modCn);
    ldapDisconnect(connection);
    waitForTaskNextRunAssertSuccess(getSyncTaskOid(), true);
    // THEN
    TestUtil.displayThen(TEST_NAME);
    result.computeStatus();
    TestUtil.assertSuccess(result);
    long tsEnd = System.currentTimeMillis();
    PrismObject<UserType> user = findUserByUsername(ACCOUNT_HT_UID);
    assertNotNull("No user " + ACCOUNT_HT_UID + " created", user);
    assertUser(user, user.getOid(), ACCOUNT_HT_UID, ACCOUNT_HT_CN, ACCOUNT_HT_GIVENNAME, ACCOUNT_HT_SN_MODIFIED);
    assertStepSyncToken(getSyncTaskOid(), 7, tsStart, tsEnd);
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) Task(com.evolveum.midpoint.task.api.Task) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) Test(org.testng.annotations.Test) AbstractModelIntegrationTest(com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)

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