Skip to content

Commit

Permalink
Fixes an issue with resolving config properties (#248)
Browse files Browse the repository at this point in the history
## 1.12.1

### Fixes 

- Fixes an issue with resolving config properties
  • Loading branch information
haroon-sheikh authored Nov 4, 2024
1 parent 5c9ced5 commit 2c22286
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 28 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.12.1

### Fixes

- Fixes an issue with resolving config properties

## 1.12.0

### Updates
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.sitture</groupId>
<artifactId>env-config</artifactId>
<packaging>jar</packaging>
<version>1.12.0</version>
<version>1.12.1</version>

<name>env-config</name>
<description>A simple utility to manage environment configs in Java-based projects by merging *.properties files with environment variables overrides.</description>
Expand Down Expand Up @@ -72,7 +72,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down Expand Up @@ -115,7 +115,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.10.1</version>
<version>3.11.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.sitture.envconfig;

import static com.github.sitture.envconfig.EnvConfigUtils.getConfigProperty;
import static com.github.sitture.envconfig.EnvConfigUtils.getRequiredConfigProperty;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -8,12 +11,9 @@
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class EnvConfigProperties {

private static final Logger LOG = LoggerFactory.getLogger(EnvConfigProperties.class);
private final List<String> environments;
private final Path configDir;
private final Path configProfilesPath;
Expand Down Expand Up @@ -74,23 +74,6 @@ private Path getPath(final Path configPath) {
return configPath;
}

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 EnvConfigKey key) {
LOG.debug("Getting {} from system.env", key);
return Optional.ofNullable(System.getenv(EnvConfigUtils.getProcessedEnvKey(key.getProperty())))
.orElse(System.getenv(key.getProperty()));
}

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(getConfigProperty(EnvConfigKey.CONFIG_KEEPASS_ENABLED, "false"));
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/github/sitture/envconfig/EnvConfigUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -30,4 +31,14 @@ static List<String> getListOfValues(final String value, final String delimiter)
.collect(Collectors.toList());
}

static String getConfigProperty(final EnvConfigKey key, final String defaultValue) {
return Optional.ofNullable(System.getProperty(key.getProperty()))
.orElse(Optional.ofNullable(System.getenv(key.getEnvProperty())).orElse(defaultValue));
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class KeepassConfiguration {
}
}

private static String getProcessedPropertyKey(final String envVar) {
return envVar.replaceAll("_", ".").toLowerCase();
}

public Configuration getConfiguration(final String env) {
final String keePassGroupName = !database.getRootGroup().getGroups().isEmpty()
? database.getRootGroup().getGroups().get(0).getName()
Expand All @@ -63,7 +59,7 @@ private Map<String, String> getEntriesMap(final String groupName, final String e
envGroup.ifPresent(group -> group.getEntries()
.forEach(entry -> {
entriesMap.put(entry.getTitle().trim(), entry.getPassword());
entriesMap.put(getProcessedPropertyKey(entry.getTitle().trim()), entry.getPassword());
entriesMap.put(EnvConfigUtils.getProcessedPropertyKey(entry.getTitle().trim()), entry.getPassword());
}));
return entriesMap;
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/github/sitture/envconfig/EnvConfigKeyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.sitture.envconfig;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class EnvConfigKeyTest {

@Test
void testCanGetConfigKey() {
assertEquals("env.config.environment", EnvConfigKey.CONFIG_ENV.getProperty());
assertEquals("ENV_CONFIG_ENVIRONMENT", EnvConfigKey.CONFIG_ENV.getEnvProperty());
}

}
36 changes: 36 additions & 0 deletions src/test/java/com/github/sitture/envconfig/EnvConfigUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

@ExtendWith(SystemStubsExtension.class)
class EnvConfigUtilsTest {

@SystemStub
private final EnvironmentVariables environmentVariables = new EnvironmentVariables();

@SystemStub
private final SystemProperties systemProperties = new SystemProperties();

@Test
void testCanGetProcessedPropertyKey() {
assertEquals("foo.key", EnvConfigUtils.getProcessedPropertyKey("FOO_KEY"));
Expand All @@ -24,4 +37,27 @@ void testCanGetListOfValues() {
assertEquals(List.of(), EnvConfigUtils.getListOfValues(null, EnvConfigUtils.CONFIG_DELIMITER_DEFAULT));
}

@Test
void testCanGetConfigProperty() {
// When no system property or environment variable is set
assertEquals("default", EnvConfigUtils.getConfigProperty(EnvConfigKey.CONFIG_ENV, "default"));
// When system property is set
systemProperties.set(EnvConfigKey.CONFIG_ENV.getProperty(), "property");
assertEquals("property", EnvConfigUtils.getConfigProperty(EnvConfigKey.CONFIG_ENV, "default"));
// When environment var is also set
environmentVariables.set(EnvConfigKey.CONFIG_ENV.getEnvProperty(), "env");
// Then system property takes precedence
assertEquals("property", EnvConfigUtils.getConfigProperty(EnvConfigKey.CONFIG_ENV, "default"));
// when only environment variable is set
systemProperties.remove(EnvConfigKey.CONFIG_ENV.getProperty());
assertEquals("env", EnvConfigUtils.getConfigProperty(EnvConfigKey.CONFIG_ENV, "default"));
}

@Test
void testExceptionWhenRequiredConfigMissing() {
final EnvConfigException exception = Assertions.assertThrows(EnvConfigException.class,
() -> EnvConfigUtils.getRequiredConfigProperty(EnvConfigKey.CONFIG_KEEPASS_ENABLED));
assertEquals(String.format("Missing required variable '%s'", EnvConfigKey.CONFIG_KEEPASS_ENABLED.getProperty()), exception.getMessage());
}

}

0 comments on commit 2c22286

Please sign in to comment.