Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Create integration tests using testcontainers #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ private Optional<AttributePrincipal> getAttributePrincipal(Object o) {
/**
* Hacky code please do not use that in production
*/
@SuppressWarnings("Duplicates")
private Optional<String> getProxyGrantingTicket(Authentication authentication) {
Optional<AttributePrincipal> attributePrincipal = getAttributePrincipal(authentication);
if (!attributePrincipal.isPresent() || !(attributePrincipal.get() instanceof AttributePrincipalImpl)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.kakawait</groupId>
<artifactId>cas-security-spring-boot-1-integration-tests</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>Cas security spring boot 1 integration tests</name>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<assertj-core.version>3.10.0</assertj-core.version>
<testcontainers.version>1.7.3</testcontainers.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.kakawait</groupId>
<artifactId>cas-security-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>com.kakawait</groupId>
<artifactId>cas-security-spring-boot-common-integration-tests</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.kakawait.spring.boot.security.cas.integration.tests;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.authentication.AttributePrincipalImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.security.cas.authentication.CasAuthenticationToken;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.filter.ForwardedHeaderFilter;

import java.lang.reflect.Field;
import java.util.Optional;

/**
* @author Thibaud Leprêtre
*/
@SpringBootApplication
@EnableGlobalMethodSecurity(securedEnabled = true)
public class IntegrationTestsSpringBootApplication {

public static void main(String[] args) {
SpringApplication.run(IntegrationTestsSpringBootApplication.class, args);
}

@Bean
FilterRegistrationBean forwardedHeaderFilter() {
FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
filterRegBean.setFilter(new ForwardedHeaderFilter());
filterRegBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return filterRegBean;
}

@RestController
static class IndexController {

@RequestMapping
public Identity index(Authentication authentication) {
if (authentication != null && StringUtils.hasText(authentication.getName())) {
return new Identity(authentication.getName(), getProxyGrantingTicket(authentication).orElse(null));
}
throw new IllegalStateException();
}

private Optional<AttributePrincipal> getAttributePrincipal(Object o) {
if (!(o instanceof CasAuthenticationToken)) {
return Optional.empty();
}
return Optional.of(((CasAuthenticationToken) o).getAssertion().getPrincipal());
}

/**
* Hacky code please do not use that in production
*/
@SuppressWarnings("Duplicates")
private Optional<String> getProxyGrantingTicket(Authentication authentication) {
Optional<AttributePrincipal> attributePrincipal = getAttributePrincipal(authentication);
if (!attributePrincipal.isPresent() || !(attributePrincipal.get() instanceof AttributePrincipalImpl)) {
return Optional.empty();
}
Field field = ReflectionUtils.findField(AttributePrincipalImpl.class, "proxyGrantingTicket");
ReflectionUtils.makeAccessible(field);
return Optional.ofNullable(ReflectionUtils.getField(field, attributePrincipal.get())).map(Object::toString);
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Identity {
private String username;

private String ptg;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.kakawait.spring.boot.security.cas.integration.tests;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import static java.lang.String.format;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.boot.test.util.EnvironmentTestUtils.addEnvironment;

/**
* @author Thibaud Leprêtre
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, properties = {"security.cas.service.resolution-mode=dynamic", "logging.level.org.springframework.security=debug"})
@ContextConfiguration(initializers = {CasIntegrationTest.Initializer.class})
public class CasIntegrationTest extends AbstractCasIntegrationTest {

@Autowired
private Environment environment;

@Autowired
private RestTemplateBuilder restTemplateBuilder;

@Override
protected TestRestTemplate getTestRestTemplate(TestRestTemplate.HttpClientOption... httpClientOptions) {
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(environment);
testRestTemplate.setUriTemplateHandler(handler);
return testRestTemplate;
}

public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
addEnvironment(configurableApplicationContext, property("security.cas.server.base-url", getCasBaseUrl()));
}

private String property(String key, String value) {
return format("%s=%s", key, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.kakawait</groupId>
<artifactId>cas-security-spring-boot-2-integration-tests</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>Cas security spring boot 2 integration tests</name>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<assertj-core.version>3.10.0</assertj-core.version>
<testcontainers.version>1.7.3</testcontainers.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.kakawait</groupId>
<artifactId>cas-security-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>com.kakawait</groupId>
<artifactId>cas-security-spring-boot-common-integration-tests</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.kakawait.spring.boot.security.cas.integration.tests;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.authentication.AttributePrincipalImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.security.cas.authentication.CasAuthenticationToken;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.filter.ForwardedHeaderFilter;

import java.lang.reflect.Field;
import java.util.Optional;

/**
* @author Thibaud Leprêtre
*/
@SpringBootApplication
@EnableGlobalMethodSecurity(securedEnabled = true)
public class IntegrationTestsSpringBootApplication {

public static void main(String[] args) {
SpringApplication.run(IntegrationTestsSpringBootApplication.class, args);
}

@Bean
FilterRegistrationBean forwardedHeaderFilter() {
FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
filterRegBean.setFilter(new ForwardedHeaderFilter());
filterRegBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return filterRegBean;
}

@RestController
static class IndexController {

@RequestMapping
public Identity index(Authentication authentication) {
if (authentication != null && StringUtils.hasText(authentication.getName())) {
return new Identity(authentication.getName(), getProxyGrantingTicket(authentication).orElse(null));
}
throw new IllegalStateException();
}

private Optional<AttributePrincipal> getAttributePrincipal(Object o) {
if (!(o instanceof CasAuthenticationToken)) {
return Optional.empty();
}
return Optional.of(((CasAuthenticationToken) o).getAssertion().getPrincipal());
}

/**
* Hacky code please do not use that in production
*/
@SuppressWarnings("Duplicates")
private Optional<String> getProxyGrantingTicket(Authentication authentication) {
Optional<AttributePrincipal> attributePrincipal = getAttributePrincipal(authentication);
if (!attributePrincipal.isPresent() || !(attributePrincipal.get() instanceof AttributePrincipalImpl)) {
return Optional.empty();
}
Field field = ReflectionUtils.findField(AttributePrincipalImpl.class, "proxyGrantingTicket");
ReflectionUtils.makeAccessible(field);
return Optional.ofNullable(ReflectionUtils.getField(field, attributePrincipal.get())).map(Object::toString);
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Identity {
private String username;

private String ptg;
}
}

}
Loading