Skip to content

Commit

Permalink
Add support for user-defined policy rule outputs
Browse files Browse the repository at this point in the history
cerbos/cerbos#1594

Signed-off-by: Andrew Haines <haines@cerbos.dev>
  • Loading branch information
haines committed Jun 1, 2023
1 parent f761ae9 commit 91f64e7
Show file tree
Hide file tree
Showing 54 changed files with 718 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docs/core.checkresourcesresult._constructor_.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Constructs a new instance of the `CheckResourcesResult` class
**Signature:**

```typescript
constructor({ resource, actions, validationErrors, metadata, }: Pick<CheckResourcesResult, "resource" | "actions" | "validationErrors" | "metadata">);
constructor({ resource, actions, validationErrors, metadata, outputs, }: Pick<CheckResourcesResult, "resource" | "actions" | "validationErrors" | "metadata" | "outputs">);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| { resource, actions, validationErrors, metadata, } | Pick&lt;[CheckResourcesResult](./core.checkresourcesresult.md)<!-- -->, "resource" \| "actions" \| "validationErrors" \| "metadata"&gt; | |
| { resource, actions, validationErrors, metadata, outputs, } | Pick&lt;[CheckResourcesResult](./core.checkresourcesresult.md)<!-- -->, "resource" \| "actions" \| "validationErrors" \| "metadata" \| "outputs"&gt; | |

4 changes: 3 additions & 1 deletion docs/core.checkresourcesresult.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export declare class CheckResourcesResult

| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)({ resource, actions, validationErrors, metadata, })](./core.checkresourcesresult._constructor_.md) | | Constructs a new instance of the <code>CheckResourcesResult</code> class |
| [(constructor)({ resource, actions, validationErrors, metadata, outputs, })](./core.checkresourcesresult._constructor_.md) | | Constructs a new instance of the <code>CheckResourcesResult</code> class |

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [actions](./core.checkresourcesresult.actions.md) | | Record&lt;string, [Effect](./core.effect.md) \| undefined&gt; | The policy decisions for each action. |
| [metadata](./core.checkresourcesresult.metadata.md) | | [CheckResourcesResultMetadata](./core.checkresourcesresultmetadata.md) \| undefined | Additional information about how the policy decisions were reached. |
| [outputs](./core.checkresourcesresult.outputs.md) | | [OutputResult](./core.outputresult.md)<!-- -->\[\] | User-defined outputs from policy rule evaluations. |
| [resource](./core.checkresourcesresult.resource.md) | | [CheckResourcesResultResource](./core.checkresourcesresultresource.md) | The resource that was checked. |
| [validationErrors](./core.checkresourcesresult.validationerrors.md) | | [ValidationError](./core.validationerror.md)<!-- -->\[\] | Any schema validation errors for the principal or resource attributes. |

Expand All @@ -34,4 +35,5 @@ export declare class CheckResourcesResult
| [allAllowed()](./core.checkresourcesresult.allallowed.md) | | Check if the policy decision was that all input actions should be allowed for the resource. |
| [allowedActions()](./core.checkresourcesresult.allowedactions.md) | | List the actions that should be allowed for the resource. |
| [isAllowed(action)](./core.checkresourcesresult.isallowed.md) | | Check if the policy decision was that a given action should be allowed for the resource. |
| [output(source)](./core.checkresourcesresult.output.md) | | Find the value of the user-defined output for a particular policy rule. |

26 changes: 26 additions & 0 deletions docs/core.checkresourcesresult.output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/core](./core.md) &gt; [CheckResourcesResult](./core.checkresourcesresult.md) &gt; [output](./core.checkresourcesresult.output.md)

## CheckResourcesResult.output() method

Find the value of the user-defined output for a particular policy rule.

**Signature:**

```typescript
output(source: string): Value | undefined;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| source | string | the identifier of the policy rule that produced the output. |

**Returns:**

[Value](./core.value.md) \| undefined

`undefined` if the result does not include an output for the source.

18 changes: 18 additions & 0 deletions docs/core.checkresourcesresult.outputs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/core](./core.md) &gt; [CheckResourcesResult](./core.checkresourcesresult.md) &gt; [outputs](./core.checkresourcesresult.outputs.md)

## CheckResourcesResult.outputs property

User-defined outputs from policy rule evaluations.

**Signature:**

```typescript
outputs: OutputResult[];
```

## Remarks

Requires the Cerbos policy decision point server to be at least v0.27.

