Initial implementation to support multiple labels with aws secret manager (#2567)

Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
This commit is contained in:
Ryan Baxter
2024-10-02 10:14:28 -04:00
committed by GitHub
parent e48ac403c4
commit 0e64e810d0
2 changed files with 66 additions and 18 deletions

View File

@@ -101,29 +101,37 @@ public class AwsSecretsManagerEnvironmentRepository implements EnvironmentReposi
environment.add(new PropertySource("overrides", overrides));
}
List<String> labels;
if (StringUtils.hasText(label) && label.contains(",")) {
labels = Arrays.asList(StringUtils.commaDelimitedListToStringArray(label));
Collections.reverse(labels);
}
else {
labels = Collections.singletonList(label);
}
List<String> reversedProfiles = new ArrayList<>(Arrays.asList(profiles));
Collections.reverse(reversedProfiles);
for (String profile : reversedProfiles) {
addPropertySource(environment, application, profile, label);
if (!defaultApplication.equals(application)) {
addPropertySource(environment, defaultApplication, profile, label);
for (String l : labels) {
for (String profile : reversedProfiles) {
addPropertySource(environment, application, profile, l);
if (!defaultApplication.equals(application)) {
addPropertySource(environment, defaultApplication, profile, l);
}
}
if (!Arrays.asList(profiles).contains(defaultProfile)) {
addPropertySource(environment, application, defaultProfile, l);
}
if (!Arrays.asList(profiles).contains(defaultProfile) && !defaultApplication.equals(application)) {
addPropertySource(environment, defaultApplication, defaultProfile, l);
}
}
if (!Arrays.asList(profiles).contains(defaultProfile)) {
addPropertySource(environment, application, defaultProfile, label);
if (!defaultApplication.equals(application)) {
addPropertySource(environment, application, null, l);
}
addPropertySource(environment, defaultApplication, null, l);
}
if (!Arrays.asList(profiles).contains(defaultProfile) && !defaultApplication.equals(application)) {
addPropertySource(environment, defaultApplication, defaultProfile, label);
}
if (!defaultApplication.equals(application)) {
addPropertySource(environment, application, null, label);
}
addPropertySource(environment, defaultApplication, null, label);
return environment;
}

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.config.server.environment;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -1595,6 +1596,41 @@ public class AwsSecretsManagerEnvironmentRepositoryTests {
assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv);
}
@Test
public void testFindOneWithExistingApplicationAndDefaultProfileAndExistingLabelWhenMultipleLabelIsSet() {
String application = "foo";
String profile = configServerProperties.getDefaultProfile();
String label = "release,test";
String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);
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 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, label, null, null);
expectedEnv.addAll(Arrays.asList(applicationDefaultProperties, fooProperties));
putSecrets("release", Collections.singletonList(fooProperties));
putSecrets("dev", Collections.singletonList(fooDefaultProperties));
putSecrets("test", Collections.singletonList(applicationDefaultProperties));
putSecrets("", Collections.singletonList(applicationProperties));
Environment resultEnv = labeledRepository.findOne(application, profile, label);
assertThat(resultEnv).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(expectedEnv);
}
@Test
public void testFindOneWithExistingApplicationAndNonExistingProfileAndExistingLabelWhenDefaultLabelIsSet() {
String application = "foo";
@@ -2640,7 +2676,11 @@ public class AwsSecretsManagerEnvironmentRepositoryTests {
private void putSecrets(Environment environment) {
String label = environment.getLabel() != null ? environment.getLabel()
: environmentProperties.getDefaultLabel();
for (PropertySource ps : environment.getPropertySources()) {
putSecrets(label, environment.getPropertySources());
}
private void putSecrets(String label, List<PropertySource> propertySources) {
for (PropertySource ps : propertySources) {
String path = StringUtils.delete(ps.getName(), environmentProperties.getOrigin());
String secrets = getSecrets(ps);
CreateSecretResponse response = smClient