Add permission V2 operations.

Fixes #44
This commit is contained in:
Scott Frederick
2018-10-31 14:12:57 -05:00
parent fd811f36a5
commit 49fe1223b3
18 changed files with 765 additions and 77 deletions

View File

@@ -21,6 +21,7 @@ import org.springframework.credhub.core.credential.CredHubCredentialOperations;
import org.springframework.credhub.core.info.CredHubInfoOperations;
import org.springframework.credhub.core.interpolation.CredHubInterpolationOperations;
import org.springframework.credhub.core.permission.CredHubPermissionOperations;
import org.springframework.credhub.core.permissionV2.CredHubPermissionV2Operations;
import org.springframework.web.client.RestTemplate;
/**
@@ -43,6 +44,13 @@ public interface CredHubOperations {
*/
CredHubPermissionOperations permissions();
/**
* Get the operations for adding, retrieving, and deleting credential permissions.
*
* @return the permissions operations
*/
CredHubPermissionV2Operations permissionsV2();
/**
* Get the operations for retrieving, regenerating, and updating certificates.
*

View File

@@ -26,6 +26,8 @@ import org.springframework.credhub.core.interpolation.CredHubInterpolationOperat
import org.springframework.credhub.core.interpolation.CredHubInterpolationTemplate;
import org.springframework.credhub.core.permission.CredHubPermissionOperations;
import org.springframework.credhub.core.permission.CredHubPermissionTemplate;
import org.springframework.credhub.core.permissionV2.CredHubPermissionV2Operations;
import org.springframework.credhub.core.permissionV2.CredHubPermissionV2Template;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.util.Assert;
import org.springframework.web.client.HttpStatusCodeException;
@@ -88,6 +90,16 @@ public class CredHubTemplate implements CredHubOperations {
return new CredHubPermissionTemplate(this);
}
/**
* Get the operations for adding, retrieving, and deleting permissions from a credential.
*
* @return the permissions operations
*/
@Override
public CredHubPermissionV2Operations permissionsV2() {
return new CredHubPermissionV2Template(this);
}
/**
* Get the operations for retrieving, regenerating, and updating certificates.
*

View File

@@ -18,7 +18,7 @@ package org.springframework.credhub.core.permission;
import org.springframework.credhub.support.CredentialName;
import org.springframework.credhub.support.permissions.Actor;
import org.springframework.credhub.support.permissions.CredentialPermission;
import org.springframework.credhub.support.permissions.Permission;
import java.util.List;
@@ -34,7 +34,7 @@ public interface CredHubPermissionOperations {
* @param name the name of the credential; must not be {@literal null}
* @return the collection of permissions associated with the credential
*/
List<CredentialPermission> getPermissions(final CredentialName name);
List<Permission> getPermissions(final CredentialName name);
/**
* Add permissions to an existing credential.
@@ -42,7 +42,7 @@ public interface CredHubPermissionOperations {
* @param name the name of the credential; must not be {@literal null}
* @param permissions a collection of permissions to add
*/
void addPermissions(final CredentialName name, final CredentialPermission... permissions);
void addPermissions(final CredentialName name, final Permission... permissions);
/**
* Delete a permission associated with a credential.

View File

@@ -21,7 +21,7 @@ import org.springframework.credhub.core.RestOperationsCallback;
import org.springframework.credhub.support.CredentialName;
import org.springframework.credhub.support.CredentialPermissions;
import org.springframework.credhub.support.permissions.Actor;
import org.springframework.credhub.support.permissions.CredentialPermission;
import org.springframework.credhub.support.permissions.Permission;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
@@ -53,12 +53,12 @@ public class CredHubPermissionTemplate implements CredHubPermissionOperations {
}
@Override
public List<CredentialPermission> getPermissions(final CredentialName name) {
public List<Permission> getPermissions(final CredentialName name) {
Assert.notNull(name, "credential name must not be null");
return credHubOperations.doWithRest(new RestOperationsCallback<List<CredentialPermission>>() {
return credHubOperations.doWithRest(new RestOperationsCallback<List<Permission>>() {
@Override
public List<CredentialPermission> doWithRestOperations(RestOperations restOperations) {
public List<Permission> doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialPermissions> response =
restOperations.getForEntity(PERMISSIONS_URL_QUERY,
CredentialPermissions.class, name.getName());
@@ -69,7 +69,7 @@ public class CredHubPermissionTemplate implements CredHubPermissionOperations {
@Override
public void addPermissions(final CredentialName name,
final CredentialPermission... permissions) {
final Permission... permissions) {
Assert.notNull(name, "credential name must not be null");
final CredentialPermissions credentialPermissions = new CredentialPermissions(name, permissions);

View File

@@ -0,0 +1,61 @@
/*
* 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.core.permissionV2;
import org.springframework.credhub.support.CredentialName;
import org.springframework.credhub.support.CredentialPermission;
import org.springframework.credhub.support.permissions.Permission;
/**
* Specifies the interactions with CredHub to add, retrieve, and delete permissions.
*
* @author Scott Frederick
*/
public interface CredHubPermissionV2Operations {
/**
* Get a permission.
*
* @param id the CredHub-assigned ID of the permission; must not be {@literal null}
* @return the details if the specified permission
*/
CredentialPermission getPermissions(final String id);
/**
* Add permissions to an credential path.
*
* @param path the path of the credentials; must not be {@literal null}
* @param permission a permission to add
* @return the details if the added permission
*/
CredentialPermission addPermissions(final CredentialName path, final Permission permission);
/**
* Add permissions to an existing credential.
*
* @param path the path of the credentials; must not be {@literal null}
* @param permission a permission to add
* @return the details if the added permission
*/
CredentialPermission updatePermissions(final String id, final CredentialName path, final Permission permission);
/**
* Delete a permission.
*
* @param id the CredHub-assigned ID of the permission; must not be {@literal null}
*/
void deletePermission(final String id);
}

View File

@@ -0,0 +1,119 @@
/*
* 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.core.permissionV2;
import org.springframework.credhub.core.CredHubOperations;
import org.springframework.credhub.core.RestOperationsCallback;
import org.springframework.credhub.support.CredentialName;
import org.springframework.credhub.support.CredentialPermission;
import org.springframework.credhub.support.permissions.Permission;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.client.RestOperations;
/**
* Implements the main interaction with CredHub to add, retrieve,
* and delete permissions.
*
* @author Scott Frederick
*/
public class CredHubPermissionV2Template implements CredHubPermissionV2Operations {
static final String PERMISSIONS_URL_PATH = "/api/v2/permissions";
static final String PERMISSIONS_ID_URL_PATH = PERMISSIONS_URL_PATH + "/{id}";
private CredHubOperations credHubOperations;
/**
* Create a new {@link CredHubPermissionV2Template}.
*
* @param credHubOperations the {@link CredHubOperations} to use for interactions with CredHub
*/
public CredHubPermissionV2Template(CredHubOperations credHubOperations) {
this.credHubOperations = credHubOperations;
}
@Override
public CredentialPermission getPermissions(final String id) {
Assert.notNull(id, "credential ID must not be null");
return credHubOperations.doWithRest(new RestOperationsCallback<CredentialPermission>() {
@Override
public CredentialPermission doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialPermission> response =
restOperations.getForEntity(PERMISSIONS_ID_URL_PATH,
CredentialPermission.class, id);
return response.getBody();
}
});
}
@Override
public CredentialPermission addPermissions(final CredentialName path,
final Permission permission) {
Assert.notNull(path, "credential path must not be null");
Assert.notNull(permission, "credential permission must not be null");
final CredentialPermission credentialPermission = new CredentialPermission(path, permission);
return credHubOperations.doWithRest(new RestOperationsCallback<CredentialPermission>() {
@Override
public CredentialPermission doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialPermission> response =
restOperations.exchange(PERMISSIONS_URL_PATH, HttpMethod.POST,
new HttpEntity<>(credentialPermission),
CredentialPermission.class);
return response.getBody();
}
});
}
@Override
public CredentialPermission updatePermissions(final String id, final CredentialName path,
final Permission permission) {
Assert.notNull(id, "credential ID must not be null");
Assert.notNull(path, "credential path must not be null");
Assert.notNull(permission, "credential permission must not be null");
final CredentialPermission credentialPermission = new CredentialPermission(path, permission);
return credHubOperations.doWithRest(new RestOperationsCallback<CredentialPermission>() {
@Override
public CredentialPermission doWithRestOperations(RestOperations restOperations) {
ResponseEntity<CredentialPermission> response =
restOperations.exchange(PERMISSIONS_ID_URL_PATH, HttpMethod.PUT,
new HttpEntity<>(credentialPermission),
CredentialPermission.class, id);
return response.getBody();
}
});
}
@Override
public void deletePermission(final String id) {
Assert.notNull(id, "credential ID must not be null");
credHubOperations.doWithRest(new RestOperationsCallback<Void>() {
@Override
public Void doWithRestOperations(RestOperations restOperations) {
restOperations.delete(PERMISSIONS_ID_URL_PATH, id);
return null;
}
});
}
}

View File

@@ -0,0 +1,20 @@
/*
* 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.
*/
/**
* Core API abstractions for permission operations.
*/
package org.springframework.credhub.core.permissionV2;

View File

