Browse Source

Use existing PropertyUtils.bootstrapEnabled().

This will take into account if the bootstrap marker class is present and bring parity between ConditionalOnBootstrapDisabled and TextEncryptorConfigBootstrapper
pull/881/head
spencergibb 4 years ago
parent
commit
5bbe5331b7
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 26
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/TextEncryptorConfigBootstrapper.java
  2. 3
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/DecryptEnvironmentPostProcessor.java
  3. 32
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java
  4. 3
      spring-cloud-context/src/main/java/org/springframework/cloud/util/ConditionalOnBootstrapDisabled.java
  5. 14
      spring-cloud-context/src/main/java/org/springframework/cloud/util/PropertyUtils.java

26
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/TextEncryptorConfigBootstrapper.java

@ -22,13 +22,13 @@ import org.springframework.boot.BootstrapRegistry; @@ -22,13 +22,13 @@ import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.Bootstrapper;
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration;
import org.springframework.cloud.bootstrap.encrypt.KeyProperties;
import org.springframework.cloud.bootstrap.encrypt.RsaProperties;
import org.springframework.cloud.context.encrypt.EncryptorFactory;
import org.springframework.cloud.util.PropertyUtils;
import org.springframework.core.env.Environment;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
import org.springframework.security.rsa.crypto.RsaSecretEncryptor;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
@ -60,7 +60,7 @@ public class TextEncryptorConfigBootstrapper implements Bootstrapper { @@ -60,7 +60,7 @@ public class TextEncryptorConfigBootstrapper implements Bootstrapper {
if (keysConfigured(keyProperties)) {
if (RSA_IS_PRESENT) {
RsaProperties rsaProperties = context.get(RsaProperties.class);
return rsaTextEncryptor(keyProperties, rsaProperties);
return EncryptionBootstrapConfiguration.createTextEncryptor(keyProperties, rsaProperties);
}
return new EncryptorFactory(keyProperties.getSalt()).create(keyProperties.getKey());
}
@ -100,22 +100,6 @@ public class TextEncryptorConfigBootstrapper implements Bootstrapper { @@ -100,22 +100,6 @@ public class TextEncryptorConfigBootstrapper implements Bootstrapper {
});
}
public static TextEncryptor rsaTextEncryptor(KeyProperties keyProperties, RsaProperties rsaProperties) {
KeyProperties.KeyStore keyStore = keyProperties.getKeyStore();
if (keyStore.getLocation() != null) {
if (keyStore.getLocation().exists()) {
return new RsaSecretEncryptor(
new KeyStoreKeyFactory(keyStore.getLocation(), keyStore.getPassword().toCharArray())
.getKeyPair(keyStore.getAlias(), keyStore.getSecret().toCharArray()),
rsaProperties.getAlgorithm(), rsaProperties.getSalt(), rsaProperties.isStrong());
}
throw new IllegalStateException("Invalid keystore location");
}
return new EncryptorFactory(keyProperties.getSalt()).create(keyProperties.getKey());
}
public static boolean keysConfigured(KeyProperties properties) {
if (hasProperty(properties.getKeyStore().getLocation())) {
if (hasProperty(properties.getKeyStore().getPassword())) {
@ -137,8 +121,8 @@ public class TextEncryptorConfigBootstrapper implements Bootstrapper { @@ -137,8 +121,8 @@ public class TextEncryptorConfigBootstrapper implements Bootstrapper {
}
static boolean isLegacyBootstrap(Environment environment) {
boolean isLegacy = environment.getProperty("spring.config.use-legacy-processing", Boolean.class, false);
boolean isBootstrapEnabled = environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, false);
boolean isLegacy = PropertyUtils.useLegacyProcessing(environment);
boolean isBootstrapEnabled = PropertyUtils.bootstrapEnabled(environment);
return isLegacy || isBootstrapEnabled;
}

3
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/DecryptEnvironmentPostProcessor.java

@ -21,7 +21,6 @@ import java.util.Map; @@ -21,7 +21,6 @@ import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.cloud.bootstrap.TextEncryptorConfigBootstrapper;
import org.springframework.cloud.bootstrap.TextEncryptorConfigBootstrapper.FailsafeTextEncryptor;
import org.springframework.cloud.context.encrypt.EncryptorFactory;
import org.springframework.core.Ordered;
@ -85,7 +84,7 @@ public class DecryptEnvironmentPostProcessor extends AbstractEnvironmentDecrypt @@ -85,7 +84,7 @@ public class DecryptEnvironmentPostProcessor extends AbstractEnvironmentDecrypt
if (ClassUtils.isPresent("org.springframework.security.rsa.crypto.RsaSecretEncryptor", null)) {
RsaProperties rsaProperties = binder.bind(RsaProperties.PREFIX, RsaProperties.class)
.orElseGet(RsaProperties::new);
return TextEncryptorConfigBootstrapper.rsaTextEncryptor(keyProperties, rsaProperties);
return EncryptionBootstrapConfiguration.createTextEncryptor(keyProperties, rsaProperties);
}
return new EncryptorFactory(keyProperties.getSalt()).create(keyProperties.getKey());
}

32
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java

@ -68,6 +68,22 @@ public class EncryptionBootstrapConfiguration { @@ -68,6 +68,22 @@ public class EncryptionBootstrapConfiguration {
return listener;
}
public static TextEncryptor createTextEncryptor(KeyProperties keyProperties, RsaProperties rsaProperties) {
KeyStore keyStore = keyProperties.getKeyStore();
if (keyStore.getLocation() != null) {
if (keyStore.getLocation().exists()) {
return new RsaSecretEncryptor(
new KeyStoreKeyFactory(keyStore.getLocation(), keyStore.getPassword().toCharArray())
.getKeyPair(keyStore.getAlias(), keyStore.getSecret().toCharArray()),
rsaProperties.getAlgorithm(), rsaProperties.getSalt(), rsaProperties.isStrong());
}
throw new IllegalStateException("Invalid keystore location");
}
return new EncryptorFactory(keyProperties.getSalt()).create(keyProperties.getKey());
}
@Configuration(proxyBeanMethods = false)
@Conditional(KeyCondition.class)
@ConditionalOnClass(RsaSecretEncryptor.class)
@ -82,20 +98,8 @@ public class EncryptionBootstrapConfiguration { @@ -82,20 +98,8 @@ public class EncryptionBootstrapConfiguration {
@Bean
@ConditionalOnMissingBean(TextEncryptor.class)
public TextEncryptor textEncryptor(RsaProperties rsaProperties, KeyProperties keyProperties) {
KeyStore keyStore = keyProperties.getKeyStore();
if (keyStore.getLocation() != null) {
if (keyStore.getLocation().exists()) {
return new RsaSecretEncryptor(
new KeyStoreKeyFactory(keyStore.getLocation(), keyStore.getPassword().toCharArray())
.getKeyPair(keyStore.getAlias(), keyStore.getSecret().toCharArray()),
rsaProperties.getAlgorithm(), rsaProperties.getSalt(), rsaProperties.isStrong());
}
throw new IllegalStateException("Invalid keystore location");
}
return new EncryptorFactory(keyProperties.getSalt()).create(keyProperties.getKey());
public TextEncryptor textEncryptor(KeyProperties keyProperties, RsaProperties rsaProperties) {
return createTextEncryptor(keyProperties, rsaProperties);
}
}

3
spring-cloud-context/src/main/java/org/springframework/cloud/util/ConditionalOnBootstrapDisabled.java

@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.NoneNestedConditions; @@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
import org.springframework.context.annotation.Conditional;
import static org.springframework.cloud.util.PropertyUtils.BOOTSTRAP_ENABLED_PROPERTY;
import static org.springframework.cloud.util.PropertyUtils.MARKER_CLASS;
import static org.springframework.cloud.util.PropertyUtils.USE_LEGACY_PROCESSING_PROPERTY;
@Target({ ElementType.TYPE, ElementType.METHOD })
@ -42,7 +43,7 @@ public @interface ConditionalOnBootstrapDisabled { @@ -42,7 +43,7 @@ public @interface ConditionalOnBootstrapDisabled {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnClass(name = "org.springframework.cloud.bootstrap.marker.Marker")
@ConditionalOnClass(name = MARKER_CLASS)
static class OnBootstrapMarkerClassPresent {
}

14
spring-cloud-context/src/main/java/org/springframework/cloud/util/PropertyUtils.java

@ -39,20 +39,10 @@ public abstract class PropertyUtils { @@ -39,20 +39,10 @@ public abstract class PropertyUtils {
/**
* Boolean if bootstrap marker class exists.
*/
public static final boolean MARKER_CLASS_EXISTS = markerClassExists();
private static boolean markerClassExists() {
try {
ClassUtils.forName(MARKER_CLASS, null);
return true;
}
catch (ClassNotFoundException e) {
return false;
}
}
public static final boolean MARKER_CLASS_EXISTS = ClassUtils.isPresent(MARKER_CLASS, null);
private PropertyUtils() {
throw new UnsupportedOperationException("unable to instatiate utils class");
}
public static boolean bootstrapEnabled(Environment environment) {

Loading…
Cancel
Save