|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2002-2013 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2002-2016 the original author or authors.
|
|
*
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -17,23 +17,37 @@
|
|
package org.springframework.security.ldap.server;
|
|
package org.springframework.security.ldap.server;
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.fail;
|
|
import static org.assertj.core.api.Assertions.fail;
|
|
|
|
+import static org.junit.Assert.assertEquals;
|
|
|
|
+import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.net.ServerSocket;
|
|
import java.net.ServerSocket;
|
|
|
|
+import java.security.UnrecoverableKeyException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
+import org.apache.commons.lang.exception.ExceptionUtils;
|
|
|
|
+import org.junit.Rule;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
+import org.junit.rules.TemporaryFolder;
|
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
|
+import org.springframework.util.FileCopyUtils;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Useful for debugging the container by itself.
|
|
* Useful for debugging the container by itself.
|
|
*
|
|
*
|
|
* @author Luke Taylor
|
|
* @author Luke Taylor
|
|
* @author Rob Winch
|
|
* @author Rob Winch
|
|
|
|
+ * @author Gunnar Hillert
|
|
* @since 3.0
|
|
* @since 3.0
|
|
*/
|
|
*/
|
|
public class ApacheDSContainerTests {
|
|
public class ApacheDSContainerTests {
|
|
|
|
|
|
|
|
+ @Rule
|
|
|
|
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
|
|
|
+
|
|
// SEC-2162
|
|
// SEC-2162
|
|
@Test
|
|
@Test
|
|
public void failsToStartThrowsException() throws Exception {
|
|
public void failsToStartThrowsException() throws Exception {
|
|
@@ -95,6 +109,96 @@ public class ApacheDSContainerTests {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void startWithLdapOverSslWithoutCertificate() throws Exception {
|
|
|
|
+ ApacheDSContainer server = new ApacheDSContainer("dc=springframework,dc=org",
|
|
|
|
+ "classpath:test-server.ldif");
|
|
|
|
+ List<Integer> ports = getDefaultPorts(1);
|
|
|
|
+ server.setPort(ports.get(0));
|
|
|
|
+ server.setLdapOverSslEnabled(true);
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ server.afterPropertiesSet();
|
|
|
|
+ }
|
|
|
|
+ catch (IllegalArgumentException e){
|
|
|
|
+ assertEquals("When LdapOverSsl is enabled, the keyStoreFile property must be set.", e.getMessage());
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ fail("Expected an IllegalArgumentException to be thrown.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void startWithLdapOverSslWithWrongPassword() throws Exception {
|
|
|
|
+ final ClassPathResource keyStoreResource = new ClassPathResource("/org/springframework/security/ldap/server/spring.keystore");
|
|
|
|
+ final File temporaryKeyStoreFile = new File(temporaryFolder.getRoot(), "spring.keystore");
|
|
|
|
+ FileCopyUtils.copy(keyStoreResource.getInputStream(), new FileOutputStream(temporaryKeyStoreFile));
|
|
|
|
+
|
|
|
|
+ assertTrue(temporaryKeyStoreFile.isFile());
|
|
|
|
+
|
|
|
|
+ ApacheDSContainer server = new ApacheDSContainer("dc=springframework,dc=org",
|
|
|
|
+ "classpath:test-server.ldif");
|
|
|
|
+
|
|
|
|
+ List<Integer> ports = getDefaultPorts(1);
|
|
|
|
+ server.setPort(ports.get(0));
|
|
|
|
+
|
|
|
|
+ server.setLdapOverSslEnabled(true);
|
|
|
|
+ server.setKeyStoreFile(temporaryKeyStoreFile);
|
|
|
|
+ server.setCertificatePassord("incorrect-password");
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ server.afterPropertiesSet();
|
|
|
|
+ }
|
|
|
|
+ catch (RuntimeException e){
|
|
|
|
+ assertEquals("Server startup failed", e.getMessage());
|
|
|
|
+ assertTrue("Expected an instance of 'UnrecoverableKeyException' but got " + ExceptionUtils.getRootCause(e).getClass().getName(), ExceptionUtils.getRootCause(e) instanceof UnrecoverableKeyException);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ fail("Expected a RuntimeException to be thrown.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * This test starts an LDAP server using LDAPs (LDAP over SSL). A self-signed certificate is being used, which was
|
|
|
|
+ * previously generated with:
|
|
|
|
+ *
|
|
|
|
+ * <pre>
|
|
|
|
+ * {@code
|
|
|
|
+ * keytool -genkey -alias spring -keyalg RSA -keystore spring.keystore -validity 3650 -storetype JKS \
|
|
|
|
+ * -dname "CN=localhost, OU=Spring, O=Pivotal, L=Kailua-Kona, ST=HI, C=US" -keypass spring -storepass spring
|
|
|
|
+ * }
|
|
|
|
+ * </pre>
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void startWithLdapOverSsl() throws Exception {
|
|
|
|
+
|
|
|
|
+ final ClassPathResource keyStoreResource = new ClassPathResource("/org/springframework/security/ldap/server/spring.keystore");
|
|
|
|
+ final File temporaryKeyStoreFile = new File(temporaryFolder.getRoot(), "spring.keystore");
|
|
|
|
+ FileCopyUtils.copy(keyStoreResource.getInputStream(), new FileOutputStream(temporaryKeyStoreFile));
|
|
|
|
+
|
|
|
|
+ assertTrue(temporaryKeyStoreFile.isFile());
|
|
|
|
+
|
|
|
|
+ ApacheDSContainer server = new ApacheDSContainer("dc=springframework,dc=org",
|
|
|
|
+ "classpath:test-server.ldif");
|
|
|
|
+
|
|
|
|
+ List<Integer> ports = getDefaultPorts(1);
|
|
|
|
+ server.setPort(ports.get(0));
|
|
|
|
+
|
|
|
|
+ server.setLdapOverSslEnabled(true);
|
|
|
|
+ server.setKeyStoreFile(temporaryKeyStoreFile);
|
|
|
|
+ server.setCertificatePassord("spring");
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ server.afterPropertiesSet();
|
|
|
|
+ }
|
|
|
|
+ finally {
|
|
|
|
+ try {
|
|
|
|
+ server.destroy();
|
|
|
|
+ }
|
|
|
|
+ catch (Throwable t) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
private List<Integer> getDefaultPorts(int count) throws IOException {
|
|
private List<Integer> getDefaultPorts(int count) throws IOException {
|
|
List<ServerSocket> connections = new ArrayList<ServerSocket>();
|
|
List<ServerSocket> connections = new ArrayList<ServerSocket>();
|
|
List<Integer> availablePorts = new ArrayList<Integer>(count);
|
|
List<Integer> availablePorts = new ArrayList<Integer>(count);
|