Polish "Verify ssl key alias on server startup"

See gh-19202
This commit is contained in:
Madhura Bhave 2020-02-11 17:03:50 -08:00
parent e351605962
commit ac91f14f05
9 changed files with 31 additions and 32 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -37,8 +37,8 @@ import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.springframework.boot.web.server.Http2;
import org.springframework.boot.web.server.Ssl;
import org.springframework.boot.web.server.SslConfigurationValidator;
import org.springframework.boot.web.server.SslStoreProvider;
import org.springframework.boot.web.server.SslUtils;
import org.springframework.boot.web.server.WebServerException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@ -50,6 +50,7 @@ import org.springframework.util.ResourceUtils;
*
* @author Brian Clozel
* @author Olivier Lamy
* @author Chris Bono
*/
class SslServerCustomizer implements JettyServerCustomizer {
@ -245,7 +246,7 @@ class SslServerCustomizer implements JettyServerCustomizer {
@Override
protected void doStart() throws Exception {
super.doStart();
SslUtils.assertStoreContainsAlias(this.sslContextFactory.getKeyStore(), this.keyAlias);
SslConfigurationValidator.validateKeyAlias(this.sslContextFactory.getKeyStore(), this.keyAlias);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -43,8 +43,8 @@ import reactor.netty.tcp.SslProvider;
import org.springframework.boot.web.server.Http2;
import org.springframework.boot.web.server.Ssl;
import org.springframework.boot.web.server.SslConfigurationValidator;
import org.springframework.boot.web.server.SslStoreProvider;
import org.springframework.boot.web.server.SslUtils;
import org.springframework.boot.web.server.WebServerException;
import org.springframework.util.ResourceUtils;
@ -107,8 +107,7 @@ public class SslServerCustomizer implements NettyServerCustomizer {
protected KeyManagerFactory getKeyManagerFactory(Ssl ssl, SslStoreProvider sslStoreProvider) {
try {
KeyStore keyStore = getKeyStore(ssl, sslStoreProvider);
SslUtils.assertStoreContainsAlias(keyStore, ssl.getKeyAlias());
SslConfigurationValidator.validateKeyAlias(keyStore, ssl.getKeyAlias());
KeyManagerFactory keyManagerFactory = (ssl.getKeyAlias() == null)
? KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
: new ConfigurableAliasKeyManagerFactory(ssl.getKeyAlias(),

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -40,8 +40,8 @@ import org.xnio.Sequence;
import org.xnio.SslClientAuthMode;
import org.springframework.boot.web.server.Ssl;
import org.springframework.boot.web.server.SslConfigurationValidator;
import org.springframework.boot.web.server.SslStoreProvider;
import org.springframework.boot.web.server.SslUtils;
import org.springframework.boot.web.server.WebServerException;
import org.springframework.util.ResourceUtils;
@ -108,8 +108,7 @@ class SslBuilderCustomizer implements UndertowBuilderCustomizer {
private KeyManager[] getKeyManagers(Ssl ssl, SslStoreProvider sslStoreProvider) {
try {
KeyStore keyStore = getKeyStore(ssl, sslStoreProvider);
SslUtils.assertStoreContainsAlias(keyStore, ssl.getKeyAlias());
SslConfigurationValidator.validateKeyAlias(keyStore, ssl.getKeyAlias());
KeyManagerFactory keyManagerFactory = KeyManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm());
char[] keyPassword = (ssl.getKeyPassword() != null) ? ssl.getKeyPassword().toCharArray() : null;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -26,14 +26,14 @@ import org.springframework.util.StringUtils;
* Provides utilities around SSL.
*
* @author Chris Bono
* @since 2.1.x
* @since 2.1.13
*/
public final class SslUtils {
public final class SslConfigurationValidator {
private SslUtils() {
private SslConfigurationValidator() {
}
public static void assertStoreContainsAlias(KeyStore keyStore, String keyAlias) {
public static void validateKeyAlias(KeyStore keyStore, String keyAlias) {
if (!StringUtils.isEmpty(keyAlias)) {
try {
Assert.state(keyStore.containsAlias(keyAlias),

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.

View File

@ -27,12 +27,12 @@ import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Tests for {@link SslUtils}.
* Tests for {@link SslConfigurationValidator}.
*
* @author Chris Bono
*/
public class SslUtilsTest {
public class SslConfigurationValidatorTest {
private static final String VALID_ALIAS = "test-alias";
@ -47,31 +47,31 @@ public class SslUtilsTest {
}
@Test
public void assertStoreContainsAliasPassesWhenAliasFound() throws KeyStoreException {
SslUtils.assertStoreContainsAlias(this.keyStore, VALID_ALIAS);
public void validateKeyAliasWhenAliasFoundShouldNotFail() {
SslConfigurationValidator.validateKeyAlias(this.keyStore, VALID_ALIAS);
}
@Test
public void assertStoreContainsAliasPassesWhenNullAlias() throws KeyStoreException {
SslUtils.assertStoreContainsAlias(this.keyStore, null);
public void validateKeyAliasWhenNullAliasShouldNotFail() {
SslConfigurationValidator.validateKeyAlias(this.keyStore, null);
}
@Test
public void assertStoreContainsAliasPassesWhenEmptyAlias() throws KeyStoreException {
SslUtils.assertStoreContainsAlias(this.keyStore, "");
public void validateKeyAliasWhenEmptyAliasShouldNotFail() {
SslConfigurationValidator.validateKeyAlias(this.keyStore, "");
}
@Test
public void assertStoreContainsAliasFailsWhenAliasNotFound() throws KeyStoreException {
assertThatThrownBy(() -> SslUtils.assertStoreContainsAlias(this.keyStore, INVALID_ALIAS))
public void validateKeyAliasWhenAliasNotFoundShouldThrowException() {
assertThatThrownBy(() -> SslConfigurationValidator.validateKeyAlias(this.keyStore, INVALID_ALIAS))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Keystore does not contain specified alias '" + INVALID_ALIAS + "'");
}
@Test
public void assertStoreContainsAliasFailsWhenKeyStoreThrowsExceptionOnContains() throws KeyStoreException {
public void validateKeyAliasWhenKeyStoreThrowsExceptionOnContains() throws KeyStoreException {
KeyStore uninitializedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
assertThatThrownBy(() -> SslUtils.assertStoreContainsAlias(uninitializedKeyStore, "alias"))
assertThatThrownBy(() -> SslConfigurationValidator.validateKeyAlias(uninitializedKeyStore, "alias"))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Could not determine if keystore contains alias 'alias'");
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.