Skip to content

Commit b39257d

Browse files
committed
add jasypt for encryption
use jasypt for encryption db password and improve security.
1 parent 5e3ab0f commit b39257d

File tree

8 files changed

+123
-9
lines changed

8 files changed

+123
-9
lines changed

dockerfile renamed to Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ MAINTAINER pouya
44
EXPOSE 9090
55
ADD target/springboot-real-*.jar /springboot-real.jar
66
ENTRYPOINT ["java","-jar", "/springboot-real.jar"]
7-
#ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar", "/springboot-real-1.0.0.jar"]
7+
# add jasypt secret-key in jar file:
8+
#ENTRYPOINT ["java", "-jar", "/springboot-real.jar", "--jasypt.encryptor.password=secret-key"]
9+
10+
#ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar", "/springboot-real-1.0.0.jar", "--spring.config.location=file:application.properties"]

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ This project, implement instance of basic feature, <br> you maybe want use for A
2727
- decoupling layer
2828
- controller, service and repository layer for easy extend
2929
- flexible search
30-
- use CriteriaQuery and CriteriaBuilder for search base on Entity
30+
- use CriteriaQuery and CriteriaBuilder for search base on Entity
31+
- jasypt
32+
- use for encrypt password and added to config file, then decrypted pass in runtime
3133

3234
## Run guide
3335
### Run for develop and debug: <br>
@@ -84,3 +86,26 @@ This project, implement instance of basic feature, <br> you maybe want use for A
8486
### dataSource feature
8587
1 - choose between use simpleDataSource or HikariCp datasource <br>
8688
2 - you can customize properties for dataSource in application.properties
89+
90+
### database password generator
91+
if you want to use encrypt password in config file for access database, you must follow below statement. <br>
92+
93+
1 - use jasypt for generate encrypted database password with secret-key
94+
~~~
95+
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input={password} password={secret-key} algorithm=PBEWithMD5AndTripleDES
96+
~~~
97+
2 - add output into application.properties <br>
98+
note: I use '<b>ENC()<b>' as convention to use check password for decrypt or not. <br>
99+
100+
normal datasource:
101+
~~~
102+
demo.datasource.password="ENC({output})"
103+
~~~
104+
hikari datasource:
105+
~~~
106+
hikari.dataSource.password="ENC({output})"
107+
~~~
108+
3 - run program and add in program environment secret-key:
109+
~~~
110+
--jasypt.encryptor.password={secret-key}
111+
~~~

docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ version: '3.9'
22

33
services:
44
app:
5+
# "`-._,-'"`-._,-'"`-._,-'"`-._,-'"`-._,-'"`-._,-'"`-._,-'"`-._,-'"`-._,-
6+
# An important note about accessing database encryption:
7+
# -----------------------------------------------------------------------
8+
# JASYPT_ENCRYPTOR_PASSWORD: secret-key added to environment to use this function
9+
# "`-._,-'"`-._,-'"`-._,-'"`-._,-'"`-._,-'"`-._,-'"`-._,-'"`-._,-'"`-._,-
10+
#
511
container_name: springapp-with-db
612
image: springapp:latest
713
build: ./
814
ports:
915
- "9090:9090"
16+
environment:
17+
# - JASYPT_ENCRYPTOR_PASSWORD: secret-key
1018
depends_on:
1119
- dbpostgresql
1220
dbpostgresql:

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@
194194
<version>4.0.3</version>
195195
</dependency>
196196

197+
<!-- jasypt-Encryption -->
198+
<dependency>
199+
<groupId>com.github.ulisesbocchio</groupId>
200+
<artifactId>jasypt-spring-boot-starter</artifactId>
201+
<version>3.0.4</version>
202+
</dependency>
203+
197204
<dependency>
198205
<groupId>org.springframework.boot</groupId>
199206
<artifactId>spring-boot-starter-test</artifactId>

src/main/java/ir/bigz/springbootreal/configuration/HikariDataSourceInit.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.zaxxer.hikari.HikariConfig;
44
import com.zaxxer.hikari.HikariDataSource;
5+
import org.jasypt.util.text.StrongTextEncryptor;
56
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Value;
68
import org.springframework.boot.jdbc.DataSourceBuilder;
79
import org.springframework.context.annotation.Bean;
810
import org.springframework.context.annotation.Configuration;
@@ -14,8 +16,13 @@
1416
/**
1517
* {@link HikariDataSourceInit} use for build connection-pool base on HikariCp with customize properties,
1618
* and then added to {@link DataSourceConfiguration} class as datasource for use in app.
17-
* also if you want use for production un comment line 30 and comment line 31 and 32
18-
* and if you want test with testContainer uncomment line 31 and 32 and comment line 30
19+
* also if you want use for production uncomment line 32 and comment line 33 and 34
20+
* and if you want test with testContainer uncomment line 33 and 34 and comment line 32
21+
*
22+
* DataBase password encryption with JASYPT:
23+
* first of all use jasypt to encrypted db-password and added into application.properties file,
24+
* then uncomment line 34 (encryptorPassword)
25+
* at the end add --jasypt.encryptor.password={secret-key} in program environment
1926
*/
2027

