Skip to content

Commit

Permalink
Merge branch '2.3.x' into 2.4.x
Browse files Browse the repository at this point in the history
Closes gh-25922
  • Loading branch information
snicoll committed Apr 8, 2021
2 parents 4d52fe6 + 1d61da1 commit c6205f1
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.boot.autoconfigure.couchbase;

import java.io.InputStream;
import java.net.URL;
import java.security.KeyStore;

Expand Down Expand Up @@ -107,7 +108,9 @@ private TrustManagerFactory getTrustManagerFactory(CouchbaseProperties.Ssl ssl)
private KeyStore loadKeyStore(String resource, String keyStorePassword) throws Exception {
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
URL url = ResourceUtils.getURL(resource);
store.load(url.openStream(), (keyStorePassword != null) ? keyStorePassword.toCharArray() : null);
try (InputStream stream = url.openStream()) {
store.load(stream, (keyStorePassword != null) ? keyStorePassword.toCharArray() : null);
}
return store;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.boot.web.embedded.netty;

import java.io.InputStream;
import java.net.Socket;
import java.net.URL;
import java.security.InvalidAlgorithmParameterException;
Expand Down Expand Up @@ -170,7 +171,9 @@ private KeyStore loadStore(String type, String provider, String resource, String
KeyStore store = (provider != null) ? KeyStore.getInstance(type, provider) : KeyStore.getInstance(type);
try {
URL url = ResourceUtils.getURL(resource);
store.load(url.openStream(), (password != null) ? password.toCharArray() : null);
try (InputStream stream = url.openStream()) {
store.load(stream, (password != null) ? password.toCharArray() : null);
}
return store;
}
catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.boot.web.embedded.undertow;

import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
Expand Down Expand Up @@ -181,7 +182,9 @@ private KeyStore loadStore(String type, String provider, String resource, String
KeyStore store = (provider != null) ? KeyStore.getInstance(type, provider) : KeyStore.getInstance(type);
try {
URL url = ResourceUtils.getURL(resource);
store.load(url.openStream(), (password != null) ? password.toCharArray() : null);
try (InputStream stream = url.openStream()) {
store.load(stream, (password != null) ? password.toCharArray() : null);
}
return store;
}
catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
Expand Down Expand Up @@ -220,8 +220,8 @@ void trustStorePasswordIsNotSetWhenNull() {
private KeyStore loadStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
KeyStore keyStore = KeyStore.getInstance("JKS");
Resource resource = new ClassPathResource("test.jks");
try (InputStream inputStream = resource.getInputStream()) {
keyStore.load(inputStream, "secret".toCharArray());
try (InputStream stream = resource.getInputStream()) {
keyStore.load(stream, "secret".toCharArray());
return keyStore;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package org.springframework.boot.web.reactive.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
Expand Down Expand Up @@ -226,7 +226,9 @@ void sslWantsClientAuthenticationSucceedsWithoutClientCertificate() {

protected ReactorClientHttpConnector buildTrustAllSslWithClientKeyConnector() throws Exception {
KeyStore clientKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
clientKeyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray());
try (InputStream stream = new FileInputStream("src/test/resources/test.jks")) {
clientKeyStore.load(stream, "secret".toCharArray());
}
KeyManagerFactory clientKeyManagerFactory = KeyManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm());
clientKeyManagerFactory.init(clientKeyStore, "password".toCharArray());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-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.
Expand All @@ -16,8 +16,8 @@

package org.springframework.boot.web.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;

Expand All @@ -43,7 +43,9 @@ class SslConfigurationValidatorTests {
@BeforeEach
void loadKeystore() throws Exception {
this.keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
this.keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray());
try (InputStream stream = new FileInputStream("src/test/resources/test.jks")) {
this.keyStore.load(stream, "secret".toCharArray());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.boot.web.servlet.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -126,6 +125,7 @@
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.server.Session.SessionTrackingMode;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -543,7 +543,7 @@ void pkcs12KeyStoreAndTrustStore() throws Exception {
this.webServer = factory.getWebServer();
this.webServer.start();
KeyStore keyStore = KeyStore.getInstance("pkcs12");
keyStore.load(new FileInputStream(new File("src/test/resources/test.p12")), "secret".toCharArray());
loadStore(keyStore, new FileSystemResource("src/test/resources/test.p12"));
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "secret".toCharArray()).build());
Expand All @@ -561,7 +561,7 @@ void sslNeedsClientAuthenticationSucceedsWithClientCertificate() throws Exceptio
this.webServer = factory.getWebServer();
this.webServer.start();
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray());
loadStore(keyStore, new FileSystemResource("src/test/resources/test.jks"));
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "password".toCharArray()).build());
Expand Down Expand Up @@ -594,7 +594,7 @@ void sslWantsClientAuthenticationSucceedsWithClientCertificate() throws Exceptio
this.webServer = factory.getWebServer();
this.webServer.start();
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray());
loadStore(keyStore, new FileSystemResource("src/test/resources/test.jks"));
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "password".toCharArray()).build());
Expand Down Expand Up @@ -632,7 +632,7 @@ void sslWithCustomSslStoreProvider() throws Exception {
this.webServer = factory.getWebServer();
this.webServer.start();
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray());
loadStore(keyStore, new FileSystemResource("src/test/resources/test.jks"));
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "password".toCharArray()).build());
Expand Down Expand Up @@ -1357,9 +1357,14 @@ protected final void doWithBlockedPort(BlockedPortAction action) throws Exceptio
private KeyStore loadStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
KeyStore keyStore = KeyStore.getInstance("JKS");
Resource resource = new ClassPathResource("test.jks");
try (InputStream inputStream = resource.getInputStream()) {
keyStore.load(inputStream, "secret".toCharArray());
return keyStore;
loadStore(keyStore, resource);
return keyStore;
}

private void loadStore(KeyStore keyStore, Resource resource)
throws IOException, NoSuchAlgorithmException, CertificateException {
try (InputStream stream = resource.getInputStream()) {
keyStore.load(stream, "secret".toCharArray());
}
}

Expand Down

0 comments on commit c6205f1

Please sign in to comment.