use of com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest in project ldapsdk by pingidentity.
the class InMemoryDirectoryServerTestCase method testServerWithStartTLS.
/**
* Tests the ability to create an in-memory directory server instance that
* supports the StartTLS extended operation.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testServerWithStartTLS() throws Exception {
// Create the SSL socket factory to use for StartTLS.
final File resourceDir = new File(System.getProperty("unit.resource.dir"));
final File serverKeyStore = new File(resourceDir, "server.keystore");
final SSLUtil serverSSLUtil = new SSLUtil(new KeyStoreKeyManager(serverKeyStore, "password".toCharArray(), "JKS", "server-cert"), new TrustAllTrustManager());
// Create the in-memory directory server instance.
final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
cfg.addAdditionalBindCredentials("cn=Directory Manager", "password");
cfg.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP+StartTLS", null, 0, serverSSLUtil.createSSLSocketFactory()));
cfg.setCodeLogDetails(createTempFile().getAbsolutePath(), true);
final InMemoryDirectoryServer ds = new InMemoryDirectoryServer(cfg);
ds.startListening();
final int listenPort = ds.getListenPort();
// Verify that we can use the server's getConnection method.
final LDAPConnection conn = ds.getConnection();
assertNull(conn.getSSLSession());
RootDSE rootDSE = conn.getRootDSE();
assertNotNull(rootDSE);
assertTrue(rootDSE.supportsExtendedOperation(StartTLSExtendedRequest.STARTTLS_REQUEST_OID));
// Use the StartTLS extended operation to secure the connection.
final SSLUtil clientSSLUtil = new SSLUtil(new TrustAllTrustManager());
final ExtendedResult startTLSResult = conn.processExtendedOperation(new StartTLSExtendedRequest(clientSSLUtil.createSSLContext()));
assertNotNull(startTLSResult);
assertEquals(startTLSResult.getResultCode(), ResultCode.SUCCESS);
assertNotNull(conn.getSSLSession());
assertNotNull(conn.getSSLSession().getPeerCertificateChain());
assertTrue(conn.getSSLSession().getPeerCertificateChain().length > 0);
// Test an additional set of operations over the newly-secured connection.
conn.bind("cn=Directory Manager", "password");
conn.processExtendedOperation(new WhoAmIExtendedRequest());
conn.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
conn.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
conn.modify("dn: ou=People,dc=example,dc=com", "changeType: modify", "replace: description", "description: foo");
assertTrue(conn.compare("ou=People,dc=example,dc=com", "description", "foo").compareMatched());
conn.search("dc=example,dc=com", SearchScope.BASE, "(objectClass=*)");
conn.modifyDN("ou=People,dc=example,dc=com", "ou=Users", true);
conn.delete("ou=Users,dc=example,dc=com");
conn.delete("dc=example,dc=com");
final Control[] abandonControls = { new Control("1.2.3.4", false), new Control("1.2.3.5", false, new ASN1OctetString("foo")) };
conn.abandon(InternalSDKHelper.createAsyncRequestID(1, conn), abandonControls);
final Control[] unbindControls = { new Control("1.2.3.4", false), new Control("1.2.3.5", false, new ASN1OctetString("foo")) };
conn.close(unbindControls);
assertNull(conn.getSSLSession());
ds.shutDown(true);
}
use of com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest in project ldapsdk by pingidentity.
the class RateLimiterRequestHandlerTestCase method testRateLimiterCreatedWithFixedRateBarrierDefaultOperationTypes.
/**
* Tests the behavior of the rate limiter when created with a fixed-rate
* barrier for the default set of operation types.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testRateLimiterCreatedWithFixedRateBarrierDefaultOperationTypes() throws Exception {
final InMemoryDirectoryServerConfig inMemoryConfig = new InMemoryDirectoryServerConfig("dc=example,dc=com");
inMemoryConfig.addAdditionalBindCredentials("cn=Directory Manager", "password");
final InMemoryRequestHandler inMemoryRequestHandler = new InMemoryRequestHandler(inMemoryConfig);
final FixedRateBarrier rateLimiter = new FixedRateBarrier(1000L, 100);
final RateLimiterRequestHandler rateLimiterRequestHandler = new RateLimiterRequestHandler(inMemoryRequestHandler, rateLimiter);
final LDAPListenerConfig listenerConfig = new LDAPListenerConfig(0, rateLimiterRequestHandler);
final LDAPListener listener = new LDAPListener(listenerConfig);
listener.startListening();
final LDAPConnection conn = new LDAPConnection("127.0.0.1", listener.getListenPort());
conn.bind("cn=Directory Manager", "password");
conn.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
conn.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
conn.search("dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
conn.compare("dc=example,dc=com", "dc", "example");
conn.modify("dn: dc=example,dc=com", "changetype: modify", "replace: description", "description: foo");
conn.modifyDN("ou=People,dc=example,dc=com", "ou=Users", true);
conn.delete("ou=Users,dc=example,dc=com");
conn.delete("dc=example,dc=com");
conn.processExtendedOperation(new WhoAmIExtendedRequest());
conn.abandon(InternalSDKHelper.createAsyncRequestID(1, conn));
conn.close();
listener.shutDown(true);
}
use of com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest in project ldapsdk by pingidentity.
the class RateLimiterRequestHandlerTestCase method testRateLimiterCreatedWithFixedRateBarrierAllOperationTypes.
/**
* Tests the behavior of the rate limiter when created with a fixed-rate
* barrier for all operation types.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testRateLimiterCreatedWithFixedRateBarrierAllOperationTypes() throws Exception {
final InMemoryDirectoryServerConfig inMemoryConfig = new InMemoryDirectoryServerConfig("dc=example,dc=com");
inMemoryConfig.addAdditionalBindCredentials("cn=Directory Manager", "password");
final InMemoryRequestHandler inMemoryRequestHandler = new InMemoryRequestHandler(inMemoryConfig);
final FixedRateBarrier rateLimiter = new FixedRateBarrier(1000L, 100);
final RateLimiterRequestHandler rateLimiterRequestHandler = new RateLimiterRequestHandler(inMemoryRequestHandler, rateLimiter, rateLimiter, rateLimiter, rateLimiter, rateLimiter, rateLimiter, rateLimiter, rateLimiter, rateLimiter);
final LDAPListenerConfig listenerConfig = new LDAPListenerConfig(0, rateLimiterRequestHandler);
final LDAPListener listener = new LDAPListener(listenerConfig);
listener.startListening();
final LDAPConnection conn = new LDAPConnection("127.0.0.1", listener.getListenPort());
conn.bind("cn=Directory Manager", "password");
conn.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
conn.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
conn.search("dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
conn.compare("dc=example,dc=com", "dc", "example");
conn.modify("dn: dc=example,dc=com", "changetype: modify", "replace: description", "description: foo");
conn.modifyDN("ou=People,dc=example,dc=com", "ou=Users", true);
conn.delete("ou=Users,dc=example,dc=com");
conn.delete("dc=example,dc=com");
conn.processExtendedOperation(new WhoAmIExtendedRequest());
conn.abandon(InternalSDKHelper.createAsyncRequestID(1, conn));
conn.close();
listener.shutDown(true);
}
use of com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest in project ldapsdk by pingidentity.
the class RateLimiterRequestHandlerTestCase method testRateLimiterCreatedWithRatePerSecond.
/**
* Tests the behavior of the rate limiter when created with a specified rate
* per second.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testRateLimiterCreatedWithRatePerSecond() throws Exception {
final InMemoryDirectoryServerConfig inMemoryConfig = new InMemoryDirectoryServerConfig("dc=example,dc=com");
inMemoryConfig.addAdditionalBindCredentials("cn=Directory Manager", "password");
final InMemoryRequestHandler inMemoryRequestHandler = new InMemoryRequestHandler(inMemoryConfig);
final RateLimiterRequestHandler rateLimiterRequestHandler = new RateLimiterRequestHandler(inMemoryRequestHandler, 100);
final LDAPListenerConfig listenerConfig = new LDAPListenerConfig(0, rateLimiterRequestHandler);
final LDAPListener listener = new LDAPListener(listenerConfig);
listener.startListening();
final LDAPConnection conn = new LDAPConnection("127.0.0.1", listener.getListenPort());
conn.bind("cn=Directory Manager", "password");
conn.add("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
conn.add("dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People");
conn.search("dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
conn.compare("dc=example,dc=com", "dc", "example");
conn.modify("dn: dc=example,dc=com", "changetype: modify", "replace: description", "description: foo");
conn.modifyDN("ou=People,dc=example,dc=com", "ou=Users", true);
conn.delete("ou=Users,dc=example,dc=com");
conn.delete("dc=example,dc=com");
conn.processExtendedOperation(new WhoAmIExtendedRequest());
conn.abandon(InternalSDKHelper.createAsyncRequestID(1, conn));
conn.close();
listener.shutDown(true);
}
use of com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest in project ldapsdk by pingidentity.
the class InMemoryDirectoryServerWhoAmITestCase method testWhoAmI.
/**
* Provides test coverage for various "Who Am I?" scenarios.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testWhoAmI() throws Exception {
final InMemoryDirectoryServer ds = getTestDS(true, true);
final LDAPConnection conn = ds.getConnection();
// Without authentication, the authorization identity should be anonymous.
WhoAmIExtendedResult result = (WhoAmIExtendedResult) conn.processExtendedOperation(new WhoAmIExtendedRequest());
assertEquals(result.getResultCode(), ResultCode.SUCCESS);
assertNotNull(result.getAuthorizationID());
assertTrue(result.getAuthorizationID().equals("dn:"));
// Authenticate as a normal user, and verify that the change is reflected in
// the extended result.
conn.bind("uid=test.user,ou=People,dc=example,dc=com", "password");
result = (WhoAmIExtendedResult) conn.processExtendedOperation(new WhoAmIExtendedRequest());
assertEquals(result.getResultCode(), ResultCode.SUCCESS);
assertNotNull(result.getAuthorizationID());
assertTrue(result.getAuthorizationID().startsWith("dn:"));
assertEquals(new DN(result.getAuthorizationID().substring(3)), new DN("uid=test.user,ou=People,dc=example,dc=com"));
// Bind anonymously and verify that the identity is again anonymous
conn.bind("", "");
result = (WhoAmIExtendedResult) conn.processExtendedOperation(new WhoAmIExtendedRequest());
assertEquals(result.getResultCode(), ResultCode.SUCCESS);
assertNotNull(result.getAuthorizationID());
assertTrue(result.getAuthorizationID().equals("dn:"));
// Authenticate as an additional bind user, and verify that the change is
// reflected in the extended result.
conn.bind("cn=Directory Manager", "password");
result = (WhoAmIExtendedResult) conn.processExtendedOperation(new WhoAmIExtendedRequest());
assertEquals(result.getResultCode(), ResultCode.SUCCESS);
assertNotNull(result.getAuthorizationID());
assertTrue(result.getAuthorizationID().startsWith("dn:"));
assertEquals(new DN(result.getAuthorizationID().substring(3)), new DN("cn=Directory Manager"));
// Verify that processing fails with a critical control.
Control[] controls = { new Control("1.2.3.4", true) };
result = (WhoAmIExtendedResult) conn.processExtendedOperation(new WhoAmIExtendedRequest(controls));
assertEquals(result.getResultCode(), ResultCode.UNAVAILABLE_CRITICAL_EXTENSION);
// Verify that processing succeeds with only non-critical controls.
controls = new Control[] { new Control("1.2.3.4", false) };
result = (WhoAmIExtendedResult) conn.processExtendedOperation(new WhoAmIExtendedRequest(controls));
assertEquals(result.getResultCode(), ResultCode.SUCCESS);
assertNotNull(result.getAuthorizationID());
assertTrue(result.getAuthorizationID().startsWith("dn:"));
assertEquals(new DN(result.getAuthorizationID().substring(3)), new DN("cn=Directory Manager"));
conn.close();
}
Aggregations