Certificate array in pem (#583)

Fix a bug where previous trust manager effectively only read the first certificate in pem file.
This commit is contained in:
Tony Clarke
2018-10-05 18:30:21 -04:00
committed by Spencer Gibb
parent b27ea537ba
commit f1db5e063c
8 changed files with 146 additions and 8 deletions

View File

@@ -950,6 +950,7 @@ spring:
- cert2.pem
----
If the Spring Cloud Gateway is not provisioned with trusted certificates the default trust store is used (which can be overriden with system property javax.net.ssl.trustStore.
== Configuration

View File

@@ -25,11 +25,13 @@ import reactor.ipc.netty.resources.PoolResources;
import java.io.IOException;
import java.net.URL;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
@@ -231,20 +233,20 @@ public class HttpClientProperties {
try {
CertificateFactory certificateFactory = CertificateFactory
.getInstance("X.509");
ArrayList<X509Certificate> certs = new ArrayList<>();
ArrayList<Certificate> allCerts = new ArrayList<>();
for (String trustedCert : ssl.getTrustedX509Certificates()) {
try {
URL url = ResourceUtils.getURL(trustedCert);
X509Certificate cert = (X509Certificate) certificateFactory
.generateCertificate(url.openStream());
certs.add(cert);
Collection<? extends Certificate> certs = certificateFactory
.generateCertificates(url.openStream());
allCerts.addAll(certs);
}
catch (IOException e) {
throw new WebServerException(
"Could not load certificate '" + trustedCert + "'", e);
}
}
return certs.toArray(new X509Certificate[certs.size()]);
return allCerts.toArray(new X509Certificate[allCerts.size()]);
}
catch (CertificateException e1) {
throw new WebServerException("Could not load CertificateFactory X.509",

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2013-2017 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
*
* http://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.gateway.test.ssl;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
@ActiveProfiles("multi-cert-ssl")
public class MultiCertSSLTests extends SingleCertSSLTests {
}

View File

@@ -49,8 +49,8 @@ import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
@ActiveProfiles("ssl")
public class SSLTests extends BaseWebClientTests {
@ActiveProfiles("single-cert-ssl")
public class SingleCertSSLTests extends BaseWebClientTests {
@Before
public void setup() {

View File

@@ -16,7 +16,7 @@ spring:
httpclient:
ssl:
trustedX509Certificates:
- src/test/resources/scg-cert.pem
- src/test/resources/multi-cert.pem
default-filters:
- PrefixPath=/httpbin
routes:

View File

@@ -0,0 +1,38 @@
test:
hostport: httpbin.org:80
uri: lb://testservice
server:
ssl:
enabled: true
key-alias: scg
key-store-password: scg1234
key-store: classpath:scg-keystore.p12
key-store-type: PKCS12
spring:
cloud:
gateway:
httpclient:
ssl:
trustedX509Certificates:
- src/test/resources/single-cert.pem
default-filters:
- PrefixPath=/httpbin
routes:
- id: default_path_to_httpbin
uri: ${test.uri}
order: 10000
predicates:
- name: Path
args:
pattern: /**
logging:
level:
org.springframework.cloud.gateway: TRACE
org.springframework.http.server.reactive: DEBUG
org.springframework.web.reactive: DEBUG
reactor.ipc.netty: DEBUG
redisratelimiter: DEBUG

View File

@@ -0,0 +1,62 @@
-----BEGIN CERTIFICATE-----
MIIGKzCCBROgAwIBAgIQByMt0ja6e5B8OohEG0rsHzANBgkqhkiG9w0BAQsFADBN
MQswCQYDVQQGEwJ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