Skip to content

Commit

Permalink
Add support for Vault
Browse files Browse the repository at this point in the history
for issue #2
  • Loading branch information
aharonha committed Mar 28, 2018
1 parent dd811c3 commit cad87b6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
24 changes: 24 additions & 0 deletions config-vault/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.scalecube</groupId>
<artifactId>config-parent</artifactId>
<version>0.3.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>config-vault</artifactId>

<dependencies>
<dependency>
<groupId>io.scalecube</groupId>
<artifactId>config</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.bettercloud</groupId>
<artifactId>vault-java-driver</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.scalecube.config.vault;

import io.scalecube.config.ConfigProperty;
import io.scalecube.config.source.ConfigSource;
import io.scalecube.config.source.LoadedConfigProperty;
import io.scalecube.config.source.LoadedConfigProperty.Builder;

import com.bettercloud.vault.SslConfig;
import com.bettercloud.vault.Vault;
import com.bettercloud.vault.VaultConfig;
import com.bettercloud.vault.VaultException;
import com.bettercloud.vault.response.LogicalResponse;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class VaultConfigSource implements ConfigSource {

private Vault vault;
final String SECRET_DEFAULT_PATH;

public VaultConfigSource() {
SECRET_DEFAULT_PATH = System.getenv("VAULT_SECRETS_PATH");
VaultConfig config;
try {
config =
new VaultConfig()
// Defaults to "VAULT_ADDR" environment variable
// .address("http://localhost:8200")
// Defaults to "VAULT_TOKEN" environment variable
// .token("00000000-0000-0000-0000-000000000000")
// Defaults to "VAULT_OPEN_TIMEOUT" environment variable
// .openTimeout(5)
// Defaults to "VAULT_READ_TIMEOUT" environment variable
// .readTimeout(30)
// See "SSL Config" section below
.sslConfig(new SslConfig().build())
.build();
vault = new Vault(config);
Boolean initialized = vault.debug().health().getInitialized();
if (!initialized) {
throw new VaultException("Vault yet initialized");
}
if (vault.seal().sealStatus().getSealed()) {
throw new VaultException("Vault is sealed");
}
} catch (VaultException ignoredException) {
ignoredException.printStackTrace();
}
}

@Override
public Map<String, ConfigProperty> loadConfig() {
try {
LogicalResponse response = vault.logical().read(SECRET_DEFAULT_PATH);

return response.getData().entrySet().stream().map(LoadedConfigProperty::withNameAndValue).map(Builder::build)
.collect(Collectors.toMap(LoadedConfigProperty::name, Function.identity()));
} catch (VaultException ignoredException) {
return new HashMap<>();
}
}

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<module>config-mongo</module>
<module>config-http-server</module>
<module>config-examples</module>
<module>config-vault</module>
</modules>

<dependencyManagement>
Expand Down

0 comments on commit cad87b6

Please sign in to comment.