2128
//todo i must be find better solution for use hikariDataSource between production and test with testContainer
@@ -26,6 +33,9 @@ public class HikariDataSourceInit{
2633
@Autowired
2734
private Environment env;
2835

36+
@Value("${jasypt.encryptor.password}")
37+
String encryptorPassword;
38+
2939
@Bean(name = "HikariDataSourceInit")
3040
public DataSource dataSource(){
3141
HikariConfig hikariConfig = new HikariConfig(hikariProperties()); // use for production
@@ -40,21 +50,42 @@ public DataSource dataSource(){
4050
}
4151

4252
protected DataSource InitDataSource(){
53+
String passwordProperty = env.getProperty("demo.datasource.password");
54+
String plainPassword = "";
55+
if(passwordProperty.contains("ENC(")){
56+
plainPassword = decryptPassword(passwordProperty);
57+
}else{
58+
plainPassword = passwordProperty;
59+
}
4360
return DataSourceBuilder.create()
4461
.driverClassName(env.getProperty("demo.datasource.driver-class-name"))
4562
.url(env.getProperty("demo.datasource.url"))
4663
.username(env.getProperty("demo.datasource.username"))
47-
.password(env.getProperty("demo.datasource.password"))
64+
.password(plainPassword)
4865
.build();
4966
}
5067

5168
protected Properties hikariProperties(){
69+
String passwordProperty = env.getProperty("hikari.dataSource.password");
70+
String plainPassword = "";
71+
if(passwordProperty.contains("ENC(")){
72+
plainPassword = decryptPassword(passwordProperty);
73+
}else{
74+
plainPassword = passwordProperty;
75+
}
5276
Properties hikariProps = new Properties();
5377
hikariProps.setProperty("dataSourceClassName", env.getProperty("hikari.dataSourceClassName"));
5478
hikariProps.setProperty("dataSource.user", env.getProperty("hikari.dataSource.user"));
55-
hikariProps.setProperty("dataSource.password", env.getProperty("hikari.dataSource.password"));
79+
hikariProps.setProperty("dataSource.password", plainPassword);
5680
hikariProps.setProperty("dataSource.databaseName", env.getProperty("hikari.dataSource.databaseName"));
5781
return hikariProps;
5882
}
5983

84+
@Bean
85+
protected String decryptPassword(String value) {
86+
var textEncryptor = new StrongTextEncryptor();
87+
textEncryptor.setPassword(encryptorPassword);
88+
return textEncryptor.decrypt(value.substring(4, value.length() - 1));
89+
}
90+
6091
}

src/main/resources/application-dev.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ server.port=9090
55
demo.datasource.driver-class-name=org.postgresql.Driver
66
demo.datasource.url=jdbc:postgresql://localhost:5432/myapp
77
demo.datasource.username=postgres
8-
demo.datasource.password=postgres
8+
demo.datasource.password="ENC(vlB3w8KRSweC2PdYoVHpjYW+GQxeTSCv)"
99

1010
################### Hibernate Configuration ##########################
1111
demo.jpa.hibernate.ddl-auto=update
@@ -18,7 +18,7 @@ spring.cache.jcache.config=classpath:ehcache.xml
1818
################### Hikari DataSource Configuration ##########################
1919
hikari.dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
2020
hikari.dataSource.user=postgres
21-
hikari.dataSource.password=postgres
21+
hikari.dataSource.password="ENC(vlB3w8KRSweC2PdYoVHpjYW+GQxeTSCv)"
2222
hikari.dataSource.databaseName=myapp
2323
hikari.dataSource.portNumber=5432
2424
hikari.dataSource.serverName=localhost

src/main/resources/application-docker.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ demo.datasource.password=postgres
1212
################### Hibernate Configuration ##########################
1313

1414
demo.jpa.hibernate.ddl-auto=create-drop
15-
demo.jpa.show-sql=true
15+
demo.jpa.show-sql=false
1616

1717
################### cache Configuration ##########################
1818

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
################### Server Configuration ##########################
2+
server.port=9090
3+
4+
################### Simple DataSource Configuration ##########################
5+
demo.datasource.driver-class-name=org.postgresql.Driver
6+
demo.datasource.url=jdbc:postgresql://localhost:5432/myapp
7+
demo.datasource.username=postgres
8+
demo.datasource.password=postgres
9+
10+
################### Hibernate Configuration ##########################
11+
demo.jpa.hibernate.ddl-auto=update
12+
demo.jpa.show-sql=true
13+
demo.enity.packageScan=ir.bigz.springbootreal.dto
14+
15+
################### cache Configuration ##########################
16+
spring.cache.jcache.config=classpath:ehcache.xml
17+
18+
################### Hikari DataSource Configuration ##########################
19+
hikari.dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
20+
hikari.dataSource.user=postgres
21+
# when connecting to database and using encrypted password in config
22+
# hikari.dataSource.password="ENC(vlB3w8KRSweC2PdYoVHpjYW+GQxeTSCv)"
23+
# else use below properties
24+
# hikari.dataSource.password=postgres
25+
hikari.dataSource.databaseName=myapp
26+
hikari.dataSource.portNumber=5432
27+
hikari.dataSource.serverName=localhost
28+
hikari.connectionTimeout=30000
29+
hikari.idleTimeout=600000
30+
hikari.maxLifetime=1800000
31+
32+
################### condition Configuration ##########################
33+
app.generator.enabled=false
34+
35+
################### Application MetaData ##########################
36+
37+
38+
springdoc.swagger-ui.path=/swagger-ui.html
39+
springdoc.api-docs.path=/api-docs
40+
springdoc.use-fqn=true

0 commit comments

Comments
 (0)