forked from ShiftLeftSecurity/shiftleft-java-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataLoader.java
More file actions
104 lines (79 loc) · 3.3 KB
/
DataLoader.java
File metadata and controls
104 lines (79 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package io.shiftleft.data;
import java.io.IOException;
import java.util.Properties;
import io.shiftleft.model.Patient;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.properties.EncryptableProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.stereotype.Component;
import io.shiftleft.model.Customer;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import io.shiftleft.repository.CustomerRepository;
import io.shiftleft.repository.PatientRepository;
@Component
@Configuration
@EnableEncryptableProperties
@PropertySource({ "classpath:config/application-aws.properties", "classpath:config/application-mysql.properties",
"classpath:config/application-sfdc.properties" })
public class DataLoader implements CommandLineRunner {
private static Logger log = LoggerFactory.getLogger(DataLoader.class);
@Autowired
private DataBuilder dataBuilder;
@Autowired
Environment env;
@Autowired
private CustomerRepository customerRepository;
@Autowired
private PatientRepository patientRepository;
/*
* Internal workings of @EnableEncryptableProperties annotation
*/
private String getSecurePassword(String masterPassword) throws IOException {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(masterPassword);
Properties props = new EncryptableProperties(encryptor);
props.load(this.getClass().getClassLoader().getResourceAsStream("config/application-mysql.properties"));
return props.getProperty("db.password");
}
private boolean connectToAws() {
log.info("Start Loading AWS Properties");
log.info("AWS AccessKey is {} and SecretKey is {}", env.getProperty("aws.accesskey"),
env.getProperty("aws.secretkey"));
log.info("AWS Bucket is {}", env.getProperty("aws.bucket"));
log.info("End Loading AWS Properties");
// Connect to AWS resources and do something
return true;
}
private boolean connectToMySQL() {
log.info("Start Loading MySQL Properties");
log.info("Url is {}", env.getProperty("db.url"));
log.info("UserName is {}", env.getProperty("db.username"));
log.info("Password is {}", env.getProperty("db.password"));
log.info("End Loading MySQL Properties");
// Connect to DB MySQL resources and do something
return true;
}
@Override
public void run(String... arg0) throws Exception {
SimpleCommandLinePropertySource ps = new SimpleCommandLinePropertySource(arg0);
String encryptor = (String) ps.getProperty("jasypt.encryptor.password");
log.info("JASP Master Creds is {}", encryptor);
connectToMySQL();
connectToAws();
log.debug("Loading test data...");
for (Customer customer : dataBuilder.createCustomers()) {
customerRepository.save(customer);
}
for (Patient patient : dataBuilder.createPatients()) {
patientRepository.save(patient);
}
log.debug("Test data loaded...");
}
}