From a8b9d8bd06289e1a862cf1edf1dbb99cfb3e6697 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Wed, 31 May 2017 16:01:20 -0500 Subject: [PATCH] Add support for generation of user credentials. --- .../support/{rsa => }/KeyParameters.java | 4 +- .../certificate/CertificateParameters.java | 2 +- .../support/password/PasswordParameters.java | 2 +- .../credhub/support/rsa/RsaParameters.java | 1 + .../credhub/support/ssh/SshParameters.java | 2 +- .../support/user/UserParametersRequest.java | 122 ++++++++++++++++++ .../user/UserParametersRequestUnitTests.java | 93 +++++++++++++ 7 files changed, 220 insertions(+), 6 deletions(-) rename spring-credhub-core/src/main/java/org/springframework/credhub/support/{rsa => }/KeyParameters.java (93%) create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/support/user/UserParametersRequest.java create mode 100644 spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserParametersRequestUnitTests.java diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/rsa/KeyParameters.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/KeyParameters.java similarity index 93% rename from spring-credhub-core/src/main/java/org/springframework/credhub/support/rsa/KeyParameters.java rename to spring-credhub-core/src/main/java/org/springframework/credhub/support/KeyParameters.java index ab41b85..6f4a626 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/rsa/KeyParameters.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/KeyParameters.java @@ -14,9 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.support.rsa; - -import org.springframework.credhub.support.KeyLength; +package org.springframework.credhub.support; /** * Base class for parameter types that contain specifications for key generation. diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/certificate/CertificateParameters.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/certificate/CertificateParameters.java index 83b519e..2ed0b5e 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/certificate/CertificateParameters.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/certificate/CertificateParameters.java @@ -17,7 +17,7 @@ package org.springframework.credhub.support.certificate; import org.springframework.credhub.support.KeyLength; -import org.springframework.credhub.support.rsa.KeyParameters; +import org.springframework.credhub.support.KeyParameters; import org.springframework.util.Assert; /** diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/password/PasswordParameters.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/password/PasswordParameters.java index 541fa13..ff233c5 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/password/PasswordParameters.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/password/PasswordParameters.java @@ -168,7 +168,7 @@ public class PasswordParameters { } /** - * Set the value of the include characters parameter. + * Set the value of the include special characters parameter. * * @param include {@literal true} to include non-alphanumeric characters in generated credential value * @return the builder diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/rsa/RsaParameters.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/rsa/RsaParameters.java index 38dce78..0d8b9bb 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/rsa/RsaParameters.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/rsa/RsaParameters.java @@ -17,6 +17,7 @@ package org.springframework.credhub.support.rsa; import org.springframework.credhub.support.KeyLength; +import org.springframework.credhub.support.KeyParameters; import org.springframework.util.Assert; /** diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/ssh/SshParameters.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/ssh/SshParameters.java index cc5d883..48a08c1 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/ssh/SshParameters.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/ssh/SshParameters.java @@ -17,7 +17,7 @@ package org.springframework.credhub.support.ssh; import org.springframework.credhub.support.KeyLength; -import org.springframework.credhub.support.rsa.KeyParameters; +import org.springframework.credhub.support.KeyParameters; import org.springframework.util.Assert; /** diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/user/UserParametersRequest.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/user/UserParametersRequest.java new file mode 100644 index 0000000..88001da --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/user/UserParametersRequest.java @@ -0,0 +1,122 @@ +/* + * Copyright 2016-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.credhub.support.user; + +import org.springframework.credhub.support.ParametersRequest; +import org.springframework.credhub.support.password.PasswordParameters; +import org.springframework.util.Assert; + +import static org.springframework.credhub.support.CredentialType.USER; + +/** + * The details of a request to generate a new {@link UserCredential} in CredHub. + * + * @author Scott Frederick + */ +public class UserParametersRequest extends ParametersRequest { + private UserValue value; + + /** + * Create a {@link UserParametersRequest}. + */ + UserParametersRequest() { + super(USER); + } + + /** + * Set the value of the username parameter. + * + * @param username the username + */ + void setValue(String username) { + this.value = new UserValue(username); + } + + /** + * Get the value of the username parameter. + * + * @return the value of the parameter + */ + public UserValue getValue() { + return value; + } + + /** + * Create a builder that provides a fluent API for providing the values required + * to construct a {@link UserParametersRequest}. + * + * @return a builder + */ + public static UserParametersRequestBuilder builder() { + return new UserParametersRequestBuilder(); + } + + /** + * A builder that provides a fluent API for constructing {@link UserParametersRequest}s. + */ + public static class UserParametersRequestBuilder + extends ParametersRequestBuilder { + @Override + protected UserParametersRequest createTarget() { + return new UserParametersRequest(); + } + + @Override + protected UserParametersRequestBuilder createBuilder() { + return this; + } + + /** + * Set the parameters for generation of the password for a user credential. + * + * @param parameters the password generation parameters; must not be {@literal null} + * @return the builder + */ + public UserParametersRequestBuilder parameters(PasswordParameters parameters) { + Assert.notNull(parameters, "parameters must not be null"); + targetObj.setParameters(parameters); + return this; + } + + /** + * Set the username for the generated user. + * + * @param username the username; must not be {@literal null} + * @return the builder + */ + public UserParametersRequestBuilder username(String username) { + Assert.notNull(username, "username must not be null"); + targetObj.setValue(username); + return this; + } + } + + /** + * Holds the value of the username parameter. + */ + private static class UserValue { + private String username; + + UserValue(String username) { + this.username = username; + } + + public String getUsername() { + return username; + } + } +} diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserParametersRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserParametersRequestUnitTests.java new file mode 100644 index 0000000..929d27a --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserParametersRequestUnitTests.java @@ -0,0 +1,93 @@ +/* + * Copyright 2016-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.credhub.support.user; + +import org.junit.Test; + +import org.springframework.credhub.support.ParametersRequestUnitTestsBase; +import org.springframework.credhub.support.SimpleCredentialName; +import org.springframework.credhub.support.password.PasswordParameters; +import org.springframework.credhub.support.user.UserParametersRequest.UserParametersRequestBuilder; + +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; +import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; + +public class UserParametersRequestUnitTests extends ParametersRequestUnitTestsBase { + @Test + public void serializeWithParameters() throws Exception { + UserParametersRequestBuilder requestBuilder = UserParametersRequest.builder() + .name(new SimpleCredentialName("example", "credential")) + .overwrite(true) + .username("user") + .parameters(PasswordParameters.builder() + .length(20) + .excludeLower(true) + .excludeUpper(false) + .excludeNumber(true) + .includeSpecial(false) + .build()); + + String jsonValue = serializeToJson(requestBuilder); + + assertCommonRequestFields(jsonValue, true, "/example/credential", "user"); + assertThat(jsonValue, + allOf(hasJsonPath("$.value.username", equalTo("user")), + hasJsonPath("$.parameters.length", equalTo(20)), + hasJsonPath("$.parameters.exclude_lower", equalTo(true)), + hasJsonPath("$.parameters.exclude_upper", equalTo(false)), + hasJsonPath("$.parameters.exclude_number", equalTo(true)), + hasJsonPath("$.parameters.include_special", equalTo(false)))); + } + + @Test + public void serializeWithEmptyParameters() throws Exception { + UserParametersRequestBuilder requestBuilder = UserParametersRequest.builder() + .name(new SimpleCredentialName("example", "credential")) + .overwrite(true) + .parameters(new PasswordParameters()); + + String jsonValue = serializeToJson(requestBuilder); + + assertCommonRequestFields(jsonValue, true, "/example/credential", "user"); + assertParametersNotSet(jsonValue); + } + + @Test + public void serializeWithNoParameters() throws Exception { + UserParametersRequestBuilder requestBuilder = UserParametersRequest.builder() + .name(new SimpleCredentialName("example", "credential")) + .overwrite(true); + + String jsonValue = serializeToJson(requestBuilder); + + assertCommonRequestFields(jsonValue, true, "/example/credential", "user"); + assertParametersNotSet(jsonValue); + } + + private void assertParametersNotSet(String jsonValue) { + assertThat(jsonValue, + allOf(hasNoJsonPath("$.value.username"), + hasNoJsonPath("$.parameters.length"), + hasNoJsonPath("$.parameters.exclude_lower"), + hasNoJsonPath("$.parameters.exclude_upper"), + hasNoJsonPath("$.parameters.exclude_number"), + hasNoJsonPath("$.parameters.include_special"))); + } +} \ No newline at end of file