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);
}
}
}
}
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;
}
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();
}
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());
}
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());
}
Aggregations