Skip to content

Commit

Permalink
ARC-1807: Bump authzed client to 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrichner-oviva committed Jun 16, 2024
1 parent bf85e5e commit 8a5cae1
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 91 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<mockito.jupiter.version>${mockito.version}</mockito.jupiter.version>
<mockito.version>5.11.0</mockito.version>
<testcontainers.version>1.19.7</testcontainers.version>
<authzed.version>0.7.0</authzed.version>
<authzed.version>0.8.0</authzed.version>

<spotless.maven.plugin.version>2.43.0</spotless.maven.plugin.version>
<jacoco.version>0.8.12</jacoco.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.oviva.spicegen.spicedbbinding.internal;

import com.authzed.api.v1.Core;
import com.authzed.api.v1.ObjectReference;
import com.oviva.spicegen.api.ObjectRef;

public class ObjectReferenceMapper {

public Core.ObjectReference map(ObjectRef ref) {
return Core.ObjectReference.newBuilder()
.setObjectType(ref.kind())
.setObjectId(ref.id())
.build();
public ObjectReference map(ObjectRef ref) {
return ObjectReference.newBuilder().setObjectType(ref.kind()).setObjectId(ref.id()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@

public class PreconditionMapper {

public com.authzed.api.v1.PermissionService.Precondition map(Precondition precondition) {
public com.authzed.api.v1.Precondition map(Precondition precondition) {

var builder = com.authzed.api.v1.PermissionService.Precondition.newBuilder();
var builder = com.authzed.api.v1.Precondition.newBuilder();

builder.setOperation(mapOperation(precondition.condition()));
builder.setFilter(mapFilter(precondition.filter()));

return builder.build();
}

private com.authzed.api.v1.PermissionService.RelationshipFilter mapFilter(
RelationshipFilter filter) {
var builder = com.authzed.api.v1.PermissionService.RelationshipFilter.newBuilder();
private com.authzed.api.v1.RelationshipFilter mapFilter(RelationshipFilter filter) {
var builder = com.authzed.api.v1.RelationshipFilter.newBuilder();

builder.setResourceType(filter.resourceKind());
filter.resourceId().ifPresent(builder::setOptionalResourceId);
Expand All @@ -28,31 +27,25 @@ private com.authzed.api.v1.PermissionService.RelationshipFilter mapFilter(
return builder.build();
}

private com.authzed.api.v1.PermissionService.SubjectFilter mapSubjectFilter(
private com.authzed.api.v1.SubjectFilter mapSubjectFilter(
RelationshipFilter.SubjectFilter subjectFilter) {
var subjectFilterBuilder =
com.authzed.api.v1.PermissionService.SubjectFilter.newBuilder()
.setSubjectType(subjectFilter.subjectKind());
com.authzed.api.v1.SubjectFilter.newBuilder().setSubjectType(subjectFilter.subjectKind());

subjectFilter.subjectId().ifPresent(subjectFilterBuilder::setOptionalSubjectId);
subjectFilter
.relation()
.map(
r ->
com.authzed.api.v1.PermissionService.SubjectFilter.RelationFilter.newBuilder()
.setRelation(r)
.build())
com.authzed.api.v1.SubjectFilter.RelationFilter.newBuilder().setRelation(r).build())
.ifPresent(subjectFilterBuilder::setOptionalRelation);
return subjectFilterBuilder.build();
}

private com.authzed.api.v1.PermissionService.Precondition.Operation mapOperation(
Precondition.Condition condition) {
private com.authzed.api.v1.Precondition.Operation mapOperation(Precondition.Condition condition) {
return switch (condition) {
case MUST_MATCH ->
com.authzed.api.v1.PermissionService.Precondition.Operation.OPERATION_MUST_MATCH;
case MUST_NOT_MATCH ->
com.authzed.api.v1.PermissionService.Precondition.Operation.OPERATION_MUST_NOT_MATCH;
case MUST_MATCH -> com.authzed.api.v1.Precondition.Operation.OPERATION_MUST_MATCH;
case MUST_NOT_MATCH -> com.authzed.api.v1.Precondition.Operation.OPERATION_MUST_NOT_MATCH;
};
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.oviva.spicegen.spicedbbinding.internal;

import com.authzed.api.v1.Core;
import com.authzed.api.v1.PermissionsServiceGrpc;
import com.authzed.api.v1.*;
import com.oviva.spicegen.api.*;
import com.oviva.spicegen.api.Consistency;
import com.oviva.spicegen.api.PermissionService;
import io.grpc.StatusRuntimeException;

public class SpiceDbPermissionServiceImpl implements PermissionService {
Expand All @@ -29,7 +30,7 @@ public UpdateResult updateRelationships(UpdateRelationships updates) {
updates.preconditions().stream().map(preconditionMapper::map).toList();

var req =
com.authzed.api.v1.PermissionService.WriteRelationshipsRequest.newBuilder()
WriteRelationshipsRequest.newBuilder()
.addAllOptionalPreconditions(mappedPreconditions)
.addAllUpdates(mappedUpdates)
.build();
Expand All @@ -51,36 +52,32 @@ public boolean checkPermission(CheckPermission checkPermission) {
try {
var response = permissionsService.checkPermission(request);
return response.getPermissionship()
== com.authzed.api.v1.PermissionService.CheckPermissionResponse.Permissionship
.PERMISSIONSHIP_HAS_PERMISSION;
== CheckPermissionResponse.Permissionship.PERMISSIONSHIP_HAS_PERMISSION;
} catch (StatusRuntimeException e) {
throw exceptionMapper.map(e);
}
}

private com.authzed.api.v1.PermissionService.CheckPermissionRequest mapCheckPermission(
CheckPermission checkPermission) {
private CheckPermissionRequest mapCheckPermission(CheckPermission checkPermission) {

var consistency = mapConsistency(checkPermission.consistency());

return com.authzed.api.v1.PermissionService.CheckPermissionRequest.newBuilder()
return CheckPermissionRequest.newBuilder()
.setConsistency(consistency)
.setResource(objectReferenceMapper.map(checkPermission.resource()))
.setSubject(subjectReferenceMapper.map(checkPermission.subject()))
.setPermission(checkPermission.permission())
.build();
}

private com.authzed.api.v1.PermissionService.Consistency mapConsistency(Consistency consistency) {
private com.authzed.api.v1.Consistency mapConsistency(Consistency consistency) {
return switch (consistency.requirement()) {
case FULLY_CONSISTENT ->
com.authzed.api.v1.PermissionService.Consistency.newBuilder()
.setFullyConsistent(true)
.build();
com.authzed.api.v1.Consistency.newBuilder().setFullyConsistent(true).build();
case AT_LEAST_AS_FRESH ->
com.authzed.api.v1.PermissionService.Consistency.newBuilder()
com.authzed.api.v1.Consistency.newBuilder()
.setAtLeastAsFresh(
Core.ZedToken.newBuilder().setToken(consistency.consistencyToken()).build())
ZedToken.newBuilder().setToken(consistency.consistencyToken()).build())
.build();
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.oviva.spicegen.spicedbbinding.internal;

import com.authzed.api.v1.Core;
import com.authzed.api.v1.ObjectReference;
import com.authzed.api.v1.SubjectReference;
import com.oviva.spicegen.api.SubjectRef;

public class SubjectReferenceMapper {

public Core.SubjectReference map(SubjectRef subjectRef) {
public SubjectReference map(SubjectRef subjectRef) {
var ref =
Core.ObjectReference.newBuilder()
ObjectReference.newBuilder()
.setObjectType(subjectRef.kind())
.setObjectId(subjectRef.id())
.build();
return Core.SubjectReference.newBuilder().setObject(ref).build();
return SubjectReference.newBuilder().setObject(ref).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
package com.oviva.spicegen.spicedbbinding.internal;

import com.authzed.api.v1.Core;
import com.authzed.api.v1.Relationship;
import com.authzed.api.v1.RelationshipUpdate;
import com.oviva.spicegen.api.UpdateRelationship;

public class UpdateRelationshipMapper {

private final ObjectReferenceMapper objectReferenceMapper = new ObjectReferenceMapper();
private final SubjectReferenceMapper subjectReferenceMapper = new SubjectReferenceMapper();

public Core.RelationshipUpdate map(UpdateRelationship updateRelationship) {
public RelationshipUpdate map(UpdateRelationship updateRelationship) {

var subjectRef = subjectReferenceMapper.map(updateRelationship.subject());
var resourceRef = objectReferenceMapper.map(updateRelationship.resource());

return Core.RelationshipUpdate.newBuilder()
return RelationshipUpdate.newBuilder()
.setOperation(mapOperation(updateRelationship.operation()))
.setRelationship(
Core.Relationship.newBuilder()
Relationship.newBuilder()
.setRelation(updateRelationship.relation())
.setSubject(subjectRef)
.setResource(resourceRef))
.build();
}

private Core.RelationshipUpdate.Operation mapOperation(UpdateRelationship.Operation operation) {
private RelationshipUpdate.Operation mapOperation(UpdateRelationship.Operation operation) {
return switch (operation) {
case UPDATE -> Core.RelationshipUpdate.Operation.OPERATION_TOUCH;
case DELETE -> Core.RelationshipUpdate.Operation.OPERATION_DELETE;
case UPDATE -> RelationshipUpdate.Operation.OPERATION_TOUCH;
case DELETE -> RelationshipUpdate.Operation.OPERATION_DELETE;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.authzed.api.v1.Core;
import com.authzed.api.v1.PermissionService;
import com.authzed.api.v1.PermissionsServiceGrpc;
import com.authzed.api.v1.SchemaServiceGrpc;
import com.authzed.api.v1.*;
import com.oviva.spicegen.api.ObjectRef;
import com.oviva.spicegen.api.SubjectRef;
import com.oviva.spicegen.spicedbbinding.test.Fixtures;
Expand Down Expand Up @@ -158,25 +155,25 @@ private boolean checkPermission(ObjectRef resource, String permission, SubjectRe
var req = SpiceDbUtils.checkPermissionRequest(resource, permission, subject);
var res = permissionsService.checkPermission(req);
return res.getPermissionship()
== PermissionService.CheckPermissionResponse.Permissionship.PERMISSIONSHIP_HAS_PERMISSION;
== CheckPermissionResponse.Permissionship.PERMISSIONSHIP_HAS_PERMISSION;
}

private String updateRelationship(ObjectRef resource, String relation, ObjectRef subject) {
return writeRelationship(
resource, relation, subject, Core.RelationshipUpdate.Operation.OPERATION_TOUCH);
resource, relation, subject, RelationshipUpdate.Operation.OPERATION_TOUCH);
}

private String deleteRelationship(ObjectRef resource, String relation, ObjectRef subject) {

return writeRelationship(
resource, relation, subject, Core.RelationshipUpdate.Operation.OPERATION_DELETE);
resource, relation, subject, RelationshipUpdate.Operation.OPERATION_DELETE);
}

private String writeRelationship(
ObjectRef resource,
String relation,
ObjectRef subject,
Core.RelationshipUpdate.Operation operation) {
RelationshipUpdate.Operation operation) {

var req = SpiceDbUtils.writeRelationshipRequest(resource, relation, subject, operation);
var res = permissionsService.writeRelationships(req);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.oviva.spicegen.spicedbbinding.internal;

import com.authzed.api.v1.Core;
import com.authzed.api.v1.PermissionService;
import com.authzed.api.v1.SchemaServiceOuterClass;
import com.authzed.api.v1.*;
import com.oviva.spicegen.api.ObjectRef;
import com.oviva.spicegen.api.SubjectRef;
import java.util.UUID;
Expand All @@ -18,84 +16,79 @@ public static String newId() {
return UUID.randomUUID().toString().replaceAll("-", "");
}

public static Core.ObjectReference toRef(ObjectRef ref) {
public static ObjectReference toRef(ObjectRef ref) {

return Core.ObjectReference.newBuilder()
.setObjectType(ref.kind())
.setObjectId(ref.id())
.build();
return ObjectReference.newBuilder().setObjectType(ref.kind()).setObjectId(ref.id()).build();
}

public static Core.SubjectReference toRef(SubjectRef ref) {
public static SubjectReference toRef(SubjectRef ref) {

return Core.SubjectReference.newBuilder()
.setObject(
Core.ObjectReference.newBuilder().setObjectId(ref.id()).setObjectType(ref.kind()))
return SubjectReference.newBuilder()
.setObject(ObjectReference.newBuilder().setObjectId(ref.id()).setObjectType(ref.kind()))
.build();
}

public static PermissionService.WriteRelationshipsRequest updateRelationshipRequest(
public static WriteRelationshipsRequest updateRelationshipRequest(
ObjectRef resource, String relation, ObjectRef subject) {

logger.info("update: " + resource.toString() + "#" + relation + "@" + subject);
return writeRelationshipRequest(
resource, relation, subject, Core.RelationshipUpdate.Operation.OPERATION_TOUCH);
resource, relation, subject, RelationshipUpdate.Operation.OPERATION_TOUCH);
}

public static PermissionService.WriteRelationshipsRequest deleteRelationshipRequest(
public static WriteRelationshipsRequest deleteRelationshipRequest(
ObjectRef resource, String relation, ObjectRef subject) {

logger.info("delete: " + resource.toString() + "#" + relation + "@" + subject);
return writeRelationshipRequest(
resource, relation, subject, Core.RelationshipUpdate.Operation.OPERATION_DELETE);
resource, relation, subject, RelationshipUpdate.Operation.OPERATION_DELETE);
}

public static PermissionService.WriteRelationshipsRequest writeRelationshipRequest(
public static WriteRelationshipsRequest writeRelationshipRequest(
ObjectRef resource,
String relation,
ObjectRef subject,
Core.RelationshipUpdate.Operation operation) {
RelationshipUpdate.Operation operation) {

logger.info("write: " + resource.toString() + "#" + relation + "@" + subject);

var subjectRef = toRef(subject);

var resourceRef = toRef(resource);

return PermissionService.WriteRelationshipsRequest.newBuilder()
return WriteRelationshipsRequest.newBuilder()
.addUpdates(
Core.RelationshipUpdate.newBuilder()
RelationshipUpdate.newBuilder()
.setOperation(operation)
.setRelationship(
Core.Relationship.newBuilder()
Relationship.newBuilder()
.setRelation(relation)
.setSubject(
Core.SubjectReference.newBuilder().setObject(subjectRef).build())
.setSubject(SubjectReference.newBuilder().setObject(subjectRef).build())
.setResource(resourceRef))
.build())
.build();
}

public static SchemaServiceOuterClass.WriteSchemaRequest writeSchemaRequest(String schema) {
return SchemaServiceOuterClass.WriteSchemaRequest.newBuilder().setSchema(schema).build();
public static WriteSchemaRequest writeSchemaRequest(String schema) {
return WriteSchemaRequest.newBuilder().setSchema(schema).build();
}

public static PermissionService.CheckPermissionRequest checkPermissionRequest(
public static CheckPermissionRequest checkPermissionRequest(
ObjectRef resource, String permission, SubjectRef subject) {

return PermissionService.CheckPermissionRequest.newBuilder()
.setConsistency(PermissionService.Consistency.newBuilder().setFullyConsistent(true).build())
return CheckPermissionRequest.newBuilder()
.setConsistency(Consistency.newBuilder().setFullyConsistent(true).build())
.setResource(toRef(resource))
.setPermission(permission)
.setSubject(toRef(subject))
.build();
}

public static PermissionService.LookupResourcesRequest lookupResourcesRequest(
public static LookupResourcesRequest lookupResourcesRequest(
String resourceType, String permission, SubjectRef subject) {

return PermissionService.LookupResourcesRequest.newBuilder()
.setConsistency(PermissionService.Consistency.newBuilder().setFullyConsistent(true).build())
return LookupResourcesRequest.newBuilder()
.setConsistency(Consistency.newBuilder().setFullyConsistent(true).build())
.setResourceObjectType(resourceType)
.setPermission(permission)
.setSubject(toRef(subject))
Expand Down
Loading

0 comments on commit 8a5cae1

Please sign in to comment.