Merge remote-tracking branch 'origin/main' into native-image-for-server

This commit is contained in:
Olga MaciaszekSharma
2023-11-30 17:23:15 +01:00
14 changed files with 221 additions and 19 deletions

View File

@@ -54,6 +54,11 @@ public class AwsSecretsManagerEnvironmentProperties implements EnvironmentReposi
*/
private String defaultLabel;
/**
* Do not set staging label when fetching the secret values.
*/
private boolean ignoreLabel;
/**
* The order of the environment repository.
*/
@@ -105,6 +110,14 @@ public class AwsSecretsManagerEnvironmentProperties implements EnvironmentReposi
this.defaultLabel = defaultLabel;
}
public boolean isIgnoreLabel() {
return this.ignoreLabel;
}
public void setIgnoreLabel(boolean ignoreLabel) {
this.ignoreLabel = ignoreLabel;
}
public int getOrder() {
return order;
}

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;
@@ -72,6 +73,7 @@ public class AwsSecretsManagerEnvironmentRepository implements EnvironmentReposi
final String defaultApplication = configServerProperties.getDefaultApplicationName();
final String defaultProfile = configServerProperties.getDefaultProfile();
final String defaultLabel = environmentProperties.getDefaultLabel();
final boolean ignoreLabel = environmentProperties.isIgnoreLabel();
if (ObjectUtils.isEmpty(application)) {
application = defaultApplication;
@@ -81,7 +83,10 @@ public class AwsSecretsManagerEnvironmentRepository implements EnvironmentReposi
profileList = defaultProfile;
}
if (StringUtils.isEmpty(label)) {
if (ignoreLabel) {
label = null;
}
else if (StringUtils.isEmpty(label)) {
label = defaultLabel;
}
@@ -155,7 +160,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;
@@ -87,10 +88,19 @@ public class AwsSecretsManagerEnvironmentRepositoryTests {
private final AwsSecretsManagerEnvironmentRepository labeledRepository = new AwsSecretsManagerEnvironmentRepository(
smClient, configServerProperties, labeledEnvironmentProperties);
private final AwsSecretsManagerEnvironmentProperties ignoreLabelEnvironmentProperties = new AwsSecretsManagerEnvironmentProperties() {{
setIgnoreLabel(true);
}};
private final AwsSecretsManagerEnvironmentRepository ignoreLabelRepository = new AwsSecretsManagerEnvironmentRepository(
smClient, configServerProperties, ignoreLabelEnvironmentProperties);
private final ObjectMapper objectMapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true);
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 +247,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();
@@ -1820,6 +1834,47 @@ public class AwsSecretsManagerEnvironmentRepositoryTests {
assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv);
}
@Test
public void testFindOneWithExistingApplicationAndExistingProfileAndExistingLabelWhenIgnoreLabelIsSet() {
String application = "foo";
String profile = "prod";
String label = "release";
String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);
String fooProdPropertiesName = "aws:secrets:/secret/foo-prod/";
PropertySource fooProdProperties = new PropertySource(fooProdPropertiesName, getFooProdReleaseProperties());
String fooPropertiesName = "aws:secrets:/secret/foo/";
PropertySource fooProperties = new PropertySource(fooPropertiesName, getFooReleaseProperties());
String fooDefaultPropertiesName = "aws:secrets:/secret/foo-default/";
PropertySource fooDefaultProperties = new PropertySource(fooDefaultPropertiesName,
getFooDefaultReleaseProperties());
String applicationProdPropertiesName = "aws:secrets:/secret/application-prod/";
PropertySource applicationProdProperties = new PropertySource(applicationProdPropertiesName,
getApplicationProdReleaseProperties());
String applicationDefaultPropertiesName = "aws:secrets:/secret/application-default/";
PropertySource applicationDefaultProperties = new PropertySource(applicationDefaultPropertiesName,
getApplicationDefaultReleaseProperties());
String applicationPropertiesName = "aws:secrets:/secret/application/";
PropertySource applicationProperties = new PropertySource(applicationPropertiesName,
getApplicationReleaseProperties());
Environment expectedEnv = new Environment(application, profiles, null, null, null);
expectedEnv.addAll(Arrays.asList(
fooProdProperties, applicationProdProperties, fooDefaultProperties,
applicationDefaultProperties, fooProperties, applicationProperties));
putSecrets(expectedEnv);
Environment resultEnv = ignoreLabelRepository.findOne(application, profile, label);
assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv);
}
@Test
public void testFindOneWithNullApplicationAndNullProfile() {
String application = null;
@@ -2502,6 +2557,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 +2624,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 {