Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.springframework.credhub.support.CredentialDetails;
import org.springframework.credhub.support.SimpleCredentialName;
import org.springframework.credhub.support.json.JsonCredential;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import static java.util.stream.Collectors.toMap;
Expand Down Expand Up @@ -67,18 +66,29 @@ public CredhubEnvironmentRepository(CredHubOperations credHubOperations, Credhub

@Override
public Environment findOne(String application, String profile, String label) {
if (ObjectUtils.isEmpty(profile)) {
if (!StringUtils.hasText(profile)) {
profile = DEFAULT_PROFILE;
}
if (ObjectUtils.isEmpty(label)) {
if (!StringUtils.hasText(label)) {
label = this.defaultLabel;
}

List<String> applications = normalize(application, DEFAULT_APPLICATION);
Comment thread
yashdotdev13 marked this conversation as resolved.
Resolved
List<String> profiles = normalize(profile, DEFAULT_PROFILE);
List<String> labels = normalize(label);

Environment environment = new Environment(application, split(profile), label, null, null);
List<String> normalizedApplications = normalize(application);
Collections.reverse(normalizedApplications);

List<String> normalizedProfiles = normalize(profile);
Collections.reverse(normalizedProfiles);

List<String> normalizedLabels = normalize(label);
Collections.reverse(normalizedLabels);

Environment environment = new Environment(String.join(",", normalizedApplications),
normalizedProfiles.toArray(new String[0]), String.join(",", normalizedLabels), null, null);

for (String l : labels) {
for (String prof : profiles) {
for (String app : applications) {
Expand All @@ -103,10 +113,10 @@ private List<String> normalize(String commaDelimitedItems, String defaultItem) {
*/
private List<String> normalize(String commaDelimitedItems) {
var items = Arrays.stream(split(commaDelimitedItems))
.map(String::trim)
Comment thread
yashdotdev13 marked this conversation as resolved.
Resolved
.distinct()
.filter(StringUtils::hasText)
.collect(Collectors.toList());

Collections.reverse(items);
return items;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.config.server.environment;

import java.nio.file.Path;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void setUp() {

@Test
public void shouldDisplayEmptyPropertiesWhenNoPathFound() {
stubCredentials("/myApp/prod/myLabel");
stubCredentials(Path.of("/", "myApp", "prod", "myLabel").toString());

Environment environment = this.credhubEnvironmentRepository.findOne("myApp", "prod", "myLabel");

Expand All @@ -71,7 +72,7 @@ public void shouldDisplayEmptyPropertiesWhenNoPathFound() {

@Test
public void shouldRetrieveDefaultsWhenNoLabelNorProfileProvided() {
stubCredentials("/myApp/default/master", credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "myApp", "default", "master").toString(), credential("c1", "k1", "v1"));

Environment environment = this.credhubEnvironmentRepository.findOne("myApp", null, null);

Expand All @@ -80,14 +81,28 @@ public void shouldRetrieveDefaultsWhenNoLabelNorProfileProvided() {
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"));
}

@Test
public void shouldRetrieveDefaultsWhenWhitespaceOnlyProfileAndLabelProvided() {
stubCredentials(Path.of("/", "myApp", "default", "master").toString(), credential("c1", "k1", "v1"));

Environment environment = this.credhubEnvironmentRepository.findOne("myApp", " ", " ");

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"));
}

@Test
public void shouldRetrieveGivenProfileAndLabel() {
stubCredentials("/myApp/prod/myLabel", credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "myApp", "prod", "myLabel").toString(), credential("c1", "k1", "v1"));

Environment environment = this.credhubEnvironmentRepository.findOne("myApp", "prod", "myLabel");

Expand All @@ -96,15 +111,14 @@ public void shouldRetrieveGivenProfileAndLabel() {
assertThat(environment.getLabel()).isEqualTo("myLabel");

assertThat(environment.getPropertySources()).hasSize(1);

assertThat(environment.getPropertySources().get(0).getName()).isEqualTo("credhub-myApp-prod-myLabel");
assertThat(environment.getPropertySources().get(0).getSource()).isEqualTo(Map.of("k1", "v1"));
}

@Test
public void shouldRetrieveGivenMultipleProfiles() {
stubCredentials("/myApp/prod/myLabel", credential("c1", "k1", "v1"));
stubCredentials("/myApp/cloud/myLabel", credential("c2", "k2", "v2"));
stubCredentials(Path.of("/", "myApp", "prod", "myLabel").toString(), credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "myApp", "cloud", "myLabel").toString(), credential("c2", "k2", "v2"));

Environment environment = this.credhubEnvironmentRepository.findOne("myApp", "prod,cloud", "myLabel");

Expand All @@ -123,9 +137,9 @@ public void shouldRetrieveGivenMultipleProfiles() {

@Test
public void shouldRetrieveGivenLabelsProfiles() {
stubCredentials("/myApp/prod/myLabel", credential("c1", "k1", "v1"));
stubCredentials("/myApp/cloud/mySecondLabel", credential("c2", "k2", "v2"));
stubCredentials("/myApp/prod/master", credential("c3", "k3", "v3"));
stubCredentials(Path.of("/", "myApp", "prod", "myLabel").toString(), credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "myApp", "cloud", "mySecondLabel").toString(), credential("c2", "k2", "v2"));
stubCredentials(Path.of("/", "myApp", "prod", "master").toString(), credential("c3", "k3", "v3"));

Environment environment = this.credhubEnvironmentRepository.findOne("myApp", "prod,cloud",
"myLabel,mySecondLabel");
Expand All @@ -145,8 +159,8 @@ public void shouldRetrieveGivenLabelsProfiles() {

@Test
public void shouldRetrieveGivenMultipleApplicationNames() {
stubCredentials("/app1/default/myLabel", credential("c1", "k1", "v1"));
stubCredentials("/app2/default/myLabel", credential("c2", "k2", "v2"));
stubCredentials(Path.of("/", "app1", "default", "myLabel").toString(), credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "app2", "default", "myLabel").toString(), credential("c2", "k2", "v2"));

Environment environment = this.credhubEnvironmentRepository.findOne("app1,app2", null, "myLabel");

Expand All @@ -163,9 +177,31 @@ public void shouldRetrieveGivenMultipleApplicationNames() {
assertThat(environment.getPropertySources().get(1).getSource()).isEqualTo(Map.of("k1", "v1"));
}

@Test
public void shouldTrimWhitespaceFromApplicationNames() {
stubCredentials(Path.of("/", "myApp", "prod", "myLabel").toString(), credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "application", "prod", "myLabel").toString(), credential("c2", "k2", "v2"));

Environment environment = this.credhubEnvironmentRepository.findOne("myApp, application ", "prod", "myLabel");

assertThat(environment.getPropertySources()).hasSize(2);
assertThat(environment.getPropertySources().get(0).getSource()).isEqualTo(Map.of("k1", "v1"));
assertThat(environment.getPropertySources().get(1).getSource()).isEqualTo(Map.of("k2", "v2"));
}

@Test
public void shouldTrimWhitespaceFromEnvironmentMetadata() {
Environment environment = this.credhubEnvironmentRepository.findOne(" myApp , app2 ", " prod , cloud ",
" myLabel , mySecondLabel ");

assertThat(environment.getName()).isEqualTo("myApp,app2");
assertThat(environment.getProfiles()).containsExactly("prod", "cloud");
assertThat(environment.getLabel()).isEqualTo("myLabel,mySecondLabel");
}

@Test
public void shouldMergeWhenMoreThanOneCredentialsFound() {
stubCredentials("/myApp/prod/myLabel", credential("c1", Map.of("k1", "v1")),
stubCredentials(Path.of("/", "myApp", "prod", "myLabel").toString(), credential("c1", Map.of("k1", "v1")),
credential("c2", Map.of("k2", "v2")));

Environment environment = this.credhubEnvironmentRepository.findOne("myApp", "prod", "myLabel");
Expand All @@ -181,12 +217,12 @@ public void shouldMergeWhenMoreThanOneCredentialsFound() {

@Test
public void shouldIncludeDefaultApplicationWhenOtherProvided() {
stubCredentials("/app1/prod/myLabel", credential("c1", "k1", "v1"));
stubCredentials("/app2/prod/myLabel", credential("c2", "k2", "v2"));
stubCredentials("/application/prod/myLabel", credential("c3", "k3", "v3"));
stubCredentials("/app1/default/myLabel", credential("c4", "k4", "v4"));
stubCredentials("/app2/default/myLabel", credential("c5", "k5", "v5"));
stubCredentials("/application/default/myLabel", credential("c6", "k6", "v6"));
stubCredentials(Path.of("/", "app1", "prod", "myLabel").toString(), credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "app2", "prod", "myLabel").toString(), credential("c2", "k2", "v2"));
stubCredentials(Path.of("/", "application", "prod", "myLabel").toString(), credential("c3", "k3", "v3"));
stubCredentials(Path.of("/", "app1", "default", "myLabel").toString(), credential("c4", "k4", "v4"));
stubCredentials(Path.of("/", "app2", "default", "myLabel").toString(), credential("c5", "k5", "v5"));
stubCredentials(Path.of("/", "application", "default", "myLabel").toString(), credential("c6", "k6", "v6"));

Environment environment = this.credhubEnvironmentRepository.findOne("app1,app2", "prod", "myLabel");

Expand Down Expand Up @@ -217,12 +253,12 @@ public void shouldIncludeDefaultApplicationWhenOtherProvided() {

@Test
public void shouldIncludeDefaultProfileWhenOtherProvided() {
stubCredentials("/myApp/dev/myLabel", credential("c1", "k1", "v1"));
stubCredentials("/application/dev/myLabel", credential("c2", "k2", "v2"));
stubCredentials("/myApp/prod/myLabel", credential("c3", "k3", "v3"));
stubCredentials("/application/prod/myLabel", credential("c4", "k4", "v4"));
stubCredentials("/myApp/default/myLabel", credential("c5", "k5", "v5"));
stubCredentials("/application/default/myLabel", credential("c6", "k6", "v6"));
stubCredentials(Path.of("/", "myApp", "dev", "myLabel").toString(), credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "application", "dev", "myLabel").toString(), credential("c2", "k2", "v2"));
stubCredentials(Path.of("/", "myApp", "prod", "myLabel").toString(), credential("c3", "k3", "v3"));
stubCredentials(Path.of("/", "application", "prod", "myLabel").toString(), credential("c4", "k4", "v4"));
stubCredentials(Path.of("/", "myApp", "default", "myLabel").toString(), credential("c5", "k5", "v5"));
stubCredentials(Path.of("/", "application", "default", "myLabel").toString(), credential("c6", "k6", "v6"));

Environment environment = this.credhubEnvironmentRepository.findOne("myApp", "dev,prod", "myLabel");

Expand Down Expand Up @@ -253,10 +289,10 @@ public void shouldIncludeDefaultProfileWhenOtherProvided() {

@Test
public void shouldIncludeDefaultProfileAndApplicationNameAtTheEnd() {
stubCredentials("/myApp/dev/myLabel", credential("c1", "k1", "v1"));
stubCredentials("/application/dev/myLabel", credential("c2", "k2", "v2"));
stubCredentials("/myApp/default/myLabel", credential("c3", "k3", "v3"));
stubCredentials("/application/default/myLabel", credential("c4", "k4", "v4"));
stubCredentials(Path.of("/", "myApp", "dev", "myLabel").toString(), credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "application", "dev", "myLabel").toString(), credential("c2", "k2", "v2"));
stubCredentials(Path.of("/", "myApp", "default", "myLabel").toString(), credential("c3", "k3", "v3"));
stubCredentials(Path.of("/", "application", "default", "myLabel").toString(), credential("c4", "k4", "v4"));

Environment environment = this.credhubEnvironmentRepository.findOne("application,myApp", "default,dev",
"myLabel");
Expand All @@ -282,8 +318,8 @@ public void shouldIncludeDefaultProfileAndApplicationNameAtTheEnd() {

@Test
public void shouldUseCustomDefaultLabelIfProvided() {
stubCredentials("/myApp/default/master", credential("c1", "k1", "v1"));
stubCredentials("/myApp/default/main", credential("c2", "k2", "v2"));
stubCredentials(Path.of("/", "myApp", "default", "master").toString(), credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "myApp", "default", "main").toString(), credential("c2", "k2", "v2"));

var credhubOperations = Mockito.mock(CredHubOperations.class);
when(credhubOperations.credentials()).thenReturn(this.credhubCredentialOperations);
Expand All @@ -298,14 +334,14 @@ public void shouldUseCustomDefaultLabelIfProvided() {
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"));
}

@Test
public void shouldUseBasePathIfProvided() {
stubCredentials("/base/path/myApp/default/master", credential("c1", "k1", "v1"));
stubCredentials(Path.of("/", "base", "path", "myApp", "default", "master").toString(),
credential("c1", "k1", "v1"));

var credhubOperations = Mockito.mock(CredHubOperations.class);
when(credhubOperations.credentials()).thenReturn(this.credhubCredentialOperations);
Expand All @@ -320,7 +356,6 @@ public void shouldUseBasePathIfProvided() {
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"));
}
Expand Down
Loading