Add support for generation of user credentials.
This commit is contained in:
@@ -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.
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<PasswordParameters> {
|
||||
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<PasswordParameters, UserParametersRequest, UserParametersRequestBuilder> {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user