/
Configuration.java
78 lines (61 loc) · 2.17 KB
/
Configuration.java
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
package cloud.spikeseed.helloworld;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
import java.io.IOException;
import java.util.Map;
class DbSecret {
private final String username;
private final String password;
public DbSecret(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
public class Configuration {
private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
private final String jdbcUrl;
private final DbSecret dbClusterSecret;
private final String dbHelloWorldName;
private final DbSecret dbHelloWorldSecret;
public Configuration() {
var smClient = SecretsManagerClient.create();
this.jdbcUrl = System.getenv("DATABASE_JDBC_URL");
var dbClusterSecretName = System.getenv("DATABASE_CLUSTER_SECRET_NAME");
var dbHelloWorldSecretName = System.getenv("DATABASE_HELLOWORLD_SECRET_NAME");
this.dbHelloWorldName = System.getenv("DATABASE_HELLOWORLD_NAME");
try {
this.dbClusterSecret = getDbSecret(smClient, dbClusterSecretName);
this.dbHelloWorldSecret = getDbSecret(smClient, dbHelloWorldSecretName);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private DbSecret getDbSecret(SecretsManagerClient client, String secretName) throws JsonProcessingException {
var secretResponse = client.getSecretValue(
GetSecretValueRequest.builder().secretId(secretName).build()
);
@SuppressWarnings("unchecked")
var secretJson = (Map<String, String>) JSON_MAPPER.readValue(secretResponse.secretString(), Map.class);
return new DbSecret(secretJson.get("username"), secretJson.get("password"));
}
public String getJdbcUrl() {
return jdbcUrl;
}
public DbSecret getDbClusterSecret() {
return dbClusterSecret;
}
public String getDbHelloWorldName() {
return dbHelloWorldName;
}
public DbSecret getDbHelloWorldSecret() {
return dbHelloWorldSecret;
}
}