Skip to content

Commit

Permalink
enhancement: Add getters for request and call IDs (#121)
Browse files Browse the repository at this point in the history
Check and plan results now include `getCerbosCallId` and `getRequestId`
methods.

Signed-off-by: Charith Ellawala <charith@cerbos.dev>
charithe authored Dec 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 27a37b3 commit 138b7d8
Showing 4 changed files with 105 additions and 77 deletions.
4 changes: 2 additions & 2 deletions src/main/java/dev/cerbos/sdk/CerbosBlockingClient.java
Original file line number Diff line number Diff line change
@@ -116,9 +116,9 @@ public CheckResult check(Principal principal, Resource resource, String... actio
try {
Response.CheckResourcesResponse response = withClient().checkResources(request);
if (response.getResultsCount() == 1) {
return new CheckResult(response.getResults(0));
return new CheckResult(response.getRequestId(), response.getCerbosCallId(), response.getResults(0));
}
return new CheckResult(null);
return new CheckResult(response.getRequestId(), response.getCerbosCallId(), null);
} catch (StatusRuntimeException sre) {
throw new CerbosException(sre.getStatus(), sre.getCause());
}
88 changes: 48 additions & 40 deletions src/main/java/dev/cerbos/sdk/CheckResourcesResult.java
Original file line number Diff line number Diff line change
@@ -12,44 +12,52 @@
import java.util.stream.Stream;

public class CheckResourcesResult {
private final Response.CheckResourcesResponse resp;

CheckResourcesResult(Response.CheckResourcesResponse resp) {
this.resp = resp;
}

public Stream<CheckResult> results() {
return this.resp.getResultsList().stream().map(CheckResult::new);
}

public Optional<CheckResult> find(String resourceID) {
return find(resourceID, null);
}

public Optional<CheckResult> find(
String resourceID,
Predicate<Response.CheckResourcesResponse.ResultEntry.Resource> predicate) {
return this.resp.getResultsList().stream()
.filter(
re -> {
if (!re.getResource().getId().equals(resourceID)) {
return false;
}
if (predicate != null) {
return predicate.test(re.getResource());
}

return true;
})
.map(CheckResult::new)
.findFirst();
}

public boolean hasValidationErrors() {
return resp.getResultsList().stream().anyMatch(re -> re.getValidationErrorsCount() > 0);
}

public Response.CheckResourcesResponse getRaw() {
return resp;
}
private final Response.CheckResourcesResponse resp;

CheckResourcesResult(Response.CheckResourcesResponse resp) {
this.resp = resp;
}

public Stream<CheckResult> results() {
return this.resp.getResultsList().stream().map( entry -> new CheckResult(resp.getRequestId(), resp.getCerbosCallId(), entry));
}

public Optional<CheckResult> find(String resourceID) {
return find(resourceID, null);
}

public Optional<CheckResult> find(
String resourceID,
Predicate<Response.CheckResourcesResponse.ResultEntry.Resource> predicate) {
return this.resp.getResultsList().stream()
.filter(
re -> {
if (!re.getResource().getId().equals(resourceID)) {
return false;
}
if (predicate != null) {
return predicate.test(re.getResource());
}

return true;
})
.map(entry -> new CheckResult(resp.getRequestId(), resp.getCerbosCallId(), entry))
.findFirst();
}

public boolean hasValidationErrors() {
return resp.getResultsList().stream().anyMatch(re -> re.getValidationErrorsCount() > 0);
}

public Response.CheckResourcesResponse getRaw() {
return resp;
}

public String getRequestId() {
return this.resp.getRequestId();
}

public String getCerbosCallId() {
return this.resp.getCerbosCallId();
}
}
14 changes: 13 additions & 1 deletion src/main/java/dev/cerbos/sdk/CheckResult.java
Original file line number Diff line number Diff line change
@@ -18,12 +18,24 @@
import java.util.stream.Collectors;

public final class CheckResult {
private final String cerbosCallId;
private final String requestId;
private final Response.CheckResourcesResponse.ResultEntry entry;

CheckResult(Response.CheckResourcesResponse.ResultEntry entry) {
CheckResult(String requestId, String cerbosCallId, Response.CheckResourcesResponse.ResultEntry entry) {
this.requestId = requestId;
this.cerbosCallId = cerbosCallId;
this.entry = entry;
}

public String getRequestId() {
return this.requestId;
}

public String getCerbosCallId() {
return this.cerbosCallId;
}

/**
* Returns whether the given action is allowed.
*
76 changes: 42 additions & 34 deletions src/main/java/dev/cerbos/sdk/PlanResourcesResult.java
Original file line number Diff line number Diff line change
@@ -13,49 +13,57 @@
import java.util.Optional;

public class PlanResourcesResult {
private final Response.PlanResourcesResponse resp;
private final Response.PlanResourcesResponse resp;

PlanResourcesResult(Response.PlanResourcesResponse resp) {
this.resp = resp;
}
PlanResourcesResult(Response.PlanResourcesResponse resp) {
this.resp = resp;
}

public String getAction() {
return this.resp.getAction();
}
public String getAction() {
return this.resp.getAction();
}

public String getResourceKind() {
return this.resp.getResourceKind();
}
public String getResourceKind() {
return this.resp.getResourceKind();
}

public String getPolicyVersion() {
return this.resp.getPolicyVersion();
}
public String getPolicyVersion() {
return this.resp.getPolicyVersion();
}

public boolean isAlwaysAllowed() {
return this.resp.getFilter().getKind() == Engine.PlanResourcesFilter.Kind.KIND_ALWAYS_ALLOWED;
}
public boolean isAlwaysAllowed() {
return this.resp.getFilter().getKind() == Engine.PlanResourcesFilter.Kind.KIND_ALWAYS_ALLOWED;
}

public boolean isAlwaysDenied() {
return this.resp.getFilter().getKind() == Engine.PlanResourcesFilter.Kind.KIND_ALWAYS_DENIED;
}
public boolean isAlwaysDenied() {
return this.resp.getFilter().getKind() == Engine.PlanResourcesFilter.Kind.KIND_ALWAYS_DENIED;
}

public boolean isConditional() {
return this.resp.getFilter().getKind() == Engine.PlanResourcesFilter.Kind.KIND_CONDITIONAL;
}
public boolean isConditional() {
return this.resp.getFilter().getKind() == Engine.PlanResourcesFilter.Kind.KIND_CONDITIONAL;
}

public Optional<Engine.PlanResourcesFilter.Expression.Operand> getCondition() {
return Optional.of(this.resp.getFilter().getCondition());
}
public Optional<Engine.PlanResourcesFilter.Expression.Operand> getCondition() {
return Optional.of(this.resp.getFilter().getCondition());
}

public boolean hasValidationErrors() {
return this.resp.getValidationErrorsCount() > 0;
}
public boolean hasValidationErrors() {
return this.resp.getValidationErrorsCount() > 0;
}

public List<SchemaOuterClass.ValidationError> getValidationErrors() {
return this.resp.getValidationErrorsList();
}
public List<SchemaOuterClass.ValidationError> getValidationErrors() {
return this.resp.getValidationErrorsList();
}

public Response.PlanResourcesResponse getRaw() {
return this.resp;
}
public Response.PlanResourcesResponse getRaw() {
return this.resp;
}

public String getRequestId() {
return this.resp.getRequestId();
}

public String getCerbosCallId() {
return this.resp.getCerbosCallId();
}
}

0 comments on commit 138b7d8

Please sign in to comment.