Skip to content

Commit e4d11ff

Browse files
authored
Support output errors (#249)
Signed-off-by: Charith Ellawala <charith@cerbos.dev> Signed-off-by: Charith Ellawala <charith@cerbos.dev>
1 parent 76f2759 commit e4d11ff

2 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎src/main/java/dev/cerbos/sdk/CheckResult.java‎

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ public boolean isAllowed(String action) {
4747
return false;
4848
}
4949

50-
return this.entry.getActionsMap().getOrDefault(action, EffectOuterClass.Effect.EFFECT_DENY)
51-
== EffectOuterClass.Effect.EFFECT_ALLOW;
50+
return this.entry.getActionsMap().getOrDefault(action,
51+
EffectOuterClass.Effect.EFFECT_DENY) == EffectOuterClass.Effect.EFFECT_ALLOW;
5252
}
5353

5454
/**
5555
* Return all actions and effects in this instance.
5656
*
57-
* @return Map of action to boolean indicating whether the action is allowed or not
57+
* @return Map of action to boolean indicating whether the action is allowed or
58+
* not
5859
*/
5960
public Map<String, Boolean> getAll() {
6061
if (this.entry == null) {
@@ -83,7 +84,8 @@ public boolean hasValidationErrors() {
8384
/**
8485
* Returns the list of validation errors if there are any.
8586
*
86-
* @return List of {@link dev.cerbos.api.v1.schema.SchemaOuterClass.ValidationError}
87+
* @return List of
88+
* {@link dev.cerbos.api.v1.schema.SchemaOuterClass.ValidationError}
8789
*/
8890
public List<SchemaOuterClass.ValidationError> getValidationErrors() {
8991
if (this.entry == null) {
@@ -170,11 +172,54 @@ public int size() {
170172
* @return Map of output rule names and {@link Value}
171173
*/
172174
public Map<String, Value> asMap() {
173-
return this.outputs.stream().collect(Collectors.toUnmodifiableMap(Engine.OutputEntry::getSrc, Engine.OutputEntry::getVal));
175+
return this.outputs.stream()
176+
.collect(Collectors.toUnmodifiableMap(Engine.OutputEntry::getSrc, Engine.OutputEntry::getVal));
177+
}
178+
179+
/**
180+
* Returns the output entries as a map keyed by rule name.
181+
*
182+
* @return Map of output rule names and {@link Entry}
183+
*/
184+
public Map<String, Entry> entriesAsMap() {
185+
return this.outputs.stream()
186+
.collect(Collectors.toUnmodifiableMap(Engine.OutputEntry::getSrc, (entry) -> {
187+
return new Entry(entry.getSrc(), entry.getVal(), entry.getAction(), entry.getError());
188+
}));
174189
}
175190

176191
public List<Engine.OutputEntry> getRaw() {
177192
return this.outputs;
178193
}
194+
195+
public static final class Entry {
196+
private final String source;
197+
private final Value value;
198+
private final String action;
199+
private final Optional<String> error;
200+
201+
Entry(String source, Value value, String action, String error) {
202+
this.source = source;
203+
this.value = value;
204+
this.action = action;
205+
this.error = Optional.ofNullable(error);
206+
}
207+
208+
public String getSource() {
209+
return source;
210+
}
211+
212+
public Value getValue() {
213+
return value;
214+
}
215+
216+
public String getAction() {
217+
return action;
218+
}
219+
220+
public Optional<String> getError() {
221+
return error;
222+
}
223+
}
179224
}
180225
}

‎src/test/java/dev/cerbos/sdk/CerbosClientTests.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.google.protobuf.util.Values;
1212
import dev.cerbos.api.v1.engine.Engine;
1313
import dev.cerbos.api.v1.response.Response;
14+
import dev.cerbos.sdk.CheckResult.Outputs.Entry;
1415
import dev.cerbos.sdk.builders.AuxData;
1516
import dev.cerbos.sdk.builders.Principal;
1617
import dev.cerbos.sdk.builders.Resource;
@@ -196,9 +197,13 @@ public void checkResources() {
196197
Response.CheckResourcesResponse.ResultEntry.Meta.EffectMeta res1DeferMeta = res1DeferMetaOpt.get();
197198
Assertions.assertEquals("resource.leave_request.v20210210", res1DeferMeta.getMatchedPolicy());
198199

199-
Map<String, Value> res1Outputs = res1.getOutputs().asMap();
200+
Map<String, Entry> res1Outputs = res1.getOutputs().entriesAsMap();
200201
Assertions.assertEquals(1, res1Outputs.size());
201-
Value res1ViewOutput = res1Outputs.get("resource.leave_request.v20210210#public-view");
202+
Entry res1ViewOutputEntry = res1Outputs.get("resource.leave_request.v20210210#public-view");
203+
Assertions.assertNotNull(res1ViewOutputEntry);
204+
Assertions.assertEquals("view:public", res1ViewOutputEntry.getAction());
205+
206+
Value res1ViewOutput = res1ViewOutputEntry.getValue();
202207
Assertions.assertNotNull(res1ViewOutput);
203208
Assertions.assertEquals(res1ViewOutput, Values.of(Struct.newBuilder()
204209
.putFields("pID", Values.of("john"))

0 commit comments

Comments
 (0)