-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptVaultAutoConfiguration.java
77 lines (60 loc) · 2.06 KB
/
CryptVaultAutoConfiguration.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
package com.bol.config;
import com.bol.crypt.CryptVault;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Base64;
import java.util.List;
@AutoConfiguration
@ConditionalOnProperty("cryptvault.keys[0].key")
public class CryptVaultAutoConfiguration {
@Bean
CryptVault cryptVault(CryptVaultConfigurationProperties properties) {
CryptVault cryptVault = new CryptVault();
if (properties.keys == null || properties.keys.isEmpty()) throw new IllegalArgumentException("property 'keys' is not set");
for (Key key : properties.keys) {
byte[] secretKeyBytes = Base64.getDecoder().decode(key.key);
cryptVault.with256BitAesCbcPkcs5PaddingAnd128BitSaltKey(key.version, secretKeyBytes);
}
if (properties.defaultKey != null) {
cryptVault.withDefaultKeyVersion(properties.defaultKey);
}
return cryptVault;
}
@Component
@ConfigurationProperties("cryptvault")
public static class CryptVaultConfigurationProperties {
List<Key> keys;
Integer defaultKey;
public void setKeys(List<Key> keys) {
this.keys = keys;
}
public void setDefaultKey(Integer defaultKey) {
this.defaultKey = defaultKey;
}
public List<Key> getKeys() {
return keys;
}
public Integer getDefaultKey() {
return defaultKey;
}
}
public static class Key {
int version;
String key;
public void setVersion(int version) {
this.version = version;
}
public void setKey(String key) {
this.key = key;
}
public int getVersion() {
return version;
}
public String getKey() {
return key;
}
}
}