@@ -18,7 +18,7 @@ package org.springframework.credhub.support;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.credhub.core.permission.CredHubPermissionOperations;
import org.springframework.credhub.support.permissions.CredentialPermission;
import org.springframework.credhub.support.permissions.Permission;
import org.springframework.util.Assert;
import java.util.ArrayList;
@@ -38,7 +38,7 @@ public class CredHubRequest<T> {
protected WriteMode mode;
protected CredentialName name;
protected CredentialType credentialType;
protected List<CredentialPermission> additionalPermissions;
protected List<Permission> additionalPermissions;
protected T details;
public CredHubRequest() {
@@ -105,11 +105,11 @@ public class CredHubRequest<T> {
}
/**
* Get the set of {@link CredentialPermission} to assign to the credential.
* Get the set of {@link Permission} to assign to the credential.
*
* @return the set of {@link CredentialPermission}
* @return the set of {@link Permission}
*/
public List<CredentialPermission> getAdditionalPermissions() {
public List<Permission> getAdditionalPermissions() {
return this.additionalPermissions;
}
@@ -219,45 +219,45 @@ public class CredHubRequest<T> {
}
/**
* Add an {@link CredentialPermission} to the permissions that will be assigned to the
* Add an {@link Permission} to the permissions that will be assigned to the
* credential.
*
* @param permission a {@link CredentialPermission} to assign to the credential
* @param permission a {@link Permission} to assign to the credential
* @return the builder
* @deprecated as of CredHub 2.0, use {@link CredHubPermissionOperations} to assign
* permissions to a credential after it is created
*/
public B permission(CredentialPermission permission) {
public B permission(Permission permission) {
targetObj.getAdditionalPermissions().add(permission);
return thisObj;
}
/**
* Add a collection of {@link CredentialPermission}s to the controls that will be
* Add a collection of {@link Permission}s to the controls that will be
* assigned to the credential.
*
* @param permissions a collection of {@link CredentialPermission}s to
* @param permissions a collection of {@link Permission}s to
* assign to the credential
* @return the builder
* @deprecated as of CredHub 2.0, use {@link CredHubPermissionOperations} to assign
* permissions to a credential after it is created
*/
public B permissions(Collection<? extends CredentialPermission> permissions) {
public B permissions(Collection<? extends Permission> permissions) {
targetObj.getAdditionalPermissions().addAll(permissions);
return thisObj;
}
/**
* Add a collection of {@link CredentialPermission}s to the controls that will be
* Add a collection of {@link Permission}s to the controls that will be
* assigned to the credential.
*
* @param permissions a collection of {@link CredentialPermission}s to
* @param permissions a collection of {@link Permission}s to
* assign to the credential
* @return the builder
* @deprecated as of CredHub 2.0, use {@link CredHubPermissionOperations} to assign
* permissions to a credential after it is created
*/
public B permissions(CredentialPermission... permissions) {
public B permissions(Permission... permissions) {
targetObj.getAdditionalPermissions().addAll(Arrays.asList(permissions));
return thisObj;
}

View File

@@ -0,0 +1,118 @@
/*
*
* 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 permission and
* limitations under the License.
*
*/
package org.springframework.credhub.support;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import org.springframework.credhub.support.permissions.Permission;
import java.util.Objects;
/**
* A {@link Permission} associated with a credential. Clients don't typically instantiate
* objects of this type, but will receive them in response to write and retrieve
* requests.
*
* @author Scott Frederick
*/
public class CredentialPermission {
private final String uuid;
private final CredentialName path;
@JsonUnwrapped
private final Permission permission;
/**
* Create a {@link CredentialPermission}.
*/
@SuppressWarnings("unused")
private CredentialPermission() {
this.uuid = null;
this.path = null;
this.permission = null;
}
/**
* Create a {@link CredentialPermission} from the provided parameters. Intended for internal
* use. Clients will get {@link CredentialPermission} objects populated from
* CredHub responses.
*
* @param path the path of the credential(s) that the permission will apply to
* @param permission a collection of {@link Permission}s
*/
public CredentialPermission(CredentialName path, Permission permission) {
this.path = path;
this.permission = permission;
this.uuid = null;
}
/**
* Get the CredHub-assigned ID of the permission.
*
* @return the permission ID
*/
public String getId() {
return this.uuid;
}
/**
* Get the name of the credential that the permission apply to.
*
* @return the credential name
*/
public String getPath() {
return this.path.getName();
}
/**
* Get the collection of {@link Permission}s.
*
* @return the collection of {@link Permission}s
*/
public Permission getPermission() {
return this.permission;
}
@Override
public String toString() {
return "CredentialPermissions{"
+ "uuid=" + uuid
+ ", path=" + path
+ ", permission=" + permission
+ '}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CredentialPermission)) return false;
CredentialPermission that = (CredentialPermission) o;
if (uuid != null ? !uuid.equals(that.uuid) : that.uuid != null)
return false;
if (path != null ? !path.equals(that.path) : that.path != null)
return false;
return permission != null ? permission.equals(that.permission) : that.permission == null;
}
@Override
public int hashCode() {
return Objects.hash(uuid, path, permission);
}
}

View File

@@ -18,22 +18,22 @@
package org.springframework.credhub.support;
import org.springframework.credhub.support.permissions.CredentialPermission;
import org.springframework.credhub.support.permissions.Permission;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* A collection of {@link CredentialPermission}s. Clients don't typically instantiate
* objects of this type, but will receive them in response to write and retrieve
* requests.
* A collection of {@link Permission}s associated with a credential. Clients don't
* typically instantiate objects of this type, but will receive them in response
* to write and retrieve requests.
*
* @author Scott Frederick
*/
public class CredentialPermissions {
private final CredentialName credentialName;
private final List<CredentialPermission> permissions;
private final List<Permission> permissions;
/**
* Create a {@link CredentialPermissions}.
@@ -50,9 +50,9 @@ public class CredentialPermissions {
* CredHub responses.
*
* @param credentialName the name of the credential that the permissions will apply to
* @param permissions a collection of {@link CredentialPermission}s
* @param permissions a collection of {@link Permission}s
*/
public CredentialPermissions(CredentialName credentialName, CredentialPermission... permissions) {
public CredentialPermissions(CredentialName credentialName, Permission... permissions) {
this.credentialName = credentialName;
this.permissions = Arrays.asList(permissions);
}
@@ -67,11 +67,11 @@ public class CredentialPermissions {
}
/**
* Get the collection of {@link CredentialPermission}s.
* Get the collection of {@link Permission}s.
*
* @return the collection of {@link CredentialPermission}s
* @return the collection of {@link Permission}s
*/
public List<CredentialPermission> getPermissions() {
public List<Permission> getPermissions() {
return this.permissions;
}

View File

@@ -38,7 +38,7 @@ import java.util.Objects;
*
* @author Scott Frederick
*/
public class CredentialPermission {
public class Permission {
private final Actor actor;
@JsonProperty
@@ -48,7 +48,7 @@ public class CredentialPermission {
* Create a {@literal CredentialPermission}.
*/
@SuppressWarnings("unused")
private CredentialPermission() {
private Permission() {
this.actor = null;
this.operations = null;
}
@@ -61,7 +61,7 @@ public class CredentialPermission {
* @param operations the operations that the actor will be allowed to perform on the
* credential
*/
private CredentialPermission(Actor actor, List<Operation> operations) {
private Permission(Actor actor, List<Operation> operations) {
this.actor = actor;
this.operations = operations;
}
@@ -97,7 +97,7 @@ public class CredentialPermission {
return null;
}
List<String> operationValues = new ArrayList<String>(operations.size());
List<String> operationValues = new ArrayList<>(operations.size());
for (Operation operation : operations) {
operationValues.add(operation.operation());
}
@@ -106,7 +106,7 @@ public class CredentialPermission {
/**
* Create a builder that provides a fluent API for providing the values required
* to construct a {@link CredentialPermission}.
* to construct a {@link Permission}.
*
* @return a builder
*/
@@ -118,10 +118,10 @@ public class CredentialPermission {
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof CredentialPermission))
if (!(o instanceof Permission))
return false;
CredentialPermission that = (CredentialPermission) o;
Permission that = (Permission) o;
if (actor != null ? !actor.equals(that.actor) : that.actor != null)
return false;
@@ -143,7 +143,7 @@ public class CredentialPermission {
}
/**
* A builder that provides a fluent API for constructing {@link CredentialPermission}
* A builder that provides a fluent API for constructing {@link Permission}
* instances.
*/
public static class CredentialPermissionBuilder {
@@ -253,15 +253,15 @@ public class CredentialPermission {
}
private void initOperations() {
if (this.operations == null) this.operations = new ArrayList<Operation>();
if (this.operations == null) this.operations = new ArrayList<>();
}
/**
* Construct a {@link CredentialPermission} with the provided values.
* Construct a {@link Permission} with the provided values.
*
* @return a {@link CredentialPermission}
* @return a {@link Permission}
*/
public CredentialPermission build() {
public Permission build() {
List<Operation> operations;
switch (this.operations == null ? 0 : this.operations.size()) {
case 0:
@@ -271,10 +271,10 @@ public class CredentialPermission {
operations = java.util.Collections.singletonList(this.operations.get(0));
break;
default:
operations = java.util.Collections.unmodifiableList(new ArrayList<Operation>(this.operations));
operations = java.util.Collections.unmodifiableList(new ArrayList<>(this.operations));
}
return new CredentialPermission(actor, operations);
return new Permission(actor, operations);
}
}
}

View File

@@ -26,7 +26,7 @@ import org.springframework.credhub.support.CredentialPermissions;
import org.springframework.credhub.support.SimpleCredentialName;
import org.springframework.credhub.support.permissions.Actor;
import org.springframework.credhub.support.permissions.ActorType;
import org.springframework.credhub.support.permissions.CredentialPermission;
import org.springframework.credhub.support.permissions.Permission;
import org.springframework.credhub.support.permissions.Operation;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
@@ -44,7 +44,7 @@ import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpStatus.OK;
@RunWith(MockitoJUnitRunner.class)
public class CredHubPermissionsTemplateUnitTests {
public class CredHubPermissionTemplateUnitTests {
private static final SimpleCredentialName NAME = new SimpleCredentialName("example", "credential");
@Mock
@@ -61,12 +61,12 @@ public class CredHubPermissionsTemplateUnitTests {
public void getPermissions() {
CredentialPermissions expectedResponse = new CredentialPermissions(
NAME,
CredentialPermission.builder()
Permission.builder()
.app("app-id")
.operation(Operation.READ)
.operation(Operation.WRITE)
.build(),
CredentialPermission.builder()
Permission.builder()
.user("zone1", "user-id")
.operations(Operation.READ_ACL)
.operations(Operation.WRITE_ACL)
@@ -77,7 +77,7 @@ public class CredHubPermissionsTemplateUnitTests {
when(restTemplate.getForEntity(PERMISSIONS_URL_QUERY, CredentialPermissions.class, NAME.getName()))
.thenReturn(new ResponseEntity<>(expectedResponse, OK));
List<CredentialPermission> response = credHubTemplate.getPermissions(NAME);
List<Permission> response = credHubTemplate.getPermissions(NAME);
assertThat(response).isNotNull();
assertThat(response).hasSize(expectedResponse.getPermissions().size());
@@ -86,13 +86,13 @@ public class CredHubPermissionsTemplateUnitTests {
@Test
public void addPermissions() {
CredentialPermission permission1 = CredentialPermission.builder()
Permission permission1 = Permission.builder()
.app("app-id")
.operation(Operation.READ)
.operation(Operation.WRITE)
.build();
CredentialPermission permission2 = CredentialPermission.builder()
Permission permission2 = Permission.builder()
.user("zone1", "user-id")
.operations(Operation.READ_ACL)
.operations(Operation.WRITE_ACL)

View File

@@ -0,0 +1,133 @@
/*
* 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.core.permissionV2;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.credhub.core.CredHubTemplate;
import org.springframework.credhub.support.CredentialPermission;
import org.springframework.credhub.support.SimpleCredentialName;
import org.springframework.credhub.support.permissions.Operation;
import org.springframework.credhub.support.permissions.Permission;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.credhub.core.permissionV2.CredHubPermissionV2Template.PERMISSIONS_ID_URL_PATH;
import static org.springframework.credhub.core.permissionV2.CredHubPermissionV2Template.PERMISSIONS_URL_PATH;
import static org.springframework.credhub.support.permissions.ActorType.APP;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpMethod.PUT;
import static org.springframework.http.HttpStatus.OK;
@RunWith(MockitoJUnitRunner.class)
public class CredHubPermissionV2TemplateUnitTests {
private static final SimpleCredentialName PATH =
new SimpleCredentialName("example", "credential", "*");
@Mock
private RestTemplate restTemplate;
private CredHubPermissionV2Operations credHubTemplate;
@Before
public void setUp() {
credHubTemplate = new CredHubTemplate(restTemplate).permissionsV2();
}
@Test
public void getPermissions() {
String permissionId = "uuid";
CredentialPermission expectedResponse = new CredentialPermission(
PATH,
Permission.builder()
.app("app-id")
.operation(Operation.READ)
.operation(Operation.WRITE)
.build());
when(restTemplate.getForEntity(PERMISSIONS_ID_URL_PATH, CredentialPermission.class, permissionId))
.thenReturn(new ResponseEntity<>(expectedResponse, OK));
CredentialPermission response = credHubTemplate.getPermissions(permissionId);
assertThat(response).isNotNull();
assertThat(response.getPath()).isEqualTo(PATH.getName());
assertThat(response.getPermission().getActor().getAuthType()).isEqualTo(APP);
assertThat(response.getPermission().getOperations()).contains(Operation.READ, Operation.WRITE);
}
@Test
public void addPermissions() {
Permission permission = Permission.builder()
.app("app-id")
.operation(Operation.READ)
.operation(Operation.WRITE)
.build();
CredentialPermission expectedResponse = new CredentialPermission(PATH, permission);
when(restTemplate.exchange(PERMISSIONS_URL_PATH, POST, new HttpEntity<>(expectedResponse),
CredentialPermission.class))
.thenReturn(new ResponseEntity<>(expectedResponse, OK));
CredentialPermission response = credHubTemplate.addPermissions(PATH, permission);
assertThat(response).isNotNull();
assertThat(response.getPath()).isEqualTo(PATH.getName());
assertThat(response.getPermission().getActor().getAuthType()).isEqualTo(APP);
assertThat(response.getPermission().getOperations()).contains(Operation.READ, Operation.WRITE);
}
@Test
public void updatePermissions() {
String permissionId = "uuid";
Permission permission = Permission.builder()
.app("app-id")
.operation(Operation.READ)
.operation(Operation.WRITE)
.build();
CredentialPermission expectedResponse = new CredentialPermission(PATH, permission);
when(restTemplate.exchange(PERMISSIONS_ID_URL_PATH, PUT, new HttpEntity<>(expectedResponse),
CredentialPermission.class, permissionId))
.thenReturn(new ResponseEntity<>(expectedResponse, OK));
CredentialPermission response = credHubTemplate.updatePermissions(permissionId, PATH, permission);
assertThat(response).isNotNull();
assertThat(response.getPath()).isEqualTo(PATH.getName());
assertThat(response.getPermission().getActor().getAuthType()).isEqualTo(APP);
assertThat(response.getPermission().getOperations()).contains(Operation.READ, Operation.WRITE);
}
@Test
public void deletePermission() {
credHubTemplate.deletePermission("uuid");
verify(restTemplate).delete(PERMISSIONS_ID_URL_PATH, "uuid");
}
}

View File

@@ -21,7 +21,7 @@ import org.junit.Test;
import org.springframework.credhub.support.CredHubRequest.CredHubRequestBuilder;
import org.springframework.credhub.support.permissions.Actor;
import org.springframework.credhub.support.permissions.CredentialPermission;
import org.springframework.credhub.support.permissions.Permission;
import static org.springframework.credhub.support.JsonPathAssert.assertThat;
@@ -37,7 +37,7 @@ public abstract class CredHubRequestUnitTestsBase {
@SuppressWarnings("deprecation")
public void serializationWithOnePermission() {
requestBuilder
.permission(CredentialPermission.builder()
.permission(Permission.builder()
.app("app-id")
.operation(READ)
.build());
@@ -54,15 +54,15 @@ public abstract class CredHubRequestUnitTestsBase {
@SuppressWarnings({"unchecked", "deprecation"})
public void serializationWithThreePermissions() {
requestBuilder
.permission(CredentialPermission.builder()
.permission(Permission.builder()
.app("app-id")
.operation(READ).operation(WRITE)
.build())
.permission(CredentialPermission.builder()
.permission(Permission.builder()
.user("zone1", "user-id")
.operations(READ_ACL, WRITE_ACL)
.build())
.permission(CredentialPermission.builder()
.permission(Permission.builder()
.client("client-id")
.operations(READ, WRITE, READ_ACL, WRITE_ACL)
.build());

View File

@@ -0,0 +1,79 @@
/*
*
* 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.permissions;
import com.jayway.jsonpath.DocumentContext;
import org.junit.Test;
import org.springframework.credhub.support.CredentialPermission;
import org.springframework.credhub.support.JsonParsingUnitTestsBase;
import org.springframework.credhub.support.JsonTestUtils;
import org.springframework.credhub.support.SimpleCredentialName;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.credhub.support.JsonPathAssert.assertThat;
public class CredentialPermissionUnitTests extends JsonParsingUnitTestsBase {
@Test
public void deserializePermission() {
String json = "{" +
"\"uuid\": \"uuid\",\n" +
"\"path\": \"/example-directory/example-specific-credential\",\n" +
"\"actor\": \"uaa-user:106f52e2-5d01-4675-8d7a-c05ff9a2c081\",\n" +
"\"operations\": [\"read\",\"write\"]" +
"}";
CredentialPermission credentialPermission = parsePermission(json);
assertThat(credentialPermission.getId()).isEqualTo("uuid");
assertThat(credentialPermission.getPath()).isEqualTo("/example-directory/example-specific-credential");
Permission permission = credentialPermission.getPermission();
Actor actor = permission.getActor();
assertThat(actor.getAuthType()).isEqualTo(ActorType.USER);
assertThat(actor.getPrimaryIdentifier()).isEqualTo("106f52e2-5d01-4675-8d7a-c05ff9a2c081");
List<Operation> operations = permission.getOperations();
assertThat(operations.size()).isEqualTo(2);
assertThat(operations).contains(Operation.READ, Operation.WRITE);
}
@Test
public void serializePermission() {
CredentialPermission permission =
new CredentialPermission(new SimpleCredentialName("example", "credential", "*"),
Permission.builder()
.app("appid1")
.operations(Operation.READ, Operation.WRITE)
.build());
DocumentContext json = JsonTestUtils.toJsonPath(permission);
assertThat(json).hasPath("$.path").isEqualTo("/example/credential/*");
assertThat(json).hasPath("$.actor").isEqualTo(Actor.app("appid1").getIdentity());
assertThat(json).hasPath("$.operations[0]").isEqualTo("read");
assertThat(json).hasPath("$.operations[1]").isEqualTo("write");
}
private CredentialPermission parsePermission(String json) {
return JsonTestUtils.fromJson(json, CredentialPermission.class);
}
}

View File

@@ -32,11 +32,6 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.credhub.support.JsonPathAssert.assertThat;
import static org.springframework.credhub.support.permissions.ActorType.APP;
import static org.springframework.credhub.support.permissions.ActorType.USER;
import static org.springframework.credhub.support.permissions.Operation.DELETE;
import static org.springframework.credhub.support.permissions.Operation.READ;
import static org.springframework.credhub.support.permissions.Operation.READ_ACL;
import static org.springframework.credhub.support.permissions.Operation.WRITE;
import static org.springframework.credhub.support.permissions.Operation.WRITE_ACL;
public class CredentialPermissionsUnitTests extends JsonParsingUnitTestsBase {
@Test
@@ -66,13 +61,14 @@ public class CredentialPermissionsUnitTests extends JsonParsingUnitTestsBase {
assertThat(permissions.getCredentialName()).isEqualTo("/c/example");
assertThat(permissions.getPermissions().size()).isEqualTo(2);
CredentialPermission permission = permissions.getPermissions().get(0);
Permission permission = permissions.getPermissions().get(0);
assertThat(permission.getActor().getAuthType()).isEqualTo(APP);
assertThat(permission.getActor().getPrimaryIdentifier()).isEqualTo("appid1");
List<Operation> operations = permission.getOperations();
assertThat(operations.size()).isEqualTo(5);
assertThat(operations).contains(READ, WRITE, DELETE, READ_ACL, WRITE_ACL);
assertThat(operations).contains(Operation.READ, Operation.WRITE, Operation.DELETE,
Operation.READ_ACL, Operation.WRITE_ACL);
permission = permissions.getPermissions().get(1);
assertThat(permission.getActor().getAuthType()).isEqualTo(USER);
@@ -80,7 +76,7 @@ public class CredentialPermissionsUnitTests extends JsonParsingUnitTestsBase {
operations = permission.getOperations();
assertThat(operations.size()).isEqualTo(1);
assertThat(operations).contains(READ);
assertThat(operations).contains(Operation.READ);
}
@Test
@@ -94,11 +90,12 @@ public class CredentialPermissionsUnitTests extends JsonParsingUnitTestsBase {
@Test
public void serialize() {
CredentialPermissions permissions = new CredentialPermissions(new SimpleCredentialName("example", "credentialName"),
CredentialPermission.builder()
.app("appid1")
.operations(READ, WRITE)
.build());
CredentialPermissions permissions =
new CredentialPermissions(new SimpleCredentialName("example", "credentialName"),
Permission.builder()
.app("appid1")
.operations(Operation.READ, Operation.WRITE)
.build());
DocumentContext json = JsonTestUtils.toJsonPath(permissions);

View File

@@ -23,7 +23,7 @@ import org.springframework.credhub.core.credential.CredHubCredentialOperations;
import org.springframework.credhub.core.permission.CredHubPermissionOperations;
import org.springframework.credhub.support.SimpleCredentialName;
import org.springframework.credhub.support.permissions.Actor;
import org.springframework.credhub.support.permissions.CredentialPermission;
import org.springframework.credhub.support.permissions.Permission;
import org.springframework.credhub.support.permissions.Operation;
import org.springframework.credhub.support.value.ValueCredentialRequest;
@@ -59,15 +59,15 @@ public class PermissionIntegrationTests extends CredHubIntegrationTests {
.value(CREDENTIAL_VALUE)
.build());
CredentialPermission appPermission = CredentialPermission.builder()
Permission appPermission = Permission.builder()
.app("app1")
.operation(Operation.READ)
.build();
CredentialPermission userPermission = CredentialPermission.builder()
Permission userPermission = Permission.builder()
.user("user1")
.operations(Operation.READ, Operation.WRITE, Operation.DELETE)
.build();
CredentialPermission clientPermission = CredentialPermission.builder()
Permission clientPermission = Permission.builder()
.client("client1")
.operations(Operation.READ_ACL, Operation.WRITE_ACL)
.build();
@@ -77,7 +77,7 @@ public class PermissionIntegrationTests extends CredHubIntegrationTests {
userPermission,
clientPermission);
List<CredentialPermission> retrievedPermissions = permissions.getPermissions(CREDENTIAL_NAME);
List<Permission> retrievedPermissions = permissions.getPermissions(CREDENTIAL_NAME);
// CredHub 1.x will automatically add a permission for the authenticated user;
// CredHub 2.x will not
assertThat(retrievedPermissions.size()).isBetween(3, 4);
@@ -88,7 +88,7 @@ public class PermissionIntegrationTests extends CredHubIntegrationTests {
permissions.deletePermission(CREDENTIAL_NAME, Actor.user("user1"));
permissions.deletePermission(CREDENTIAL_NAME, Actor.client("client1"));
List<CredentialPermission> afterDelete = permissions.getPermissions(CREDENTIAL_NAME);
List<Permission> afterDelete = permissions.getPermissions(CREDENTIAL_NAME);
assertThat(afterDelete.size()).isBetween(0, 1);
}
}

View File

@@ -0,0 +1,141 @@
/*
* 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.integration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.credhub.core.credential.CredHubCredentialOperations;
import org.springframework.credhub.core.permissionV2.CredHubPermissionV2Operations;
import org.springframework.credhub.support.CredentialPermission;
import org.springframework.credhub.support.SimpleCredentialName;
import org.springframework.credhub.support.permissions.Actor;
import org.springframework.credhub.support.permissions.ActorType;
import org.springframework.credhub.support.permissions.Operation;
import org.springframework.credhub.support.permissions.Permission;
import org.springframework.credhub.support.value.ValueCredentialRequest;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.springframework.credhub.support.permissions.ActorType.OAUTH_CLIENT;
public class PermissionV2IntegrationTests extends CredHubIntegrationTests {
private static final SimpleCredentialName CREDENTIAL_NAME =
new SimpleCredentialName("spring-credhub", "integration-test", "test-permissionsV2-credential");
private static final String CREDENTIAL_VALUE = "test-value";
private CredHubCredentialOperations credentials;
private CredHubPermissionV2Operations permissions;
@Before
public void setUp() {
credentials = operations.credentials();
permissions = operations.permissionsV2();
deleteCredentialIfExists(credentials, CREDENTIAL_NAME);
}
@After
public void tearDown() {
deleteCredentialIfExists(credentials, CREDENTIAL_NAME);
}
@Test
public void managePermissions() {
assumeTrue(serverApiIsV2());
credentials.write(ValueCredentialRequest.builder()
.name(CREDENTIAL_NAME)
.value(CREDENTIAL_VALUE)
.build());
Permission clientPermission = Permission.builder()
.client("client1")
.operations(Operation.READ, Operation.WRITE, Operation.DELETE)
.build();
CredentialPermission added = permissions.addPermissions(CREDENTIAL_NAME, clientPermission);
assertThat(added.getId()).isNotNull();
assertThat(added.getPath()).isEqualTo(CREDENTIAL_NAME.getName());
assertPermissions(added, OAUTH_CLIENT, "client1",
Operation.READ, Operation.WRITE, Operation.DELETE);
String permissionId = added.getId();
CredentialPermission retrieved = permissions.getPermissions(permissionId);
assertThat(retrieved.getId()).isEqualTo(permissionId);
assertThat(retrieved.getPath()).isEqualTo(CREDENTIAL_NAME.getName());
assertPermissions(retrieved, OAUTH_CLIENT, "client1",
Operation.READ, Operation.WRITE, Operation.DELETE);
permissions.deletePermission(permissionId);
}
@Test
public void updatePermissions() {
assumeTrue(serverApiIsV2());
credentials.write(ValueCredentialRequest.builder()
.name(CREDENTIAL_NAME)
.value(CREDENTIAL_VALUE)
.build());
Permission clientPermission = Permission.builder()
.client("client1")
.operations(Operation.READ, Operation.WRITE, Operation.DELETE)
.build();
CredentialPermission added = permissions.addPermissions(CREDENTIAL_NAME, clientPermission);
assertThat(added.getId()).isNotNull();
assertThat(added.getPath()).isEqualTo(CREDENTIAL_NAME.getName());
assertPermissions(added, OAUTH_CLIENT, "client1",
Operation.READ, Operation.WRITE, Operation.DELETE);
String permissionId = added.getId();
Permission newPermission = Permission.builder()
.client("client1")
.operations(Operation.READ_ACL, Operation.WRITE_ACL)
.build();
CredentialPermission updated = permissions.updatePermissions(permissionId, CREDENTIAL_NAME, newPermission);
assertThat(updated.getId()).isEqualTo(permissionId);
assertThat(updated.getPath()).isEqualTo(CREDENTIAL_NAME.getName());
assertPermissions(updated, OAUTH_CLIENT, "client1",
Operation.READ_ACL, Operation.WRITE_ACL);
CredentialPermission retrieved = permissions.getPermissions(permissionId);
assertThat(retrieved.getId()).isEqualTo(permissionId);
assertThat(retrieved.getPath()).isEqualTo(CREDENTIAL_NAME.getName());
assertPermissions(retrieved, OAUTH_CLIENT, "client1",
Operation.READ_ACL, Operation.WRITE_ACL);
permissions.deletePermission(permissionId);
}
private void assertPermissions(CredentialPermission credentialPermission,
ActorType actorType, String actorId, Operation... operations) {
Actor actor = credentialPermission.getPermission().getActor();
assertThat(actor.getAuthType()).isEqualTo(actorType);
assertThat(actor.getPrimaryIdentifier()).isEqualTo(actorId);
List<Operation> ops = credentialPermission.getPermission().getOperations();
assertThat(ops).containsExactlyInAnyOrder(operations);
}
}