Merge branch '4.0.x'

This commit is contained in:
Ryan Baxter
2023-11-14 18:56:00 -05:00
2 changed files with 47 additions and 1 deletions

View File

@@ -28,6 +28,7 @@ import org.apache.commons.logging.LogFactory;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse;
import software.amazon.awssdk.services.secretsmanager.model.InvalidRequestException;
import software.amazon.awssdk.services.secretsmanager.model.ResourceNotFoundException;
import org.springframework.cloud.config.environment.Environment;
@@ -155,7 +156,7 @@ public class AwsSecretsManagerEnvironmentRepository implements EnvironmentReposi
}
}
}
catch (ResourceNotFoundException | IOException e) {
catch (InvalidRequestException | ResourceNotFoundException | IOException e) {
log.debug(String.format(
"Skip adding propertySource. Unable to load secrets from AWS Secrets Manager for secretId=%s",
path), e);

View File

@@ -42,6 +42,7 @@ import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.model.CreateSecretRequest;
import software.amazon.awssdk.services.secretsmanager.model.CreateSecretResponse;
import software.amazon.awssdk.services.secretsmanager.model.DeleteSecretRequest;
import software.amazon.awssdk.services.secretsmanager.model.RestoreSecretRequest;
import software.amazon.awssdk.services.secretsmanager.model.UpdateSecretVersionStageRequest;
import org.springframework.cloud.config.environment.Environment;
@@ -91,6 +92,8 @@ public class AwsSecretsManagerEnvironmentRepositoryTests {
private final List<String> toBeRemoved = new ArrayList<>();
private final List<String> markedForDeletion = new ArrayList<>();
private static Map<String, String> getFooProperties() {
return new HashMap<String, String>() {
{
@@ -237,6 +240,10 @@ public class AwsSecretsManagerEnvironmentRepositoryTests {
@AfterEach
public void cleanUp() {
markedForDeletion
.forEach(value -> smClient.restoreSecret(RestoreSecretRequest.builder().secretId(value).build()));
markedForDeletion.clear();
toBeRemoved.forEach(value -> smClient
.deleteSecret(DeleteSecretRequest.builder().secretId(value).forceDeleteWithoutRecovery(true).build()));
toBeRemoved.clear();
@@ -2502,6 +2509,36 @@ public class AwsSecretsManagerEnvironmentRepositoryTests {
assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(environment);
}
@Test
public void testFindOneWithExistingApplicationAndNonExistingProfileAndNoDefaultProfileForFooMarkedForDeletion() {
String application = "foo";
String profile = randomAlphabetic(RandomUtils.nextInt(2, 25));
String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);
String fooPropertiesName = "aws:secrets:/secret/foo/";
PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooProperties());
String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/";
PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName,
getApplicationDefaultProperties());
String applicationPropertiesName = "aws:secrets:/secret/application/";
PropertySource applicationProperties = new PropertySource(applicationPropertiesName,
getApplicationProperties());
Environment environment = new Environment(application, profiles, null, null, null);
environment.addAll(Arrays.asList(applicationDefaultProperties, fooProperties, applicationProperties));
putSecrets(environment);
deleteSecrets(environment);
Environment emptyEnvironment = new Environment(application, profiles, null, null, null);
Environment resultEnv = repository.findOne(application, profile, null);
assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(emptyEnvironment);
}
@Test
public void factoryCustomizableWithRegion() {
AwsSecretsManagerEnvironmentRepositoryFactory factory = new AwsSecretsManagerEnvironmentRepositoryFactory(
@@ -2539,6 +2576,14 @@ public class AwsSecretsManagerEnvironmentRepositoryTests {
}
}
private void deleteSecrets(Environment environment) {
for (PropertySource ps : environment.getPropertySources()) {
String path = StringUtils.delete(ps.getName(), environmentProperties.getOrigin());
smClient.deleteSecret(DeleteSecretRequest.builder().secretId(path).recoveryWindowInDays(30L).build());
markedForDeletion.add(path);
}
}
private String getSecrets(PropertySource ps) {
Map<String, String> map = (Map<String, String>) ps.getSource();
try {