Skip to content

Commit bd8c2e7

Browse files
Vault Autoconfigure (#65)
Signed-off-by: Anders Swanson <anders.swanson@oracle.com>
1 parent 54292c8 commit bd8c2e7

File tree

2 files changed

+56
-51
lines changed

2 files changed

+56
-51
lines changed

spring-cloud-oci-autoconfigure/src/main/java/com/oracle/cloud/spring/vault/VaultEnvironmentPostProcessor.java

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,13 @@
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
33
package com.oracle.cloud.spring.vault;
44

5-
import java.io.IOException;
6-
7-
import com.oracle.bmc.auth.RegionProvider;
8-
import com.oracle.bmc.secrets.Secrets;
9-
import com.oracle.bmc.vault.Vaults;
10-
import com.oracle.cloud.spring.autoconfigure.core.CredentialsProperties;
11-
import com.oracle.cloud.spring.autoconfigure.core.CredentialsProvider;
12-
import com.oracle.cloud.spring.autoconfigure.core.RegionProperties;
135
import org.springframework.boot.SpringApplication;
146
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
15-
import org.springframework.boot.context.properties.bind.Bindable;
16-
import org.springframework.boot.context.properties.bind.Binder;
177
import org.springframework.boot.env.EnvironmentPostProcessor;
188
import org.springframework.core.Ordered;
199
import org.springframework.core.env.ConfigurableEnvironment;
20-
import org.springframework.core.env.MutablePropertySources;
2110
import org.springframework.util.ClassUtils;
2211

23-
import static com.oracle.cloud.spring.autoconfigure.core.RegionProviderAutoConfiguration.createRegionProvider;
24-
import static com.oracle.cloud.spring.vault.VaultAutoConfiguration.createSecretsClient;
25-
import static com.oracle.cloud.spring.vault.VaultAutoConfiguration.createVaultClient;
26-
import static org.springframework.core.env.StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME;
27-
2812
/**
2913
* Injects a VaultPropertySource for each OCI Vault property source specified in the application properties.
3014
* OCI Vault property sources will only be loaded if the com.oracle.cloud.spring.vault.VaulTemplate class is on the classpath.
@@ -33,33 +17,7 @@ public class VaultEnvironmentPostProcessor implements EnvironmentPostProcessor,
3317
@Override
3418
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
3519
if (areClassesLoaded()) {
36-
// Load Vault Properties
37-
Binder binder = Binder.get(environment);
38-
CredentialsProperties credentialsProperties = binder.bind(CredentialsProperties.PREFIX, Bindable.of(CredentialsProperties.class))
39-
.orElse(new CredentialsProperties());
40-
RegionProperties regionProperties = binder.bind(RegionProperties.PREFIX, Bindable.of(RegionProperties.class))
41-
.orElse(new RegionProperties());
42-
VaultProperties vaultProperties = binder.bind(VaultProperties.PREFIX, Bindable.of(VaultProperties.class))
43-
.orElse(new VaultProperties());
44-
45-
// Create vault/secrets clients
46-
RegionProvider regionProvider = createRegionProvider(regionProperties);
47-
CredentialsProvider credentialsProvider = getCredentialsProvider(credentialsProperties);
48-
Secrets secretsClient = createSecretsClient(regionProvider, credentialsProvider);
49-
Vaults vaultClient = createVaultClient(regionProvider, credentialsProvider);
50-
51-
// Inject VaultPropertySources into the system property sources
52-
MutablePropertySources propertySources = environment.getPropertySources();
53-
for (VaultPropertySourceProperties properties : vaultProperties.getPropertySources()) {
54-
VaultTemplate vaultTemplate = new VaultTemplateImpl(vaultClient, secretsClient, properties.getVaultId(), vaultProperties.getCompartment());
55-
VaultPropertyLoader vaultPropertyLoader = new VaultPropertyLoader(vaultTemplate, vaultProperties.getPropertyRefreshInterval());
56-
VaultPropertySource vaultPropertySource = new VaultPropertySource(properties.getVaultId(), vaultPropertyLoader);
57-
if (propertySources.contains(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
58-
propertySources.addAfter(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, vaultPropertySource);
59-
} else {
60-
propertySources.addFirst(vaultPropertySource);
61-
}
62-
}
20+
VaultPropertySource.configure(environment);
6321
}
6422
}
6523

@@ -71,12 +29,4 @@ public int getOrder() {
7129
private boolean areClassesLoaded() {
7230
return ClassUtils.isPresent("com.oracle.cloud.spring.vault.VaultTemplate", VaultEnvironmentPostProcessor.class.getClassLoader());
7331
}
74-
75-
private CredentialsProvider getCredentialsProvider(CredentialsProperties credentialsProperties) {
76-
try {
77-
return new CredentialsProvider(credentialsProperties);
78-
} catch (IOException e) {
79-
throw new RuntimeException(e);
80-
}
81-
}
8232
}

spring-cloud-oci-autoconfigure/src/main/java/com/oracle/cloud/spring/vault/VaultPropertySource.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,68 @@
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
33
package com.oracle.cloud.spring.vault;
44

5+
import java.io.IOException;
6+
7+
import com.oracle.bmc.auth.RegionProvider;
8+
import com.oracle.bmc.secrets.Secrets;
9+
import com.oracle.bmc.vault.Vaults;
10+
import com.oracle.cloud.spring.autoconfigure.core.CredentialsProperties;
11+
import com.oracle.cloud.spring.autoconfigure.core.CredentialsProvider;
12+
import com.oracle.cloud.spring.autoconfigure.core.RegionProperties;
13+
import org.springframework.boot.context.properties.bind.Bindable;
14+
import org.springframework.boot.context.properties.bind.Binder;
15+
import org.springframework.core.env.ConfigurableEnvironment;
516
import org.springframework.core.env.EnumerablePropertySource;
17+
import org.springframework.core.env.MutablePropertySources;
18+
19+
import static com.oracle.cloud.spring.autoconfigure.core.RegionProviderAutoConfiguration.createRegionProvider;
20+
import static com.oracle.cloud.spring.vault.VaultAutoConfiguration.createSecretsClient;
21+
import static com.oracle.cloud.spring.vault.VaultAutoConfiguration.createVaultClient;
22+
import static org.springframework.core.env.StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME;
623

724
public class VaultPropertySource extends EnumerablePropertySource<VaultPropertyLoader> {
825
public VaultPropertySource(String name, VaultPropertyLoader source) {
926
super(name, source);
1027
}
1128

29+
public static void configure(ConfigurableEnvironment environment) {
30+
// Load Vault Properties
31+
Binder binder = Binder.get(environment);
32+
CredentialsProperties credentialsProperties = binder.bind(CredentialsProperties.PREFIX, Bindable.of(CredentialsProperties.class))
33+
.orElse(new CredentialsProperties());
34+
RegionProperties regionProperties = binder.bind(RegionProperties.PREFIX, Bindable.of(RegionProperties.class))
35+
.orElse(new RegionProperties());
36+
VaultProperties vaultProperties = binder.bind(VaultProperties.PREFIX, Bindable.of(VaultProperties.class))
37+
.orElse(new VaultProperties());
38+
39+
// Create vault/secrets clients
40+
RegionProvider regionProvider = createRegionProvider(regionProperties);
41+
CredentialsProvider credentialsProvider = getCredentialsProvider(credentialsProperties);
42+
Secrets secretsClient = createSecretsClient(regionProvider, credentialsProvider);
43+
Vaults vaultClient = createVaultClient(regionProvider, credentialsProvider);
44+
45+
// Inject VaultPropertySources into the system property sources
46+
MutablePropertySources propertySources = environment.getPropertySources();
47+
for (VaultPropertySourceProperties properties : vaultProperties.getPropertySources()) {
48+
VaultTemplate vaultTemplate = new VaultTemplateImpl(vaultClient, secretsClient, properties.getVaultId(), vaultProperties.getCompartment());
49+
VaultPropertyLoader vaultPropertyLoader = new VaultPropertyLoader(vaultTemplate, vaultProperties.getPropertyRefreshInterval());
50+
VaultPropertySource vaultPropertySource = new VaultPropertySource(properties.getVaultId(), vaultPropertyLoader);
51+
if (propertySources.contains(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
52+
propertySources.addAfter(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, vaultPropertySource);
53+
} else {
54+
propertySources.addFirst(vaultPropertySource);
55+
}
56+
}
57+
}
58+
59+
private static CredentialsProvider getCredentialsProvider(CredentialsProperties credentialsProperties) {
60+
try {
61+
return new CredentialsProvider(credentialsProperties);
62+
} catch (IOException e) {
63+
throw new RuntimeException(e);
64+
}
65+
}
66+
1267
@Override
1368
public String[] getPropertyNames() {
1469
return source.getPropertyNames();

0 commit comments

Comments
 (0)