ソースを参照

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
Josh Cummings 9 ヶ月 前
コミット
d1630a6032
23 ファイル変更80 行追加337 行削除
  1. 0 12
      build.gradle
  2. 3 4
      servlet/spring-boot/java/saml2/custom-urls/build.gradle
  3. 3 0
      servlet/spring-boot/java/saml2/custom-urls/settings.gradle
  4. 0 70
      servlet/spring-boot/java/saml2/custom-urls/src/integTest/java/example/PreDockerComposeServerPortInitializer.java
  5. 0 1
      servlet/spring-boot/java/saml2/custom-urls/src/integTest/resources/META-INF/spring.factories
  6. 1 3
      servlet/spring-boot/java/saml2/custom-urls/src/main/resources/application.yml
  7. 3 2
      servlet/spring-boot/java/saml2/identity-provider/build.gradle
  8. 0 30
      servlet/spring-boot/java/saml2/identity-provider/src/main/java/example/ComposeFilePropertyPlaceholderApplicationContextInitializer.java
  9. 0 56
      servlet/spring-boot/java/saml2/identity-provider/src/main/java/example/DockerProtocolResolver.java
  10. 53 0
      servlet/spring-boot/java/saml2/identity-provider/src/main/java/example/IdentityProviderTestExecutionListener.java
  11. 1 2
      servlet/spring-boot/java/saml2/identity-provider/src/main/resources/META-INF/spring.factories
  12. 2 2
      servlet/spring-boot/java/saml2/identity-provider/src/main/resources/docker/compose.yml
  13. 3 3
      servlet/spring-boot/java/saml2/login/build.gradle
  14. 3 0
      servlet/spring-boot/java/saml2/login/settings.gradle
  15. 0 70
      servlet/spring-boot/java/saml2/login/src/integTest/java/example/PreDockerComposeServerPortInitializer.java
  16. 0 1
      servlet/spring-boot/java/saml2/login/src/integTest/resources/META-INF/spring.factories
  17. 1 3
      servlet/spring-boot/java/saml2/login/src/main/resources/application.yml
  18. 3 3
      servlet/spring-boot/java/saml2/refreshable-metadata/build.gradle
  19. 3 0
      servlet/spring-boot/java/saml2/refreshable-metadata/settings.gradle
  20. 0 70
      servlet/spring-boot/java/saml2/refreshable-metadata/src/integTest/java/example/PreDockerComposeServerPortInitializer.java
  21. 0 1
      servlet/spring-boot/java/saml2/refreshable-metadata/src/integTest/java/example/Saml2LoginApplicationITests.java
  22. 0 1
      servlet/spring-boot/java/saml2/refreshable-metadata/src/integTest/resources/META-INF/spring.factories
  23. 1 3
      servlet/spring-boot/java/saml2/refreshable-metadata/src/main/resources/application.yml

+ 0 - 12
build.gradle

@@ -29,18 +29,6 @@ allprojects {
 		options.compilerArgs.add("-parameters")
 	}
 
-	tasks.withType(Format).tap {
-		configureEach {
-			it.enabled = !it.identityPath.toString().contains("saml2")
-		}
-	}
-
-	tasks.withType(CheckFormat).tap {
-		configureEach {
-			it.enabled = !it.identityPath.toString().contains("saml2")
-		}
-	}
-
 	tasks.matching { it.name == 'formatAot' }.all { task ->
 		task.enabled = false
 	}

+ 3 - 4
servlet/spring-boot/java/saml2/custom-urls/build.gradle

@@ -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 {

+ 3 - 0
servlet/spring-boot/java/saml2/custom-urls/settings.gradle

@@ -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")

+ 0 - 70
servlet/spring-boot/java/saml2/custom-urls/src/integTest/java/example/PreDockerComposeServerPortInitializer.java

@@ -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;
-		}
-
-	}
-
-}

+ 0 - 1
servlet/spring-boot/java/saml2/custom-urls/src/integTest/resources/META-INF/spring.factories

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

+ 1 - 3
servlet/spring-boot/java/saml2/custom-urls/src/main/resources/application.yml

@@ -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

+ 3 - 2
servlet/spring-boot/java/saml2/identity-provider/build.gradle

@@ -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 {

+ 0 - 30
servlet/spring-boot/java/saml2/identity-provider/src/main/java/example/ComposeFilePropertyPlaceholderApplicationContextInitializer.java

@@ -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();
-	}
-
-}

+ 0 - 56
servlet/spring-boot/java/saml2/identity-provider/src/main/java/example/DockerProtocolResolver.java

@@ -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);
-		}
-	}
-
-}

+ 53 - 0
servlet/spring-boot/java/saml2/identity-provider/src/main/java/example/IdentityProviderTestExecutionListener.java

@@ -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();
+		}
+	}
+
+}

+ 1 - 2
servlet/spring-boot/java/saml2/identity-provider/src/main/resources/META-INF/spring.factories

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

+ 2 - 2
servlet/spring-boot/java/saml2/identity-provider/src/main/resources/docker/compose.yml

@@ -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

+ 3 - 3
servlet/spring-boot/java/saml2/login/build.gradle

@@ -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'

+ 3 - 0
servlet/spring-boot/java/saml2/login/settings.gradle

@@ -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")

+ 0 - 70
servlet/spring-boot/java/saml2/login/src/integTest/java/example/PreDockerComposeServerPortInitializer.java

@@ -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;
-		}
-
-	}
-
-}

+ 0 - 1
servlet/spring-boot/java/saml2/login/src/integTest/resources/META-INF/spring.factories

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

+ 1 - 3
servlet/spring-boot/java/saml2/login/src/main/resources/application.yml

@@ -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:

+ 3 - 3
servlet/spring-boot/java/saml2/refreshable-metadata/build.gradle

@@ -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'

+ 3 - 0
servlet/spring-boot/java/saml2/refreshable-metadata/settings.gradle

@@ -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")

+ 0 - 70
servlet/spring-boot/java/saml2/refreshable-metadata/src/integTest/java/example/PreDockerComposeServerPortInitializer.java

@@ -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;
-		}
-
-	}
-
-}

+ 0 - 1
servlet/spring-boot/java/saml2/refreshable-metadata/src/integTest/java/example/Saml2LoginApplicationITests.java

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

+ 0 - 1
servlet/spring-boot/java/saml2/refreshable-metadata/src/integTest/resources/META-INF/spring.factories

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

+ 1 - 3
servlet/spring-boot/java/saml2/refreshable-metadata/src/main/resources/application.yml

@@ -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: