Skip to content

Commit fd98997

Browse files
authored
feat: Dart evaluation (#39)
Adds support for evaluation of Cedar policies in Dart. Largely copied from https://github.com/cedar-policy/cedar-go
1 parent a8afe97 commit fd98997

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+6238
-3886
lines changed

.github/workflows/cedar.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: cedar
2+
on:
3+
pull_request:
4+
paths:
5+
- ".github/workflows/cedar.yaml"
6+
- "packages/cedar/**"
7+
8+
# Prevent duplicate runs due to Graphite
9+
# https://graphite.dev/docs/troubleshooting#why-are-my-actions-running-twice
10+
concurrency:
11+
group: ${{ github.repository }}-${{ github.workflow }}-${{ github.ref }}-${{ github.ref == 'refs/heads/main' && github.sha || ''}}
12+
cancel-in-progress: true
13+
14+
defaults:
15+
run:
16+
shell: bash
17+
working-directory: packages/cedar
18+
19+
jobs:
20+
test:
21+
runs-on: ubuntu-latest
22+
timeout-minutes: 10
23+
steps:
24+
- name: Git Checkout
25+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # 4.1.7
26+
with:
27+
submodules: true
28+
- name: Setup Dart
29+
uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672 # main
30+
- name: Get Packages
31+
run: dart pub get
32+
- name: Test
33+
run: dart test --fail-fast

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dart.vmAdditionalArgs": [
3+
"--enable-experiment=native-assets",
4+
],
5+
}

packages/cedar/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.0
2+
3+
- feat: Add Dart evaluation
4+
15
## 0.1.3
26

37
- Update repository URL

packages/cedar/lib/ast.dart

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export 'src/ast.dart';

packages/cedar/lib/cedar.dart

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
/// native assets of `package:cedar_ffi`.
1515
library;
1616

17-
export 'src/ast/cedar_entity.dart';
18-
export 'src/ast/cedar_entity_id.dart';
19-
export 'src/ast/cedar_schema.dart';
2017
export 'src/authorization/cedar_authorization_request.dart';
2118
export 'src/authorization/cedar_authorization_response.dart';
2219
export 'src/authorization/cedar_authorizer.dart';
20+
export 'src/eval/errors.dart';
21+
export 'src/model/cedar_entity.dart';
22+
export 'src/model/cedar_schema.dart' hide CedarEntityType;
23+
export 'src/model/types/cedar_value.dart';
2324
export 'src/policy/cedar_policy.dart';
2425
export 'src/policy/cedar_policy_set.dart';
25-
export 'src/policy/json_expr.dart';
26-
export 'src/serializers.dart';

packages/cedar/lib/src/ast.dart

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export 'ast/annotation.dart';
2+
export 'ast/expr.dart';
3+
export 'ast/operator.dart';
4+
export 'ast/pattern.dart';
5+
export 'ast/value.dart';
6+
export 'ast/variable.dart';
7+
export 'ast/visitor.dart';
8+
export 'parser/position.dart';
9+
export 'policy/cedar_policy.dart';
10+
export 'policy/cedar_scope.dart';
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'dart:collection';
2+
3+
import 'package:cedar/ast.dart';
4+
5+
final class Annotations with IterableMixin<Annotation> {
6+
Annotations(this.annotations);
7+
8+
factory Annotations.fromJson(Map<String, Object?> json) {
9+
return Annotations(json.cast());
10+
}
11+
12+
final Map<String, String> annotations;
13+
14+
operator [](String key) => annotations[key];
15+
operator []=(String key, String value) => annotations[key] = value;
16+
17+
void add(Annotation annotation) {
18+
annotations[annotation.key] = annotation.value;
19+
}
20+
21+
Annotations annotation(String key, String value) {
22+
return Annotations({
23+
...annotations,
24+
key: value,
25+
});
26+
}
27+
28+
CedarPolicy permit() {
29+
return CedarPolicy(effect: CedarEffect.permit, annotations: this);
30+
}
31+
32+
CedarPolicy forbid() {
33+
return CedarPolicy(effect: CedarEffect.forbid, annotations: this);
34+
}
35+
36+
Iterable<Annotation> get iterable sync* {
37+
for (final entry in annotations.entries) {
38+
yield (key: entry.key, value: entry.value);
39+
}
40+
}
41+
42+
@override
43+
Iterator<Annotation> get iterator => iterable.iterator;
44+
45+
Map<String, String> toJson() => annotations;
46+
}
47+
48+
Annotations annotation(String key, String value) {
49+
return Annotations({key: value});
50+
}
51+
52+
typedef Annotation = ({String key, String value});

packages/cedar/lib/src/ast/cedar_entity.dart

-54
This file was deleted.

packages/cedar/lib/src/ast/cedar_entity.g.dart

-201
This file was deleted.

0 commit comments

Comments
 (0)