Skip to content

Commit b5848b1

Browse files
authored
Add another approach - to encrypt property values with Jasypt (#4361)
* Add another approach - to encrypt property values with Jasypt In spring boot apps, propery values can be encrypted inline with Jasypt. This update demonstrates simple steps to do that. * implementing language review recommendations * fix language recommendations * address review comments - note about jasypt (what/why) - explain that @EnableEncryptableProperties can be added in any configuration class * add guidance on source control management for encrypted files * address vale comments * address review comments and add caution note caution node added to highlight that password used for encryption should not be committed to source control
1 parent 8a5f1e5 commit b5848b1

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

articles/flow/security/advanced-topics/external-configuration.adoc

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ It's often bad practice to put sensitive information, such as database URI, user
1313

1414
To avoid leaking sensitive information, you should consider storing sensitive information outside of your project files. In no case should you ever `git commit` passwords or other secrets into the repository.
1515

16-
This guide demonstrates two ways to externalize sensitive data: using system environment variables; and using an external properties file
16+
This guide demonstrates three ways to externalize sensitive data: using system environment variables; using an external properties file; and using encrypted property values with Jasypt.
1717

1818

1919
== Use System Environment Variables
@@ -90,4 +90,96 @@ spring.config.import = file:/Users/MyUserName/secret/db.properties
9090
...
9191
----
9292

93+
94+
== Use Encrypted Property Values with Jasypt
95+
Jasypt (Java Simplified Encryption) is a Java library that provides simple and transparent encryption/decryption for application data. It integrates with Spring Boot to encrypt sensitive values (e.g., database passwords) directly in properties files like `application.properties`.
96+
97+
This prevents accidental exposure of sensitive values by decrypting values at runtime using a master password.
98+
99+
For using this in your project, add the Jasypt Spring Boot starter dependency in the project like this:
100+
101+
.pom.xml
102+
[source,xml]
103+
----
104+
...
105+
<dependency>
106+
<groupId>com.github.ulisesbocchio</groupId>
107+
<artifactId>jasypt-spring-boot-starter</artifactId>
108+
<version>3.0.4</version>
109+
</dependency>
110+
...
111+
----
112+
113+
Add the Jasypt Maven plugin to the plugins section.
114+
115+
.pom.xml
116+
[source,xml]
117+
----
118+
...
119+
<plugin>
120+
<groupId>com.github.ulisesbocchio</groupId>
121+
<artifactId>jasypt-maven-plugin</artifactId>
122+
<version>3.0.4</version>
123+
</plugin>
124+
...
125+
----
126+
127+
128+
Enable Jasypt for properties decryption by annotating a `@Configuration` class with `@EnableEncryptableProperties`. Only one occurrence of this annotation is needed.
129+
130+
For example, add `@EnableEncryptableProperties` annotation to the Spring Boot `Application` class like this:
131+
132+
.Application.java
133+
[source,java]
134+
----
135+
...
136+
@SpringBootApplication
137+
@EnableEncryptableProperties
138+
public class Application implements AppShellConfigurator {
139+
...
140+
----
141+
142+
Now, wrap the values that you want to encrypt with `DEC()`, for example, `DEC(your-secret-value-here)` in any properties file that is used in the Spring Boot application like this:
143+
144+
.application.properties
145+
[source,properties]
146+
----
147+
...
148+
spring.datasource.password=DEC(super-secret-password)
149+
...
150+
----
151+
152+
Run the following command to encrypt the values in place. The `jasypt.plugin.path` should point to the properties file where you have the `DEC()` wrapped value which you want to encrypt.
153+
154+
The file path specified is relative to the directory the command is executed in.
155+
156+
[source,sh]
157+
----
158+
mvn jasypt:encrypt -Djasypt.encryptor.password=<choose-a-password-to-use-for-encryption> -Djasypt.plugin.path="file:src/main/resources/application.properties"
159+
----
160+
161+
Once the above command completes, all `DEC()` wrapped values in the properties file should have been encrypted in place and replaced with `ENC(....)` like this:
162+
163+
.application.properties
164+
[source,properties]
165+
----
166+
...
167+
spring.datasource.password=ENC(C7lfsna/9gxDsdfsdfsXiJQcFzpsdfsdfss70sdfsdfsr2wfjEa+qDM)
168+
...
169+
----
170+
171+
The Jasypt encrypted property files can be checked into source control, since the values are now encrypted.
172+
173+
.Do not commit the password used for encryption
174+
[CAUTION]
175+
If the password is leaked all encrypted values are compromised. Attackers with read access to both the encrypted file and the password can recover the sensitive values.
176+
177+
When starting the application, set the system property `jasypt.encryptor.password` to the password that was used for encryption in the step above.
178+
179+
For example,
180+
[source,sh]
181+
----
182+
java -Djasypt.encryptor.password=<the-password-used-for-encryption> -jar your-application.jar
183+
----
184+
93185
[discussion-id]`FCC4C231-5DB9-4950-9559-C89630042A43`

0 commit comments

Comments
 (0)