From 49fe1223b3f5873d31a929b5ed3028de5f575cf8 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Wed, 31 Oct 2018 14:12:57 -0500 Subject: [PATCH] Add permission V2 operations. Fixes #44 --- .../credhub/core/CredHubOperations.java | 8 + .../credhub/core/CredHubTemplate.java | 12 ++ .../CredHubPermissionOperations.java | 6 +- .../permission/CredHubPermissionTemplate.java | 10 +- .../CredHubPermissionV2Operations.java | 61 ++++++++ .../CredHubPermissionV2Template.java | 119 +++++++++++++++ .../core/permissionV2/package-info.java | 20 +++ .../credhub/support/CredHubRequest.java | 28 ++-- .../credhub/support/CredentialPermission.java | 118 +++++++++++++++ .../support/CredentialPermissions.java | 20 +-- ...dentialPermission.java => Permission.java} | 28 ++-- ...> CredHubPermissionTemplateUnitTests.java} | 14 +- .../CredHubPermissionV2TemplateUnitTests.java | 133 +++++++++++++++++ .../support/CredHubRequestUnitTestsBase.java | 10 +- .../CredentialPermissionUnitTests.java | 79 ++++++++++ .../CredentialPermissionsUnitTests.java | 23 ++- .../PermissionIntegrationTests.java | 12 +- .../PermissionV2IntegrationTests.java | 141 ++++++++++++++++++ 18 files changed, 765 insertions(+), 77 deletions(-) create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2Operations.java create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2Template.java create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/package-info.java create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPermission.java rename spring-credhub-core/src/main/java/org/springframework/credhub/support/permissions/{CredentialPermission.java => Permission.java} (92%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/permission/{CredHubPermissionsTemplateUnitTests.java => CredHubPermissionTemplateUnitTests.java} (90%) create mode 100644 spring-credhub-core/src/test/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2TemplateUnitTests.java create mode 100644 spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionUnitTests.java create mode 100644 spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionV2IntegrationTests.java diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java index 32684b1..d5e0de2 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java @@ -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. * diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java index dbd0dc8..e0d28d2 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java @@ -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. * diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionOperations.java index 21e4f83..c80e6ab 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionOperations.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionOperations.java @@ -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 getPermissions(final CredentialName name); + List 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. diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionTemplate.java index 435b9a9..a8ea983 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionTemplate.java @@ -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 getPermissions(final CredentialName name) { + public List getPermissions(final CredentialName name) { Assert.notNull(name, "credential name must not be null"); - return credHubOperations.doWithRest(new RestOperationsCallback>() { + return credHubOperations.doWithRest(new RestOperationsCallback>() { @Override - public List doWithRestOperations(RestOperations restOperations) { + public List doWithRestOperations(RestOperations restOperations) { ResponseEntity 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); diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2Operations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2Operations.java new file mode 100644 index 0000000..a9ddc65 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2Operations.java @@ -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); +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2Template.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2Template.java new file mode 100644 index 0000000..d62f382 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2Template.java @@ -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() { + @Override + public CredentialPermission doWithRestOperations(RestOperations restOperations) { + ResponseEntity 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() { + @Override + public CredentialPermission doWithRestOperations(RestOperations restOperations) { + ResponseEntity 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() { + @Override + public CredentialPermission doWithRestOperations(RestOperations restOperations) { + ResponseEntity 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() { + @Override + public Void doWithRestOperations(RestOperations restOperations) { + restOperations.delete(PERMISSIONS_ID_URL_PATH, id); + return null; + } + }); + } +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/package-info.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/package-info.java new file mode 100644 index 0000000..1ce6f51 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permissionV2/package-info.java @@ -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; \ No newline at end of file diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredHubRequest.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredHubRequest.java index 20ea1d9..41bdfc5 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredHubRequest.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredHubRequest.java @@ -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 { protected WriteMode mode; protected CredentialName name; protected CredentialType credentialType; - protected List additionalPermissions; + protected List additionalPermissions; protected T details; public CredHubRequest() { @@ -105,11 +105,11 @@ public class CredHubRequest { } /** - * 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 getAdditionalPermissions() { + public List getAdditionalPermissions() { return this.additionalPermissions; } @@ -219,45 +219,45 @@ public class CredHubRequest { } /** - * 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 permissions) { + public B permissions(Collection 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; } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPermission.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPermission.java new file mode 100644 index 0000000..cbfcdae --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPermission.java @@ -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); + } +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPermissions.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPermissions.java index 3337cc3..9272666 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPermissions.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialPermissions.java @@ -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 permissions; + private final List 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 getPermissions() { + public List getPermissions() { return this.permissions; } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/permissions/CredentialPermission.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/permissions/Permission.java similarity index 92% rename from spring-credhub-core/src/main/java/org/springframework/credhub/support/permissions/CredentialPermission.java rename to spring-credhub-core/src/main/java/org/springframework/credhub/support/permissions/Permission.java index a5aa935..3316eb6 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/permissions/CredentialPermission.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/permissions/Permission.java @@ -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 operations) { + private Permission(Actor actor, List operations) { this.actor = actor; this.operations = operations; } @@ -97,7 +97,7 @@ public class CredentialPermission { return null; } - List operationValues = new ArrayList(operations.size()); + List 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(); + 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 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(this.operations)); + operations = java.util.Collections.unmodifiableList(new ArrayList<>(this.operations)); } - return new CredentialPermission(actor, operations); + return new Permission(actor, operations); } } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/permission/CredHubPermissionsTemplateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/permission/CredHubPermissionTemplateUnitTests.java similarity index 90% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/permission/CredHubPermissionsTemplateUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/permission/CredHubPermissionTemplateUnitTests.java index a8ec745..7f94f00 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/permission/CredHubPermissionsTemplateUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/permission/CredHubPermissionTemplateUnitTests.java @@ -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 response = credHubTemplate.getPermissions(NAME); + List 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) diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2TemplateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2TemplateUnitTests.java new file mode 100644 index 0000000..916ca28 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/permissionV2/CredHubPermissionV2TemplateUnitTests.java @@ -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"); + } +} \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredHubRequestUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredHubRequestUnitTestsBase.java index 1179638..ab03763 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredHubRequestUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredHubRequestUnitTestsBase.java @@ -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()); diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionUnitTests.java new file mode 100644 index 0000000..f335ecd --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionUnitTests.java @@ -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 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); + } +} diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionsUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionsUnitTests.java index 244a65e..19b6856 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionsUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionsUnitTests.java @@ -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 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); diff --git a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java index 983581a..6bb040c 100644 --- a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java +++ b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java @@ -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 retrievedPermissions = permissions.getPermissions(CREDENTIAL_NAME); + List 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 afterDelete = permissions.getPermissions(CREDENTIAL_NAME); + List afterDelete = permissions.getPermissions(CREDENTIAL_NAME); assertThat(afterDelete.size()).isBetween(0, 1); } } diff --git a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionV2IntegrationTests.java b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionV2IntegrationTests.java new file mode 100644 index 0000000..7df0345 --- /dev/null +++ b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionV2IntegrationTests.java @@ -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 ops = credentialPermission.getPermission().getOperations(); + assertThat(ops).containsExactlyInAnyOrder(operations); + } +}