Merge branch '4.1.x'

This commit is contained in:
Ryan Baxter
2024-09-06 11:51:00 -04:00
5 changed files with 51 additions and 10 deletions

View File

@@ -52,7 +52,10 @@ All client applications with the name `spring.cloud.config.name=demo-app` will h
}
----
NOTE: When no profile is specified `default` will be used and when no label is specified `master` will be used as a default value.
NOTE: When no label is specified `master` will be used as a default value. You can change that by setting `spring.cloud.config.server.credhub.defaultLabel`.
NOTE: When no profile is specified `default` will be used.
NOTE: Values added to `application` will be shared by all the applications.
[[oauth-2-0]]

View File

@@ -26,8 +26,18 @@ import org.springframework.core.Ordered;
@ConfigurationProperties("spring.cloud.config.server.credhub")
public class CredhubEnvironmentProperties implements EnvironmentRepositoryProperties {
private String defaultLabel = "master";
private int order = Ordered.LOWEST_PRECEDENCE;
public void setDefaultLabel(String defaultLabel) {
this.defaultLabel = defaultLabel;
}
public String getDefaultLabel() {
return defaultLabel;
}
public int getOrder() {
return this.order;
}

View File

@@ -43,16 +43,23 @@ public class CredhubEnvironmentRepository implements EnvironmentRepository, Orde
private static final String DEFAULT_PROFILE = "default";
private static final String DEFAULT_LABEL = "master";
private static final String DEFAULT_APPLICATION = "application";
private int order = Ordered.LOWEST_PRECEDENCE;
private final String defaultLabel;
private int order;
private final CredHubOperations credHubOperations;
public CredhubEnvironmentRepository(CredHubOperations credHubOperations) {
this(credHubOperations, new CredhubEnvironmentProperties());
}
public CredhubEnvironmentRepository(CredHubOperations credHubOperations, CredhubEnvironmentProperties properties) {
this.credHubOperations = credHubOperations;
this.order = properties.getOrder();
this.defaultLabel = properties.getDefaultLabel();
}
@Override
@@ -61,7 +68,7 @@ public class CredhubEnvironmentRepository implements EnvironmentRepository, Orde
profile = DEFAULT_PROFILE;
}
if (ObjectUtils.isEmpty(label)) {
label = DEFAULT_LABEL;
label = this.defaultLabel;
}
List<String> applications = normalize(application, DEFAULT_APPLICATION);

View File

@@ -25,7 +25,7 @@ import org.springframework.credhub.core.CredHubOperations;
public class CredhubEnvironmentRepositoryFactory
implements EnvironmentRepositoryFactory<CredhubEnvironmentRepository, CredhubEnvironmentProperties> {
private CredHubOperations credhubOperations;
private final CredHubOperations credhubOperations;
public CredhubEnvironmentRepositoryFactory(CredHubOperations credhubOperations) {
this.credhubOperations = credhubOperations;
@@ -33,9 +33,7 @@ public class CredhubEnvironmentRepositoryFactory
@Override
public CredhubEnvironmentRepository build(CredhubEnvironmentProperties environmentProperties) {
CredhubEnvironmentRepository repository = new CredhubEnvironmentRepository(this.credhubOperations);
repository.setOrder(environmentProperties.getOrder());
return repository;
return new CredhubEnvironmentRepository(this.credhubOperations, environmentProperties);
}
}

View File

@@ -47,8 +47,8 @@ public class CredhubEnvironmentRepositoryTests {
@BeforeEach
public void setUp() {
CredHubOperations credhubOperations = Mockito.mock(CredHubOperations.class);
this.credhubCredentialOperations = Mockito.mock(CredHubCredentialOperations.class);
CredHubOperations credhubOperations = Mockito.mock(CredHubOperations.class);
when(credhubOperations.credentials()).thenReturn(this.credhubCredentialOperations);
this.credhubEnvironmentRepository = new CredhubEnvironmentRepository(credhubOperations);
@@ -258,6 +258,29 @@ public class CredhubEnvironmentRepositoryTests {
assertThat(environment.getPropertySources().get(3).getSource()).isEqualTo(Map.of("k4", "v4"));
}
@Test
public void shouldUseCustomDefaultLabelIfProvided() {
stubCredentials("/myApp/default/master", credential("c1", "k1", "v1"));
stubCredentials("/myApp/default/main", credential("c2", "k2", "v2"));
var credhubOperations = Mockito.mock(CredHubOperations.class);
when(credhubOperations.credentials()).thenReturn(this.credhubCredentialOperations);
var properties = new CredhubEnvironmentProperties();
properties.setDefaultLabel("main");
var environment = new CredhubEnvironmentRepository(credhubOperations, properties).findOne("myApp", null, null);
assertThat(environment.getName()).isEqualTo("myApp");
assertThat(environment.getProfiles()).containsExactly("default");
assertThat(environment.getLabel()).isEqualTo("main");
assertThat(environment.getPropertySources()).hasSize(1);
assertThat(environment.getPropertySources().get(0).getName()).isEqualTo("credhub-myApp-default-main");
assertThat(environment.getPropertySources().get(0).getSource()).isEqualTo(Map.of("k2", "v2"));
}
@SafeVarargs
private void stubCredentials(String path, CredentialDetails<JsonCredential>... details) {
when(this.credhubCredentialOperations.findByPath(path)).thenReturn(