Skip to content

Commit

Permalink
Adds ability to specify a default secret path when loading from vault
Browse files Browse the repository at this point in the history
  • Loading branch information
haroon-sheikh committed Sep 12, 2024
1 parent acc6801 commit 1599a3a
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 142 deletions.
34 changes: 34 additions & 0 deletions src/main/java/com/github/sitture/envconfig/EnvConfigKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.sitture.envconfig;

enum EnvConfigKey {

CONFIG_PATH("env.config.path"),
CONFIG_ENV("env.config.environment"),
CONFIG_PROFILE("env.config.profile"),
CONFIG_PROFILES_PATH("env.config.profiles.path"),
CONFIG_KEEPASS_ENABLED("env.config.keepass.enabled"),
CONFIG_KEEPASS_FILENAME("env.config.keepass.filename"),
CONFIG_KEEPASS_MASTERKEY("env.config.keepass.masterkey"),
CONFIG_VAULT_ENABLED("env.config.vault.enabled"),
CONFIG_VAULT_ADDRESS("env.config.vault.address"),
CONFIG_VAULT_NAMESPACE("env.config.vault.namespace"),
CONFIG_VAULT_DEFAULT_PATH("env.config.vault.default.secret.path"),
CONFIG_VAULT_SECRET_PATH("env.config.vault.secret.path"),
CONFIG_VAULT_TOKEN("env.config.vault.token"),
CONFIG_VAULT_VALIDATE_MAX_RETRIES("env.config.vault.validate.token.max.retries");

private final String property;

EnvConfigKey(final String property) {
this.property = property;
}

public String getProperty() {
return this.property;
}

public String getEnvProperty() {
return EnvConfigUtils.getProcessedEnvKey(this.property);
}

}
44 changes: 22 additions & 22 deletions src/main/java/com/github/sitture/envconfig/EnvConfigProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ String getCurrentEnvironment() {
}

String getConfigProfile() {
return getConfigurationProperty(EnvConfigUtils.CONFIG_PROFILE_KEY, "");
return getConfigProperty(EnvConfigKey.CONFIG_PROFILE, "");
}

private List<String> getEnvList() {
final List<String> environments = new ArrayList<>();
environments.add(EnvConfigUtils.CONFIG_ENV_DEFAULT);
environments.addAll(EnvConfigUtils.getListOfValues(getConfigurationProperty(EnvConfigUtils.CONFIG_ENV_KEY, EnvConfigUtils.CONFIG_ENV_DEFAULT).toLowerCase(), EnvConfigUtils.CONFIG_DELIMITER_DEFAULT));
environments.addAll(EnvConfigUtils.getListOfValues(getConfigProperty(EnvConfigKey.CONFIG_ENV, EnvConfigUtils.CONFIG_ENV_DEFAULT).toLowerCase(), EnvConfigUtils.CONFIG_DELIMITER_DEFAULT));
Collections.reverse(environments);
return environments.stream().distinct().collect(Collectors.toList());
}
Expand All @@ -54,15 +54,15 @@ Path getConfigPath(final String env) {
}

private Path getConfigPath() {
return getPath(Paths.get(getConfigurationProperty(EnvConfigUtils.CONFIG_PATH_KEY, EnvConfigUtils.CONFIG_PATH_DEFAULT)).toAbsolutePath());
return getPath(Paths.get(getConfigProperty(EnvConfigKey.CONFIG_PATH, EnvConfigUtils.CONFIG_PATH_DEFAULT)).toAbsolutePath());
}

Path getConfigProfilePath(final String env, final String configProfile) {
return Paths.get(this.configProfilesPath.toString(), env, configProfile);
}

private Path getConfigProfilePath() {
return getPath(Paths.get(getConfigurationProperty(EnvConfigUtils.CONFIG_PROFILES_PATH_KEY, this.configDir.toString())).toAbsolutePath());
return getPath(Paths.get(getConfigProperty(EnvConfigKey.CONFIG_PROFILES_PATH, this.configDir.toString())).toAbsolutePath());
}

