-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load config with secrets from vault (#160)
- Loading branch information
1 parent
ce9d3f8
commit a95e72d
Showing
14 changed files
with
341 additions
and
26 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
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
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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/github/sitture/envconfig/EnvConfigKeepassProperties.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,19 @@ | ||
package com.github.sitture.envconfig; | ||
|
||
class EnvConfigKeepassProperties { | ||
private final String filename; | ||
private final String masterKey; | ||
|
||
EnvConfigKeepassProperties(final String filename, final String masterKey) { | ||
this.filename = filename; | ||
this.masterKey = masterKey; | ||
} | ||
|
||
String getFilename() { | ||
return filename; | ||
} | ||
|
||
String getMasterKey() { | ||
return masterKey; | ||
} | ||
} |
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
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
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/github/sitture/envconfig/EnvConfigVaultProperties.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,33 @@ | ||
package com.github.sitture.envconfig; | ||
|
||
class EnvConfigVaultProperties { | ||
|
||
private final String address; | ||
private final String namespace; | ||
private final String token; | ||
private final String secretPath; | ||
|
||
EnvConfigVaultProperties(final String address, final String namespace, final String token, final String secretPath) { | ||
this.address = address; | ||
this.namespace = namespace; | ||
this.token = token; | ||
this.secretPath = secretPath; | ||
} | ||
|
||
String getAddress() { | ||
return address; | ||
} | ||
|
||
String getNamespace() { | ||
return namespace; | ||
} | ||
|
||
String getToken() { | ||
return token; | ||
} | ||
|
||
String getSecretPath() { | ||
return secretPath; | ||
} | ||
|
||
} |
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
45 changes: 45 additions & 0 deletions
45
src/main/java/com/github/sitture/envconfig/VaultConfiguration.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,45 @@ | ||
package com.github.sitture.envconfig; | ||
|
||
import io.github.jopenlibs.vault.Vault; | ||
import io.github.jopenlibs.vault.VaultConfig; | ||
import io.github.jopenlibs.vault.VaultException; | ||
import io.github.jopenlibs.vault.response.LogicalResponse; | ||
import org.apache.commons.configuration2.Configuration; | ||
import org.apache.commons.configuration2.MapConfiguration; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
class VaultConfiguration { | ||
|
||
private final Vault vault; | ||
private final EnvConfigVaultProperties vaultProperties; | ||
|
||
VaultConfiguration(final EnvConfigVaultProperties vaultProperties) { | ||
this.vaultProperties = vaultProperties; | ||
try { | ||
final VaultConfig config = new VaultConfig() | ||
.address(vaultProperties.getAddress()) | ||
.nameSpace(vaultProperties.getNamespace()) | ||
.token(vaultProperties.getToken()) | ||
.build(); | ||
this.vault = Vault.create(config); | ||
// attempt to lookupSelf to validate token | ||
this.vault.auth().lookupSelf(); | ||
} catch (VaultException e) { | ||
throw new EnvConfigException("Could not connect to vault", e); | ||
} | ||
} | ||
|
||
public Configuration getConfiguration(final String env) { | ||
try { | ||
final String secret = String.format("%s/%s", StringUtils.removeEnd(this.vaultProperties.getSecretPath(), "/"), env); | ||
final LogicalResponse response = this.vault.logical().read(secret); | ||
if (response.getRestResponse().getStatus() != 200 && EnvConfigUtils.CONFIG_ENV_DEFAULT.equals(env)) { | ||
throw new EnvConfigException(String.format("Could not find the vault secret: %s", secret)); | ||
} | ||
return new MapConfiguration(response.getData()); | ||
} catch (VaultException e) { | ||
throw new EnvConfigException("Could not read data from vault.", e); | ||
} | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.