SHARE
TWEET

Untitled

a guest Jun 16th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class DecryptingPropertiesListener
  2.     implements ApplicationListener<ContextRefreshedEvent>, Ordered {
  3. public static final String PREFIX_KEY = "{cipher}";
  4.  
  5. private String prefix;
  6. private Encrypter encrypter = Encrypter.defaultInstance();
  7.  
  8. @Override
  9. public void onApplicationEvent(ContextRefreshedEvent event ) {
  10.     Environment environment = event.getApplicationContext().getEnvironment();
  11.     prefix = environment.getProperty(PREFIX_KEY, "{encrypted}");
  12.  
  13.     final MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
  14.  
  15.     Set<String> encryptedKeys = getKeysOfEncryptedPropertyValues(environment, propertySources);
  16.     addDecryptedValues(environment, propertySources, encryptedKeys);
  17. }
  18.  
  19. private Set<String> getKeysOfEncryptedPropertyValues(Environment environment, MutablePropertySources propertySources) {
  20.     return streamFromIterator(propertySources.iterator())
  21.             .filter(EnumerablePropertySource.class::isInstance)
  22.             .map(EnumerablePropertySource.class::cast)
  23.             .flatMap(source -> asList(source.getPropertyNames()).stream())
  24.             .filter(this::isNotEncryptionConfigProperty)
  25.             .filter(key -> isEncrypted(environment.getProperty(key)))
  26.             .collect(toSet());
  27. }
  28.  
  29. private boolean isNotEncryptionConfigProperty(String key) {
  30.     return !PREFIX_KEY.equals(key);
  31. }
  32.  
  33. private Stream<PropertySource<?>> streamFromIterator(Iterator<PropertySource<?>> iterator) {
  34.     Iterable<PropertySource<?>> iterable = () -> iterator;
  35.     return StreamSupport.stream(iterable.spliterator(), false);
  36. }
  37.  
  38. private void addDecryptedValues(Environment environment, MutablePropertySources propertySources, Set<String> encryptedKeys) {
  39.     Map<String, Object> decryptedProperties = encryptedKeys.stream()
  40.             .collect(toMap(
  41.                     key -> key,
  42.                     key -> decryptPropertyValue(environment.getProperty(key))));
  43.     propertySources.addFirst(new MapPropertySource("decryptedValues", decryptedProperties));
  44. }
  45.  
  46. private String decryptPropertyValue(String encryptedPropertyValue) {
  47.     try {
  48.         return encrypter.decryptIfEncrypted(encryptedPropertyValue);
  49.     }
  50.     catch (EncryptionException e) {
  51.         throw new RuntimeException("Unable to decrypt property value '" + encryptedPropertyValue + "'", e);
  52.     }
  53. }
  54.  
  55. private boolean isEncrypted(Object propertyValue) {
  56.     return propertyValue != null && propertyValue instanceof String && ((String)propertyValue).startsWith(prefix);
  57. }
  58.  
  59. @Override
  60. public int getOrder() {
  61.     return Ordered.LOWEST_PRECEDENCE;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top