From b49ccbb0c2e36e105300ed38082290eb52a8f29d Mon Sep 17 00:00:00 2001 From: amparab Date: Mon, 15 Jan 2024 01:38:38 +0530 Subject: [PATCH 1/2] Improve toString of SslBundle implementations See gh-39137 --- .../autoconfigure/ssl/PropertiesSslBundle.java | 16 ++++++++++++++++ .../boot/web/server/WebServerSslBundle.java | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundle.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundle.java index b25b88ad14f..102703d142c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundle.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundle.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.ssl; +import java.util.Arrays; + import org.springframework.boot.autoconfigure.ssl.SslBundleProperties.Key; import org.springframework.boot.ssl.SslBundle; import org.springframework.boot.ssl.SslBundleKey; @@ -26,6 +28,7 @@ import org.springframework.boot.ssl.jks.JksSslStoreBundle; import org.springframework.boot.ssl.jks.JksSslStoreDetails; import org.springframework.boot.ssl.pem.PemSslStoreBundle; import org.springframework.boot.ssl.pem.PemSslStoreDetails; +import org.springframework.core.style.ToStringCreator; /** * {@link SslBundle} backed by {@link JksSslBundleProperties} or @@ -128,4 +131,17 @@ public final class PropertiesSslBundle implements SslBundle { properties.getPassword()); } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + creator.append("key-alias", this.key.getAlias()); + creator.append("protocol", this.protocol); + creator.append("keystore-type", this.stores.getKeyStore().getType()); + creator.append("truststore-type", + (this.stores.getTrustStore() != null) ? this.stores.getTrustStore().getType() : ""); + creator.append("ciphers", Arrays.toString(this.options.getCiphers())); + creator.append("enabled-protocols", Arrays.toString(this.options.getEnabledProtocols())); + return creator.toString(); + } + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerSslBundle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerSslBundle.java index 0e10ace7fda..48865151f7e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerSslBundle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerSslBundle.java @@ -17,6 +17,7 @@ package org.springframework.boot.web.server; import java.security.KeyStore; +import java.util.Arrays; import org.springframework.boot.ssl.NoSuchSslBundleException; import org.springframework.boot.ssl.SslBundle; @@ -29,6 +30,7 @@ import org.springframework.boot.ssl.jks.JksSslStoreBundle; import org.springframework.boot.ssl.jks.JksSslStoreDetails; import org.springframework.boot.ssl.pem.PemSslStoreBundle; import org.springframework.boot.ssl.pem.PemSslStoreDetails; +import org.springframework.core.style.ToStringCreator; import org.springframework.util.Assert; import org.springframework.util.StringUtils; import org.springframework.util.function.ThrowingSupplier; @@ -284,4 +286,17 @@ public final class WebServerSslBundle implements SslBundle { } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + creator.append("key-alias", this.key.getAlias()); + creator.append("protocol", this.protocol); + creator.append("keystore-type", this.stores.getKeyStore().getType()); + creator.append("truststore-type", + (this.stores.getTrustStore() != null) ? this.stores.getTrustStore().getType() : ""); + creator.append("ciphers", Arrays.toString(this.options.getCiphers())); + creator.append("enabled-protocols", Arrays.toString(this.options.getEnabledProtocols())); + return creator.toString(); + } + } From a7d52226d5488ba7e37f852538be030b66026fa4 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 17 Jan 2024 11:37:37 +0000 Subject: [PATCH 2/2] Polish "Improve toString of SslBundle implementations" See gh-39137 --- .../ssl/PropertiesSslBundle.java | 13 ++---- .../springframework/boot/ssl/SslBundle.java | 13 +++++- .../boot/ssl/SslBundleKey.java | 11 ++++- .../springframework/boot/ssl/SslOptions.java | 12 +++++- .../boot/ssl/SslStoreBundle.java | 13 +++++- .../boot/ssl/jks/JksSslStoreBundle.java | 13 +++++- .../boot/ssl/pem/PemSslStoreBundle.java | 12 +++++- .../boot/web/server/WebServerSslBundle.java | 42 +++++++++++++------ 8 files changed, 101 insertions(+), 28 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundle.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundle.java index 102703d142c..4222a21609f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundle.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundle.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -16,8 +16,6 @@ package org.springframework.boot.autoconfigure.ssl; -import java.util.Arrays; - import org.springframework.boot.autoconfigure.ssl.SslBundleProperties.Key; import org.springframework.boot.ssl.SslBundle; import org.springframework.boot.ssl.SslBundleKey; @@ -134,13 +132,10 @@ public final class PropertiesSslBundle implements SslBundle { @Override public String toString() { ToStringCreator creator = new ToStringCreator(this); - creator.append("key-alias", this.key.getAlias()); + creator.append("key", this.key); + creator.append("options", this.options); creator.append("protocol", this.protocol); - creator.append("keystore-type", this.stores.getKeyStore().getType()); - creator.append("truststore-type", - (this.stores.getTrustStore() != null) ? this.stores.getTrustStore().getType() : ""); - creator.append("ciphers", Arrays.toString(this.options.getCiphers())); - creator.append("enabled-protocols", Arrays.toString(this.options.getEnabledProtocols())); + creator.append("stores", this.stores); return creator.toString(); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslBundle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslBundle.java index 790b225df7a..218e44b68f6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslBundle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslBundle.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -20,6 +20,7 @@ import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; +import org.springframework.core.style.ToStringCreator; import org.springframework.util.StringUtils; /** @@ -160,6 +161,16 @@ public interface SslBundle { return managersToUse; } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + creator.append("key", getKey()); + creator.append("options", getOptions()); + creator.append("protocol", getProtocol()); + creator.append("stores", getStores()); + return creator.toString(); + } + }; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslBundleKey.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslBundleKey.java index cf941302002..13d095f9bf4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslBundleKey.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslBundleKey.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -19,6 +19,7 @@ package org.springframework.boot.ssl; import java.security.KeyStore; import java.security.KeyStoreException; +import org.springframework.core.style.ToStringCreator; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -94,6 +95,14 @@ public interface SslBundleKey { return alias; } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + creator.append("alias", alias); + creator.append("password", (password != null) ? "******" : null); + return creator.toString(); + } + }; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslOptions.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslOptions.java index d377d749efb..d45e9163a34 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslOptions.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslOptions.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -24,6 +24,8 @@ import java.util.Set; import javax.net.ssl.SSLEngine; +import org.springframework.core.style.ToStringCreator; + /** * Configuration options that should be applied when establishing an SSL connection. * @@ -81,6 +83,14 @@ public interface SslOptions { return enabledProtocols; } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + creator.append("ciphers", ciphers); + creator.append("enabledProtocols", enabledProtocols); + return creator.toString(); + } + }; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslStoreBundle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslStoreBundle.java index a5f5a0e7d88..c4a543121d2 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslStoreBundle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslStoreBundle.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -18,6 +18,8 @@ package org.springframework.boot.ssl; import java.security.KeyStore; +import org.springframework.core.style.ToStringCreator; + /** * A bundle of key and trust stores that can be used to establish an SSL connection. * @@ -75,6 +77,15 @@ public interface SslStoreBundle { return keyStorePassword; } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + creator.append("keyStore.type", (keyStore != null) ? keyStore.getType() : "none"); + creator.append("keyStorePassword", (keyStorePassword != null) ? "******" : null); + creator.append("trustStore.type", (trustStore != null) ? trustStore.getType() : "none"); + return creator.toString(); + } + }; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/jks/JksSslStoreBundle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/jks/JksSslStoreBundle.java index ef0b924ffb2..8f475f1c833 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/jks/JksSslStoreBundle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/jks/JksSslStoreBundle.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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,6 +26,7 @@ import java.security.NoSuchProviderException; import java.security.cert.CertificateException; import org.springframework.boot.ssl.SslStoreBundle; +import org.springframework.core.style.ToStringCreator; import org.springframework.util.Assert; import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; @@ -123,4 +124,14 @@ public class JksSslStoreBundle implements SslStoreBundle { } } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + creator.append("keyStore.type", (this.keyStore != null) ? this.keyStore.getType() : "none"); + String keyStorePassword = getKeyStorePassword(); + creator.append("keyStorePassword", (keyStorePassword != null) ? "******" : null); + creator.append("trustStore.type", (this.trustStore != null) ? this.trustStore.getType() : "none"); + return creator.toString(); + } + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemSslStoreBundle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemSslStoreBundle.java index 2514798e51e..da711770110 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemSslStoreBundle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemSslStoreBundle.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -22,6 +22,7 @@ import java.security.PrivateKey; import java.security.cert.X509Certificate; import org.springframework.boot.ssl.SslStoreBundle; +import org.springframework.core.style.ToStringCreator; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -110,4 +111,13 @@ public class PemSslStoreBundle implements SslStoreBundle { } } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + creator.append("keyStore.type", (this.keyStore != null) ? this.keyStore.getType() : "none"); + creator.append("keyStorePassword", null); + creator.append("trustStore.type", (this.trustStore != null) ? this.trustStore.getType() : "none"); + return creator.toString(); + } + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerSslBundle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerSslBundle.java index 48865151f7e..c9fc5e456ce 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerSslBundle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerSslBundle.java @@ -17,7 +17,6 @@ package org.springframework.boot.web.server; import java.security.KeyStore; -import java.util.Arrays; import org.springframework.boot.ssl.NoSuchSslBundleException; import org.springframework.boot.ssl.SslBundle; @@ -225,6 +224,16 @@ public final class WebServerSslBundle implements SslBundle { || (ssl.getTrustStoreType() != null && ssl.getTrustStoreType().equals("PKCS11"))); } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + creator.append("key", this.key); + creator.append("protocol", this.protocol); + creator.append("stores", this.stores); + creator.append("options", this.options); + return creator.toString(); + } + /** * Class to adapt a {@link SslStoreProvider} into a {@link SslStoreBundle}. */ @@ -252,6 +261,17 @@ public final class WebServerSslBundle implements SslBundle { return ThrowingSupplier.of(this.sslStoreProvider::getTrustStore).get(); } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + KeyStore keyStore = getKeyStore(); + creator.append("keyStore.type", (keyStore != null) ? keyStore.getType() : "none"); + creator.append("keyStorePassword", null); + KeyStore trustStore = getTrustStore(); + creator.append("trustStore.type", (trustStore != null) ? trustStore.getType() : "none"); + return creator.toString(); + } + } private static final class WebServerSslStoreBundle implements SslStoreBundle { @@ -284,19 +304,15 @@ public final class WebServerSslBundle implements SslBundle { return this.keyStorePassword; } - } + @Override + public String toString() { + ToStringCreator creator = new ToStringCreator(this); + creator.append("keyStore.type", (this.keyStore != null) ? this.keyStore.getType() : "none"); + creator.append("keyStorePassword", (this.keyStorePassword != null) ? "******" : null); + creator.append("trustStore.type", (this.trustStore != null) ? this.trustStore.getType() : "none"); + return creator.toString(); + } - @Override - public String toString() { - ToStringCreator creator = new ToStringCreator(this); - creator.append("key-alias", this.key.getAlias()); - creator.append("protocol", this.protocol); - creator.append("keystore-type", this.stores.getKeyStore().getType()); - creator.append("truststore-type", - (this.stores.getTrustStore() != null) ? this.stores.getTrustStore().getType() : ""); - creator.append("ciphers", Arrays.toString(this.options.getCiphers())); - creator.append("enabled-protocols", Arrays.toString(this.options.getEnabledProtocols())); - return creator.toString(); } }