
com.github.sitture.envconfig.KeepassConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of env-config Show documentation
Show all versions of env-config Show documentation
A simple utility to manage environment configs in Java-based projects by merging *.properties files with environment variables overrides.
package com.github.sitture.envconfig;
import de.slackspace.openkeepass.KeePassDatabase;
import de.slackspace.openkeepass.domain.Group;
import de.slackspace.openkeepass.domain.KeePassFile;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.MapConfiguration;
class KeepassConfiguration {
private static final String KEEPASS_DB_FILE_EXTENSION = ".kdbx";
private final KeePassFile keePassFile;
KeepassConfiguration(final EnvConfigKeepassProperties keepassProperties) {
final String groupName = keepassProperties.getFilename();
final String keePassGroupName = null != groupName && groupName.endsWith(KEEPASS_DB_FILE_EXTENSION)
? groupName.split(KEEPASS_DB_FILE_EXTENSION)[0]
: groupName;
keePassFile = KeePassDatabase.getInstance(getKeepassDatabaseFile(keePassGroupName.concat(KEEPASS_DB_FILE_EXTENSION)))
.openDatabase(keepassProperties.getMasterKey());
}
private static String getProcessedPropertyKey(final String envVar) {
return envVar.replaceAll("_", ".").toLowerCase();
}
public Configuration getConfiguration(final String env) {
final String keePassGroupName = !keePassFile.getTopGroups().isEmpty()
? keePassFile.getTopGroups().get(0).getName()
: "Root";
return new MapConfiguration(getEntriesMap(keePassGroupName, env));
}
private File getKeepassDatabaseFile(final String fileName) {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
final URL resource = classLoader.getResource(fileName);
File keepassFile; // NOPMD
if (null == resource) {
throw new EnvConfigException(String.format("Database %s does not exist!", fileName));
} else {
try {
keepassFile = new File(resource.toURI());
} catch (final URISyntaxException e) {
keepassFile = new File(resource.getFile());
}
}
return keepassFile;
}
private Map getEntriesMap(final String groupName, final String env) {
final Optional projectGroup = keePassFile.getTopGroups().stream()
.filter(group -> group.getName().trim().equals(groupName)).findFirst();
if (projectGroup.isEmpty()) {
throw new IllegalArgumentException(String.format("Group %s not found in the database!", groupName));
}
final Optional envGroup = projectGroup.get().getGroups().stream().filter(group -> group.getName().trim().equals(env)).findFirst();
final Map entriesMap = new HashMap<>();
envGroup.ifPresent(group -> group.getEntries()
.forEach(entry -> {
entriesMap.put(entry.getTitle().trim(), entry.getPassword());
entriesMap.put(getProcessedPropertyKey(entry.getTitle().trim()), entry.getPassword());
}));
return entriesMap;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy