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

feat: Modifies the AWS vault implementation to update existing secrets #379

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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import software.amazon.awssdk.services.secretsmanager.model.DeleteSecretRequest;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
import software.amazon.awssdk.services.secretsmanager.model.ResourceNotFoundException;
import software.amazon.awssdk.services.secretsmanager.model.UpdateSecretRequest;

/**
* Vault adapter for AWS Secrets Manager.
Expand Down Expand Up @@ -62,7 +63,7 @@ public AwsSecretsManagerVault(SecretsManagerClient smClient, Monitor monitor, Aw
}

/**
* Creates a new secret. Does not overwrite secrets.
* Creates/Updates a secret.
*
* @param key the secret key
* @param value the serialized secret value
Expand All @@ -71,15 +72,21 @@ public AwsSecretsManagerVault(SecretsManagerClient smClient, Monitor monitor, Aw
@Override
public Result<Void> storeSecret(String key, String value) {
var sanitizedKey = sanitizer.sanitizeKey(key);
var request = CreateSecretRequest.builder().name(sanitizedKey)
.secretString(value).build();
try {
monitor.debug(String.format("Storing secret '%s' to AWS Secrets manager", sanitizedKey));
smClient.createSecret(request);
var updateSecretRequest = UpdateSecretRequest.builder().secretId(sanitizedKey).secretString(value).build();
smClient.updateSecret(updateSecretRequest);
monitor.debug(String.format("Secret '%s' updated in AWS Secrets Manager", sanitizedKey));
return Result.success();
} catch (RuntimeException serviceException) {
monitor.severe(serviceException.getMessage(), serviceException);
return Result.failure(serviceException.getMessage());
} catch (ResourceNotFoundException e) {
ndr-brt marked this conversation as resolved.
Show resolved Hide resolved
try {
var createSecretRequest = CreateSecretRequest.builder().name(sanitizedKey).secretString(value).build();
smClient.createSecret(createSecretRequest);
monitor.debug(String.format("Secret '%s' stored in AWS Secrets Manager", sanitizedKey));
return Result.success();
} catch (RuntimeException serviceException) {
monitor.severe(serviceException.getMessage(), serviceException);
return Result.failure(serviceException.getMessage());
}
}
}

Expand All @@ -104,5 +111,4 @@ public Result<Void> deleteSecret(String key) {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
import software.amazon.awssdk.services.secretsmanager.model.DeleteSecretRequest;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
import software.amazon.awssdk.services.secretsmanager.model.ResourceNotFoundException;
import software.amazon.awssdk.services.secretsmanager.model.UpdateSecretRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.TestInstance.Lifecycle;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -63,16 +66,31 @@ void storeSecret_shouldSanitizeKey() {

vault.storeSecret(KEY, value);

verify(secretClient).createSecret(CreateSecretRequest.builder().name(SANITIZED_KEY)
verify(secretClient).updateSecret(UpdateSecretRequest.builder().secretId(SANITIZED_KEY)
.secretString(value).build());
}

@Test
void storeSecret_shouldNotOverwriteSecrets() {
var value = "value";
void storeSecret_shouldUpdateSecretIfExist() {
String value = "value";

vault.storeSecret(KEY, value);

verify(secretClient).updateSecret(UpdateSecretRequest.builder().secretId(SANITIZED_KEY)
.secretString(value).build());
verify(secretClient, never()).createSecret(any(CreateSecretRequest.class));

}

@Test
void storeSecret_shouldCreateSecretIfNotExist() {
String value = "value";

doThrow(ResourceNotFoundException.class).when(secretClient).updateSecret(any(UpdateSecretRequest.class));

vault.storeSecret(KEY, value);

verify(secretClient).updateSecret(any(UpdateSecretRequest.class));
verify(secretClient).createSecret(CreateSecretRequest.builder().name(SANITIZED_KEY)
.secretString(value).build());
}
Expand Down
Loading