diff --git a/src/main/java/dev/cerbos/sdk/CerbosBlockingClient.java b/src/main/java/dev/cerbos/sdk/CerbosBlockingClient.java index ee48d2e..0c46cca 100644 --- a/src/main/java/dev/cerbos/sdk/CerbosBlockingClient.java +++ b/src/main/java/dev/cerbos/sdk/CerbosBlockingClient.java @@ -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()); } diff --git a/src/main/java/dev/cerbos/sdk/CheckResourcesResult.java b/src/main/java/dev/cerbos/sdk/CheckResourcesResult.java index 4e8420a..1c46828 100644 --- a/src/main/java/dev/cerbos/sdk/CheckResourcesResult.java +++ b/src/main/java/dev/cerbos/sdk/CheckResourcesResult.java @@ -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 results() { - return this.resp.getResultsList().stream().map(CheckResult::new); - } - - public Optional find(String resourceID) { - return find(resourceID, null); - } - - public Optional find( - String resourceID, - Predicate 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 results() { + return this.resp.getResultsList().stream().map( entry -> new CheckResult(resp.getRequestId(), resp.getCerbosCallId(), entry)); + } + + public Optional find(String resourceID) { + return find(resourceID, null); + } + + public Optional find( + String resourceID, + Predicate 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(); + } } diff --git a/src/main/java/dev/cerbos/sdk/CheckResult.java b/src/main/java/dev/cerbos/sdk/CheckResult.java index efe1339..0d943f6 100644 --- a/src/main/java/dev/cerbos/sdk/CheckResult.java +++ b/src/main/java/dev/cerbos/sdk/CheckResult.java @@ -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. * diff --git a/src/main/java/dev/cerbos/sdk/PlanResourcesResult.java b/src/main/java/dev/cerbos/sdk/PlanResourcesResult.java index 115da17..8908f78 100644 --- a/src/main/java/dev/cerbos/sdk/PlanResourcesResult.java +++ b/src/main/java/dev/cerbos/sdk/PlanResourcesResult.java @@ -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 getCondition() { - return Optional.of(this.resp.getFilter().getCondition()); - } + public Optional 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 getValidationErrors() { - return this.resp.getValidationErrorsList(); - } + public List 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(); + } }