com.codacy.scoobydoo.configuration.Configuration Maven / Gradle / Ivy
package com.codacy.scoobydoo.configuration;
import com.codacy.scoobydoo.Decryptor;
import com.codacy.scoobydoo.LoggingHelper;
import com.codacy.scoobydoo.SystemEnvWrapper;
import java.util.Properties;
public class Configuration {
private final Properties properties;
private final Decryptor decryptor;
private final SystemEnvWrapper systemEnvWrapper;
private SsmClientWrapper ssmClientWrapper;
private LoggingHelper loggingHelper;
public Configuration(Properties properties, Decryptor decryptor, SystemEnvWrapper systemEnvWrapper, LoggingHelper loggingHelper, SsmClientWrapper ssmClientWrapper) {
this.properties = properties;
this.decryptor = decryptor;
this.loggingHelper = loggingHelper;
this.systemEnvWrapper = systemEnvWrapper;
this.ssmClientWrapper = ssmClientWrapper;
}
public Configuration(Properties properties, Decryptor decryptor, SystemEnvWrapper systemEnvWrapper, LoggingHelper loggingHelper) {
this.properties = properties;
this.decryptor = decryptor;
this.loggingHelper = loggingHelper;
this.systemEnvWrapper = systemEnvWrapper;
}
public String getEncryptedData(String key) {
String valueFromEnvFile = systemEnvWrapper.getDataFromEnvironmentVariable(key, true);
if (valueFromEnvFile != null) {
return valueFromEnvFile;
}
return getDataFromConfigurationFile(key, true);
}
public String getData(String key) {
String valueFromEnvFile = systemEnvWrapper.getDataFromEnvironmentVariable(key, false);
if (valueFromEnvFile != null) {
return valueFromEnvFile;
}
return getDataFromConfigurationFile(key, false);
}
public String getSsmParameter(String key, Boolean encrypted) {
if (key == null) {
IllegalArgumentException illegalArgumentException = new IllegalArgumentException("Key is null!");
loggingHelper.error("Error getting key " + key, illegalArgumentException.getMessage());
throw illegalArgumentException;
}
return ssmClientWrapper.getParameter(key, encrypted, true);
}
public void setSsmParameter(String key, String value, Boolean secure) {
if (key == null || value == null) {
IllegalArgumentException illegalArgumentException = new IllegalArgumentException("Key (" + key + ") or value (" + value + ") are null!");
loggingHelper.error("Error setting SSM parameter " + key, illegalArgumentException.getMessage());
throw illegalArgumentException;
}
ssmClientWrapper.putParameter(key, value, secure);
}
public void deleteSsmParameter(String key) {
if (key == null) {
IllegalArgumentException illegalArgumentException = new IllegalArgumentException("Key is null!");
loggingHelper.error("Error deleting SSM parameter", illegalArgumentException.getMessage());
throw illegalArgumentException;
}
ssmClientWrapper.deleteParameter(key);
}
private String getDataFromConfigurationFile(String key, Boolean encrypt) {
Object value = properties.get(key);
if (value == null) {
IllegalArgumentException illegalArgumentException = new IllegalArgumentException("Property " + key + " is not defined on the properties file!");
loggingHelper.error("Error getting key " + key, illegalArgumentException.getMessage());
throw illegalArgumentException;
}
String[] valueSpliced = value.toString().split(":");
if (valueSpliced[0].equals("ssm")) {
return getSsmParameter(valueSpliced[1], encrypt);
}
if (encrypt) {
return decryptor.decrypt(value.toString());
}
return value.toString();
}
public Properties getConfiguration() {
return properties;
}
public void addProperty(String key, String value) {
if (key.isEmpty() || value.isEmpty()) {
IllegalArgumentException illegalArgumentException = new IllegalArgumentException("Key or value is empty");
loggingHelper.error("Error adding key", illegalArgumentException.getMessage());
throw illegalArgumentException;
}
properties.setProperty(key, value);
}
}