2 changes: 2 additions & 0 deletions docs/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Common types used by the [gRPC](./grpc.md) and [HTTP](./http.md) client librarie
| [MatchExpr](./core.matchexpr.md) | A single expression to evaluate in a condition. |
| [MatchNone](./core.matchnone.md) | A set of expressions to evaluate in a condition that must all be false. |
| [Options](./core.options.md) | Options for creating a new [Client](./core.client.md)<!-- -->. |
| [Output](./core.output.md) | User-defined output to be produced when evaluating a policy rule. |
| [OutputResult](./core.outputresult.md) | User-defined output from a policy rule evaluation. |
| [PlanResourcesConditionalResponse](./core.planresourcesconditionalresponse.md) | A query plan for when the specified action is conditionally allowed for the principal on resources matching the input. |
| [PlanResourcesMetadata](./core.planresourcesmetadata.md) | Additional information about the query plan. |
| [PlanResourcesRequest](./core.planresourcesrequest.md) | Input to [Client.planResources()](./core.client.planresources.md)<!-- -->. |
Expand Down
13 changes: 13 additions & 0 deletions docs/core.output.expr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/core](./core.md) &gt; [Output](./core.output.md) &gt; [expr](./core.output.expr.md)

## Output.expr property

A [Common Expression Language](https://docs.cerbos.dev/cerbos/latest/policies/conditions.html) expression to evaluate.

**Signature:**

```typescript
expr: string;
```
20 changes: 20 additions & 0 deletions docs/core.output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/core](./core.md) &gt; [Output](./core.output.md)

## Output interface

User-defined output to be produced when evaluating a policy rule.

**Signature:**

```typescript
export interface Output
```

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [expr](./core.output.expr.md) | | string | A [Common Expression Language](https://docs.cerbos.dev/cerbos/latest/policies/conditions.html) expression to evaluate. |

21 changes: 21 additions & 0 deletions docs/core.outputresult.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/core](./core.md) &gt; [OutputResult](./core.outputresult.md)

## OutputResult interface

User-defined output from a policy rule evaluation.

**Signature:**

```typescript
export interface OutputResult
```

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [source](./core.outputresult.source.md) | | string | The identifier of the policy rule that produced the output. |
| [value](./core.outputresult.value.md) | | [Value](./core.value.md) \| undefined | The result of evaluating the output expression. |

13 changes: 13 additions & 0 deletions docs/core.outputresult.source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/core](./core.md) &gt; [OutputResult](./core.outputresult.md) &gt; [source](./core.outputresult.source.md)

## OutputResult.source property

The identifier of the policy rule that produced the output.

**Signature:**

```typescript
source: string;
```
13 changes: 13 additions & 0 deletions docs/core.outputresult.value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/core](./core.md) &gt; [OutputResult](./core.outputresult.md) &gt; [value](./core.outputresult.value.md)

## OutputResult.value property

The result of evaluating the output expression.

**Signature:**

```typescript
value: Value | undefined;
```
1 change: 1 addition & 0 deletions docs/core.principalruleaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export interface PrincipalRuleAction
| [condition?](./core.principalruleaction.condition.md) | | [Condition](./core.condition.md) \| undefined | _(Optional)_ The condition that must be met for the override to apply. |
| [effect](./core.principalruleaction.effect.md) | | [Effect](./core.effect.md) | The effect of the override. |
| [name?](./core.principalruleaction.name.md) | | string | _(Optional)_ A descriptive name for the rule. |
| [output?](./core.principalruleaction.output.md) | | [Output](./core.output.md) \| undefined | _(Optional)_ User-defined output to be produced when evaluating the rule. |

18 changes: 18 additions & 0 deletions docs/core.principalruleaction.output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/core](./core.md) &gt; [PrincipalRuleAction](./core.principalruleaction.md) &gt; [output](./core.principalruleaction.output.md)

## PrincipalRuleAction.output property

User-defined output to be produced when evaluating the rule.

**Signature:**

```typescript
output?: Output | undefined;
```

## Remarks

Requires the Cerbos policy decision point server to be at least v0.27.

1 change: 1 addition & 0 deletions docs/core.resourcerule.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export interface ResourceRule
| [derivedRoles?](./core.resourcerule.derivedroles.md) | | string\[\] | _(Optional)_ Derived roles to which the rule applies. |
| [effect](./core.resourcerule.effect.md) | | [Effect](./core.effect.md) | The effect of the rule. |
| [name?](./core.resourcerule.name.md) | | string | _(Optional)_ A descriptive name for the rule. |
| [output?](./core.resourcerule.output.md) | | [Output](./core.output.md) \| undefined | _(Optional)_ User-defined output to be produced when evaluating the rule. |
| [roles?](./core.resourcerule.roles.md) | | string\[\] | _(Optional)_ Static roles to which the rule applies. |

