Refactor WriteRequest creation to use type-safe builders and constructors. Remove builders only used in tests.
This commit is contained in:
@@ -17,15 +17,12 @@
|
||||
package org.springframework.credhub.support;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* The details of a credential that has been written to CredHub. Clients don't
|
||||
* typically instantiate objects of this type, but will receive them in response
|
||||
@@ -60,8 +57,8 @@ public class CredentialDetails extends CredentialSummary {
|
||||
* @param versionCreatedAt the {@link Date} when this version of the credential was
|
||||
* created
|
||||
*/
|
||||
CredentialDetails(String id, CredentialName name, ValueType valueType,
|
||||
Object value, Date versionCreatedAt) {
|
||||
public CredentialDetails(String id, CredentialName name, ValueType valueType,
|
||||
Object value, Date versionCreatedAt) {
|
||||
super(name, versionCreatedAt);
|
||||
this.id = id;
|
||||
this.valueType = valueType;
|
||||
@@ -95,17 +92,6 @@ public class CredentialDetails extends CredentialSummary {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder for a {@link CredentialDetails} object. Intended for internal
|
||||
* use in tests. Clients will get {@link CredentialDetails} objects populated from
|
||||
* CredHub responses.
|
||||
*
|
||||
* @return the builder
|
||||
*/
|
||||
public static CredentialDetailsBuilder detailsBuilder() {
|
||||
return new CredentialDetailsBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
@@ -145,90 +131,4 @@ public class CredentialDetails extends CredentialSummary {
|
||||
+ ", versionCreatedAt='" + versionCreatedAt + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
/**
|
||||
* A builder that provides a fluent API for constructing {@link CredentialDetails}
|
||||
* instances. Intended to be used internally for testing.
|
||||
*/
|
||||
public static class CredentialDetailsBuilder {
|
||||
private String id;
|
||||
private CredentialName name;
|
||||
private ValueType valueType;
|
||||
private Object value;
|
||||
private Date versionCreatedAt;
|
||||
|
||||
CredentialDetailsBuilder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the ID of the credential.
|
||||
*
|
||||
* @param id the ID; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialDetailsBuilder id(String id) {
|
||||
Assert.notNull(id, "id must not be null");
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the credential.
|
||||
*
|
||||
* @param name the name; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialDetailsBuilder name(CredentialName name) {
|
||||
Assert.notNull(name, "name must not be null");
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a password value and {@link ValueType#PASSWORD} type for the credential.
|
||||
*
|
||||
* @param value the password value; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialDetailsBuilder passwordValue(String value) {
|
||||
Assert.notNull(value, "value must not be null");
|
||||
this.valueType = ValueType.PASSWORD;
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a JSON value and {@link ValueType#JSON} type for the credential.
|
||||
*
|
||||
* @param value the JSON value; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialDetailsBuilder jsonValue(Map<String, Object> value) {
|
||||
Assert.notNull(value, "value must not be null");
|
||||
this.valueType = ValueType.JSON;
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a creation date for the credential.
|
||||
*
|
||||
* @param versionCreatedAt the creation date; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialDetailsBuilder versionCreatedAt(Date versionCreatedAt) {
|
||||
Assert.notNull(versionCreatedAt, "versionCreatedAt must not be null");
|
||||
this.versionCreatedAt = versionCreatedAt;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a {@link CredentialDetails} from the provided values.
|
||||
*
|
||||
* @return a {@link CredentialDetails}
|
||||
*/
|
||||
public CredentialDetails build() {
|
||||
return new CredentialDetails(id, name, valueType, value, versionCreatedAt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,11 @@
|
||||
|
||||
package org.springframework.credhub.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
|
||||
|
||||
@@ -53,8 +51,8 @@ public class CredentialDetailsData {
|
||||
*
|
||||
* @param data a collection of {@link CredentialDetails}
|
||||
*/
|
||||
CredentialDetailsData(List<CredentialDetails> data) {
|
||||
this.data = data;
|
||||
public CredentialDetailsData(CredentialDetails... data) {
|
||||
this.data = Arrays.asList(data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,17 +64,6 @@ public class CredentialDetailsData {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder for a {@link CredentialDetailsData} object. Intended for internal
|
||||
* use. Clients will get {@link CredentialDetailsData} objects populated from
|
||||
* CredHub responses.
|
||||
*
|
||||
* @return the builder
|
||||
*/
|
||||
public static CredentialDetailsDataBuilder builder() {
|
||||
return new CredentialDetailsDataBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
@@ -104,74 +91,4 @@ public class CredentialDetailsData {
|
||||
+ "data=" + data
|
||||
+ '}';
|
||||
}
|
||||
|
||||
/**
|
||||
* A builder that provides a fluent API for constructing {@link CredentialDetailsData}
|
||||
* instances. Intended to be used internally for testing.
|
||||
*/
|
||||
public static class CredentialDetailsDataBuilder {
|
||||
private List<CredentialDetails> data;
|
||||
|
||||
/**
|
||||
* Create a {@link CredentialDetailsDataBuilder}.
|
||||
*/
|
||||
CredentialDetailsDataBuilder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a {@link CredentialDetails} to the collection of details.
|
||||
*
|
||||
* @param datum a {@link CredentialDetails} to add; must not be
|
||||
* {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialDetailsDataBuilder datum(CredentialDetails datum) {
|
||||
Assert.notNull(datum, "datum must not be null");
|
||||
initData();
|
||||
this.data.add(datum);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a collection of {@link CredentialDetails} to the collection of details.
|
||||
*
|
||||
* @param data a collection of {@link CredentialDetails} to add;
|
||||
* must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialDetailsDataBuilder data(Collection<? extends CredentialDetails> data) {
|
||||
Assert.notNull(data, "data must not be null");
|
||||
initData();
|
||||
this.data.addAll(data);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void initData() {
|
||||
if (this.data == null) {
|
||||
this.data = new ArrayList<CredentialDetails>();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a {@link CredentialDetailsData} from the provided values.
|
||||
*
|
||||
* @return a {@link CredentialDetailsData}
|
||||
*/
|
||||
public CredentialDetailsData build() {
|
||||
List<CredentialDetails> data;
|
||||
switch (this.data == null ? 0 : this.data.size()) {
|
||||
case 0:
|
||||
data = java.util.Collections.emptyList();
|
||||
break;
|
||||
case 1:
|
||||
data = java.util.Collections.singletonList(this.data.get(0));
|
||||
break;
|
||||
default:
|
||||
data = java.util.Collections
|
||||
.unmodifiableList(new ArrayList<CredentialDetails>(this.data));
|
||||
}
|
||||
|
||||
return new CredentialDetailsData(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class CredentialSummary {
|
||||
* @param versionCreatedAt the {@link Date} when this version of the credential was
|
||||
* created
|
||||
*/
|
||||
CredentialSummary(CredentialName name, Date versionCreatedAt) {
|
||||
public CredentialSummary(CredentialName name, Date versionCreatedAt) {
|
||||
this.name = name;
|
||||
this.versionCreatedAt = versionCreatedAt;
|
||||
}
|
||||
@@ -74,17 +74,6 @@ public class CredentialSummary {
|
||||
return this.versionCreatedAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder for a {@link CredentialSummary} object. Intended for internal
|
||||
* use in tests. Clients will get {@link CredentialSummary} objects populated from
|
||||
* CredHub responses.
|
||||
*
|
||||
* @return the builder
|
||||
*/
|
||||
public static CredentialSummaryBuilder summaryBuilder() {
|
||||
return new CredentialSummaryBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
@@ -115,49 +104,4 @@ public class CredentialSummary {
|
||||
+ ", versionCreatedAt='" + versionCreatedAt + '\''
|
||||
+ '}';
|
||||
}
|
||||
|
||||
/**
|
||||
* A builder that provides a fluent API for constructing {@link CredentialSummary}
|
||||
* instances. Intended to be used internally for testing.
|
||||
*/
|
||||
public static class CredentialSummaryBuilder {
|
||||
protected CredentialName name;
|
||||
protected Date versionCreatedAt;
|
||||
|
||||
CredentialSummaryBuilder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the credential.
|
||||
*
|
||||
* @param name the name; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialSummaryBuilder name(CredentialName name) {
|
||||
Assert.notNull(name, "name must not be null");
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a creation date for the credential.
|
||||
*
|
||||
* @param versionCreatedAt the creation date; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialSummaryBuilder versionCreatedAt(Date versionCreatedAt) {
|
||||
Assert.notNull(versionCreatedAt, "versionCreatedAt must not be null");
|
||||
this.versionCreatedAt = versionCreatedAt;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a {@link CredentialSummary} from the provided values.
|
||||
*
|
||||
* @return a {@link CredentialSummary}
|
||||
*/
|
||||
public CredentialSummary build() {
|
||||
return new CredentialSummary(name, versionCreatedAt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,14 +16,12 @@
|
||||
|
||||
package org.springframework.credhub.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A collection of {@link CredentialSummary}s. Clients don't typically instantiate
|
||||
@@ -50,8 +48,8 @@ public class CredentialSummaryData {
|
||||
*
|
||||
* @param credentials a collection of {@link CredentialSummary}s
|
||||
*/
|
||||
CredentialSummaryData(List<CredentialSummary> credentials) {
|
||||
this.credentials = credentials;
|
||||
public CredentialSummaryData(CredentialSummary... credentials) {
|
||||
this.credentials = Arrays.asList(credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,17 +61,6 @@ public class CredentialSummaryData {
|
||||
return this.credentials;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder for a {@link CredentialSummaryData} object. Intended for internal
|
||||
* use. Clients will get {@link CredentialSummaryData} objects populated from
|
||||
* CredHub responses.
|
||||
*
|
||||
* @return the builder
|
||||
*/
|
||||
public static CredentialSummaryDataBuilder builder() {
|
||||
return new CredentialSummaryDataBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
@@ -102,78 +89,4 @@ public class CredentialSummaryData {
|
||||
+ "credentials=" + credentials
|
||||
+ '}';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder for a {@link CredentialSummaryData} object. Intended for internal
|
||||
* use. Clients will get {@link CredentialSummaryData} objects populated from
|
||||
* CredHub responses.
|
||||
*
|
||||
* @return the builder
|
||||
*/
|
||||
public static class CredentialSummaryDataBuilder {
|
||||
private List<CredentialSummary> credentialSummaries;
|
||||
|
||||
CredentialSummaryDataBuilder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a {@link CredentialSummary} to the collection of summaries.
|
||||
*
|
||||
* @param credential the {@link CredentialSummary} to add; must not be
|
||||
* {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialSummaryDataBuilder credential(CredentialSummary credential) {
|
||||
Assert.notNull(credential, "credential must not be null");
|
||||
initCredentials();
|
||||
this.credentialSummaries.add(credential);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a collection of {@link CredentialSummary}s to the collection of summaries.
|
||||
*
|
||||
* @param credentials the {@link CredentialSummary}s to add; must not be
|
||||
* {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public CredentialSummaryDataBuilder credentials(
|
||||
Collection<? extends CredentialSummary> credentials) {
|
||||
Assert.notNull(credentials, "credentials must not be null");
|
||||
initCredentials();
|
||||
this.credentialSummaries.addAll(credentials);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void initCredentials() {
|
||||
if (this.credentialSummaries == null) {
|
||||
this.credentialSummaries = new ArrayList<CredentialSummary>();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a {@link CredentialSummaryData} from the provided values.
|
||||
*
|
||||
* @return a {@link CredentialSummaryData}
|
||||
*/
|
||||
public CredentialSummaryData build() {
|
||||
List<CredentialSummary> credentialSummaries;
|
||||
switch (this.credentialSummaries == null ? 0
|
||||
: this.credentialSummaries.size()) {
|
||||
case 0:
|
||||
credentialSummaries = java.util.Collections.emptyList();
|
||||
break;
|
||||
case 1:
|
||||
credentialSummaries = java.util.Collections
|
||||
.singletonList(this.credentialSummaries.get(0));
|
||||
break;
|
||||
default:
|
||||
credentialSummaries = java.util.Collections.unmodifiableList(
|
||||
new ArrayList<CredentialSummary>(this.credentialSummaries));
|
||||
}
|
||||
|
||||
return new CredentialSummaryData(credentialSummaries);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.springframework.credhub.support.ValueType.JSON;
|
||||
|
||||
/**
|
||||
* The details of a request to write a new or update an existing JSON credential in CredHub.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
|
||||
public class JsonWriteRequest extends WriteRequest<Map<String, Object>> {
|
||||
/**
|
||||
* Create a {@link JsonWriteRequest} from the provided parameters. Intended for internal
|
||||
* use. Clients should use {@link #builder()} to construct instances of this class.
|
||||
*
|
||||
* @param name the name of the credential
|
||||
* @param overwrite {@literal false} to create a new credential, or
|
||||
* {@literal true} to update and existing credential
|
||||
* @param value the value of the credential
|
||||
* @param additionalPermissions access control permissions for the credential
|
||||
*/
|
||||
private JsonWriteRequest(CredentialName name, boolean overwrite, Map<String, Object> value,
|
||||
List<AdditionalPermission> additionalPermissions) {
|
||||
super(name, overwrite, value, ValueType.PASSWORD, additionalPermissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder that provides a fluent API for providing the values required
|
||||
* to construct a {@link JsonWriteRequest}.
|
||||
*
|
||||
* @return a builder
|
||||
*/
|
||||
public static JsonWriteRequestBuilder builder() {
|
||||
return new JsonWriteRequestBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* A builder that provides a fluent API for constructing {@link JsonWriteRequest}s.
|
||||
*/
|
||||
public static class JsonWriteRequestBuilder extends WriteRequestBuilder<Map<String, Object>, JsonWriteRequestBuilder> {
|
||||
/**
|
||||
* Create a {@link JsonWriteRequestBuilder}. Intended for internal use.
|
||||
*/
|
||||
JsonWriteRequestBuilder() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonWriteRequestBuilder getBuilder() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a JSON credential. A JSON credential consists of
|
||||
* one or more fields in a JSON document. The provided {@literal Map} parameter.
|
||||
* will be converted to a JSON document before sending to CredHub. The type of
|
||||
* the credential is set to {@link ValueType#JSON}.
|
||||
*
|
||||
* @param value the json credential value; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public JsonWriteRequestBuilder value(Map<String, Object> value) {
|
||||
Assert.notNull(value, "value must not be null");
|
||||
this.valueType = JSON;
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* The details of a request to write a new or update an existing password credential in CredHub.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
|
||||
public class PasswordWriteRequest extends WriteRequest<String> {
|
||||
/**
|
||||
* Create a {@link PasswordWriteRequest} from the provided parameters. Intended for internal
|
||||
* use. Clients should use {@link #builder()} to construct instances of this class.
|
||||
*
|
||||
* @param name the name of the credential
|
||||
* @param overwrite {@literal false} to create a new credential, or
|
||||
* {@literal true} to update and existing credential
|
||||
* @param value the value of the credential
|
||||
* @param additionalPermissions access control permissions for the credential
|
||||
*/
|
||||
private PasswordWriteRequest(CredentialName name, boolean overwrite, String value,
|
||||
List<AdditionalPermission> additionalPermissions) {
|
||||
super(name, overwrite, value, ValueType.PASSWORD, additionalPermissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder that provides a fluent API for providing the values required
|
||||
* to construct a {@link PasswordWriteRequest}.
|
||||
*
|
||||
* @return a builder
|
||||
*/
|
||||
public static PasswordWriteRequestBuilder builder() {
|
||||
return new PasswordWriteRequestBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* A builder that provides a fluent API for constructing {@link PasswordWriteRequest}s.
|
||||
*/
|
||||
public static class PasswordWriteRequestBuilder extends WriteRequestBuilder<String, PasswordWriteRequestBuilder> {
|
||||
/**
|
||||
* Create a {@link PasswordWriteRequestBuilder}. Intended for internal use.
|
||||
*/
|
||||
PasswordWriteRequestBuilder() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PasswordWriteRequestBuilder getBuilder() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a password credential. A password credential consists of
|
||||
* a single string value. The type of the credential is set to {@link ValueType#PASSWORD}.
|
||||
*
|
||||
* @param value the password credential value; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public PasswordWriteRequestBuilder value(String value) {
|
||||
Assert.notNull(value, "value must not be null");
|
||||
this.valueType = ValueType.PASSWORD;
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,9 +19,9 @@
|
||||
package org.springframework.credhub.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
@@ -30,7 +30,6 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
|
||||
import static org.springframework.credhub.support.ValueType.JSON;
|
||||
|
||||
/**
|
||||
* The details of a request to write a new or update an existing credential in CredHub.
|
||||
@@ -38,17 +37,17 @@ import static org.springframework.credhub.support.ValueType.JSON;
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
|
||||
public class WriteRequest {
|
||||
public class WriteRequest<T> {
|
||||
private boolean overwrite;
|
||||
private CredentialName name;
|
||||
private ValueType valueType;
|
||||
private Object value;
|
||||
private T value;
|
||||
@JsonInclude(NON_EMPTY)
|
||||
private List<AdditionalPermission> additionalPermissions;
|
||||
|
||||
/**
|
||||
* Create a {@link WriteRequest} from the provided parameters. Intended for internal
|
||||
* use. Clients should use {@link #builder()} to construct instances of this class.
|
||||
* use.
|
||||
*
|
||||
* @param name the name of the credential
|
||||
* @param overwrite {@literal false} to create a new credential, or
|
||||
@@ -57,9 +56,9 @@ public class WriteRequest {
|
||||
* @param valueType the {@link ValueType} of the credential
|
||||
* @param additionalPermissions access control permissions for the credential
|
||||
*/
|
||||
private WriteRequest(CredentialName name, boolean overwrite,
|
||||
Object value, ValueType valueType,
|
||||
List<AdditionalPermission> additionalPermissions) {
|
||||
WriteRequest(CredentialName name, boolean overwrite,
|
||||
T value, ValueType valueType,
|
||||
List<AdditionalPermission> additionalPermissions) {
|
||||
this.name = name;
|
||||
this.overwrite = overwrite;
|
||||
this.valueType = valueType;
|
||||
@@ -93,7 +92,7 @@ public class WriteRequest {
|
||||
*
|
||||
* @return the value of the credential
|
||||
*/
|
||||
public Object getValue() {
|
||||
public T getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@@ -115,16 +114,6 @@ public class WriteRequest {
|
||||
return this.additionalPermissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder that provides a fluent API for providing the values required
|
||||
* to construct a {@link WriteRequest}.
|
||||
*
|
||||
* @return a builder
|
||||
*/
|
||||
public static WriteRequestBuilder builder() {
|
||||
return new WriteRequestBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
@@ -169,48 +158,38 @@ public class WriteRequest {
|
||||
/**
|
||||
* A builder that provides a fluent API for constructing {@link WriteRequest}s.
|
||||
*/
|
||||
public static class WriteRequestBuilder {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static abstract class WriteRequestBuilder<T, B extends WriteRequestBuilder<T, B>> {
|
||||
private final B thisObj;
|
||||
|
||||
private CredentialName name;
|
||||
private boolean overwrite;
|
||||
private Object value;
|
||||
private ValueType valueType;
|
||||
protected T value;
|
||||
protected ValueType valueType;
|
||||
private ArrayList<AdditionalPermission> additionalPermissions;
|
||||
|
||||
/**
|
||||
* Create a {@link WriteRequestBuilder}. Intended for internal use.
|
||||
*/
|
||||
WriteRequestBuilder() {
|
||||
this.thisObj = getBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a password credential. A password credential consists of
|
||||
* a single string value. The type of the credential is set to {@link ValueType#PASSWORD}.
|
||||
* Provide the concrete builder.
|
||||
*
|
||||
* @param value the password credential value; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public WriteRequestBuilder passwordValue(String value) {
|
||||
Assert.notNull(value, "value must not be null");
|
||||
this.valueType = ValueType.PASSWORD;
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
protected abstract B getBuilder();
|
||||
|
||||
/**
|
||||
* Set the value of a JSON credential. A JSON credential consists of
|
||||
* one or more fields in a JSON document. The provided {@literal Map} parameter.
|
||||
* will be converted to a JSON document before sending to CredHub. The type of
|
||||
* the credential is set to {@link ValueType#JSON}.
|
||||
* Set the value of a credential. In concrete builders, this should set the value
|
||||
* and the value type.
|
||||
*
|
||||
* @param value the json credential value; must not be {@literal null}
|
||||
* @param value the credential value; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public WriteRequestBuilder jsonValue(Map<String, Object> value) {
|
||||
Assert.notNull(value, "value must not be null");
|
||||
this.valueType = JSON;
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
public abstract B value(T value);
|
||||
|
||||
/**
|
||||
* Set the {@link CredentialName} for the credential.
|
||||
@@ -218,10 +197,10 @@ public class WriteRequest {
|
||||
* @param name the credential name; must not be {@literal null}
|
||||
* @return the builder
|
||||
*/
|
||||
public WriteRequestBuilder name(CredentialName name) {
|
||||
public B name(CredentialName name) {
|
||||
Assert.notNull(name, "name must not be null");
|
||||
this.name = name;
|
||||
return this;
|
||||
return thisObj;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,9 +211,9 @@ public class WriteRequest {
|
||||
* {@literal true} to update and existing credential
|
||||
* @return the builder
|
||||
*/
|
||||
public WriteRequestBuilder overwrite(boolean overwrite) {
|
||||
public B overwrite(boolean overwrite) {
|
||||
this.overwrite = overwrite;
|
||||
return this;
|
||||
return thisObj;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,10 +224,10 @@ public class WriteRequest {
|
||||
* credential
|
||||
* @return the builder
|
||||
*/
|
||||
public WriteRequestBuilder additionalPermission(AdditionalPermission additionalPermission) {
|
||||
public B additionalPermission(AdditionalPermission additionalPermission) {
|
||||
initPermissions();
|
||||
this.additionalPermissions.add(additionalPermission);
|
||||
return this;
|
||||
return thisObj;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -259,10 +238,24 @@ public class WriteRequest {
|
||||
* assign to the credential
|
||||
* @return the builder
|
||||
*/
|
||||
public WriteRequestBuilder additionalPermissions(Collection<? extends AdditionalPermission> permissions) {
|
||||
public B additionalPermissions(Collection<? extends AdditionalPermission> permissions) {
|
||||
initPermissions();
|
||||
this.additionalPermissions.addAll(permissions);
|
||||
return this;
|
||||
return thisObj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a collection of {@link AdditionalPermission}s to the controls that will be
|
||||
* assigned to the credential.
|
||||
*
|
||||
* @param permissions an collection of {@link AdditionalPermission}s to
|
||||
* assign to the credential
|
||||
* @return the builder
|
||||
*/
|
||||
public B additionalPermissions(AdditionalPermission... permissions) {
|
||||
initPermissions();
|
||||
this.additionalPermissions.addAll(Arrays.asList(permissions));
|
||||
return thisObj;
|
||||
}
|
||||
|
||||
private void initPermissions() {
|
||||
|
||||
@@ -23,6 +23,8 @@ import org.junit.experimental.theories.Theory;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.credhub.support.CredentialDetails;
|
||||
import org.springframework.credhub.support.PasswordWriteRequest;
|
||||
import org.springframework.credhub.support.ValueType;
|
||||
import org.springframework.credhub.support.WriteRequest;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -49,24 +51,19 @@ public class CredHubTemplateDetailResponseUnitTests extends CredHubTemplateUnitT
|
||||
|
||||
@DataPoint("responses")
|
||||
public static ResponseEntity<CredentialDetails> successfulResponse =
|
||||
new ResponseEntity<CredentialDetails>(CredentialDetails.detailsBuilder()
|
||||
.name(NAME)
|
||||
.id(CREDENTIAL_ID)
|
||||
.passwordValue(CREDENTIAL_VALUE)
|
||||
.versionCreatedAt(new Date())
|
||||
.build(),
|
||||
new ResponseEntity<CredentialDetails>(
|
||||
new CredentialDetails(CREDENTIAL_ID, NAME, ValueType.PASSWORD, CREDENTIAL_VALUE, new Date()),
|
||||
OK);
|
||||
|
||||
@DataPoint("responses")
|
||||
public static ResponseEntity<CredentialDetails> httpErrorResponse =
|
||||
new ResponseEntity<CredentialDetails>(CredentialDetails.detailsBuilder().build(),
|
||||
UNAUTHORIZED);
|
||||
new ResponseEntity<CredentialDetails>(new CredentialDetails(), UNAUTHORIZED);
|
||||
|
||||
@Theory
|
||||
public void write(@FromDataPoints("responses") ResponseEntity<CredentialDetails> expectedResponse) {
|
||||
WriteRequest request = WriteRequest.builder()
|
||||
WriteRequest request = PasswordWriteRequest.builder()
|
||||
.name(NAME)
|
||||
.passwordValue("secret")
|
||||
.value("secret")
|
||||
.build();
|
||||
|
||||
when(restTemplate.exchange(BASE_URL_PATH, PUT,
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.credhub.core;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.experimental.theories.DataPoint;
|
||||
@@ -26,6 +27,7 @@ import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.credhub.support.CredentialDetails;
|
||||
import org.springframework.credhub.support.CredentialDetailsData;
|
||||
import org.springframework.credhub.support.ValueType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
@@ -46,20 +48,14 @@ public class CredHubTemplateDetailsResponseUnitTests extends CredHubTemplateUnit
|
||||
@DataPoint("responses")
|
||||
public static ResponseEntity<CredentialDetailsData> successfulResponse =
|
||||
new ResponseEntity<CredentialDetailsData>(
|
||||
CredentialDetailsData.builder()
|
||||
.datum(CredentialDetails.detailsBuilder()
|
||||
.name(NAME)
|
||||
.id(CREDENTIAL_ID)
|
||||
.passwordValue(CREDENTIAL_VALUE)
|
||||
.build())
|
||||
.build(),
|
||||
new CredentialDetailsData(
|
||||
new CredentialDetails(CREDENTIAL_ID, NAME, ValueType.PASSWORD, CREDENTIAL_VALUE, new Date())),
|
||||
OK);
|
||||
|
||||
@DataPoint("responses")
|
||||
public static ResponseEntity<CredentialDetailsData> httpErrorResponse =
|
||||
new ResponseEntity<CredentialDetailsData>(
|
||||
CredentialDetailsData.builder()
|
||||
.build(),
|
||||
new CredentialDetailsData(),
|
||||
UNAUTHORIZED);
|
||||
|
||||
@Theory
|
||||
|
||||
@@ -45,19 +45,13 @@ public class CredHubTemplateSummaryResponseUnitTests extends CredHubTemplateUnit
|
||||
@DataPoint("responses")
|
||||
public static ResponseEntity<CredentialSummaryData> successfulResponse =
|
||||
new ResponseEntity<CredentialSummaryData>(
|
||||
CredentialSummaryData.builder()
|
||||
.credential(CredentialSummary.summaryBuilder()
|
||||
.name(NAME)
|
||||
.versionCreatedAt(new Date())
|
||||
.build())
|
||||
.build(),
|
||||
new CredentialSummaryData(new CredentialSummary(NAME, new Date())),
|
||||
OK);
|
||||
|
||||
@DataPoint("responses")
|
||||
public static ResponseEntity<CredentialSummaryData> httpErrorResponse =
|
||||
new ResponseEntity<CredentialSummaryData>(
|
||||
CredentialSummaryData.builder()
|
||||
.build(),
|
||||
new CredentialSummaryData(),
|
||||
UNAUTHORIZED);
|
||||
|
||||
@Theory
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
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 JsonWriteRequestUnitTests extends WriteRequestUnitTestsBase {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
requestBuilder = JsonWriteRequest.builder()
|
||||
.name(new SimpleCredentialName("example", "credential"))
|
||||
.value(new HashMap<String, Object>() {
|
||||
{
|
||||
put("data", "value");
|
||||
put("test", true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializationWithJsonValue() throws Exception {
|
||||
String jsonValue = serializeToJson(requestBuilder);
|
||||
|
||||
assertThat(jsonValue,
|
||||
allOf(hasJsonPath("$.overwrite", equalTo(false)),
|
||||
hasJsonPath("$.name", equalTo("/c/example/credential")),
|
||||
hasJsonPath("$.type", equalTo("json")),
|
||||
hasJsonPath("$.value.data", equalTo("value")),
|
||||
hasJsonPath("$.value.test", equalTo(true))));
|
||||
|
||||
assertThat(jsonValue, hasNoJsonPath("$.additional_permissions"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
*
|
||||
* * Copyright 2013-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;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
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 PasswordWriteRequestUnitTests extends WriteRequestUnitTestsBase {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
requestBuilder = PasswordWriteRequest.builder()
|
||||
.name(new SimpleCredentialName("example", "credential"))
|
||||
.overwrite(true)
|
||||
.value("secret");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializationWithPasswordValue() throws Exception {
|
||||
String jsonValue = serializeToJson(requestBuilder);
|
||||
|
||||
assertThat(jsonValue,
|
||||
allOf(hasJsonPath("$.overwrite", equalTo(true)),
|
||||
hasJsonPath("$.name", equalTo("/c/example/credential")),
|
||||
hasJsonPath("$.type", equalTo("password")),
|
||||
hasJsonPath("$.value", equalTo("secret"))));
|
||||
|
||||
assertThat(jsonValue, hasNoJsonPath("$.additional_permissions"));
|
||||
}
|
||||
}
|
||||
@@ -1,138 +0,0 @@
|
||||
/*
|
||||
*
|
||||
* * Copyright 2013-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;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.allOf;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.credhub.support.AdditionalPermission.Operation.READ;
|
||||
import static org.springframework.credhub.support.AdditionalPermission.Operation.WRITE;
|
||||
import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath;
|
||||
import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath;
|
||||
import static org.valid4j.matchers.jsonpath.JsonPathMatchers.isJson;
|
||||
|
||||
public class WriteRequestUnitTests {
|
||||
|
||||
private ObjectMapper mapper;
|
||||
|
||||
private WriteRequest.WriteRequestBuilder requestBuilder;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mapper = new ObjectMapper();
|
||||
|
||||
requestBuilder = WriteRequest.builder().name(
|
||||
new SimpleCredentialName("example", "credential"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeIsSerializable() {
|
||||
assertTrue(mapper.canSerialize(WriteRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializationWithJsonValue() throws Exception {
|
||||
requestBuilder.jsonValue(new HashMap<String, Object>() {
|
||||
{
|
||||
put("data", "value");
|
||||
put("test", true);
|
||||
}
|
||||
});
|
||||
|
||||
String jsonValue = serializeToJson(requestBuilder);
|
||||
|
||||
assertThat(jsonValue,
|
||||
allOf(hasJsonPath("$.overwrite", equalTo(false)),
|
||||
hasJsonPath("$.name", equalTo("/c/example/credential")),
|
||||
hasJsonPath("$.type", equalTo("json")),
|
||||
hasJsonPath("$.value.data", equalTo("value")),
|
||||
hasJsonPath("$.value.test", equalTo(true))));
|
||||
|
||||
assertThat(jsonValue, hasNoJsonPath("$.additional_permissions"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializationWithPasswordValue() throws Exception {
|
||||
requestBuilder.overwrite(true).passwordValue("secret");
|
||||
|
||||
String jsonValue = serializeToJson(requestBuilder);
|
||||
|
||||
assertThat(jsonValue,
|
||||
allOf(hasJsonPath("$.overwrite", equalTo(true)),
|
||||
hasJsonPath("$.name", equalTo("/c/example/credential")),
|
||||
hasJsonPath("$.type", equalTo("password")),
|
||||
hasJsonPath("$.value", equalTo("secret"))));
|
||||
|
||||
assertThat(jsonValue, hasNoJsonPath("$.additional_permissions"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializationWithOnePermission() throws Exception {
|
||||
requestBuilder.passwordValue("secret").additionalPermission(
|
||||
AdditionalPermission.builder().app("app-id").operation(READ).build());
|
||||
|
||||
String jsonValue = serializeToJson(requestBuilder);
|
||||
|
||||
assertThat(jsonValue,
|
||||
allOf(hasJsonPath("$.additional_permissions[0].actor",
|
||||
equalTo("mtls-app:app-id")),
|
||||
hasJsonPath("$.additional_permissions[0].operations[0]",
|
||||
equalTo("read"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializationWithTwoPermissions() throws Exception {
|
||||
requestBuilder.passwordValue("secret")
|
||||
.additionalPermission(AdditionalPermission.builder().app("app1-id")
|
||||
.operation(READ).operation(WRITE).build())
|
||||
.additionalPermission(AdditionalPermission.builder().app("app2-id")
|
||||
.operation(WRITE).operation(READ).build());
|
||||
|
||||
String jsonValue = serializeToJson(requestBuilder);
|
||||
|
||||
assertThat(jsonValue, allOf(
|
||||
hasJsonPath("$.additional_permissions[0].actor",
|
||||
equalTo("mtls-app:app1-id")),
|
||||
hasJsonPath("$.additional_permissions[0].operations[0]", equalTo("read")),
|
||||
hasJsonPath("$.additional_permissions[0].operations[1]",
|
||||
equalTo("write")),
|
||||
hasJsonPath("$.additional_permissions[1].actor",
|
||||
equalTo("mtls-app:app2-id")),
|
||||
hasJsonPath("$.additional_permissions[1].operations[0]",
|
||||
equalTo("write")),
|
||||
hasJsonPath("$.additional_permissions[1].operations[1]",
|
||||
equalTo("read"))));
|
||||
}
|
||||
|
||||
private String serializeToJson(WriteRequest.WriteRequestBuilder requestBuilder)
|
||||
throws JsonProcessingException {
|
||||
String jsonValue = mapper.writeValueAsString(requestBuilder.build());
|
||||
assertThat(jsonValue, isJson());
|
||||
return jsonValue;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.credhub.support.WriteRequest.WriteRequestBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.allOf;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.credhub.support.AdditionalPermission.Operation.READ;
|
||||
import static org.springframework.credhub.support.AdditionalPermission.Operation.WRITE;
|
||||
import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath;
|
||||
import static org.valid4j.matchers.jsonpath.JsonPathMatchers.isJson;
|
||||
|
||||
public abstract class WriteRequestUnitTestsBase {
|
||||
protected ObjectMapper mapper;
|
||||
protected WriteRequestBuilder requestBuilder;
|
||||
|
||||
@Before
|
||||
public void setUpWriteRequestUnitTestsBase() {
|
||||
mapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeIsSerializable() {
|
||||
assertTrue(mapper.canSerialize(WriteRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializationWithOnePermission() throws Exception {
|
||||
requestBuilder
|
||||
.additionalPermission(AdditionalPermission.builder()
|
||||
.app("app-id")
|
||||
.operation(READ)
|
||||
.build());
|
||||
|
||||
String jsonValue = serializeToJson(requestBuilder);
|
||||
|
||||
assertThat(jsonValue,
|
||||
allOf(hasJsonPath("$.additional_permissions[0].actor",
|
||||
equalTo("mtls-app:app-id")),
|
||||
hasJsonPath("$.additional_permissions[0].operations[0]",
|
||||
equalTo("read"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializationWithTwoPermissions() throws Exception {
|
||||
requestBuilder
|
||||
.additionalPermission(AdditionalPermission.builder()
|
||||
.app("app1-id")
|
||||
.operation(READ).operation(WRITE)
|
||||
.build())
|
||||
.additionalPermission(AdditionalPermission.builder()
|
||||
.app("app2-id")
|
||||
.operation(WRITE).operation(READ)
|
||||
.build());
|
||||
|
||||
String jsonValue = serializeToJson(requestBuilder);
|
||||
|
||||
assertThat(jsonValue, allOf(
|
||||
hasJsonPath("$.additional_permissions[0].actor",
|
||||
equalTo("mtls-app:app1-id")),
|
||||
hasJsonPath("$.additional_permissions[0].operations[0]", equalTo("read")),
|
||||
hasJsonPath("$.additional_permissions[0].operations[1]",
|
||||
equalTo("write")),
|
||||
hasJsonPath("$.additional_permissions[1].actor",
|
||||
equalTo("mtls-app:app2-id")),
|
||||
hasJsonPath("$.additional_permissions[1].operations[0]",
|
||||
equalTo("write")),
|
||||
hasJsonPath("$.additional_permissions[1].operations[1]",
|
||||
equalTo("read"))));
|
||||
}
|
||||
|
||||
protected String serializeToJson(WriteRequestBuilder requestBuilder)
|
||||
throws JsonProcessingException {
|
||||
String jsonValue = mapper.writeValueAsString(requestBuilder.build());
|
||||
assertThat(jsonValue, isJson());
|
||||
return jsonValue;
|
||||
}
|
||||
}
|
||||
@@ -29,9 +29,11 @@ import org.springframework.credhub.support.AdditionalPermission;
|
||||
import org.springframework.credhub.support.CredentialDetails;
|
||||
import org.springframework.credhub.support.CredentialName;
|
||||
import org.springframework.credhub.support.CredentialSummary;
|
||||
import org.springframework.credhub.support.JsonWriteRequest;
|
||||
import org.springframework.credhub.support.SimpleCredentialName;
|
||||
import org.springframework.credhub.support.VcapServicesData;
|
||||
import org.springframework.credhub.support.WriteRequest;
|
||||
import org.springframework.credhub.support.WriteRequest.WriteRequestBuilder;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@@ -73,12 +75,12 @@ public class CredHubDemoController {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> CredentialDetails writeCredentials(T value, Results results) {
|
||||
private CredentialDetails writeCredentials(Map<String, Object> value, Results results) {
|
||||
try {
|
||||
WriteRequest.WriteRequestBuilder requestBuilder = WriteRequest.builder()
|
||||
WriteRequestBuilder requestBuilder = JsonWriteRequest.builder()
|
||||
.overwrite(true)
|
||||
.name(new SimpleCredentialName("spring-credhub", "demo", "credentials_json"))
|
||||
.jsonValue((Map<String, Object>) value);
|
||||
.value(value);
|
||||
|
||||
if (StringUtils.hasText(appId)) {
|
||||
requestBuilder.additionalPermission(
|
||||
|
||||
Reference in New Issue
Block a user