Relax requirements on credential names containing a leading slash.
This commit is contained in:
@@ -45,10 +45,14 @@ public class CredentialName {
|
||||
|
||||
String[] split = name.split("/");
|
||||
|
||||
Assert.isTrue(split.length > 2, "name must include at least one segment separated by '/'");
|
||||
Assert.isTrue(split.length > 0, "name must include at least one segment separated by '/'");
|
||||
|
||||
// remove the "/" prefix
|
||||
this.segments = Arrays.copyOfRange(split, 1, split.length);
|
||||
if (split[0].length() == 0) {
|
||||
// name contains a leading "/"
|
||||
this.segments = Arrays.copyOfRange(split, 1, split.length);
|
||||
} else {
|
||||
this.segments = split;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +72,11 @@ public class CredentialName {
|
||||
*/
|
||||
@JsonInclude
|
||||
public String getName() {
|
||||
return "/" + StringUtils.arrayToDelimitedString(segments, "/");
|
||||
if (segments.length == 1) {
|
||||
return segments[0];
|
||||
} else {
|
||||
return "/" + StringUtils.arrayToDelimitedString(segments, "/");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,15 +16,12 @@
|
||||
|
||||
package org.springframework.credhub.support;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* The client-provided name of a credential. The name consists of one or more segments.
|
||||
* When the value of each segment are combined the full name of the credential will be of
|
||||
* the form
|
||||
* {@literal /c/segment1/segment2/segment3}.
|
||||
* the form {@literal /segment1/segment2/segment3}.
|
||||
*
|
||||
* Objects of this type are created by clients and included as part of requests.
|
||||
*
|
||||
@@ -39,8 +36,6 @@ public class SimpleCredentialName extends CredentialName {
|
||||
*/
|
||||
public SimpleCredentialName(String... segments) {
|
||||
super(segments);
|
||||
Assert.notNull(segments, "segments must not be null");
|
||||
Assert.isTrue(segments.length > 0, "at least one segment must be provided");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,6 +24,27 @@ import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class SimpleCredentialNameUnitTests {
|
||||
@Test
|
||||
public void singleElementNameIsConstructed() {
|
||||
CredentialName credentialName = new SimpleCredentialName("credential-name");
|
||||
|
||||
assertThat(credentialName.getName(), equalTo("credential-name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleElementNameWithLeadingSlashIsParsed() {
|
||||
CredentialName credentialName = new CredentialName("/credential-name");
|
||||
|
||||
assertThat(credentialName.getName(), equalTo("credential-name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleElementNameWithoutLeadingSlashIsParsed() {
|
||||
CredentialName credentialName = new CredentialName("credential-name");
|
||||
|
||||
assertThat(credentialName.getName(), equalTo("credential-name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleNameIsConstructed() {
|
||||
CredentialName credentialName =
|
||||
@@ -38,4 +59,11 @@ public class SimpleCredentialNameUnitTests {
|
||||
|
||||
assertThat(credentialName.getName(), equalTo("/myorg/example/credential-name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleNameWithoutLeadingSlashIsParsed() {
|
||||
CredentialName credentialName = new CredentialName("myorg/example/credential-name");
|
||||
|
||||
assertThat(credentialName.getName(), equalTo("/myorg/example/credential-name"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user