-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add client side TLS cert support #1679
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d56038e
add client side TLS cert support
f28ea27
Update BaseCertTest.java
67a6cbf
change format for check style
1cda318
Update pom.xml
c563011
add document for client side TLS
57df44c
Update pom.xml
c866e4f
Update spring-cloud-config.adoc
4ed2d42
Merge remote-tracking branch 'upstream/2.2.x' into 2.2.x
b5fff94
use tls common classes
c007a81
correct one test case about disable client side tls
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>spring-cloud-config-client-tls-tests</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Spring Cloud Config Client TLS Tests</name> | ||
|
||
<parent> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-config</artifactId> | ||
<version>2.2.5.BUILD-SNAPSHOT</version> | ||
<relativePath>..</relativePath> | ||
</parent> | ||
|
||
<url>https://spring.io</url> | ||
<description> | ||
<![CDATA[ | ||
This project is a Spring configuration client. | ||
]]> | ||
</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-config-server</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-config-client</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-logging</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-commons</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-context</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-annotations</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.retry</groupId> | ||
<artifactId>spring-retry</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-aop</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure-processor</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.bouncycastle</groupId> | ||
<artifactId>bcpkix-jdk15on</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
125 changes: 125 additions & 0 deletions
125
...fig-client-tls-tests/src/test/java/org/springframework/cloud/config/client/AppRunner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2018-2019 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.cloud.config.client; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.util.SocketUtils; | ||
|
||
public class AppRunner implements AutoCloseable { | ||
|
||
private Class<?> appClass; | ||
|
||
private Map<String, String> props; | ||
|
||
private ConfigurableApplicationContext app; | ||
|
||
public AppRunner(Class<?> appClass) { | ||
this.appClass = appClass; | ||
props = new LinkedHashMap<>(); | ||
} | ||
|
||
public void property(String key, String value) { | ||
props.put(key, value); | ||
} | ||
|
||
public void start() { | ||
if (app == null) { | ||
SpringApplicationBuilder builder = new SpringApplicationBuilder(appClass); | ||
builder.properties("spring.jmx.enabled=false"); | ||
builder.properties(String.format("server.port=%d", availabeTcpPort())); | ||
builder.properties(props()); | ||
|
||
app = builder.build().run(); | ||
} | ||
} | ||
|
||
private int availabeTcpPort() { | ||
return SocketUtils.findAvailableTcpPort(); | ||
} | ||
|
||
private String[] props() { | ||
List<String> result = new ArrayList<>(); | ||
|
||
for (String key : props.keySet()) { | ||
String value = props.get(key); | ||
result.add(String.format("%s=%s", key, value)); | ||
} | ||
|
||
return result.toArray(new String[0]); | ||
} | ||
|
||
public void stop() { | ||
if (app != null) { | ||
app.stop(); | ||
app = null; | ||
} | ||
} | ||
|
||
public ConfigurableApplicationContext app() { | ||
return app; | ||
} | ||
|
||
public String getProperty(String key) { | ||
return app.getEnvironment().getProperty(key); | ||
} | ||
|
||
public <T> T getBean(Class<T> type) { | ||
return app.getBean(type); | ||
} | ||
|
||
public ApplicationContext parent() { | ||
return app.getParent(); | ||
} | ||
|
||
public <T> Map<String, T> getParentBeans(Class<T> type) { | ||
return parent().getBeansOfType(type); | ||
} | ||
|
||
public int port() { | ||
if (app == null) { | ||
throw new RuntimeException("App is not running."); | ||
} | ||
return app.getEnvironment().getProperty("server.port", Integer.class, -1); | ||
} | ||
|
||
public String root() { | ||
if (app == null) { | ||
throw new RuntimeException("App is not running."); | ||
} | ||
|
||
String protocol = tlsEnabled() ? "https" : "http"; | ||
return String.format("%s://localhost:%d/", protocol, port()); | ||
} | ||
|
||
private boolean tlsEnabled() { | ||
return app.getEnvironment().getProperty("server.ssl.enabled", Boolean.class, | ||
false); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
stop(); | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
...-client-tls-tests/src/test/java/org/springframework/cloud/config/client/BaseCertTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2018-2019 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.cloud.config.client; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.OutputStream; | ||
import java.security.KeyStore; | ||
|
||
import org.junit.BeforeClass; | ||
|
||
public class BaseCertTest { | ||
|
||
protected static final String KEY_STORE_PASSWORD = "test-key-store-password"; | ||
|
||
protected static final String KEY_PASSWORD = "test-key-password"; | ||
|
||
protected static final String WRONG_PASSWORD = "test-wrong-password"; | ||
|
||
protected static File caCert; | ||
|
||
protected static File wrongCaCert; | ||
|
||
protected static File serverCert; | ||
|
||
protected static File clientCert; | ||
|
||
protected static File wrongClientCert; | ||
|
||
@BeforeClass | ||
public static void createCertificates() throws Exception { | ||
KeyTool tool = new KeyTool(); | ||
|
||
KeyAndCert ca = tool.createCA("MyCA"); | ||
KeyAndCert server = ca.sign("server"); | ||
KeyAndCert client = ca.sign("client"); | ||
|
||
caCert = saveCert(ca); | ||
serverCert = saveKeyAndCert(server); | ||
clientCert = saveKeyAndCert(client); | ||
|
||
KeyAndCert wrongCa = tool.createCA("WrongCA"); | ||
KeyAndCert wrongClient = wrongCa.sign("client"); | ||
|
||
wrongCaCert = saveCert(wrongCa); | ||
wrongClientCert = saveKeyAndCert(wrongClient); | ||
|
||
System.setProperty("javax.net.ssl.trustStore", caCert.getAbsolutePath()); | ||
System.setProperty("javax.net.ssl.trustStorePassword", KEY_STORE_PASSWORD); | ||
} | ||
|
||
private static File saveKeyAndCert(KeyAndCert keyCert) throws Exception { | ||
return saveKeyStore(keyCert.subject(), | ||
() -> keyCert.storeKeyAndCert(KEY_PASSWORD)); | ||
} | ||
|
||
private static File saveCert(KeyAndCert keyCert) throws Exception { | ||
return saveKeyStore(keyCert.subject(), () -> keyCert.storeCert()); | ||
} | ||
|
||
private static File saveKeyStore(String prefix, KeyStoreSupplier func) | ||
throws Exception { | ||
File result = File.createTempFile(prefix, ".p12"); | ||
result.deleteOnExit(); | ||
|
||
try (OutputStream output = new FileOutputStream(result)) { | ||
KeyStore store = func.createKeyStore(); | ||
store.store(output, KEY_STORE_PASSWORD.toCharArray()); | ||
} | ||
return result; | ||
} | ||
|
||
interface KeyStoreSupplier { | ||
|
||
KeyStore createKeyStore() throws Exception; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryanjbaxter move to test module