spring boot not loading correct Jasypt application.properties for environment

sonoerin picture sonoerin · Jul 14, 2016 · Viewed 7.9k times · Source

I am trying to implement Jasypt in my Spring Boot 1.4 application because it seems overkill to use Spring Cloud Config for a small app like this. However, I am clearly not understanding how Spring Boot determines which environment its running, and use the appropriate properties file. I need to encrypt the datasource properties stored such as:

spring.datasource.url=jdbc:postgresql://localhost:5432/myschema
spring.datasource.username=myuser
spring.datasource.password=ENC(ZwXHbQl^8c2U)
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.database=POSTGRESQL

In my project/config/ directory I have three files:

  • application.properties: single entry of: spring.profiles.active=local
  • application-local.properties: config values for develop, including local db credentials
  • application-test.properties: config values for test env such as db credentials, etc
  • application-prod.properties: config values for production env such as db credentials, etc

I am importing Jasypt via:

compile group: 'com.github.ulisesbocchio', name: 'jasypt-spring-boot-starter', version: '1.7'

I run local Spock / Goovy integration tests, so I annotate my Base Test class with

@ActiveProfiles("local, test")

But that didn't seem to pickup the properties file.
<FIXED> by adding @ActiveProfiles(["local", "test"])

I added the /config/application.properties file to set the

spring.profiles.active=local jasypt.encryptor.password=

I have looked at the documentation for how Jasypt works, so I can try and understand how to encrypt my db credentials per environment. Also, I have been able to figure out how to get the proper properties file loaded to test the encryption yet.

UPDATE

It would appear that the proper *.properties file is being loaded now (thanks to the great feedback!) but the database password is either not found or not able to be decrypted. I see the following in the logs:

eEncryptablePropertySourcesPostProcessor : Post-processing PropertySource instances
c.u.j.c.StringEncryptorConfiguration     : String Encryptor custom Bean not found with name 'jasyptStringEncryptor'. Initializing String Encryptor based on properties with name 'jasyptStringEncryptor'
eEncryptablePropertySourcesPostProcessor : Converting PropertySource commandLineArgs [to EncryptableEnumerablePropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource servletConfigInitParams [org.springframework.core.env.PropertySource$StubPropertySource] to EncryptablePropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource servletContextInitParams [org.springframework.core.env.PropertySource$StubPropertySource] to EncryptablePropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource systemProperties [org.springframework.core.env.MapPropertySource] to EncryptableMapPropertySourceWrapper
 eEncryptablePropertySourcesPostProcessor : Converting PropertySource systemEnvironment [org.springframework.core.env.SystemEnvironmentPropertySource] to EncryptableMapPropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource random [org.springframework.boot.context.config.RandomValuePropertySource] to EncryptablePropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource applicationConfig: [file:./config/application-local.properties] [org.springframework.core.env.PropertiesPropertySource] to EncryptableMapPropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource applicationConfig: [file:./config/application.properties] [org.springframework.core.env.PropertiesPropertySource] to EncryptableMapPropertySourceWrapper
eEncryptablePropertySourcesPostProcessor : Converting PropertySource applicationConfig: [classpath:/application.properties] [org.springframework.core.env.PropertiesPropertySource] to EncryptableMapPropertySourceWrapper
 .c.EncryptablePropertySourcesInitializer : Created Encryptable Property Source 'EncryptedProperties' from locations: [classpath:application.properties]

 Encryptor config not found for property jasypt.encryptor.algorithm, using default value: PBEWithMD5AndDES
 c.u.j.c.StringEncryptorConfiguration     : Encryptor config not found for property jasypt.encryptor.keyObtentionIterations, using default value: 1000
c.u.j.c.StringEncryptorConfiguration     : Encryptor config not found for property jasypt.encryptor.poolSize, using default value: 1
c.u.j.c.StringEncryptorConfiguration     : Encryptor config not found for property jasypt.encryptor.providerName, using default value: SunJCE
c.u.j.c.StringEncryptorConfiguration     : Encryptor config not found for property jasypt.encryptor.saltGeneratorClassname, using default value: org.jasypt.salt.RandomSaltGenerator
c.u.j.c.StringEncryptorConfiguration     : Encryptor config not found for property jasypt.encryptor.stringOutputType, using default value: base64
j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'

According to this line:

Property Source 'EncryptedProperties' from locations:[classpath:application.properties]

It almost seems like we have to explicitly declare which properties files to search for encrypted values in the @EnableEncryptableProperties( ) annotation, but that doesn't seem to take a list of files or property values, nor do I find anyone saying that needs to be done.

Answer

Rae Burawes picture Rae Burawes · Jul 15, 2016

Using {} for annotations with multiple values will not work in Groovy, try @ActiveProfiles(["local", "test"]) or @ActiveProfiles(["local", "test"] as String[]). See Arrays