Search in sources :

Example 31 with WhoAmIExtendedRequest

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);
}
Also used : KeyStoreKeyManager(com.unboundid.util.ssl.KeyStoreKeyManager) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) WhoAmIExtendedRequest(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest) TrustAllTrustManager(com.unboundid.util.ssl.TrustAllTrustManager) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) RootDSE(com.unboundid.ldap.sdk.RootDSE) SSLUtil(com.unboundid.util.ssl.SSLUtil) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) Control(com.unboundid.ldap.sdk.Control) IgnoreNoUserModificationRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.IgnoreNoUserModificationRequestControl) AuthorizationIdentityResponseControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl) ExtendedResult(com.unboundid.ldap.sdk.ExtendedResult) File(java.io.File) StartTLSExtendedRequest(com.unboundid.ldap.sdk.extensions.StartTLSExtendedRequest) Test(org.testng.annotations.Test)

Example 32 with WhoAmIExtendedRequest

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);
}
Also used : WhoAmIExtendedRequest(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest) FixedRateBarrier(com.unboundid.util.FixedRateBarrier) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) Test(org.testng.annotations.Test)

Example 33 with WhoAmIExtendedRequest

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);
}
Also used : WhoAmIExtendedRequest(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest) FixedRateBarrier(com.unboundid.util.FixedRateBarrier) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) Test(org.testng.annotations.Test)

Example 34 with WhoAmIExtendedRequest

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);
}
Also used : WhoAmIExtendedRequest(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) Test(org.testng.annotations.Test)

Example 35 with WhoAmIExtendedRequest

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();
}
Also used : WhoAmIExtendedResult(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedResult) Control(com.unboundid.ldap.sdk.Control) WhoAmIExtendedRequest(com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest) DN(com.unboundid.ldap.sdk.DN) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) Test(org.testng.annotations.Test)

Aggregations

WhoAmIExtendedRequest (com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest)37 Test (org.testng.annotations.Test)31 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)21 WhoAmIExtendedResult (com.unboundid.ldap.sdk.extensions.WhoAmIExtendedResult)15 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)9 Control (com.unboundid.ldap.sdk.Control)8 LDAPException (com.unboundid.ldap.sdk.LDAPException)7 SimpleBindRequest (com.unboundid.ldap.sdk.SimpleBindRequest)7 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)6 AuthorizationIdentityResponseControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl)6 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)4 BindResult (com.unboundid.ldap.sdk.BindResult)4 AddRequest (com.unboundid.ldap.sdk.AddRequest)3 CompareRequest (com.unboundid.ldap.sdk.CompareRequest)3 DeleteRequest (com.unboundid.ldap.sdk.DeleteRequest)3 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)3 LDAPConnectionOptions (com.unboundid.ldap.sdk.LDAPConnectionOptions)3 ModifyDNRequest (com.unboundid.ldap.sdk.ModifyDNRequest)3 ModifyRequest (com.unboundid.ldap.sdk.ModifyRequest)3 PLAINBindRequest (com.unboundid.ldap.sdk.PLAINBindRequest)3