2222import org .springframework .boot .Bootstrapper ;
2323import org .springframework .boot .context .properties .bind .BindHandler ;
2424import org .springframework .boot .context .properties .bind .Binder ;
25- import org .springframework .cloud .autoconfigure .EncryptionBootstrapAutoConfiguration ;
26- import org .springframework .cloud .bootstrap .TextEncryptorConfigurationPropertiesBindHandlerAdvisor .TextEncryptorBindHandler ;
2725import org .springframework .cloud .bootstrap .encrypt .KeyProperties ;
2826import org .springframework .cloud .bootstrap .encrypt .RsaProperties ;
2927import org .springframework .cloud .context .encrypt .EncryptorFactory ;
3028import org .springframework .core .env .Environment ;
3129import org .springframework .security .crypto .encrypt .TextEncryptor ;
30+ import org .springframework .security .rsa .crypto .KeyStoreKeyFactory ;
31+ import org .springframework .security .rsa .crypto .RsaSecretEncryptor ;
3232import org .springframework .util .ClassUtils ;
3333import org .springframework .util .StringUtils ;
3434
4040 */
4141public class TextEncryptorConfigBootstrapper implements Bootstrapper {
4242
43+ private static final boolean RSA_IS_PRESENT = ClassUtils
44+ .isPresent ("org.springframework.security.rsa.crypto.RsaSecretEncryptor" , null );
45+
4346 @ Override
4447 public void intitialize (BootstrapRegistry registry ) {
4548 if (!ClassUtils .isPresent ("org.springframework.security.crypto.encrypt.TextEncryptor" , null )) {
4649 return ;
4750 }
4851
4952 registry .registerIfAbsent (KeyProperties .class , context -> context .get (Binder .class )
50- .bind ("encrypt" , KeyProperties .class ).orElseGet (KeyProperties ::new ));
51- registry .registerIfAbsent (RsaProperties .class , context -> context .get (Binder .class )
52- .bind ("encrypt.rsa" , RsaProperties .class ).orElseGet (RsaProperties ::new ));
53+ .bind (KeyProperties .PREFIX , KeyProperties .class ).orElseGet (KeyProperties ::new ));
54+ if (RSA_IS_PRESENT ) {
55+ registry .registerIfAbsent (RsaProperties .class , context -> context .get (Binder .class )
56+ .bind (RsaProperties .PREFIX , RsaProperties .class ).orElseGet (RsaProperties ::new ));
57+ }
5358 registry .registerIfAbsent (TextEncryptor .class , context -> {
5459 KeyProperties keyProperties = context .get (KeyProperties .class );
5560 if (keysConfigured (keyProperties )) {
56- if (ClassUtils . isPresent ( "org.springframework.security.rsa.crypto.RsaSecretEncryptor" , null ) ) {
61+ if (RSA_IS_PRESENT ) {
5762 RsaProperties rsaProperties = context .get (RsaProperties .class );
58- return EncryptionBootstrapAutoConfiguration . rsaTextEncryptor (keyProperties , rsaProperties );
63+ return rsaTextEncryptor (keyProperties , rsaProperties );
5964 }
6065 return new EncryptorFactory (keyProperties .getSalt ()).create (keyProperties .getKey ());
6166 }
@@ -82,9 +87,11 @@ public void intitialize(BootstrapRegistry registry) {
8287 if (keyProperties != null ) {
8388 beanFactory .registerSingleton ("keyProperties" , keyProperties );
8489 }
85- RsaProperties rsaProperties = bootstrapContext .get (RsaProperties .class );
86- if (rsaProperties != null ) {
87- beanFactory .registerSingleton ("rsaProperties" , rsaProperties );
90+ if (RSA_IS_PRESENT ) {
91+ RsaProperties rsaProperties = bootstrapContext .get (RsaProperties .class );
92+ if (rsaProperties != null ) {
93+ beanFactory .registerSingleton ("rsaProperties" , rsaProperties );
94+ }
8895 }
8996 TextEncryptor textEncryptor = bootstrapContext .get (TextEncryptor .class );
9097 if (textEncryptor != null ) {
@@ -93,7 +100,23 @@ public void intitialize(BootstrapRegistry registry) {
93100 });
94101 }
95102
96- private boolean keysConfigured (KeyProperties properties ) {
103+ public static TextEncryptor rsaTextEncryptor (KeyProperties keyProperties , RsaProperties rsaProperties ) {
104+ KeyProperties .KeyStore keyStore = keyProperties .getKeyStore ();
105+ if (keyStore .getLocation () != null ) {
106+ if (keyStore .getLocation ().exists ()) {
107+ return new RsaSecretEncryptor (
108+ new KeyStoreKeyFactory (keyStore .getLocation (), keyStore .getPassword ().toCharArray ())
109+ .getKeyPair (keyStore .getAlias (), keyStore .getSecret ().toCharArray ()),
110+ rsaProperties .getAlgorithm (), rsaProperties .getSalt (), rsaProperties .isStrong ());
111+ }
112+
113+ throw new IllegalStateException ("Invalid keystore location" );
114+ }
115+
116+ return new EncryptorFactory (keyProperties .getSalt ()).create (keyProperties .getKey ());
117+ }
118+
119+ public static boolean keysConfigured (KeyProperties properties ) {
97120 if (hasProperty (properties .getKeyStore ().getLocation ())) {
98121 if (hasProperty (properties .getKeyStore ().getPassword ())) {
99122 return true ;
@@ -106,14 +129,14 @@ else if (hasProperty(properties.getKey())) {
106129 return false ;
107130 }
108131
109- private boolean hasProperty (Object value ) {
132+ static boolean hasProperty (Object value ) {
110133 if (value instanceof String ) {
111134 return StringUtils .hasText ((String ) value );
112135 }
113136 return value != null ;
114137 }
115138
116- private boolean isLegacyBootstrap (Environment environment ) {
139+ static boolean isLegacyBootstrap (Environment environment ) {
117140 boolean isLegacy = environment .getProperty ("spring.config.use-legacy-processing" , Boolean .class , false );
118141 boolean isBootstrapEnabled = environment .getProperty ("spring.cloud.bootstrap.enabled" , Boolean .class , false );
119142 return isLegacy || isBootstrapEnabled ;
@@ -126,7 +149,7 @@ private boolean isLegacyBootstrap(Environment environment) {
126149 * @author Dave Syer
127150 *
128151 */
129- protected static class FailsafeTextEncryptor implements TextEncryptor {
152+ public static class FailsafeTextEncryptor implements TextEncryptor {
130153
131154 @ Override
132155 public String encrypt (String text ) {
0 commit comments