private Path getPath(final Path configPath) {
Expand All @@ -74,42 +74,42 @@ private Path getPath(final Path configPath) {
return configPath;
}

private String getConfigurationProperty(final String key, final String defaultValue) {
return Optional.ofNullable(System.getProperty(key))
private String getConfigProperty(final EnvConfigKey key, final String defaultValue) {
return Optional.ofNullable(System.getProperty(key.getProperty()))
.orElse(Optional.ofNullable(getEnvByPropertyKey(key))
.orElse(defaultValue));
}

private String getEnvByPropertyKey(final String key) {
private String getEnvByPropertyKey(final EnvConfigKey key) {
LOG.debug("Getting {} from system.env", key);
return Optional.ofNullable(System.getenv(EnvConfigUtils.getProcessedEnvKey(key)))
.orElse(System.getenv(key));
return Optional.ofNullable(System.getenv(EnvConfigUtils.getProcessedEnvKey(key.getProperty())))
.orElse(System.getenv(key.getProperty()));
}

private String getRequiredConfigKey(final String key) {
return Optional.ofNullable(getConfigurationProperty(key, null))
.orElseThrow(() -> new EnvConfigException(String.format("Missing required variable '%s'", key)));
private String getRequiredConfigProperty(final EnvConfigKey key) {
return Optional.ofNullable(getConfigProperty(key, null))
.orElseThrow(() -> new EnvConfigException(String.format("Missing required variable '%s'", key.getProperty())));
}

boolean isConfigKeepassEnabled() {
return Boolean.parseBoolean(getConfigurationProperty(EnvConfigUtils.CONFIG_KEEPASS_ENABLED_KEY, "false"));
return Boolean.parseBoolean(getConfigProperty(EnvConfigKey.CONFIG_KEEPASS_ENABLED, "false"));
}

EnvConfigKeepassProperties getKeepassProperties() {
return new EnvConfigKeepassProperties(new File(getConfigurationProperty(EnvConfigUtils.CONFIG_KEEPASS_FILENAME_KEY, getBuildDir())).getName(),
getRequiredConfigKey(EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY));
return new EnvConfigKeepassProperties(new File(getConfigProperty(EnvConfigKey.CONFIG_KEEPASS_FILENAME, getBuildDir())).getName(),
getRequiredConfigProperty(EnvConfigKey.CONFIG_KEEPASS_MASTERKEY));
}

boolean isConfigVaultEnabled() {
return Boolean.parseBoolean(getConfigurationProperty(EnvConfigUtils.CONFIG_VAULT_ENABLED_KEY, "false"));
return Boolean.parseBoolean(getConfigProperty(EnvConfigKey.CONFIG_VAULT_ENABLED, "false"));
}

EnvConfigVaultProperties getVaultProperties() {
return new EnvConfigVaultProperties(getRequiredConfigKey(EnvConfigUtils.CONFIG_VAULT_ADDRESS_KEY),
getRequiredConfigKey(EnvConfigUtils.CONFIG_VAULT_NAMESPACE_KEY),
getRequiredConfigKey(EnvConfigUtils.CONFIG_VAULT_TOKEN_KEY),
getRequiredConfigKey(EnvConfigUtils.CONFIG_VAULT_SECRET_PATH_KEY),
getConfigurationProperty(EnvConfigUtils.CONFIG_VAULT_DEFAULT_PATH_KEY, null),
Integer.parseInt(getConfigurationProperty(EnvConfigUtils.CONFIG_VAULT_VALIDATE_MAX_RETRIES, "5")));
return new EnvConfigVaultProperties(getRequiredConfigProperty(EnvConfigKey.CONFIG_VAULT_ADDRESS),
getRequiredConfigProperty(EnvConfigKey.CONFIG_VAULT_NAMESPACE),
getRequiredConfigProperty(EnvConfigKey.CONFIG_VAULT_TOKEN),
getRequiredConfigProperty(EnvConfigKey.CONFIG_VAULT_SECRET_PATH),
getConfigProperty(EnvConfigKey.CONFIG_VAULT_DEFAULT_PATH, null),
Integer.parseInt(getConfigProperty(EnvConfigKey.CONFIG_VAULT_VALIDATE_MAX_RETRIES, "5")));
}
}
15 changes: 0 additions & 15 deletions src/main/java/com/github/sitture/envconfig/EnvConfigUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ final class EnvConfigUtils {
public static final String CONFIG_PATH_DEFAULT = "config";
public static final String CONFIG_ENV_DEFAULT = "default";
public static final String CONFIG_DELIMITER_DEFAULT = ",";
private static final String CONFIG_PREFIX = "env.config.";
public static final String CONFIG_PATH_KEY = CONFIG_PREFIX + "path";
public static final String CONFIG_ENV_KEY = CONFIG_PREFIX + "environment";
public static final String CONFIG_PROFILE_KEY = CONFIG_PREFIX + "profile";
public static final String CONFIG_PROFILES_PATH_KEY = CONFIG_PREFIX + "profiles.path";
public static final String CONFIG_KEEPASS_ENABLED_KEY = CONFIG_PREFIX + "keepass.enabled";
public static final String CONFIG_KEEPASS_FILENAME_KEY = CONFIG_PREFIX + "keepass.filename";
public static final String CONFIG_KEEPASS_MASTERKEY_KEY = CONFIG_PREFIX + "keepass.masterkey";
public static final String CONFIG_VAULT_ENABLED_KEY = CONFIG_PREFIX + "vault.enabled";
public static final String CONFIG_VAULT_ADDRESS_KEY = CONFIG_PREFIX + "vault.address";
public static final String CONFIG_VAULT_NAMESPACE_KEY = CONFIG_PREFIX + "vault.namespace";
public static final String CONFIG_VAULT_DEFAULT_PATH_KEY = CONFIG_PREFIX + "vault.default.secret.path";
public static final String CONFIG_VAULT_SECRET_PATH_KEY = CONFIG_PREFIX + "vault.secret.path";
public static final String CONFIG_VAULT_TOKEN_KEY = CONFIG_PREFIX + "vault.token";
public static final String CONFIG_VAULT_VALIDATE_MAX_RETRIES = CONFIG_PREFIX + "vault.validate.token.max.retries";

private EnvConfigUtils() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ void testParentEnvironmentTakesPriorityWhenEnvVarAndDefaultValueSame() {
}

private void setKeepassEnabled() {
systemProperties.set(EnvConfigUtils.CONFIG_KEEPASS_ENABLED_KEY, true);
systemProperties.set(EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY, CONFIG_KEEPASS_MASTERKEY);
systemProperties.set(EnvConfigKey.CONFIG_KEEPASS_ENABLED.getProperty(), true);
systemProperties.set(EnvConfigKey.CONFIG_KEEPASS_MASTERKEY.getProperty(), CONFIG_KEEPASS_MASTERKEY);
}

private void setEnvironment(final String environment) {
systemProperties.set(EnvConfigUtils.CONFIG_ENV_KEY, environment);
systemProperties.set(EnvConfigKey.CONFIG_ENV.getProperty(), environment);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void setUp() {

@AfterEach
void tearDown() {
System.clearProperty(EnvConfigUtils.CONFIG_PROFILE_KEY);
System.clearProperty(EnvConfigUtils.CONFIG_PROFILES_PATH_KEY);
System.clearProperty(EnvConfigKey.CONFIG_PROFILE.getProperty());
System.clearProperty(EnvConfigKey.CONFIG_PROFILES_PATH.getProperty());
}

@Test
Expand Down Expand Up @@ -154,15 +154,15 @@ void testThrowsExceptionWhenNoPropertiesInProfile() {
}

private void setEnvironment(final String environment) {
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, environment);
System.setProperty(EnvConfigKey.CONFIG_ENV.getProperty(), environment);
}

private void setProfile(final String profile) {
System.setProperty(EnvConfigUtils.CONFIG_PROFILE_KEY, profile);
System.setProperty(EnvConfigKey.CONFIG_PROFILE.getProperty(), profile);
}

private void setProfilePath(final String path) {
System.setProperty(EnvConfigUtils.CONFIG_PROFILES_PATH_KEY, path);
System.setProperty(EnvConfigKey.CONFIG_PROFILES_PATH.getProperty(), path);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class EnvConfigPropertiesTest {

@AfterEach
void tearDown() {
System.clearProperty(EnvConfigUtils.CONFIG_PATH_KEY);
System.clearProperty(EnvConfigUtils.CONFIG_KEEPASS_ENABLED_KEY);
System.clearProperty(EnvConfigUtils.CONFIG_KEEPASS_FILENAME_KEY);
System.clearProperty(EnvConfigUtils.CONFIG_PROFILE_KEY);
System.clearProperty(EnvConfigUtils.CONFIG_PROFILES_PATH_KEY);
System.clearProperty(EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY);
System.clearProperty(EnvConfigKey.CONFIG_PATH.getProperty());
System.clearProperty(EnvConfigKey.CONFIG_KEEPASS_ENABLED.getProperty());
System.clearProperty(EnvConfigKey.CONFIG_KEEPASS_FILENAME.getProperty());
System.clearProperty(EnvConfigKey.CONFIG_PROFILE.getProperty());
System.clearProperty(EnvConfigKey.CONFIG_PROFILES_PATH.getProperty());
System.clearProperty(EnvConfigKey.CONFIG_KEEPASS_MASTERKEY.getProperty());
}

@Test
Expand All @@ -30,31 +30,31 @@ void testCanGetBuildDir() {
@Test
void testCanGetConfigProfile() {
final EnvConfigProperties configProperties = new EnvConfigProperties();
System.clearProperty(EnvConfigUtils.CONFIG_PROFILE_KEY);
System.clearProperty(EnvConfigKey.CONFIG_PROFILE.getProperty());
Assertions.assertEquals("", configProperties.getConfigProfile(), "invalid config-profile!");
System.setProperty(EnvConfigUtils.CONFIG_PROFILE_KEY, "test-profile");
System.setProperty(EnvConfigKey.CONFIG_PROFILE.getProperty(), "test-profile");
Assertions.assertEquals("test-profile", configProperties.getConfigProfile());
}

@Test
void testCanGetEnvironmentsList() {
// when config.environment isn't specified
System.clearProperty(EnvConfigUtils.CONFIG_ENV_KEY);
System.clearProperty(EnvConfigKey.CONFIG_ENV.getProperty());
Assertions.assertEquals(List.of("default"), new EnvConfigProperties().getEnvironments());
Assertions.assertEquals("default", new EnvConfigProperties().getCurrentEnvironment());
// when a single environment is specified
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "test ");
System.setProperty(EnvConfigKey.CONFIG_ENV.getProperty(), "test ");
Assertions.assertEquals(List.of("test", "default"), new EnvConfigProperties().getEnvironments());
Assertions.assertEquals("test", new EnvConfigProperties().getCurrentEnvironment());
// when a multiple environments are specified
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "test , TEST2");
System.setProperty(EnvConfigKey.CONFIG_ENV.getProperty(), "test , TEST2");
Assertions.assertEquals(List.of("test2", "test", "default"), new EnvConfigProperties().getEnvironments());
Assertions.assertEquals("test2", new EnvConfigProperties().getCurrentEnvironment());
// when a default specified in environments
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "DEFAULT,alpha,zen");
System.setProperty(EnvConfigKey.CONFIG_ENV.getProperty(), "DEFAULT,alpha,zen");
Assertions.assertEquals(List.of("zen", "alpha", "default"), new EnvConfigProperties().getEnvironments());
// when only default specified in environments
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "default");
System.setProperty(EnvConfigKey.CONFIG_ENV.getProperty(), "default");
Assertions.assertEquals(List.of("default"), new EnvConfigProperties().getEnvironments());
}

Expand All @@ -70,7 +70,7 @@ void testCanGetConfigAndProfilePath() {

@Test
void testExceptionWhenConfigPathDoesNotExist() {
System.setProperty(EnvConfigUtils.CONFIG_PATH_KEY, "/non/existing/dir");
System.setProperty(EnvConfigKey.CONFIG_PATH.getProperty(), "/non/existing/dir");
final EnvConfigException exception = Assertions.assertThrows(EnvConfigException.class,
() -> new EnvConfigProperties().getConfigPath("env"));
Assertions.assertEquals("'/non/existing/dir' does not exist or not a valid config directory!",
Expand All @@ -79,7 +79,7 @@ void testExceptionWhenConfigPathDoesNotExist() {

@Test
void testExceptionWhenConfigProfilePathDoesNotExist() {
System.setProperty(EnvConfigUtils.CONFIG_PROFILES_PATH_KEY, "/non/existing/dir");
System.setProperty(EnvConfigKey.CONFIG_PROFILES_PATH.getProperty(), "/non/existing/dir");
final EnvConfigException exception = Assertions.assertThrows(EnvConfigException.class,
() -> new EnvConfigProperties().getConfigProfilePath("env", "profile"));
Assertions.assertEquals("'/non/existing/dir' does not exist or not a valid config directory!",
Expand All @@ -91,7 +91,7 @@ void testCanGetConfigPathWhenRelative() throws IOException {
final Path directory = Files.createTempDirectory(Paths.get("config"), "sample-dir");
directory.toFile().deleteOnExit();
// when config.dir is set to relative path
System.setProperty(EnvConfigUtils.CONFIG_PATH_KEY, directory.toString());
System.setProperty(EnvConfigKey.CONFIG_PATH.getProperty(), directory.toString());
final EnvConfigProperties configProperties = new EnvConfigProperties();
Assertions.assertEquals(Paths.get(directory.toAbsolutePath().toString(), "foo"),
configProperties.getConfigPath("foo"), "Incorrect config path");
Expand All @@ -104,7 +104,7 @@ void testCanGetConfigPathWhenAbsoluteWithin() throws IOException {
final Path directory = Files.createTempDirectory(Path.of(new EnvConfigProperties().getBuildDir()), "sample-dir");
directory.toFile().deleteOnExit();
// when config.dir is set to absolute
System.setProperty(EnvConfigUtils.CONFIG_PATH_KEY, directory.toString());
System.setProperty(EnvConfigKey.CONFIG_PATH.getProperty(), directory.toString());
final EnvConfigProperties configProperties = new EnvConfigProperties();
Assertions.assertEquals(Paths.get(directory.toString(), "foo"),
configProperties.getConfigPath("foo"), "Incorrect config path");
Expand All @@ -117,7 +117,7 @@ void testCanGetConfigPathWhenAbsolute() throws IOException {
final Path directory = Files.createTempDirectory("sample-dir");
directory.toFile().deleteOnExit();
// when config.dir is set to absolute
System.setProperty(EnvConfigUtils.CONFIG_PATH_KEY, directory.toString());
System.setProperty(EnvConfigKey.CONFIG_PATH.getProperty(), directory.toString());
final EnvConfigProperties configProperties = new EnvConfigProperties();
Assertions.assertEquals(Paths.get(directory.toString(), "foo"),
configProperties.getConfigPath("foo"), "Incorrect config path");
Expand All @@ -129,32 +129,32 @@ void testCanGetConfigPathWhenAbsolute() throws IOException {
void testCanGetConfigKeepassEnabled() {
final EnvConfigProperties configProperties = new EnvConfigProperties();
Assertions.assertFalse(configProperties.isConfigKeepassEnabled(), "Incorrect keepass.enabled");
System.setProperty(EnvConfigUtils.CONFIG_KEEPASS_ENABLED_KEY, "true");
System.setProperty(EnvConfigKey.CONFIG_KEEPASS_ENABLED.getProperty(), "true");
Assertions.assertTrue(configProperties.isConfigKeepassEnabled(), "Incorrect keepass.enabled");
}

@Test
void testCanGetConfigKeepassFileName() {
final EnvConfigProperties configProperties = new EnvConfigProperties();
System.setProperty(EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY, "foo");
System.setProperty(EnvConfigKey.CONFIG_KEEPASS_MASTERKEY.getProperty(), "foo");
Assertions.assertEquals(new File(configProperties.getBuildDir()).getName(),
configProperties.getKeepassProperties().getFilename(), "Incorrect keepass.filename path");
}

@Test
void testCanGetConfigKeepassFileNameWhenRelative() {
final EnvConfigProperties configProperties = new EnvConfigProperties();
System.setProperty(EnvConfigUtils.CONFIG_KEEPASS_FILENAME_KEY, "foobar.kdbx");
System.setProperty(EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY, "foo");
System.setProperty(EnvConfigKey.CONFIG_KEEPASS_FILENAME.getProperty(), "foobar.kdbx");
System.setProperty(EnvConfigKey.CONFIG_KEEPASS_MASTERKEY.getProperty(), "foo");
Assertions.assertEquals("foobar.kdbx",
configProperties.getKeepassProperties().getFilename(), "Incorrect keepass.filename path");
}

@Test
void testCanGetConfigKeepassFileNameWhenAbsolute() {
final EnvConfigProperties configProperties = new EnvConfigProperties();
System.setProperty(EnvConfigUtils.CONFIG_KEEPASS_FILENAME_KEY, "/dir/foobar.kdbx");
System.setProperty(EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY, "foo");
System.setProperty(EnvConfigKey.CONFIG_KEEPASS_FILENAME.getProperty(), "/dir/foobar.kdbx");
System.setProperty(EnvConfigKey.CONFIG_KEEPASS_MASTERKEY.getProperty(), "foo");
Assertions.assertEquals("foobar.kdbx",
configProperties.getKeepassProperties().getFilename(), "Incorrect keepass.filename path");
}
Expand Down
Loading

0 comments on commit 1599a3a

Please sign in to comment.