Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the possibility of configura a fallback file #18

Merged
merged 15 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>f2ea2cb4-c600-4bb5-88e8-e952ff5591ee</groupId>
<artifactId>mule-vault-properties-providers-module</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
adammead marked this conversation as resolved.
Show resolved Hide resolved
<packaging>mule-extension</packaging>
<name>Vault Properties Provider - Mule 4</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -36,9 +39,10 @@ public class VaultConfigurationPropertiesProvider implements ConfigurationProper
* Constructs a VaultConfigurationPropertiesProvider. Vault must not be null.
* @param vault
*/
public VaultConfigurationPropertiesProvider(final Vault vault) {
public VaultConfigurationPropertiesProvider(final Vault vault, final String fallbackFile) {
this.vault = vault;
cachedData = new HashMap<>();
evaluateFileFallbackConfig(fallbackFile);
}

/**
Expand All @@ -48,10 +52,7 @@ public VaultConfigurationPropertiesProvider(final Vault vault) {
* @param property the property to retrieve from the secret
* @return the value of the property or null if the property is not found
*/
private String getProperty(String path, String property) throws DefaultMuleException {



private String getProperty(String path, String property) throws SecretNotFoundException, VaultAccessException, DefaultMuleException {
try {
Map<String, String> data = null;
if (cachedData.containsKey(path)) {
Expand Down Expand Up @@ -171,4 +172,33 @@ private String expandedValue(final String value) {

return result;
}
private void evaluateFileFallbackConfig(String fallbackFile){
if(fallbackFile==null || fallbackFile.isEmpty()) return;
try{
adammead marked this conversation as resolved.
Show resolved Hide resolved
URL resourceUrl = this.getClass().getClassLoader().getResource(fallbackFile);
if(resourceUrl==null) return;
Properties appProps = new Properties();
appProps.load(new FileInputStream(resourceUrl.getPath()));
if(appProps!= null && !appProps.isEmpty())
adammead marked this conversation as resolved.
Show resolved Hide resolved
fillCachedData(appProps);
}catch(Exception e){
logger.error("The follow error happened: "+ e.getMessage());
}
}
private void fillCachedData(Properties properties){
adammead marked this conversation as resolved.
Show resolved Hide resolved
if(properties == null || properties.isEmpty()) return;

properties.entrySet().forEach(entry ->{
String keyS = entry.getKey().toString();
String[] pathElements = keyS.split("\\.");
if(pathElements.length != 2){
logger.warn("FAIL TO TRY TO PARSE THE PROPERTY WITH THE KEY: "+ keyS);
}else{
if(cachedData.containsKey(pathElements[0]))
cachedData.get(pathElements[0]).put(pathElements[1], entry.getValue().toString());
else
cachedData.put(pathElements[0], new HashMap<String,String>(){ { put(pathElements[1], entry.getValue().toString()); } });
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public ComponentIdentifier getSupportedComponentIdentifier() {
public ConfigurationPropertiesProvider createProvider(final ConfigurationParameters parameters,
ResourceProvider externalResourceProvider) {
try {
return new VaultConfigurationPropertiesProvider(getVault(parameters));
return new VaultConfigurationPropertiesProvider(getVault(parameters),
getFileFallback(parameters));
} catch (ConnectionException ce) {
logger.error("Error connecting to Vault", ce);
return null;
Expand Down Expand Up @@ -91,5 +92,17 @@ private Vault getVault(ConfigurationParameters parameters) throws ConnectionExce
}

}
private String getFileFallback(ConfigurationParameters parameters){
try{
for (int i=0;i<parameters.getComplexConfigurationParameters().size();i++) {
String namespace = parameters.getComplexConfigurationParameters().get(i).getFirst().getNamespace();
if (namespace.equals(VaultPropertiesProviderExtension.VAULT_PROPERTIES_PROVIDER.getNamespace()))
return parameters.getComplexConfigurationParameters().get(i).getSecond().getStringParameter("fileFallback");
}
}catch(Exception e){
logger.debug("Not possible to find file fallback configuration!");
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public abstract class AbstractConnectionProvider implements ConnectionProvider<V
@Optional
protected EngineVersion engineVersion;

@DisplayName("File Fallback")
adammead marked this conversation as resolved.
Show resolved Hide resolved
@Parameter
@Optional
protected String fileFallback;

protected abstract TlsContext getTlsContext();

public AbstractConnectionProvider() {
Expand All @@ -41,7 +46,11 @@ public AbstractConnectionProvider(ConfigurationParameters parameters) {
} catch (Exception e) {
logger.debug("kvVersion parameter is not present, or is not a valid value (v1 or v2)", e);
}

try{
fileFallback = parameters.getStringParameter("fileFallback");
} catch (Exception e){
logger.debug("fileFallback parameter is not present", e);
}
}

@Override
Expand Down