18 changes: 18 additions & 0 deletions docs/core.resourcerule.output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/core](./core.md) &gt; [ResourceRule](./core.resourcerule.md) &gt; [output](./core.resourcerule.output.md)

## ResourceRule.output property

User-defined output to be produced when evaluating the rule.

**Signature:**

```typescript
output?: Output | undefined;
```

## Remarks

Requires the Cerbos policy decision point server to be at least v0.27.

6 changes: 6 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## [Unreleased]

### Added

- Support for user-defined policy rule outputs ([#542](https://github.com/cerbos/cerbos-sdk-javascript/pull/542))

Requires a policy decision point server running Cerbos 0.27+.

### Removed

- Support for Node.js 14, which is now [end-of-life](https://github.com/nodejs/release#end-of-life-releases) ([#521](https://github.com/cerbos/cerbos-sdk-javascript/pull/521))
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/convert/fromProtobuf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Effect as EffectProtobuf } from "../protobuf/cerbos/effect/v1/effect";
import type { PlanResourcesFilter_Expression_Operand } from "../protobuf/cerbos/engine/v1/engine";
import type {
OutputEntry as OutputEntry,
PlanResourcesFilter_Expression_Operand,
} from "../protobuf/cerbos/engine/v1/engine";
import { PlanResourcesFilter_Kind } from "../protobuf/cerbos/engine/v1/engine";
import type {
Condition as ConditionProtobuf,
Expand Down Expand Up @@ -46,6 +49,7 @@ import type {
ListSchemasResponse,
Match,
Matches,
OutputResult,
PlanExpressionOperand,
PlanResourcesMetadata,
PlanResourcesResponse,
Expand Down Expand Up @@ -92,6 +96,7 @@ const checkResourcesResultFromProtobuf = ({
actions,
validationErrors,
meta,
outputs,
}: CheckResourcesResponse_ResultEntry): CheckResourcesResult => {
if (!resource) {
throw new Error("Missing resource on CheckResources result");
Expand All @@ -102,6 +107,7 @@ const checkResourcesResultFromProtobuf = ({
actions: actionsFromProtobuf(actions),
validationErrors: validationErrors.map(validationErrorFromProtobuf),
metadata: meta,
outputs: outputs.map(outputResultFromProtobuf),
});
};

Expand Down Expand Up @@ -147,6 +153,14 @@ const validationErrorSourceFromProtobuf = (
}
};

const outputResultFromProtobuf = ({
src,
val,
}: OutputEntry): OutputResult => ({
source: src,
value: val as Value | undefined,
});

export const deleteSchemasResponseFromProtobuf = ({
deletedSchemas,
}: DeleteSchemaResponse): DeleteSchemasResponse => ({
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/convert/toProtobuf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
DerivedRoles as DerivedRolesProtobuf,
Match as MatchProtobuf,
Match_ExprList,
Output as OutputProtobuf,
Policy as PolicyProtobuf,
PrincipalPolicy as PrincipalPolicyProtobuf,
PrincipalRule as PrincipalRuleProtobuf,
Expand Down Expand Up @@ -52,6 +53,7 @@ import type {
ListPoliciesRequest,
Match,
Matches,
Output,
PlanResourcesRequest,
Policy,
Principal,
Expand Down Expand Up @@ -225,18 +227,22 @@ const principalRuleActionToProtobuf = ({
effect,
condition,
name = "",
output,
}: PrincipalRuleAction): PrincipalRule_Action => ({
action,
effect: effectToProtobuf(effect),
condition: condition && conditionToProtobuf(condition),
name,
output: output && outputToProtobuf(output),
});

const effectToProtobuf = (effect: Effect): EffectProtobuf =>
effect === Effect.ALLOW
? EffectProtobuf.EFFECT_ALLOW
: EffectProtobuf.EFFECT_DENY;

const outputToProtobuf = ({ expr }: Output): OutputProtobuf => ({ expr });

const resourcePolicyToProtobuf = ({
resourcePolicy: {
resource,
Expand All @@ -262,13 +268,15 @@ const resourceRuleToProtobuf = ({
roles = [],
condition,
name = "",
output,
}: ResourceRule): ResourceRuleProtobuf => ({
actions,
effect: effectToProtobuf(effect),
derivedRoles,
roles,
condition: condition && conditionToProtobuf(condition),
name,
output: output && outputToProtobuf(output),
});

const policySchemasToProtobuf = ({
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/protobuf/cerbos/engine/v1/engine.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/core/src/protobuf/cerbos/policy/v1/policy.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 91f64e7

Please sign in to comment.