Restore Format and CheckFormat for saml2 Projects

- Update the way that the saml2 projects were dependent
on identity-provider resource files.
- Use TestContainers to simplify runtime dependencies
- Update compose.yml to use Docker-standard interpolation

Closes gh-335
This commit is contained in:
Josh Cummings
2024-11-05 12:32:36 -07:00
parent 2e39e5eea6
commit d1630a6032
23 changed files with 80 additions and 337 deletions

View File

@@ -12,9 +12,6 @@ repositories {
maven { url "https://build.shibboleth.net/nexus/content/repositories/releases/" }
}
sourceSets.main.java.srcDirs += "$projectDir/../identity-provider/src/main/java"
sourceSets.main.resources.srcDirs += "$projectDir/../identity-provider/src/main/resources"
dependencies {
constraints {
implementation "org.opensaml:opensaml-saml-api:5.1.3"
@@ -26,10 +23,12 @@ dependencies {
implementation 'org.springframework.security:spring-security-saml2-service-provider'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
runtimeOnly files("$projectDir/../identity-provider/build/resources/main")
testImplementation project(':servlet:spring-boot:java:saml2:identity-provider')
testImplementation 'org.htmlunit:htmlunit'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
runtimeOnly "org.springframework.boot:spring-boot-docker-compose"
}
tasks.withType(Test).configureEach {

View File

@@ -6,3 +6,6 @@ pluginManagement {
maven { url "https://repo.spring.io/snapshot" }
}
}
include ":servlet:spring-boot:java:saml2:identity-provider"
project(":servlet:spring-boot:java:saml2:identity-provider").projectDir = file("../identity-provider")

View File

@@ -1,70 +0,0 @@
/*
* Copyright 2002-2021 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 example;
import java.io.IOException;
import java.net.ServerSocket;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
/**
* Spring Boot doesn't determine the port before the docker containers are loaded, so
* we'll decide the test port here and override the associated properties.
*
* @author Josh Cummings
*/
public class PreDockerComposeServerPortInitializer implements EnvironmentPostProcessor {
private static final Integer port = getPort();
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
environment.getPropertySources().addFirst(new ServerPortPropertySource(port));
}
private static Integer getPort() {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private static class ServerPortPropertySource extends PropertySource<Integer> {
ServerPortPropertySource(Integer port) {
super("server.port.override", port);
}
@Override
public Object getProperty(String name) {
if ("server.port".equals(name)) {
return getSource();
}
if ("SERVER_PORT".equals(name)) {
return getSource();
}
return null;
}
}
}

View File

@@ -1 +0,0 @@
org.springframework.boot.env.EnvironmentPostProcessor=example.PreDockerComposeServerPortInitializer

View File

@@ -4,11 +4,9 @@ logging.level:
spring:
docker:
compose:
file: docker:docker/compose.yml
file: classpath:docker/compose.yml
readiness:
wait: never
skip:
in-tests: false
security:
filter:
dispatcher-types: async, error, request, forward

View File

@@ -13,8 +13,9 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
runtimeOnly "org.springframework.boot:spring-boot-docker-compose"
implementation 'org.springframework.boot:spring-boot-starter-test'
implementation "org.testcontainers:testcontainers:1.20.3"
implementation "org.testcontainers:junit-jupiter:1.20.3"
}
tasks.withType(Test).configureEach {

View File

@@ -1,30 +0,0 @@
/*
* Copyright 2002-2021 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 example;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
public class ComposeFilePropertyPlaceholderApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext context) {
DockerProtocolResolver.environment = context.getEnvironment();
}
}

View File

@@ -1,56 +0,0 @@
/*
* Copyright 2002-2021 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 example;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public class DockerProtocolResolver implements ProtocolResolver {
private static final String PREFIX = "docker:";
static Environment environment;
@Override
public Resource resolve(String location, ResourceLoader resourceLoader) {
if (!location.startsWith(PREFIX)) {
return null;
}
Resource resource = resourceLoader.getResource(location.replace(PREFIX, "classpath:"));
try {
String content = resource.getContentAsString(StandardCharsets.UTF_8);
content = environment.resolvePlaceholders(content);
File file = resource.getFile();
File tmp = new File(file.getAbsolutePath() + ".tmp");
tmp.createNewFile();
Files.write(tmp.toPath(), content.getBytes(StandardCharsets.UTF_8));
tmp.deleteOnExit();
return new FileSystemResource(tmp);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2024 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 example;
import java.io.IOException;
import java.net.ServerSocket;
import org.testcontainers.containers.DockerComposeContainer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;
public class IdentityProviderTestExecutionListener extends AbstractTestExecutionListener {
private DockerComposeContainer<?> composed;
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
String port = Integer.toString(getRandomPort());
System.setProperty("SERVER_PORT", port);
System.setProperty("server.port", port);
this.composed = new DockerComposeContainer<>("saml2", new ClassPathResource("docker/compose.yml").getFile())
.withEnv("SERVER_PORT", port);
this.composed.start();
}
@Override
public void afterTestClass(TestContext testContext) throws Exception {
this.composed.stop();
}
static int getRandomPort() throws IOException {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
}
}

View File

@@ -1,2 +1 @@
org.springframework.context.ApplicationContextInitializer=example.ComposeFilePropertyPlaceholderApplicationContextInitializer
org.springframework.core.io.ProtocolResolver=example.DockerProtocolResolver
org.springframework.test.context.TestExecutionListener=example.IdentityProviderTestExecutionListener

View File

@@ -5,7 +5,7 @@ services:
- ./metadata/authsources.php:/var/www/simplesamlphp/config/authsources.php
- ./metadata/one-relyingparties.php:/var/www/simplesamlphp/metadata/saml20-sp-remote.php
environment:
- PORT=${SERVER_PORT:8080}
- PORT=${SERVER_PORT:-8080}
idp-two.7f000001.nip.io:
image: kristophjunge/test-saml-idp:1.15
@@ -13,7 +13,7 @@ services:
- ./metadata/authsources.php:/var/www/simplesamlphp/config/authsources.php
- ./metadata/two-relyingparties.php:/var/www/simplesamlphp/metadata/saml20-sp-remote.php
environment:
- PORT=${SERVER_PORT:8080}
- PORT=${SERVER_PORT:-8080}
nginx:
image: nginx:stable

View File

@@ -12,9 +12,6 @@ repositories {
maven { url "https://build.shibboleth.net/nexus/content/repositories/releases/" }
}
sourceSets.main.java.srcDirs += "$projectDir/../identity-provider/src/main/java"
sourceSets.main.resources.srcDirs += "$projectDir/../identity-provider/src/main/resources"
dependencies {
constraints {
implementation "org.opensaml:opensaml-saml-api:5.1.3"
@@ -26,6 +23,9 @@ dependencies {
implementation 'org.springframework.security:spring-security-saml2-service-provider'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
runtimeOnly files("$projectDir/../identity-provider/build/resources/main")
testImplementation project(":servlet:spring-boot:java:saml2:identity-provider")
testImplementation 'org.htmlunit:htmlunit'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'

View File

@@ -6,3 +6,6 @@ pluginManagement {
maven { url "https://repo.spring.io/snapshot" }
}
}
include ":servlet:spring-boot:java:saml2:identity-provider"
project(":servlet:spring-boot:java:saml2:identity-provider").projectDir = file("../identity-provider")

View File

@@ -1,70 +0,0 @@
/*
* Copyright 2002-2021 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 example;
import java.io.IOException;
import java.net.ServerSocket;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
/**
* Spring Boot doesn't determine the port before the docker containers are loaded, so
* we'll decide the test port here and override the associated properties.
*
* @author Josh Cummings
*/
public class PreDockerComposeServerPortInitializer implements EnvironmentPostProcessor {
private static final Integer port = getPort();
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
environment.getPropertySources().addFirst(new ServerPortPropertySource(port));
}
private static Integer getPort() {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private static class ServerPortPropertySource extends PropertySource<Integer> {
ServerPortPropertySource(Integer port) {
super("server.port.override", port);
}
@Override
public Object getProperty(String name) {
if ("server.port".equals(name)) {
return getSource();
}
if ("SERVER_PORT".equals(name)) {
return getSource();
}
return null;
}
}
}

View File

@@ -1 +0,0 @@
org.springframework.boot.env.EnvironmentPostProcessor=example.PreDockerComposeServerPortInitializer

View File

@@ -4,11 +4,9 @@ logging.level:
spring:
docker:
compose:
file: docker:docker/compose.yml
file: classpath:docker/compose.yml
readiness:
wait: never
skip:
in-tests: false
security:
saml2:
relyingparty:

View File

@@ -13,9 +13,6 @@ repositories {
maven { url "https://build.shibboleth.net/nexus/content/repositories/releases/" }
}
sourceSets.main.java.srcDirs += "$projectDir/../identity-provider/src/main/java"
sourceSets.main.resources.srcDirs += "$projectDir/../identity-provider/src/main/resources"
dependencies {
constraints {
implementation "org.opensaml:opensaml-saml-api:5.1.2"
@@ -27,6 +24,9 @@ dependencies {
implementation 'org.springframework.security:spring-security-saml2-service-provider'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
runtimeOnly files("$projectDir/../identity-provider/build/resources/main")
testImplementation project(':servlet:spring-boot:java:saml2:identity-provider')
testImplementation 'org.htmlunit:htmlunit'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'

View File

@@ -6,3 +6,6 @@ pluginManagement {
maven { url "https://repo.spring.io/snapshot" }
}
}
include ":servlet:spring-boot:java:saml2:identity-provider"
project(":servlet:spring-boot:java:saml2:identity-provider").projectDir = file("../identity-provider")

View File

@@ -1,70 +0,0 @@
/*
* Copyright 2002-2021 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 example;
import java.io.IOException;
import java.net.ServerSocket;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
/**
* Spring Boot doesn't determine the port before the docker containers are loaded, so
* we'll decide the test port here and override the associated properties.
*
* @author Josh Cummings
*/
public class PreDockerComposeServerPortInitializer implements EnvironmentPostProcessor {
private static final Integer port = getPort();
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
environment.getPropertySources().addFirst(new ServerPortPropertySource(port));
}
private static Integer getPort() {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private static class ServerPortPropertySource extends PropertySource<Integer> {
ServerPortPropertySource(Integer port) {
super("server.port.override", port);
}
@Override
public Object getProperty(String name) {
if ("server.port".equals(name)) {
return getSource();
}
if ("SERVER_PORT".equals(name)) {
return getSource();
}
return null;
}
}
}

View File

@@ -41,7 +41,6 @@ import static org.assertj.core.api.Assertions.assertThat;
@AutoConfigureMockMvc
public class Saml2LoginApplicationITests {
@LocalServerPort
int port;

View File

@@ -1 +0,0 @@
org.springframework.boot.env.EnvironmentPostProcessor=example.PreDockerComposeServerPortInitializer

View File

@@ -1,11 +1,9 @@
spring:
docker:
compose:
file: docker:docker/compose.yml
file: classpath:docker/compose.yml
readiness:
wait: never
skip:
in-tests: false
security:
saml2:
relyingparty: