diff --git a/gradle.properties b/gradle.properties index 597262e..e5ad866 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,3 +7,5 @@ mockitoVersion=4.8.1 assertjVersion=3.23.1 servletApiVersion=6.0.0 httpclient5Version=5.1.4 +kerbyVersion=2.0.3 +okhttp3Version=3.14.9 diff --git a/settings.gradle b/settings.gradle index abfbb04..979a88e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -30,6 +30,7 @@ include 'spring-security-kerberos-bom' include 'spring-security-kerberos-core' include 'spring-security-kerberos-client' include 'spring-security-kerberos-web' +include 'spring-security-kerberos-test' include 'spring-security-kerberos-samples:sec-client-rest-template' include 'spring-security-kerberos-samples:sec-server-client-auth' include 'spring-security-kerberos-samples:sec-server-spnego-form-auth' diff --git a/spring-security-kerberos-client/spring-security-kerberos-client.gradle b/spring-security-kerberos-client/spring-security-kerberos-client.gradle index 0882cb6..589a8cd 100644 --- a/spring-security-kerberos-client/spring-security-kerberos-client.gradle +++ b/spring-security-kerberos-client/spring-security-kerberos-client.gradle @@ -14,9 +14,11 @@ dependencies { optional 'org.springframework.security:spring-security-ldap' // api('org.springframework.security:spring-security-web') // api('jakarta.servlet:jakarta.servlet-api') + testImplementation project(':spring-security-kerberos-test') testImplementation 'org.springframework:spring-test' testImplementation 'org.springframework.security:spring-security-config' testImplementation 'org.junit.jupiter:junit-jupiter' testImplementation 'org.mockito:mockito-junit-jupiter' testImplementation 'org.assertj:assertj-core' + testImplementation 'com.squareup.okhttp3:mockwebserver' } diff --git a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/KerberosRestTemplateTests.java b/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/KerberosRestTemplateTests.java index 5ec6029..ec5e910 100644 --- a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/KerberosRestTemplateTests.java +++ b/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/KerberosRestTemplateTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -15,249 +15,115 @@ */ package org.springframework.security.kerberos.client; -// import static org.hamcrest.CoreMatchers.is; -// import static org.junit.Assert.assertThat; +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.Collections; -// import java.io.File; -// import java.io.IOException; -// import java.lang.annotation.Documented; -// import java.lang.annotation.ElementType; -// import java.lang.annotation.Retention; -// import java.lang.annotation.RetentionPolicy; -// import java.lang.annotation.Target; -// import java.net.InetAddress; -// import java.util.concurrent.CountDownLatch; -// import java.util.concurrent.TimeUnit; +import okhttp3.mockwebserver.Dispatcher; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import okio.Buffer; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -// import org.junit.After; -// import org.junit.Test; -// import org.springframework.boot.SpringApplication; -// import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; -// import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration; -// import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; -// import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; -// import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration; -// import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; -// import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; -// import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; -// import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; -// import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; -// import org.springframework.context.ApplicationListener; -// import org.springframework.context.ConfigurableApplicationContext; -// import org.springframework.context.annotation.Bean; -// import org.springframework.context.annotation.Configuration; -// import org.springframework.context.annotation.Import; -// import org.springframework.http.client.ClientHttpResponse; -// import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -// import org.springframework.security.kerberos.client.KerberosRestTemplate; -// import org.springframework.security.kerberos.test.KerberosSecurityTestcase; -// import org.springframework.security.kerberos.test.MiniKdc; -// import org.springframework.stereotype.Controller; -// import org.springframework.web.bind.annotation.RequestMapping; -// import org.springframework.web.bind.annotation.RequestMethod; -// import org.springframework.web.bind.annotation.ResponseBody; -// import org.springframework.web.client.DefaultResponseErrorHandler; -// import org.springframework.web.client.RestTemplate; +import org.springframework.http.MediaType; +import org.springframework.security.kerberos.test.KerberosSecurityTestcase; +import org.springframework.security.kerberos.test.MiniKdc; -public class KerberosRestTemplateTests /*extends KerberosSecurityTestcase */{ +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.http.HttpHeaders.AUTHORIZATION; +import static org.springframework.http.HttpHeaders.CONTENT_LENGTH; +import static org.springframework.http.HttpHeaders.CONTENT_TYPE; +import static org.springframework.http.HttpHeaders.WWW_AUTHENTICATE; - // private ConfigurableApplicationContext context; +class KerberosRestTemplateTests extends KerberosSecurityTestcase { - // @After - // public void close() { - // if (context != null) { - // context.close(); - // } - // context = null; - // } + private final MockWebServer server = new MockWebServer(); + private static final String helloWorld = "Hello World"; + private static final MediaType textContentType = + new MediaType("text", "plain", Collections.singletonMap("charset", "UTF-8")); + private int port; + private String baseUrl; + private KerberosRestTemplate restTemplate; + private String clientPrincipal; + private File clientKeytab; - // @Test - // public void testSpnego() throws Exception { + @BeforeEach + void setUp() throws Exception { + this.server.setDispatcher(new TestDispatcher()); + this.server.start(); + this.port = this.server.getPort(); + this.baseUrl = "http://localhost:" + this.port; - // MiniKdc kdc = getKdc(); - // File workDir = getWorkDir(); - // String host = InetAddress.getLocalHost().getCanonicalHostName(); + MiniKdc kdc = getKdc(); + File workDir = getWorkDir(); - // String serverPrincipal = "HTTP/" + host; - // File serverKeytab = new File(workDir, "server.keytab"); - // kdc.createPrincipal(serverKeytab, serverPrincipal); + clientPrincipal = "client/localhost"; + clientKeytab = new File(workDir, "client.keytab"); + kdc.createPrincipal(clientKeytab, clientPrincipal); - // String clientPrincipal = "client/" + host; - // File clientKeytab = new File(workDir, "client.keytab"); - // kdc.createPrincipal(clientKeytab, clientPrincipal); + String serverPrincipal = "HTTP/localhost"; + File serverKeytab = new File(workDir, "server.keytab"); + kdc.createPrincipal(serverKeytab, serverPrincipal); + } + @AfterEach + void tearDown() throws Exception { + this.server.shutdown(); + } - // context = SpringApplication.run(new Object[] { WebSecurityConfig.class, VanillaWebConfiguration.class, - // WebConfiguration.class }, new String[] { "--security.basic.enabled=true", - // "--security.user.name=username", "--security.user.password=password", - // "--serverPrincipal=" + serverPrincipal, "--serverKeytab=" + serverKeytab.getAbsolutePath() }); + @Test + void sendsNegotiateHeader() { + setUpClient(); + String s = restTemplate.getForObject(baseUrl + "/get", String.class); + assertThat(s).isEqualTo(helloWorld); + } - // PortInitListener portInitListener = context.getBean(PortInitListener.class); - // assertThat(portInitListener.latch.await(10, TimeUnit.SECONDS), is(true)); - // int port = portInitListener.port; + private void setUpClient() { + restTemplate = new KerberosRestTemplate(clientKeytab.getAbsolutePath(), clientPrincipal); + } - // KerberosRestTemplate restTemplate = new KerberosRestTemplate(clientKeytab.getAbsolutePath(), clientPrincipal); + private MockResponse getRequest(RecordedRequest request, byte[] body, String contentType) { + if (request.getMethod().equals("OPTIONS")) { + return new MockResponse().setResponseCode(200).setHeader("Allow", "GET, OPTIONS, HEAD, TRACE"); + } + Buffer buf = new Buffer(); + buf.write(body); + MockResponse response = new MockResponse() + .setHeader(CONTENT_LENGTH, body.length) + .setBody(buf) + .setResponseCode(200); + if (contentType != null) { + response = response.setHeader(CONTENT_TYPE, contentType); + } + return response; + } - // String response = restTemplate.getForObject("http://" + host + ":" + port + "/hello", String.class); - // assertThat(response, is("home")); - // } + protected class TestDispatcher extends Dispatcher { - // @Test - // public void testSpnegoWithPassword() throws Exception { + @Override + public MockResponse dispatch(RecordedRequest request) throws InterruptedException { + try { + byte[] helloWorldBytes = helloWorld.getBytes(StandardCharsets.UTF_8); - // MiniKdc kdc = getKdc(); - // File workDir = getWorkDir(); - // String host = InetAddress.getLocalHost().getCanonicalHostName(); + if (request.getPath().equals("/get")) { + String header = request.getHeader(AUTHORIZATION); + if (header == null) { + return new MockResponse().setResponseCode(401).addHeader(WWW_AUTHENTICATE, "Negotiate"); + } + else if (header != null && header.startsWith("Negotiate ")) { + return getRequest(request, helloWorldBytes, textContentType.toString()); + } + } + return new MockResponse().setResponseCode(404); + } + catch (Throwable ex) { + return new MockResponse().setResponseCode(500).setBody(ex.toString()); + } - // String serverPrincipal = "HTTP/" + host; - // File serverKeytab = new File(workDir, "server.keytab"); - // kdc.createPrincipal(serverKeytab, serverPrincipal); - - // String userPrincipal = "testuser"; - // String password = "testpassword"; - // kdc.createPrincipal(userPrincipal, password); - - - // context = SpringApplication.run(new Object[] { WebSecurityConfig.class, VanillaWebConfiguration.class, - // WebConfiguration.class }, new String[] { "--security.basic.enabled=true", - // "--security.user.name=username", "--security.user.password=password", - // "--serverPrincipal=" + serverPrincipal, "--serverKeytab=" + serverKeytab.getAbsolutePath() }); - - // PortInitListener portInitListener = context.getBean(PortInitListener.class); - // assertThat(portInitListener.latch.await(10, TimeUnit.SECONDS), is(true)); - // int port = portInitListener.port; - - // KerberosRestTemplate restTemplate = new KerberosRestTemplate(null, userPrincipal, password, null); - - // String response = restTemplate.getForObject("http://" + host + ":" + port + "/hello", String.class); - // assertThat(response, is("home")); - // } - - // @Test - // public void testSpnegoWithForward() throws Exception { - - // MiniKdc kdc = getKdc(); - // File workDir = getWorkDir(); - // String host = InetAddress.getLocalHost().getCanonicalHostName(); - - // String serverPrincipal = "HTTP/" + host; - // File serverKeytab = new File(workDir, "server.keytab"); - // kdc.createPrincipal(serverKeytab, serverPrincipal); - - // context = SpringApplication.run(new Object[] { WebSecurityConfigSpnegoForward.class, VanillaWebConfiguration.class, - // WebConfiguration.class }, new String[] { "--security.basic.enabled=true", - // "--security.user.name=username", "--security.user.password=password", - // "--serverPrincipal=" + serverPrincipal, "--serverKeytab=" + serverKeytab.getAbsolutePath() }); - - // PortInitListener portInitListener = context.getBean(PortInitListener.class); - // assertThat(portInitListener.latch.await(10, TimeUnit.SECONDS), is(true)); - // int port = portInitListener.port; - - // // TODO: should tweak minikdc so that we can use kerberos principals - // // which are not valid, for now just use plain RestTemplate - - // // just checking that we get 401 which we skip and - // // get login page content - // RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory()); - // restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { - // @Override - // public void handleError(ClientHttpResponse response) throws IOException { - // } - // }); - - // String response = restTemplate.getForObject("http://" + host + ":" + port + "/hello", String.class); - // assertThat(response, is("login")); - // } - - // @Test - // public void testSpnegoWithSuccessHandler() throws Exception { - - // MiniKdc kdc = getKdc(); - // File workDir = getWorkDir(); - // String host = InetAddress.getLocalHost().getCanonicalHostName(); - - // String serverPrincipal = "HTTP/" + host; - // File serverKeytab = new File(workDir, "server.keytab"); - // kdc.createPrincipal(serverKeytab, serverPrincipal); - - // String clientPrincipal = "client/" + host; - // File clientKeytab = new File(workDir, "client.keytab"); - // kdc.createPrincipal(clientKeytab, clientPrincipal); - - - // context = SpringApplication.run(new Object[] { WebSecurityConfigSuccessHandler.class, VanillaWebConfiguration.class, - // WebConfiguration.class }, new String[] { "--security.basic.enabled=true", - // "--security.user.name=username", "--security.user.password=password", - // "--serverPrincipal=" + serverPrincipal, "--serverKeytab=" + serverKeytab.getAbsolutePath() }); - - // PortInitListener portInitListener = context.getBean(PortInitListener.class); - // assertThat(portInitListener.latch.await(10, TimeUnit.SECONDS), is(true)); - // int port = portInitListener.port; - - // KerberosRestTemplate restTemplate = new KerberosRestTemplate(clientKeytab.getAbsolutePath(), clientPrincipal); - - // String response = restTemplate.getForObject("http://" + host + ":" + port + "/hello", String.class); - // assertThat(response, is("home")); - // } - - // protected static class PortInitListener implements ApplicationListener { - - // public int port; - // public CountDownLatch latch = new CountDownLatch(1); - - // @Override - // public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) { - // port = event.getEmbeddedServletContainer().getPort(); - // latch.countDown(); - // } - - // } - - // @Configuration - // protected static class VanillaWebConfiguration { - - // @Bean - // public PortInitListener portListener() { - // return new PortInitListener(); - // } - - // @Bean - // public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() { - // TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); - // factory.setPort(0); - // return factory; - // } - // } - - // @MinimalWebConfiguration - // @Import(SecurityAutoConfiguration.class) - // @Controller - // protected static class WebConfiguration { - - // @RequestMapping(method = RequestMethod.GET) - // @ResponseBody - // public String home() { - // return "home"; - // } - - // @RequestMapping(method = RequestMethod.GET, value = "/login") - // @ResponseBody - // public String login() { - // return "login"; - // } - - // } - - // @Configuration - // @Target(ElementType.TYPE) - // @Retention(RetentionPolicy.RUNTIME) - // @Documented - // @Import({ EmbeddedServletContainerAutoConfiguration.class, - // ServerPropertiesAutoConfiguration.class, - // DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, - // HttpMessageConvertersAutoConfiguration.class, - // ErrorMvcAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class }) - // protected static @interface MinimalWebConfiguration { - // } + } + } } diff --git a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/TestMultiTierAuthentication.java b/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/TestMultiTierAuthentication.java deleted file mode 100644 index 7b96e06..0000000 --- a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/TestMultiTierAuthentication.java +++ /dev/null @@ -1,191 +0,0 @@ -/* - * Copyright 2009-2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.kerberos.client; - -import org.springframework.core.io.FileSystemResource; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.authority.AuthorityUtils; -import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; -import org.springframework.security.kerberos.authentication.KerberosAuthenticationProvider; -import org.springframework.security.kerberos.authentication.KerberosMultiTier; -import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider; -import org.springframework.security.kerberos.authentication.KerberosServiceRequestToken; -import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosClient; -import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator; -// import org.springframework.security.kerberos.test.KerberosSecurityTestcase; -// import org.springframework.security.kerberos.test.MiniKdc; - -import java.io.File; - - -/** - * @author Bogdan Mustiata - */ -public class TestMultiTierAuthentication /* extends KerberosSecurityTestcase*/ { - -// public static final String REALM_NAME = "EXAMPLE.COM"; - -// public static final String USER_LOGIN_NAME = "user1"; -// public static final String USER_FQDN_NAME = "user1@EXAMPLE.COM"; -// public static final String USER_PASSWORD = "secret"; - -// public static final String WEB_TIER_SPN = "HTTP/webtier@EXAMPLE.COM"; -// public static final String WEB_TIER_USER_PASSWORD = "secret"; - -// public static final String SERVICE_TIER_SPN = "HTTP/servicetier@EXAMPLE.COM"; -// public static final String SERVICE_TIER_USER_PASSWORD = "secret"; - -// @Test -// public void testServer() throws Exception { -// MiniKdc kdc = getKdc(); -// File workDir = getWorkDir(); - -// File webTierKeytabFile = new File(workDir, "webtier.keytab"); -// kdc.createKeyabFile(webTierKeytabFile, WEB_TIER_SPN, WEB_TIER_USER_PASSWORD); - -// File serviceTierKeytabFile = new File(workDir, "servicetier.keytab"); -// kdc.createKeyabFile(serviceTierKeytabFile, SERVICE_TIER_SPN, SERVICE_TIER_USER_PASSWORD); - -// // -// // User logs in as user1/secret -// // -// KerberosAuthenticationProvider kerberosAuthProvider = -// createUserPassAuthenticator(/* debug: */ true); - -// Authentication authentication = kerberosAuthProvider -// .authenticate(new UsernamePasswordAuthenticationToken(USER_LOGIN_NAME, USER_PASSWORD)); - -// assertEquals(USER_FQDN_NAME, authentication.getName()); - -// // -// // User creates a ticket for the HTTP/webtier@EXAMPLE.COM, using -// // and then calls the service, using the tokenData -// // -// authentication = KerberosMultiTier.authenticateService( -// authentication, USER_LOGIN_NAME, 3600, WEB_TIER_SPN); - -// byte[] tokenData = KerberosMultiTier -// .getTokenForService(authentication, WEB_TIER_SPN); - -// assertNotNull(tokenData); -// assertTrue(tokenData.length != 0); - -// // -// // The service HTTP/webtier@EXAMPLE.COM authenticates via tokens. -// // -// KerberosServiceAuthenticationProvider webTierAuthenticatorProvider = -// createServiceAuthenticator( -// true, -// WEB_TIER_SPN, -// REALM_NAME, -// webTierKeytabFile.getCanonicalPath() -// ); - - -// // -// // The service HTTP/webtier@EXAMPLE.COM authenticates the user1@EXAMPLE.COM -// // using the previously stored token, then authenticates itself further as -// // user1@EXAMPLE.COM to the HTTP/servicetier@EXAMPLE.COM. -// // -// Authentication webTierAuthentication = webTierAuthenticatorProvider -// .authenticate(new KerberosServiceRequestToken(tokenData)); - -// assertEquals(USER_FQDN_NAME, webTierAuthentication.getName()); - -// webTierAuthentication = KerberosMultiTier.authenticateService( -// webTierAuthentication, USER_FQDN_NAME, 3600, SERVICE_TIER_SPN); - -// byte[] workplaceTokenData = KerberosMultiTier.getTokenForService( -// webTierAuthentication, SERVICE_TIER_SPN); - -// // -// // The service HTTP/icr@EXAMPLE.COM authenticates via tokens. -// // -// webTierAuthenticatorProvider = -// createServiceAuthenticator( -// true, -// SERVICE_TIER_SPN, -// REALM_NAME, -// serviceTierKeytabFile.getCanonicalPath() -// ); - -// // -// // The service HTTP/servicetier@EXAMPLE.COM authenticates via the previously saved -// // token, received from the HTTP/webtier@EXAMPLE.COM on behalf of user1@EXAMPLE.COM -// // -// Authentication serviceTierAuthentication = webTierAuthenticatorProvider -// .authenticate(new KerberosServiceRequestToken(workplaceTokenData)); - -// assertEquals(USER_FQDN_NAME, serviceTierAuthentication.getName()); -// } - -// /** -// * Create a username/password authenticator. -// * @return -// */ -// private KerberosAuthenticationProvider createUserPassAuthenticator(boolean debug) { -// KerberosAuthenticationProvider kerberosAuthenticationProvider = -// new KerberosAuthenticationProvider(); - -// SunJaasKerberosClient sunJaasKerberosClient = new SunJaasKerberosClient(); - -// sunJaasKerberosClient.setDebug(debug); -// sunJaasKerberosClient.setMultiTier(true); - -// kerberosAuthenticationProvider.setKerberosClient(sunJaasKerberosClient); -// kerberosAuthenticationProvider.setUserDetailsService(userDetailsService()); - -// return kerberosAuthenticationProvider; -// } - -// private KerberosServiceAuthenticationProvider createServiceAuthenticator(boolean debug, -// String serviceName, -// String realmName, -// String keytabFileLocation) throws Exception { -// KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider = -// new KerberosServiceAuthenticationProvider(); - -// SunJaasKerberosTicketValidator ticketValidator = new SunJaasKerberosTicketValidator(); -// ticketValidator.setDebug(debug); -// ticketValidator.setServicePrincipal(serviceName); -// ticketValidator.setRealmName(realmName); -// ticketValidator.setKeyTabLocation(new FileSystemResource(keytabFileLocation)); -// ticketValidator.setMultiTier(true); - -// ticketValidator.afterPropertiesSet(); - -// kerberosServiceAuthenticationProvider.setTicketValidator(ticketValidator); -// kerberosServiceAuthenticationProvider.setUserDetailsService(userDetailsService()); - -// return kerberosServiceAuthenticationProvider; -// } - -// private UserDetailsService userDetailsService() { -// return new UserDetailsService() { -// @Override -// public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { -// return new User(username, "notUsed", true, true, true, true, -// AuthorityUtils.createAuthorityList("ROLE_USER")); - -// } -// }; -// } - -} diff --git a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/WebSecurityConfig.java b/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/WebSecurityConfig.java deleted file mode 100644 index 45a2e25..0000000 --- a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/WebSecurityConfig.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.kerberos.client; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.FileSystemResource; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -// import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -// import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; -import org.springframework.security.core.authority.AuthorityUtils; -import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; -import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider; -import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator; -import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter; -import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint; -import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; - -// @Configuration -// @EnableWebMvcSecurity -public class WebSecurityConfig /*extends WebSecurityConfigurerAdapter*/ { - - @Value("${serverPrincipal}") - private String serverPrincipal; - - @Value("${serverKeytab}") - private String serverKeytab; - - // @Override - // protected void configure(HttpSecurity http) throws Exception { - // http - // .exceptionHandling().authenticationEntryPoint(spnegoEntryPoint()).and() - // .authorizeRequests() - // .antMatchers("/", "/home").permitAll() - // .antMatchers("/hello").access("hasRole('ROLE_USER')") - // .anyRequest().authenticated() - // .and() - - // .addFilterBefore(spnegoAuthenticationProcessingFilter(authenticationManagerBean()), BasicAuthenticationFilter.class); - // } - - // @Override - // protected void configure(AuthenticationManagerBuilder auth) throws Exception { - // auth.authenticationProvider(kerberosServiceAuthenticationProvider()); - // } - - // @Bean - // public SpnegoEntryPoint spnegoEntryPoint() { - // return new SpnegoEntryPoint(); - // } - - // @Bean - // public SpnegoAuthenticationProcessingFilter spnegoAuthenticationProcessingFilter( - // AuthenticationManager authenticationManager) { - // SpnegoAuthenticationProcessingFilter filter = new SpnegoAuthenticationProcessingFilter(); - // filter.setAuthenticationManager(authenticationManager); - // return filter; - // } - - // @Bean - // public KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider() { - // KerberosServiceAuthenticationProvider provider = new KerberosServiceAuthenticationProvider(); - // provider.setTicketValidator(sunJaasKerberosTicketValidator()); - // provider.setUserDetailsService(dummyUserDetailsService()); - // return provider; - // } - - // @Bean - // public SunJaasKerberosTicketValidator sunJaasKerberosTicketValidator() { - // SunJaasKerberosTicketValidator ticketValidator = new SunJaasKerberosTicketValidator(); - // ticketValidator.setServicePrincipal(serverPrincipal); - // ticketValidator.setKeyTabLocation(new FileSystemResource(serverKeytab)); - // ticketValidator.setDebug(true); - // return ticketValidator; - // } - - // @Bean - // public DummyUserDetailsService dummyUserDetailsService() { - // return new DummyUserDetailsService(); - // } - - // static class DummyUserDetailsService implements UserDetailsService { - - // public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - // return new User(username, "notUsed", true, true, true, true, - // AuthorityUtils.createAuthorityList("ROLE_USER")); - // } - - // } - -} diff --git a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/WebSecurityConfigSpnegoForward.java b/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/WebSecurityConfigSpnegoForward.java deleted file mode 100644 index e762eac..0000000 --- a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/WebSecurityConfigSpnegoForward.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.kerberos.client; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.FileSystemResource; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -// import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -// import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; -import org.springframework.security.core.authority.AuthorityUtils; -import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; -import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider; -import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator; -import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter; -import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint; -import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; - -// @Configuration -// @EnableWebMvcSecurity -public class WebSecurityConfigSpnegoForward /*extends WebSecurityConfigurerAdapter*/ { - - // @Value("${serverPrincipal}") - // private String serverPrincipal; - - // @Value("${serverKeytab}") - // private String serverKeytab; - - // @Override - // protected void configure(HttpSecurity http) throws Exception { - // http - // .exceptionHandling().authenticationEntryPoint(spnegoEntryPoint()).and() - // .authorizeRequests() - // .antMatchers("/", "/home", "/login").permitAll() - // .antMatchers("/hello").access("hasRole('ROLE_USER')") - // .anyRequest().authenticated() - // .and() - - // .addFilterBefore(spnegoAuthenticationProcessingFilter(authenticationManagerBean()), BasicAuthenticationFilter.class); - // } - - // @Override - // protected void configure(AuthenticationManagerBuilder auth) throws Exception { - // auth.authenticationProvider(kerberosServiceAuthenticationProvider()); - // } - - // @Bean - // public SpnegoEntryPoint spnegoEntryPoint() { - // return new SpnegoEntryPoint("/login"); - // } - - // @Bean - // public SpnegoAuthenticationProcessingFilter spnegoAuthenticationProcessingFilter( - // AuthenticationManager authenticationManager) { - // SpnegoAuthenticationProcessingFilter filter = new SpnegoAuthenticationProcessingFilter(); - // filter.setAuthenticationManager(authenticationManager); - // return filter; - // } - - // @Bean - // public KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider() { - // KerberosServiceAuthenticationProvider provider = new KerberosServiceAuthenticationProvider(); - // provider.setTicketValidator(sunJaasKerberosTicketValidator()); - // provider.setUserDetailsService(dummyUserDetailsService()); - // return provider; - // } - - // @Bean - // public SunJaasKerberosTicketValidator sunJaasKerberosTicketValidator() { - // SunJaasKerberosTicketValidator ticketValidator = new SunJaasKerberosTicketValidator(); - // ticketValidator.setServicePrincipal(serverPrincipal); - // ticketValidator.setKeyTabLocation(new FileSystemResource(serverKeytab)); - // ticketValidator.setDebug(true); - // return ticketValidator; - // } - - // @Bean - // public DummyUserDetailsService dummyUserDetailsService() { - // return new DummyUserDetailsService(); - // } - - // static class DummyUserDetailsService implements UserDetailsService { - - // public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - // return new User(username, "notUsed", true, true, true, true, - // AuthorityUtils.createAuthorityList("ROLE_USER")); - // } - - // } - -} diff --git a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/WebSecurityConfigSuccessHandler.java b/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/WebSecurityConfigSuccessHandler.java deleted file mode 100644 index a63a70d..0000000 --- a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/WebSecurityConfigSuccessHandler.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.kerberos.client; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.FileSystemResource; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -// import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -// import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; -import org.springframework.security.core.authority.AuthorityUtils; -import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; -import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider; -import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator; -import org.springframework.security.kerberos.web.authentication.ResponseHeaderSettingKerberosAuthenticationSuccessHandler; -import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter; -import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint; -import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; - -// @Configuration -// @EnableWebMvcSecurity -public class WebSecurityConfigSuccessHandler /* extends WebSecurityConfigurerAdapter*/ { - - // @Value("${serverPrincipal}") - // private String serverPrincipal; - - // @Value("${serverKeytab}") - // private String serverKeytab; - - // @Override - // protected void configure(HttpSecurity http) throws Exception { - // http - // .exceptionHandling().authenticationEntryPoint(spnegoEntryPoint()).and() - // .authorizeRequests() - // .antMatchers("/", "/home").permitAll() - // .antMatchers("/hello").access("hasRole('ROLE_USER')") - // .anyRequest().authenticated() - // .and() - - // .addFilterBefore(spnegoAuthenticationProcessingFilter(authenticationManagerBean()), BasicAuthenticationFilter.class); - // } - - // @Override - // protected void configure(AuthenticationManagerBuilder auth) throws Exception { - // auth.authenticationProvider(kerberosServiceAuthenticationProvider()); - // } - - // @Bean - // public SpnegoEntryPoint spnegoEntryPoint() { - // return new SpnegoEntryPoint(); - // } - - // @Bean - // public SpnegoAuthenticationProcessingFilter spnegoAuthenticationProcessingFilter( - // AuthenticationManager authenticationManager) { - // SpnegoAuthenticationProcessingFilter filter = new SpnegoAuthenticationProcessingFilter(); - - // ResponseHeaderSettingKerberosAuthenticationSuccessHandler successHandler = new ResponseHeaderSettingKerberosAuthenticationSuccessHandler(); - // filter.setSuccessHandler(successHandler); - - // filter.setAuthenticationManager(authenticationManager); - // return filter; - // } - - // @Bean - // public KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider() { - // KerberosServiceAuthenticationProvider provider = new KerberosServiceAuthenticationProvider(); - // provider.setTicketValidator(sunJaasKerberosTicketValidator()); - // provider.setUserDetailsService(dummyUserDetailsService()); - // return provider; - // } - - // @Bean - // public SunJaasKerberosTicketValidator sunJaasKerberosTicketValidator() { - // SunJaasKerberosTicketValidator ticketValidator = new SunJaasKerberosTicketValidator(); - // ticketValidator.setServicePrincipal(serverPrincipal); - // ticketValidator.setKeyTabLocation(new FileSystemResource(serverKeytab)); - // ticketValidator.setDebug(true); - // return ticketValidator; - // } - - // @Bean - // public DummyUserDetailsService dummyUserDetailsService() { - // return new DummyUserDetailsService(); - // } - - // static class DummyUserDetailsService implements UserDetailsService { - - // public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - // return new User(username, "notUsed", true, true, true, true, - // AuthorityUtils.createAuthorityList("ROLE_USER")); - // } - - // } - -} diff --git a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/docs/KerberosLdapContextSourceConfig.java b/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/docs/KerberosLdapContextSourceConfig.java deleted file mode 100644 index acbbb95..0000000 --- a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/docs/KerberosLdapContextSourceConfig.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.kerberos.client.docs; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.core.io.FileSystemResource; -import org.springframework.security.kerberos.client.config.SunJaasKrb5LoginConfig; -import org.springframework.security.kerberos.client.ldap.KerberosLdapContextSource; -import org.springframework.security.ldap.search.FilterBasedLdapUserSearch; -import org.springframework.security.ldap.userdetails.LdapUserDetailsMapper; -import org.springframework.security.ldap.userdetails.LdapUserDetailsService; - -public class KerberosLdapContextSourceConfig { - -//tag::snippetA[] - @Value("${app.ad-server}") - private String adServer; - - @Value("${app.service-principal}") - private String servicePrincipal; - - @Value("${app.keytab-location}") - private String keytabLocation; - - @Value("${app.ldap-search-base}") - private String ldapSearchBase; - - @Value("${app.ldap-search-filter}") - private String ldapSearchFilter; - - @Bean - public KerberosLdapContextSource kerberosLdapContextSource() { - KerberosLdapContextSource contextSource = new KerberosLdapContextSource(adServer); - SunJaasKrb5LoginConfig loginConfig = new SunJaasKrb5LoginConfig(); - loginConfig.setKeyTabLocation(new FileSystemResource(keytabLocation)); - loginConfig.setServicePrincipal(servicePrincipal); - loginConfig.setDebug(true); - loginConfig.setIsInitiator(true); - contextSource.setLoginConfig(loginConfig); - return contextSource; - } - - @Bean - public LdapUserDetailsService ldapUserDetailsService() { - FilterBasedLdapUserSearch userSearch = - new FilterBasedLdapUserSearch(ldapSearchBase, ldapSearchFilter, kerberosLdapContextSource()); - LdapUserDetailsService service = new LdapUserDetailsService(userSearch); - service.setUserDetailsMapper(new LdapUserDetailsMapper()); - return service; - } -//end::snippetA[] - -} diff --git a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/docs/KerberosRestTemplateConfig.java b/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/docs/KerberosRestTemplateConfig.java deleted file mode 100644 index bacfd19..0000000 --- a/spring-security-kerberos-client/src/test/java/org/springframework/security/kerberos/client/docs/KerberosRestTemplateConfig.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.kerberos.client.docs; - -import org.springframework.security.kerberos.client.KerberosRestTemplate; - -public class KerberosRestTemplateConfig { - -//tag::snippetA[] - public void doWithTicketCache() { - KerberosRestTemplate restTemplate = - new KerberosRestTemplate(); - restTemplate.getForObject("http://neo.example.org:8080/hello", String.class); - } -//end::snippetA[] - -//tag::snippetB[] - public void doWithKeytabFile() { - KerberosRestTemplate restTemplate = - new KerberosRestTemplate("/tmp/user2.keytab", "user2@EXAMPLE.ORG"); - restTemplate.getForObject("http://neo.example.org:8080/hello", String.class); - } -//end::snippetB[] - -} diff --git a/spring-security-kerberos-management/spring-security-kerberos-management.gradle b/spring-security-kerberos-management/spring-security-kerberos-management.gradle index 050c1fd..fe72e2b 100644 --- a/spring-security-kerberos-management/spring-security-kerberos-management.gradle +++ b/spring-security-kerberos-management/spring-security-kerberos-management.gradle @@ -14,9 +14,11 @@ dependencies { api platform("org.junit:junit-bom:$junitVersion") api platform("org.mockito:mockito-bom:$mockitoVersion") constraints { - // api "org.apache.httpcomponents:httpclient:$httpclientVersion" api "org.apache.httpcomponents.client5:httpclient5:$httpclient5Version" api "org.assertj:assertj-core:$assertjVersion" api "jakarta.servlet:jakarta.servlet-api:$servletApiVersion" + api "org.apache.kerby:kerb-simplekdc:$kerbyVersion" + api "com.squareup.okhttp3:mockwebserver:$okhttp3Version" + api "com.squareup.okhttp3:okhttp:$okhttp3Version" } } diff --git a/spring-security-kerberos-test/spring-security-kerberos-test.gradle b/spring-security-kerberos-test/spring-security-kerberos-test.gradle new file mode 100644 index 0000000..d314eaa --- /dev/null +++ b/spring-security-kerberos-test/spring-security-kerberos-test.gradle @@ -0,0 +1,14 @@ +plugins { + id 'org.springframework.security.kerberos.module' +} + +description = 'Spring Security Kerberos Test' + +dependencies { + management platform(project(":spring-security-kerberos-management")) + api 'org.apache.kerby:kerb-simplekdc' + api 'org.junit.jupiter:junit-jupiter' + testImplementation 'org.springframework:spring-test' + testImplementation 'org.mockito:mockito-junit-jupiter' + testImplementation 'org.assertj:assertj-core' +} diff --git a/spring-security-kerberos-test/src/main/java/org/springframework/security/kerberos/test/KerberosSecurityTestcase.java b/spring-security-kerberos-test/src/main/java/org/springframework/security/kerberos/test/KerberosSecurityTestcase.java index 6d8e159..329eb59 100644 --- a/spring-security-kerberos-test/src/main/java/org/springframework/security/kerberos/test/KerberosSecurityTestcase.java +++ b/spring-security-kerberos-test/src/main/java/org/springframework/security/kerberos/test/KerberosSecurityTestcase.java @@ -1,11 +1,13 @@ -/* - * Copyright 2015 the original author or authors. +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -15,20 +17,21 @@ */ package org.springframework.security.kerberos.test; -import org.junit.After; -import org.junit.Before; - import java.io.File; import java.util.Properties; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + /** * KerberosSecurityTestcase provides a base class for using MiniKdc with other * testcases. KerberosSecurityTestcase starts the MiniKdc (@Before) before * running tests, and stop the MiniKdc (@After) after the testcases, using * default settings (working dir and kdc configurations). - * - * @author Original Hadoop MiniKdc Authors - * @author Janne Valkealahti + *

+ * Users can directly inherit this class and implement their own test functions + * using the default settings, or override functions getTestDir() and + * createMiniKdcConf() to provide new settings. * */ public class KerberosSecurityTestcase { @@ -36,7 +39,7 @@ public class KerberosSecurityTestcase { private File workDir; private Properties conf; - @Before + @BeforeEach public void startMiniKdc() throws Exception { createTestDir(); createMiniKdcConf(); @@ -46,9 +49,9 @@ public class KerberosSecurityTestcase { } /** - * Create a working directory, it should be the build directory. Under this - * directory an ApacheDS working directory will be created, this directory - * will be deleted when the MiniKdc stops. + * Create a working directory, it should be the build directory. Under + * this directory an ApacheDS working directory will be created, this + * directory will be deleted when the MiniKdc stops. */ public void createTestDir() { workDir = new File(System.getProperty("test.dir", "target")); @@ -61,7 +64,7 @@ public class KerberosSecurityTestcase { conf = MiniKdc.createConf(); } - @After + @AfterEach public void stopMiniKdc() { if (kdc != null) { kdc.stop(); @@ -79,5 +82,4 @@ public class KerberosSecurityTestcase { public Properties getConf() { return conf; } - } diff --git a/spring-security-kerberos-test/src/main/java/org/springframework/security/kerberos/test/MiniKdc.java b/spring-security-kerberos-test/src/main/java/org/springframework/security/kerberos/test/MiniKdc.java index 97cab7a..a764427 100644 --- a/spring-security-kerberos-test/src/main/java/org/springframework/security/kerberos/test/MiniKdc.java +++ b/spring-security-kerberos-test/src/main/java/org/springframework/security/kerberos/test/MiniKdc.java @@ -1,11 +1,11 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -15,79 +15,43 @@ */ package org.springframework.security.kerberos.test; -import org.apache.commons.io.Charsets; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.text.StrSubstitutor; -import org.apache.directory.api.ldap.model.schema.SchemaManager; -import org.apache.directory.api.ldap.schemaextractor.SchemaLdifExtractor; -import org.apache.directory.api.ldap.schemaextractor.impl.DefaultSchemaLdifExtractor; -import org.apache.directory.api.ldap.schemaloader.LdifSchemaLoader; -import org.apache.directory.api.ldap.schemamanager.impl.DefaultSchemaManager; -import org.apache.directory.server.constants.ServerDNConstants; -import org.apache.directory.server.core.DefaultDirectoryService; -import org.apache.directory.server.core.api.CacheService; -import org.apache.directory.server.core.api.DirectoryService; -import org.apache.directory.server.core.api.InstanceLayout; -import org.apache.directory.server.core.api.schema.SchemaPartition; -import org.apache.directory.server.core.kerberos.KeyDerivationInterceptor; -import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex; -import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition; -import org.apache.directory.server.core.partition.ldif.LdifPartition; -import org.apache.directory.server.kerberos.kdc.KdcServer; -import org.apache.directory.server.kerberos.shared.crypto.encryption.KerberosKeyFactory; -import org.apache.directory.server.kerberos.shared.keytab.Keytab; -import org.apache.directory.server.kerberos.shared.keytab.KeytabEntry; -import org.apache.directory.server.protocol.shared.transport.TcpTransport; -import org.apache.directory.server.protocol.shared.transport.UdpTransport; -import org.apache.directory.server.xdbm.Index; -import org.apache.directory.shared.kerberos.KerberosTime; -import org.apache.directory.shared.kerberos.codec.types.EncryptionType; -import org.apache.directory.shared.kerberos.components.EncryptionKey; -import org.apache.directory.api.ldap.model.entry.DefaultEntry; -import org.apache.directory.api.ldap.model.entry.Entry; -import org.apache.directory.api.ldap.model.ldif.LdifEntry; -import org.apache.directory.api.ldap.model.ldif.LdifReader; -import org.apache.directory.api.ldap.model.name.Dn; -import org.apache.directory.api.ldap.model.schema.registries.SchemaLoader; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.StringReader; -import java.lang.reflect.Method; -import java.net.InetAddress; -import java.net.ServerSocket; -import java.text.MessageFormat; -import java.util.ArrayList; +import java.nio.charset.StandardCharsets; import java.util.Arrays; -import java.util.HashMap; import java.util.HashSet; -import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Properties; import java.util.Set; -import java.util.UUID; + +import org.apache.kerby.kerberos.kerb.KrbException; +import org.apache.kerby.kerberos.kerb.server.KdcConfigKey; +import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer; +import org.apache.kerby.util.IOUtil; +import org.apache.kerby.util.NetworkUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - *

Mini KDC based on Apache Directory Server that can be embedded in testcases - * or used from command line as a standalone KDC.

+ * Mini KDC based on Apache Directory Server that can be embedded in testcases + * or used from command line as a standalone KDC. + *

* From within testcases: - *

MiniKdc sets 2 System properties when started and un-sets them when stopped:

+ *

+ * MiniKdc sets one System property when started and un-set when stopped: *

- *

Because of this, multiple MiniKdc instances cannot be started in parallel. + * Because of this, multiple MiniKdc instances cannot be started in parallel. * For example, running testcases in parallel that start a KDC each. To - * accomplish this a single MiniKdc should be used for all testcases running in - * parallel.

- * + * accomplish this a single MiniKdc should be used for all testcases running + * in parallel. + *

* MiniKdc default configuration values are: *