Skip to content

Commit

Permalink
System environment should now take precedence over keepass (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
haroon-sheikh committed Jan 25, 2023
1 parent 8cc3a29 commit b498e8e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project are documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 1.2.3

### Fixed

- System environment should now take precedence over keepass

## 1.2.2

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class EnvConfigLoader {
EnvConfigLoader() {
final List<String> environments = configProperties.getEnvironments();
final String configProfile = configProperties.getConfigProfile();
loadKeepassConfigurations(environments);
loadEnvConfigurations(environments);
loadKeepassConfigurations(environments);
if (!configProfile.isEmpty()) {
LOG.debug("Loading properties from profile {} under environments {}", configProfile, environments);
environments.forEach(env -> loadFileConfigurations(new EnvConfigProfileFileList(configProperties.getConfigProfilePath(env, configProfile))));
Expand Down
69 changes: 56 additions & 13 deletions src/test/java/com/github/sitture/envconfig/EnvConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class EnvConfigTest {
private static final String TEST_PROPERTY = "property";
private static final String TEST_VALUE = "value";
private static final String KEEPASS_VALUE = "KEEPASS_VALUE";
public static final String MY_KEEPASS_PROPERTY = "my.keepass.property";

@SystemStub
private final EnvironmentVariables environmentVariables = new EnvironmentVariables();
Expand All @@ -28,6 +29,7 @@ void setUp() {
System.clearProperty(EnvConfigUtils.CONFIG_KEEPASS_FILENAME_KEY);
System.clearProperty(EnvConfigUtils.CONFIG_KEEPASS_ENABLED_KEY);
System.clearProperty(EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY);
System.clearProperty(MY_KEEPASS_PROPERTY);
EnvConfig.reset();
}

Expand Down Expand Up @@ -128,7 +130,7 @@ void testCanGetPropertyWhenMultipleEnv() {

@Test
void testCanGetEntryWhenEnvVarAndDefaultValueSameWithParentEnv() {
environmentVariables.set("property.five", "default");
environmentVariables.set("PROPERTY_FIVE", "default");
final String testEnv = "test-env";
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "test," + testEnv);
// when property.five is set as env variable
Expand Down Expand Up @@ -178,6 +180,18 @@ void testCanGetEntryWhenEnvVarSet() {
Assertions.assertEquals("env.property.six", EnvConfig.get("property.six"));
}

@Test
void testCanGetEntryWhenEnvVarAndPropertySet() {
final String key = "property.precedence";
final String value = "SYS_PROPERTY_VALUE";
environmentVariables.set(key, "SYS_ENV_VALUE");
System.setProperty(key, value);
// when property.precedence is set as env variable
// and property.precedence is set system property
// then value from system property takes priority
Assertions.assertEquals(value, EnvConfig.get(key));
}

@Test
void testCanGetEntryWhenEnvVarSetInMultiEnvs() {
final String testEnv = "test-env";
Expand Down Expand Up @@ -314,7 +328,7 @@ void testCanClearProperty() {
void testCanGetEntryFromKeepassWhenFileNameSpecified() {
System.setProperty(EnvConfigUtils.CONFIG_KEEPASS_FILENAME_KEY, "env-config.kdbx");
enabledKeepass();
Assertions.assertEquals(KEEPASS_VALUE, EnvConfig.get("my.keepass.property"));
Assertions.assertEquals(KEEPASS_VALUE, EnvConfig.get(MY_KEEPASS_PROPERTY));
}

private void enabledKeepass() {
Expand All @@ -327,7 +341,7 @@ private void enabledKeepass() {
void testCanGetFromKeepassWhenFileNameWithSpace() {
System.setProperty(EnvConfigUtils.CONFIG_KEEPASS_FILENAME_KEY, "env config.kdbx");
enabledKeepass();
Assertions.assertEquals(KEEPASS_VALUE, EnvConfig.get("my.keepass.property"));
Assertions.assertEquals(KEEPASS_VALUE, EnvConfig.get(MY_KEEPASS_PROPERTY));
}

@Test
Expand All @@ -342,7 +356,7 @@ void testExceptionWhenKeepassFileMissing() {
void testExceptionWhenKeepassMasterKeyMissing() {
System.setProperty(EnvConfigUtils.CONFIG_KEEPASS_ENABLED_KEY, "true");
final EnvConfigException exception = Assertions.assertThrows(EnvConfigException.class,
() -> EnvConfig.get("my.keepass.property"));
() -> EnvConfig.get(MY_KEEPASS_PROPERTY));
Assertions.assertEquals(String.format("Missing required variable '%s'", EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY),
exception.getMessage());
}
Expand All @@ -352,7 +366,7 @@ void testCanGetEntryFromKeepassDefaultGroup() {
enabledKeepass();
// when my.keepass.property does not exist in default env.
// and only exists default group of keepass
Assertions.assertEquals(KEEPASS_VALUE, EnvConfig.get("my.keepass.property"));
Assertions.assertEquals(KEEPASS_VALUE, EnvConfig.get(MY_KEEPASS_PROPERTY));
Assertions.assertEquals(KEEPASS_VALUE, EnvConfig.get("MY_KEEPASS_PROPERTY"));
}

Expand All @@ -365,22 +379,51 @@ void testCanGetEntryFromKeepassDBWhenMultipleEnvironment() {
// when my.keepass.property exists in test env
// and only exists in keepass env group of keepass
// then keepass takes priority
Assertions.assertEquals("KEEPASS_ENVIRONMENT", EnvConfig.get("my.keepass.property"));
Assertions.assertEquals("KEEPASS_ENVIRONMENT", EnvConfig.get(MY_KEEPASS_PROPERTY));
Assertions.assertEquals("KEEPASS_ENVIRONMENT", EnvConfig.get("MY_KEEPASS_PROPERTY"));
}

@Test
void testCanGetPropertyWhenInSystemEnvAndKeepass() {
environmentVariables.set(EnvConfigUtils.CONFIG_KEEPASS_ENABLED_KEY.replace(".", "_").toUpperCase(), "true");
environmentVariables.set(EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY.replace(".", "_").toUpperCase(), CONFIG_KEEPASS_PASSWORD);
final String sysEnvValue = "SYS_ENV_VALUE";
environmentVariables.set("MY_KEEPASS_PROPERTY", sysEnvValue);
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "keepass");
// When my.keepass.property is set as environment variable
// And my.keepass.property exists in Keepass 'keepass' group
// And my.keepass.property exists in 'keepass' environment files
// Then the value from system environment takes priority
Assertions.assertEquals(sysEnvValue, EnvConfig.get(MY_KEEPASS_PROPERTY));
}

@Test
void testCanGetPropertyWhenInSystemPropertyAndSystemEnvAndKeepass() {
environmentVariables.set(EnvConfigUtils.CONFIG_KEEPASS_ENABLED_KEY.replace(".", "_").toUpperCase(), "true");
environmentVariables.set(EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY.replace(".", "_").toUpperCase(), CONFIG_KEEPASS_PASSWORD);
environmentVariables.set("MY_KEEPASS_PROPERTY", "SYS_ENV_VALUE");
final String sysPropertyValue = "SYS_PROPERTY_VALUE";
System.setProperty(MY_KEEPASS_PROPERTY, sysPropertyValue);
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "keepass");
// When my.keepass.property is set as environment variable
// And my.keepass.property is set as system property
// And my.keepass.property exists in Keepass 'keepass' group
// And my.keepass.property exists in 'keepass' environment files
// Then the value from system property takes priority
Assertions.assertEquals(sysPropertyValue, EnvConfig.get(MY_KEEPASS_PROPERTY));
}

@Test
void testCanGetPropertyFromKeepassWhenMultipleEnv() {
environmentVariables.set(EnvConfigUtils.CONFIG_KEEPASS_ENABLED_KEY.replace(".", "_").toUpperCase(), "true");
environmentVariables.set(EnvConfigUtils.CONFIG_KEEPASS_MASTERKEY_KEY.replace(".", "_").toUpperCase(), CONFIG_KEEPASS_PASSWORD);
environmentVariables.set("MY_KEEPASS_PROPERTY", "KEEPASS_ENV_VALUE");
// environmentVariables.set("PROPERTY_ONE", "KEEPASS_ENV");
final String testEnv = "test-no-keepass";
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "keepass," + testEnv);
Assertions.assertEquals(testEnv, EnvConfig.getEnvironment());
// When a property.one exists in all environments, including default
// Then keepass takes priority
Assertions.assertEquals("KEEPASS_ENVIRONMENT", EnvConfig.get("my.keepass.property"));
// When my.keepass.property exists in Keepass 'keepass' group
// And my.keepass.property exists in 'keepass' environment files
// Then the value from keepass group takes priority
Assertions.assertEquals("KEEPASS_ENVIRONMENT", EnvConfig.get(MY_KEEPASS_PROPERTY));
}

@Test
Expand All @@ -391,7 +434,7 @@ void testCanGetEntryFromKeepassDB() {
// when my.keepass.property exists in test env
// and only exists in default group of keepass
// then keepass takes priority
Assertions.assertEquals(KEEPASS_VALUE, EnvConfig.get("my.keepass.property"));
Assertions.assertEquals(KEEPASS_VALUE, EnvConfig.get(MY_KEEPASS_PROPERTY));
Assertions.assertEquals(KEEPASS_VALUE, EnvConfig.get("MY_KEEPASS_PROPERTY"));
}

Expand All @@ -403,7 +446,7 @@ void testCanGetEntryFromKeepassDisabled() {
// when my.keepass.property exists in test env
// and only exists in default group of keepass
// then keepass takes priority
Assertions.assertEquals("my_value", EnvConfig.get("my.keepass.property"));
Assertions.assertEquals("my_value", EnvConfig.get(MY_KEEPASS_PROPERTY));
}

@Test
Expand Down

0 comments on commit b498e8e

Please sign in to comment.