use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project cerberus by Nike-Inc.
the class SecretsManagerSecretEngine method decrypt.
@Override
public byte[] decrypt(EncryptedSecret encryptedSecret) {
String secretName = encryptedSecret.getParams().get(SECRET_NAME);
String secretRegion = encryptedSecret.getParams().get(SECRET_REGION);
String secretKey = encryptedSecret.getParams().get(SECRET_KEY);
AWSSecretsManager client = AWSSecretsManagerClientBuilder.standard().withRegion(secretRegion).build();
byte[] binarySecret = null;
GetSecretValueRequest getSecretValueRequest = new GetSecretValueRequest().withSecretId(secretName);
GetSecretValueResult getSecretValueResult = null;
try {
getSecretValueResult = client.getSecretValue(getSecretValueRequest);
} catch (Exception e) {
log.error("An error occurred when trying to use AWS Secrets Manager to fetch: [secretName: {}, secretRegion: {}, secretKey: {}]", secretName, secretRegion, secretKey, e);
throw new RuntimeException("Failed to fetch secret from AWS Secrets Manager", e);
}
if (getSecretValueResult.getSecretString() != null) {
String secret = getSecretValueResult.getSecretString();
Gson gson = new Gson();
Type type = new TypeToken<Map<String, String>>() {
}.getType();
Map<String, String> myMap = gson.fromJson(secret, type);
binarySecret = myMap.get(secretKey).getBytes(StandardCharsets.UTF_8);
} else {
binarySecret = getSecretValueResult.getSecretBinary().array();
}
return binarySecret;
}
use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project spring-cloud-aws by awspring.
the class AwsSecretsManagerPropertySource method readSecretValue.
private void readSecretValue(GetSecretValueRequest secretValueRequest) {
try {
GetSecretValueResult secretValueResult = source.getSecretValue(secretValueRequest);
Map<String, Object> secretMap = jsonMapper.readValue(secretValueResult.getSecretString(), new TypeReference<Map<String, Object>>() {
});
for (Map.Entry<String, Object> secretEntry : secretMap.entrySet()) {
LOG.debug("Populating property retrieved from AWS Secrets Manager: " + secretEntry.getKey());
properties.put(secretEntry.getKey(), secretEntry.getValue());
}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project spring-cloud-aws by awspring.
the class AwsSecretsManagerPropertySourceLocatorTest method contextSpecificOrderExpected.
@Test
void contextSpecificOrderExpected() {
AwsSecretsManagerProperties properties = new AwsSecretsManagerPropertiesBuilder().withDefaultContext("application").withName("messaging-service").build();
GetSecretValueResult secretValueResult = new GetSecretValueResult();
secretValueResult.setSecretString("{\"key1\": \"value1\", \"key2\": \"value2\"}");
when(smClient.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult);
AwsSecretsManagerPropertySourceLocator locator = new AwsSecretsManagerPropertySourceLocator(smClient, properties);
env.setActiveProfiles("test");
locator.locate(env);
List<String> contextToBeTested = new ArrayList<>(locator.getContexts());
assertThat(contextToBeTested.get(0)).isEqualTo("/secret/messaging-service_test");
assertThat(contextToBeTested.get(1)).isEqualTo("/secret/messaging-service");
assertThat(contextToBeTested.get(2)).isEqualTo("/secret/application_test");
assertThat(contextToBeTested.get(3)).isEqualTo("/secret/application");
}
use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project spring-cloud-aws by awspring.
the class AwsSecretsManagerPropertySourceLocatorTest method locate_nameSpecifiedInConstructor_returnsPropertySourceWithSpecifiedName.
@Test
void locate_nameSpecifiedInConstructor_returnsPropertySourceWithSpecifiedName() {
GetSecretValueResult secretValueResult = new GetSecretValueResult();
secretValueResult.setSecretString("{\"key1\": \"value1\", \"key2\": \"value2\"}");
when(smClient.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult);
AwsSecretsManagerProperties properties = new AwsSecretsManagerProperties();
AwsSecretsManagerPropertySourceLocator locator = new AwsSecretsManagerPropertySourceLocator("my-name", smClient, properties);
PropertySource propertySource = locator.locate(env);
assertThat(propertySource.getName()).isEqualTo("my-name");
}
use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project spring-cloud-aws by awspring.
the class AwsSecretsManagerPropertySourceLocatorTest method contextExpectedToHave4Elements.
@Test
void contextExpectedToHave4Elements() {
AwsSecretsManagerProperties properties = new AwsSecretsManagerPropertiesBuilder().withDefaultContext("application").withName("messaging-service").build();
GetSecretValueResult secretValueResult = new GetSecretValueResult();
secretValueResult.setSecretString("{\"key1\": \"value1\", \"key2\": \"value2\"}");
when(smClient.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult);
AwsSecretsManagerPropertySourceLocator locator = new AwsSecretsManagerPropertySourceLocator(smClient, properties);
env.setActiveProfiles("test");
locator.locate(env);
assertThat(locator.getContexts()).hasSize(4);
}
Aggregations