Skip to content

Commit 4949ffd

Browse files
committed
Merge pull request #39137 from amparab
* gh-39137: Polish "Improve toString of SslBundle implementations" Improve toString of SslBundle implementations Closes gh-39137
2 parents f66fd0e + a7d5222 commit 4949ffd

File tree

8 files changed

+111
-7
lines changed

8 files changed

+111
-7
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundle.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.ssl.jks.JksSslStoreDetails;
2727
import org.springframework.boot.ssl.pem.PemSslStoreBundle;
2828
import org.springframework.boot.ssl.pem.PemSslStoreDetails;
29+
import org.springframework.core.style.ToStringCreator;
2930

3031
/**
3132
* {@link SslBundle} backed by {@link JksSslBundleProperties} or
@@ -128,4 +129,14 @@ private static JksSslStoreDetails asStoreDetails(JksSslBundleProperties.Store pr
128129
properties.getPassword());
129130
}
130131

132+
@Override
133+
public String toString() {
134+
ToStringCreator creator = new ToStringCreator(this);
135+
creator.append("key", this.key);
136+
creator.append("options", this.options);
137+
creator.append("protocol", this.protocol);
138+
creator.append("stores", this.stores);
139+
return creator.toString();
140+
}
141+
131142
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslBundle.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import javax.net.ssl.SSLContext;
2121
import javax.net.ssl.TrustManager;
2222

23+
import org.springframework.core.style.ToStringCreator;
2324
import org.springframework.util.StringUtils;
2425

2526
/**
@@ -160,6 +161,16 @@ public SslManagerBundle getManagers() {
160161
return managersToUse;
161162
}
162163

164+
@Override
165+
public String toString() {
166+
ToStringCreator creator = new ToStringCreator(this);
167+
creator.append("key", getKey());
168+
creator.append("options", getOptions());
169+
creator.append("protocol", getProtocol());
170+
creator.append("stores", getStores());
171+
return creator.toString();
172+
}
173+
163174
};
164175
}
165176

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslBundleKey.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.security.KeyStore;
2020
import java.security.KeyStoreException;
2121

22+
import org.springframework.core.style.ToStringCreator;
2223
import org.springframework.util.Assert;
2324
import org.springframework.util.StringUtils;
2425

@@ -94,6 +95,14 @@ public String getAlias() {
9495
return alias;
9596
}
9697

98+
@Override
99+
public String toString() {
100+
ToStringCreator creator = new ToStringCreator(this);
101+
creator.append("alias", alias);
102+
creator.append("password", (password != null) ? "******" : null);
103+
return creator.toString();
104+
}
105+
97106
};
98107
}
99108

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslOptions.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@
2424

2525
import javax.net.ssl.SSLEngine;
2626

27+
import org.springframework.core.style.ToStringCreator;
28+
2729
/**
2830
* Configuration options that should be applied when establishing an SSL connection.
2931
*
@@ -81,6 +83,14 @@ public String[] getEnabledProtocols() {
8183
return enabledProtocols;
8284
}
8385

86+
@Override
87+
public String toString() {
88+
ToStringCreator creator = new ToStringCreator(this);
89+
creator.append("ciphers", ciphers);
90+
creator.append("enabledProtocols", enabledProtocols);
91+
return creator.toString();
92+
}
93+
8494
};
8595
}
8696

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/SslStoreBundle.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818

1919
import java.security.KeyStore;
2020

21+
import org.springframework.core.style.ToStringCreator;
22+
2123
/**
2224
* A bundle of key and trust stores that can be used to establish an SSL connection.
2325
*
@@ -75,6 +77,15 @@ public String getKeyStorePassword() {
7577
return keyStorePassword;
7678
}
7779

80+
@Override
81+
public String toString() {
82+
ToStringCreator creator = new ToStringCreator(this);
83+
creator.append("keyStore.type", (keyStore != null) ? keyStore.getType() : "none");
84+
creator.append("keyStorePassword", (keyStorePassword != null) ? "******" : null);
85+
creator.append("trustStore.type", (trustStore != null) ? trustStore.getType() : "none");
86+
return creator.toString();
87+
}
88+
7889
};
7990
}
8091

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/jks/JksSslStoreBundle.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import java.security.cert.CertificateException;
2727

2828
import org.springframework.boot.ssl.SslStoreBundle;
29+
import org.springframework.core.style.ToStringCreator;
2930
import org.springframework.util.Assert;
3031
import org.springframework.util.ResourceUtils;
3132
import org.springframework.util.StringUtils;
@@ -123,4 +124,14 @@ private void loadKeyStore(KeyStore store, String location, char[] password) {
123124
}
124125
}
125126

127+
@Override
128+
public String toString() {
129+
ToStringCreator creator = new ToStringCreator(this);
130+
creator.append("keyStore.type", (this.keyStore != null) ? this.keyStore.getType() : "none");
131+
String keyStorePassword = getKeyStorePassword();
132+
creator.append("keyStorePassword", (keyStorePassword != null) ? "******" : null);
133+
creator.append("trustStore.type", (this.trustStore != null) ? this.trustStore.getType() : "none");
134+
return creator.toString();
135+
}
136+
126137
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemSslStoreBundle.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.security.cert.X509Certificate;
2323

2424
import org.springframework.boot.ssl.SslStoreBundle;
25+
import org.springframework.core.style.ToStringCreator;
2526
import org.springframework.util.Assert;
2627
import org.springframework.util.StringUtils;
2728

@@ -110,4 +111,13 @@ private void addCertificates(KeyStore keyStore, X509Certificate[] certificates,
110111
}
111112
}
112113

114+
@Override
115+
public String toString() {
116+
ToStringCreator creator = new ToStringCreator(this);
117+
creator.append("keyStore.type", (this.keyStore != null) ? this.keyStore.getType() : "none");
118+
creator.append("keyStorePassword", null);
119+
creator.append("trustStore.type", (this.trustStore != null) ? this.trustStore.getType() : "none");
120+
return creator.toString();
121+
}
122+
113123
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerSslBundle.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.ssl.jks.JksSslStoreDetails;
3030
import org.springframework.boot.ssl.pem.PemSslStoreBundle;
3131
import org.springframework.boot.ssl.pem.PemSslStoreDetails;
32+
import org.springframework.core.style.ToStringCreator;
3233
import org.springframework.util.Assert;
3334
import org.springframework.util.StringUtils;
3435
import org.springframework.util.function.ThrowingSupplier;
@@ -223,6 +224,16 @@ private static boolean hasJksTrustStoreProperties(Ssl ssl) {
223224
|| (ssl.getTrustStoreType() != null && ssl.getTrustStoreType().equals("PKCS11")));
224225
}
225226

227+
@Override
228+
public String toString() {
229+
ToStringCreator creator = new ToStringCreator(this);
230+
creator.append("key", this.key);
231+
creator.append("protocol", this.protocol);
232+
creator.append("stores", this.stores);
233+
creator.append("options", this.options);
234+
return creator.toString();
235+
}
236+
226237
/**
227238
* Class to adapt a {@link SslStoreProvider} into a {@link SslStoreBundle}.
228239
*/
@@ -250,6 +261,17 @@ public KeyStore getTrustStore() {
250261
return ThrowingSupplier.of(this.sslStoreProvider::getTrustStore).get();
251262
}
252263

264+
@Override
265+
public String toString() {
266+
ToStringCreator creator = new ToStringCreator(this);
267+
KeyStore keyStore = getKeyStore();
268+
creator.append("keyStore.type", (keyStore != null) ? keyStore.getType() : "none");
269+
creator.append("keyStorePassword", null);
270+
KeyStore trustStore = getTrustStore();
271+
creator.append("trustStore.type", (trustStore != null) ? trustStore.getType() : "none");
272+
return creator.toString();
273+
}
274+
253275
}
254276

255277
private static final class WebServerSslStoreBundle implements SslStoreBundle {
@@ -282,6 +304,15 @@ public String getKeyStorePassword() {
282304
return this.keyStorePassword;
283305
}
284306

307+
@Override
308+
public String toString() {
309+
ToStringCreator creator = new ToStringCreator(this);
310+
creator.append("keyStore.type", (this.keyStore != null) ? this.keyStore.getType() : "none");
311+
creator.append("keyStorePassword", (this.keyStorePassword != null) ? "******" : null);
312+
creator.append("trustStore.type", (this.trustStore != null) ? this.trustStore.getType() : "none");
313+
return creator.toString();
314+
}
315+
285316
}
286317

287318
}

0 commit comments

Comments
 (0)