Commit ac91f14f authored by Madhura Bhave's avatar Madhura Bhave

Polish "Verify ssl key alias on server startup"

See gh-19202
parent e3516059
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; ...@@ -37,8 +37,8 @@ import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.springframework.boot.web.server.Http2; import org.springframework.boot.web.server.Http2;
import org.springframework.boot.web.server.Ssl; 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.SslStoreProvider;
import org.springframework.boot.web.server.SslUtils;
import org.springframework.boot.web.server.WebServerException; import org.springframework.boot.web.server.WebServerException;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
...@@ -50,6 +50,7 @@ import org.springframework.util.ResourceUtils; ...@@ -50,6 +50,7 @@ import org.springframework.util.ResourceUtils;
* *
* @author Brian Clozel * @author Brian Clozel
* @author Olivier Lamy * @author Olivier Lamy
* @author Chris Bono
*/ */
class SslServerCustomizer implements JettyServerCustomizer { class SslServerCustomizer implements JettyServerCustomizer {
...@@ -245,7 +246,7 @@ class SslServerCustomizer implements JettyServerCustomizer { ...@@ -245,7 +246,7 @@ class SslServerCustomizer implements JettyServerCustomizer {
@Override @Override
protected void doStart() throws Exception { protected void doStart() throws Exception {
super.doStart(); super.doStart();
SslUtils.assertStoreContainsAlias(this.sslContextFactory.getKeyStore(), this.keyAlias); SslConfigurationValidator.validateKeyAlias(this.sslContextFactory.getKeyStore(), this.keyAlias);
} }
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -43,8 +43,8 @@ import reactor.netty.tcp.SslProvider; ...@@ -43,8 +43,8 @@ import reactor.netty.tcp.SslProvider;
import org.springframework.boot.web.server.Http2; import org.springframework.boot.web.server.Http2;
import org.springframework.boot.web.server.Ssl; 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.SslStoreProvider;
import org.springframework.boot.web.server.SslUtils;
import org.springframework.boot.web.server.WebServerException; import org.springframework.boot.web.server.WebServerException;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
...@@ -107,8 +107,7 @@ public class SslServerCustomizer implements NettyServerCustomizer { ...@@ -107,8 +107,7 @@ public class SslServerCustomizer implements NettyServerCustomizer {
protected KeyManagerFactory getKeyManagerFactory(Ssl ssl, SslStoreProvider sslStoreProvider) { protected KeyManagerFactory getKeyManagerFactory(Ssl ssl, SslStoreProvider sslStoreProvider) {
try { try {
KeyStore keyStore = getKeyStore(ssl, sslStoreProvider); KeyStore keyStore = getKeyStore(ssl, sslStoreProvider);
SslUtils.assertStoreContainsAlias(keyStore, ssl.getKeyAlias()); SslConfigurationValidator.validateKeyAlias(keyStore, ssl.getKeyAlias());
KeyManagerFactory keyManagerFactory = (ssl.getKeyAlias() == null) KeyManagerFactory keyManagerFactory = (ssl.getKeyAlias() == null)
? KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()) ? KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
: new ConfigurableAliasKeyManagerFactory(ssl.getKeyAlias(), : new ConfigurableAliasKeyManagerFactory(ssl.getKeyAlias(),
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -40,8 +40,8 @@ import org.xnio.Sequence; ...@@ -40,8 +40,8 @@ import org.xnio.Sequence;
import org.xnio.SslClientAuthMode; import org.xnio.SslClientAuthMode;
import org.springframework.boot.web.server.Ssl; 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.SslStoreProvider;
import org.springframework.boot.web.server.SslUtils;
import org.springframework.boot.web.server.WebServerException; import org.springframework.boot.web.server.WebServerException;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
...@@ -108,8 +108,7 @@ class SslBuilderCustomizer implements UndertowBuilderCustomizer { ...@@ -108,8 +108,7 @@ class SslBuilderCustomizer implements UndertowBuilderCustomizer {
private KeyManager[] getKeyManagers(Ssl ssl, SslStoreProvider sslStoreProvider) { private KeyManager[] getKeyManagers(Ssl ssl, SslStoreProvider sslStoreProvider) {
try { try {
KeyStore keyStore = getKeyStore(ssl, sslStoreProvider); KeyStore keyStore = getKeyStore(ssl, sslStoreProvider);
SslUtils.assertStoreContainsAlias(keyStore, ssl.getKeyAlias()); SslConfigurationValidator.validateKeyAlias(keyStore, ssl.getKeyAlias());
KeyManagerFactory keyManagerFactory = KeyManagerFactory KeyManagerFactory keyManagerFactory = KeyManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm()); .getInstance(KeyManagerFactory.getDefaultAlgorithm());
char[] keyPassword = (ssl.getKeyPassword() != null) ? ssl.getKeyPassword().toCharArray() : null; char[] keyPassword = (ssl.getKeyPassword() != null) ? ssl.getKeyPassword().toCharArray() : null;
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,14 +26,14 @@ import org.springframework.util.StringUtils; ...@@ -26,14 +26,14 @@ import org.springframework.util.StringUtils;
* Provides utilities around SSL. * Provides utilities around SSL.
* *
* @author Chris Bono * @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)) { if (!StringUtils.isEmpty(keyAlias)) {
try { try {
Assert.state(keyStore.containsAlias(keyAlias), Assert.state(keyStore.containsAlias(keyAlias),
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
...@@ -27,12 +27,12 @@ import org.junit.Test; ...@@ -27,12 +27,12 @@ import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Tests for {@link SslUtils}. * Tests for {@link SslConfigurationValidator}.
* *
* @author Chris Bono * @author Chris Bono
*/ */
public class SslUtilsTest { public class SslConfigurationValidatorTest {
private static final String VALID_ALIAS = "test-alias"; private static final String VALID_ALIAS = "test-alias";
...@@ -47,31 +47,31 @@ public class SslUtilsTest { ...@@ -47,31 +47,31 @@ public class SslUtilsTest {
} }
@Test @Test
public void assertStoreContainsAliasPassesWhenAliasFound() throws KeyStoreException { public void validateKeyAliasWhenAliasFoundShouldNotFail() {
SslUtils.assertStoreContainsAlias(this.keyStore, VALID_ALIAS); SslConfigurationValidator.validateKeyAlias(this.keyStore, VALID_ALIAS);
} }
@Test @Test
public void assertStoreContainsAliasPassesWhenNullAlias() throws KeyStoreException { public void validateKeyAliasWhenNullAliasShouldNotFail() {
SslUtils.assertStoreContainsAlias(this.keyStore, null); SslConfigurationValidator.validateKeyAlias(this.keyStore, null);
} }
@Test @Test
public void assertStoreContainsAliasPassesWhenEmptyAlias() throws KeyStoreException { public void validateKeyAliasWhenEmptyAliasShouldNotFail() {
SslUtils.assertStoreContainsAlias(this.keyStore, ""); SslConfigurationValidator.validateKeyAlias(this.keyStore, "");
} }
@Test @Test
public void assertStoreContainsAliasFailsWhenAliasNotFound() throws KeyStoreException { public void validateKeyAliasWhenAliasNotFoundShouldThrowException() {
assertThatThrownBy(() -> SslUtils.assertStoreContainsAlias(this.keyStore, INVALID_ALIAS)) assertThatThrownBy(() -> SslConfigurationValidator.validateKeyAlias(this.keyStore, INVALID_ALIAS))
.isInstanceOf(IllegalStateException.class) .isInstanceOf(IllegalStateException.class)
.hasMessage("Keystore does not contain specified alias '" + INVALID_ALIAS + "'"); .hasMessage("Keystore does not contain specified alias '" + INVALID_ALIAS + "'");
} }
@Test @Test
public void assertStoreContainsAliasFailsWhenKeyStoreThrowsExceptionOnContains() throws KeyStoreException { public void validateKeyAliasWhenKeyStoreThrowsExceptionOnContains() throws KeyStoreException {
KeyStore uninitializedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); KeyStore uninitializedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
assertThatThrownBy(() -> SslUtils.assertStoreContainsAlias(uninitializedKeyStore, "alias")) assertThatThrownBy(() -> SslConfigurationValidator.validateKeyAlias(uninitializedKeyStore, "alias"))
.isInstanceOf(IllegalStateException.class) .isInstanceOf(IllegalStateException.class)
.hasMessage("Could not determine if keystore contains alias 'alias'"); .hasMessage("Could not determine if keystore contains alias 'alias'");
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment