-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for issue #2
- Loading branch information
aharonha
committed
Mar 28, 2018
1 parent
dd811c3
commit cad87b6
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
66 changes: 66 additions & 0 deletions
66
config-vault/src/main/java/io/scalecube/config/vault/VaultConfigSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters