Enables defining a base path for CredHub credentials (#2521)
Signed-off-by: kvmw <mshamsi@broadcom.com>
This commit is contained in:
@@ -106,3 +106,21 @@ spring:
|
||||
|
||||
NOTE: The used UAA client-id should have `credhub.read` as scope.
|
||||
|
||||
The following table describes the CredHub configuration properties.
|
||||
|
||||
|===
|
||||
|Property Name |Remarks
|
||||
|
||||
|*url*
|
||||
|CredHub server URL.
|
||||
|
||||
|*path*
|
||||
|Base path for all credentials. Optional, defaults to empty.
|
||||
|
||||
|*defaultLabel*
|
||||
| Default label to use when is not provided by client application. Optional, defaults to `master`.
|
||||
|
||||
|*oauth2*
|
||||
| OAuth2 configuration to access CredHub. Optional.
|
||||
|
||||
|===
|
||||
|
||||
@@ -26,18 +26,30 @@ import org.springframework.core.Ordered;
|
||||
@ConfigurationProperties("spring.cloud.config.server.credhub")
|
||||
public class CredhubEnvironmentProperties implements EnvironmentRepositoryProperties {
|
||||
|
||||
/** The common base path for credentials in CredHub. It is empty by default. */
|
||||
private String path = "";
|
||||
|
||||
/** The default label to be used when is not provided by client applications. */
|
||||
private String defaultLabel = "master";
|
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
|
||||
public void setDefaultLabel(String defaultLabel) {
|
||||
this.defaultLabel = defaultLabel;
|
||||
public String getPath() {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public String getDefaultLabel() {
|
||||
return defaultLabel;
|
||||
}
|
||||
|
||||
public void setDefaultLabel(String defaultLabel) {
|
||||
this.defaultLabel = defaultLabel;
|
||||
}
|
||||
|
||||
public int getOrder() {
|
||||
return this.order;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.config.server.environment;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -45,6 +46,8 @@ public class CredhubEnvironmentRepository implements EnvironmentRepository, Orde
|
||||
|
||||
private static final String DEFAULT_APPLICATION = "application";
|
||||
|
||||
private final String path;
|
||||
|
||||
private final String defaultLabel;
|
||||
|
||||
private int order;
|
||||
@@ -58,8 +61,9 @@ public class CredhubEnvironmentRepository implements EnvironmentRepository, Orde
|
||||
public CredhubEnvironmentRepository(CredHubOperations credHubOperations, CredhubEnvironmentProperties properties) {
|
||||
this.credHubOperations = credHubOperations;
|
||||
|
||||
this.order = properties.getOrder();
|
||||
this.path = properties.getPath();
|
||||
this.defaultLabel = properties.getDefaultLabel();
|
||||
this.order = properties.getOrder();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -109,7 +113,7 @@ public class CredhubEnvironmentRepository implements EnvironmentRepository, Orde
|
||||
}
|
||||
|
||||
private Map<Object, Object> findProperties(String application, String profile, String label) {
|
||||
String path = "/" + application + "/" + profile + "/" + label;
|
||||
var path = Path.of("/", this.path, application, profile, label).toString();
|
||||
|
||||
return this.credHubOperations.credentials()
|
||||
.findByPath(path)
|
||||
|
||||
@@ -281,6 +281,28 @@ public class CredhubEnvironmentRepositoryTests {
|
||||
assertThat(environment.getPropertySources().get(0).getSource()).isEqualTo(Map.of("k2", "v2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseBasePathIfProvided() {
|
||||
stubCredentials("/base/path/myApp/default/master", credential("c1", "k1", "v1"));
|
||||
|
||||
var credhubOperations = Mockito.mock(CredHubOperations.class);
|
||||
when(credhubOperations.credentials()).thenReturn(this.credhubCredentialOperations);
|
||||
|
||||
var properties = new CredhubEnvironmentProperties();
|
||||
properties.setPath("/base/path");
|
||||
|
||||
var environment = new CredhubEnvironmentRepository(credhubOperations, properties).findOne("myApp", null, null);
|
||||
|
||||
assertThat(environment.getName()).isEqualTo("myApp");
|
||||
assertThat(environment.getProfiles()).containsExactly("default");
|
||||
assertThat(environment.getLabel()).isEqualTo("master");
|
||||
|
||||
assertThat(environment.getPropertySources()).hasSize(1);
|
||||
|
||||
assertThat(environment.getPropertySources().get(0).getName()).isEqualTo("credhub-myApp-default-master");
|
||||
assertThat(environment.getPropertySources().get(0).getSource()).isEqualTo(Map.of("k1", "v1"));
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private void stubCredentials(String path, CredentialDetails<JsonCredential>... details) {
|
||||
when(this.credhubCredentialOperations.findByPath(path)).thenReturn(
|
||||
|
||||
Reference in New Issue
Block a user