com.codacy.scoobydoo.configuration.SsmClientWrapper Maven / Gradle / Ivy
package com.codacy.scoobydoo.configuration;
import com.codacy.scoobydoo.LoggingHelper;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.services.ssm.SsmClient;
import software.amazon.awssdk.services.ssm.model.DeleteParameterRequest;
import software.amazon.awssdk.services.ssm.model.GetParameterRequest;
import software.amazon.awssdk.services.ssm.model.GetParameterResponse;
import software.amazon.awssdk.services.ssm.model.ParameterNotFoundException;
import software.amazon.awssdk.services.ssm.model.PutParameterRequest;
public class SsmClientWrapper {
private final SsmClient ssmClient;
public SsmClientWrapper(String awsProfile) {
LoggingHelper.info("Creating SSM client.");
ssmClient = SsmClient.builder()
.credentialsProvider(ProfileCredentialsProvider.create(awsProfile))
.build();
}
public String getParameter(String key, boolean encryption, boolean canBeNull) {
if (key == null) {
IllegalArgumentException illegalArgumentException = new IllegalArgumentException("Key is null.");
LoggingHelper.error("Error getting parameter.", illegalArgumentException);
throw illegalArgumentException;
}
GetParameterRequest getparameterRequest = GetParameterRequest.builder()
.name(key)
.withDecryption(encryption)
.build();
try {
LoggingHelper.info("Getting ssm parameter with key [" + key + "].");
final GetParameterResponse response = ssmClient.getParameter(getparameterRequest);
return response.parameter().value();
} catch (ParameterNotFoundException parameterNotFoundException) {
if (canBeNull) {
LoggingHelper.debug("Parameter not found on SSM parameters store. Returning null.");
return null;
} else {
LoggingHelper.error("Parameter not found on SSM parameters store.", parameterNotFoundException);
throw parameterNotFoundException;
}
} catch (Exception e) {
LoggingHelper.error("Error getting parameter.", e);
throw e;
}
}
public void putParameter(String key, String value, Boolean secure) {
if (key == null || value == null || key.isEmpty() || value.isEmpty()) {
IllegalArgumentException illegalArgumentException = new IllegalArgumentException("Key or value is null.");
LoggingHelper.error("Error setting SSM parameter.", illegalArgumentException);
throw illegalArgumentException;
}
// TODO: is it ok for the value to be logged if the value should be secure?
LoggingHelper.info("Setting key [" + key + "] with value [" + value + "] and with secure boolean [" + secure + "] in SSM.");
PutParameterRequest putParameterRequest = getPutParameterRequest(key, value, secure);
try {
ssmClient.putParameter(putParameterRequest);
} catch (Exception e) {
LoggingHelper.error("Error setting parameter.", e);
throw e;
}
}
public void deleteParameter(String key) {
LoggingHelper.info("Deleting key [" + key + "].");
try {
DeleteParameterRequest deleteParameterRequest = DeleteParameterRequest.builder().name(key).build();
ssmClient.deleteParameter(deleteParameterRequest);
} catch (ParameterNotFoundException parameterNotFoundException) {
LoggingHelper.error("Error getting parameter. Parameter not found on SSM parameters store.", parameterNotFoundException);
throw parameterNotFoundException;
} catch (Exception e) {
LoggingHelper.error("Error getting parameter.", e);
throw e;
}
}
private PutParameterRequest getPutParameterRequest(String key, String value, Boolean secure) {
PutParameterRequest.Builder putParameterRequest = PutParameterRequest.builder()
.name(key)
.value(value);
if (secure) {
putParameterRequest.type("SecureString");
} else {
putParameterRequest.type("String");
}
putParameterRequest.overwrite(true);
return putParameterRequest.build();
}
}