@o3co/auth.policy-verifier.cedar 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +97 -0
- package/dist/CedarPolicyRuleCollector.d.mts +92 -0
- package/dist/CedarPolicyRuleCollector.d.mts.map +1 -0
- package/dist/CedarPolicyRuleCollector.mjs +132 -0
- package/dist/RequestFactsCollector.d.mts +21 -0
- package/dist/RequestFactsCollector.d.mts.map +1 -0
- package/dist/RequestFactsCollector.mjs +32 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +7 -0
- package/dist/keys.d.mts +5 -0
- package/dist/keys.d.mts.map +1 -0
- package/dist/keys.mjs +14 -0
- package/dist/mapping.d.mts +76 -0
- package/dist/mapping.d.mts.map +1 -0
- package/dist/mapping.mjs +248 -0
- package/dist/module.d.mts +21 -0
- package/dist/module.d.mts.map +1 -0
- package/dist/module.mjs +29 -0
- package/dist/policySource.d.mts +37 -0
- package/dist/policySource.d.mts.map +1 -0
- package/dist/policySource.mjs +83 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 1o1 Co. Ltd.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# @o3co/auth.policy-verifier.cedar
|
|
2
|
+
|
|
3
|
+
Co-resident [Cedar](https://www.cedarpolicy.com/) policy evaluation for
|
|
4
|
+
[auth.policy-verifier](https://github.com/o3co/auth.policy-verifier), as an
|
|
5
|
+
optional plugin package.
|
|
6
|
+
|
|
7
|
+
The whole Cedar policy set is evaluated in-process (official
|
|
8
|
+
`@cedar-policy/cedar-wasm`, ~15µs per decision, no network) and enters the
|
|
9
|
+
engine's AND-evaluation as **one rule in one group**. TypeScript rule groups
|
|
10
|
+
keep working beside it: TS collectors gather the facts, Cedar policies write
|
|
11
|
+
judgment over them — **no entity store to build or sync**.
|
|
12
|
+
|
|
13
|
+
This decouples policy language from PDP topology. Native logic stays
|
|
14
|
+
TypeScript — no DSL is ever required — and a deployment that adopts Cedar here
|
|
15
|
+
is not binding itself to this verifier: the same `.cedar` files load unchanged
|
|
16
|
+
into an embedded evaluator or a Cedar agent later. Design: [#185](https://github.com/o3co/auth.policy-verifier/issues/185).
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { builtinCollectorsModule } from "@o3co/auth.policy-verifier.builtins";
|
|
22
|
+
import { cedarPolicyModule } from "@o3co/auth.policy-verifier.cedar";
|
|
23
|
+
import { builtinKeyResolversModule, createApp } from "@o3co/auth.policy-verifier.server";
|
|
24
|
+
|
|
25
|
+
const app = await createApp({
|
|
26
|
+
pathResolver: import.meta.resolve,
|
|
27
|
+
config,
|
|
28
|
+
modules: [builtinCollectorsModule, builtinKeyResolversModule, cedarPolicyModule],
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```hocon
|
|
33
|
+
attribute {
|
|
34
|
+
collectors = [
|
|
35
|
+
{ collector = "PayloadSubjectIdCollector" }
|
|
36
|
+
# Promotes action / resourceType / resourceId into attributes — the rule
|
|
37
|
+
# pipeline never sees the request, so the Cedar request is built from these.
|
|
38
|
+
{ collector = "RequestFactsCollector" }
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
rule {
|
|
42
|
+
collectors = [
|
|
43
|
+
{ collector = "CedarPolicyRuleCollector"
|
|
44
|
+
# *.cedar files, sorted and concatenated — byte-identical to what a
|
|
45
|
+
# Cedar agent would load. XOR an inline `policies = "..."` string.
|
|
46
|
+
policyDir = "config/policies"
|
|
47
|
+
|
|
48
|
+
# "abstain" (default): no determining policy leaves the decision to the
|
|
49
|
+
# other rule groups — the migration posture. Flip to "deny" when the
|
|
50
|
+
# policy set is authoritative.
|
|
51
|
+
onNoDeterminingPolicy = "abstain"
|
|
52
|
+
|
|
53
|
+
principal {
|
|
54
|
+
# type = "User" # default
|
|
55
|
+
# idAttribute = "userId" # default (ATTR_USER_ID)
|
|
56
|
+
attributes { dept = "department" } # principal.dept == "eng"
|
|
57
|
+
parents { Group = "groups" } # principal in Group::"admins"
|
|
58
|
+
}
|
|
59
|
+
resource {
|
|
60
|
+
attributes {
|
|
61
|
+
# entity-reference form: resource.owner == principal
|
|
62
|
+
owner = { attribute = "resourceOwner", entityType = "User" }
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
context { mfa = "mfaVerified" } # context.mfa
|
|
66
|
+
}
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Request-context fields reach Cedar the same way everything else does — as
|
|
72
|
+
attributes. Promote them with the builtins' `RequestContextAttributeCollector`
|
|
73
|
+
(the declared-allowlist trust boundary, #123) and map them here; nothing
|
|
74
|
+
undeclared can reach a policy.
|
|
75
|
+
|
|
76
|
+
## Semantics
|
|
77
|
+
|
|
78
|
+
- **Layered PDP.** Cedar's own semantics (forbid overrides permit) hold inside
|
|
79
|
+
the group; the group ANDs with every TypeScript group. During migration both
|
|
80
|
+
are active and compose only toward strictness.
|
|
81
|
+
- **Evaluation errors always deny, and log.** Cedar reports a policy that
|
|
82
|
+
reads a missing attribute as `deny` with the cause only in diagnostics — and
|
|
83
|
+
an erroring `forbid` stops forbidding, so the top-level decision can read
|
|
84
|
+
`allow` exactly when it is least trustworthy. The rule checks
|
|
85
|
+
`diagnostics.errors` first: any error is a deny regardless of
|
|
86
|
+
`onNoDeterminingPolicy`, and is logged unless `logEvaluationErrors = false`.
|
|
87
|
+
- **Entity synthesis is one hop.** Principal and resource entities are
|
|
88
|
+
synthesized per request from the attribute map — attributes, group
|
|
89
|
+
membership, entity references. Multi-hop dereference and hierarchy walks
|
|
90
|
+
need an entity store; needing them is the signal to move to a full Cedar
|
|
91
|
+
deployment, which the same `.cedar` files already fit.
|
|
92
|
+
|
|
93
|
+
## Version pinning
|
|
94
|
+
|
|
95
|
+
`@cedar-policy/cedar-wasm` is pinned exactly: Cedar minor releases can carry
|
|
96
|
+
policy-language changes, so upgrades should be deliberate and re-validated,
|
|
97
|
+
not fall out of a range resolution.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { CollectorContext, Logger, Rule, RuleCollector } from "@o3co/auth.policy-verifier.core";
|
|
2
|
+
/** What the rule answers when no policy determined the request (see below). */
|
|
3
|
+
export type NoDeterminingPolicy = "abstain" | "deny";
|
|
4
|
+
/** Config entry accepted by `CedarPolicyRuleCollector`. */
|
|
5
|
+
export interface CedarPolicyRuleCollectorConfig {
|
|
6
|
+
/** Directory of `*.cedar` files (sorted, concatenated). XOR `policies`. */
|
|
7
|
+
policyDir?: string;
|
|
8
|
+
/** Inline Cedar policy text. XOR `policyDir`. */
|
|
9
|
+
policies?: string;
|
|
10
|
+
/** Rule group the Cedar decision joins AND-evaluation as. Default `"cedar"`. */
|
|
11
|
+
ruleType?: string;
|
|
12
|
+
/**
|
|
13
|
+
* What the rule answers when Cedar reports no determining policy — no
|
|
14
|
+
* `permit` matched and no `forbid` matched.
|
|
15
|
+
*
|
|
16
|
+
* `"abstain"` (default) passes the group, leaving the decision to the other
|
|
17
|
+
* rule groups: the migration posture, where the policy set covers part of
|
|
18
|
+
* the surface and the TypeScript rules still hold the rest. `"deny"` makes
|
|
19
|
+
* the policy set authoritative — Cedar's own implicit-deny — for the moment
|
|
20
|
+
* it covers everything.
|
|
21
|
+
*/
|
|
22
|
+
onNoDeterminingPolicy?: NoDeterminingPolicy;
|
|
23
|
+
/**
|
|
24
|
+
* Whether the rule logs Cedar evaluation errors (default `true`).
|
|
25
|
+
*
|
|
26
|
+
* The error branch is load-bearing: Cedar answers a policy that reads a
|
|
27
|
+
* missing attribute with `decision: "deny"` and the cause only in
|
|
28
|
+
* `diagnostics.errors` — without this log, a typo'd attribute mapping is
|
|
29
|
+
* indistinguishable from a policy deny. The healthy path never logs.
|
|
30
|
+
*/
|
|
31
|
+
logEvaluationErrors?: boolean;
|
|
32
|
+
/** Entity/context mapping — see `resolveMapping` for the shape. */
|
|
33
|
+
principal?: unknown;
|
|
34
|
+
action?: unknown;
|
|
35
|
+
resource?: unknown;
|
|
36
|
+
context?: unknown;
|
|
37
|
+
}
|
|
38
|
+
/** Constructor options beyond the config entry (programmatic composition only). */
|
|
39
|
+
export interface CedarPolicyRuleCollectorOptions {
|
|
40
|
+
/** Receives evaluation-error logs. Defaults to the console-backed logger. */
|
|
41
|
+
logger?: Logger;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Evaluates a Cedar policy set, co-resident, as one core `Rule`.
|
|
45
|
+
*
|
|
46
|
+
* ## Why one rule, not a translation
|
|
47
|
+
*
|
|
48
|
+
* Core's evaluator is OR within a group and AND across groups, with no global
|
|
49
|
+
* override; Cedar's forbid-overrides-permit is inexpressible in that algebra.
|
|
50
|
+
* So the policy set is never translated into core rules — the real Cedar
|
|
51
|
+
* evaluator runs, and its whole verdict enters AND-evaluation as a single
|
|
52
|
+
* group. Layered PDP: Cedar semantics inside the group, core semantics across
|
|
53
|
+
* groups, composing only toward strictness.
|
|
54
|
+
*
|
|
55
|
+
* ## Why evaluation happens in `verify`
|
|
56
|
+
*
|
|
57
|
+
* The attribute and rule pipelines run concurrently, so `collect` never sees
|
|
58
|
+
* the merged attributes — and the point of the design is that Cedar policies
|
|
59
|
+
* decide over what the attribute collectors gathered. `verify(attrs)` is where
|
|
60
|
+
* that map exists. Cedar evaluation is a deterministic, synchronous function
|
|
61
|
+
* of `(preparsed policy set, request)` with the request built from `attrs`
|
|
62
|
+
* alone, so the rule satisfies the purity contract exactly: the policy set id
|
|
63
|
+
* and mapping are fixed at boot, nothing of `CollectorContext` is retained,
|
|
64
|
+
* and equal attributes give equal answers. The rule object is built once, in
|
|
65
|
+
* the constructor — the hoisted form `metrics.test.mts` documents as the
|
|
66
|
+
* strongest compliance shape.
|
|
67
|
+
*
|
|
68
|
+
* The one deliberate softening: on the *error* branch the rule emits a log
|
|
69
|
+
* line (config `logEvaluationErrors`, default on). The decision itself remains
|
|
70
|
+
* a pure function of `attrs`; see the config doc for why silence there would
|
|
71
|
+
* cost more than the letter of "no side effects" buys.
|
|
72
|
+
*
|
|
73
|
+
* ## Answer interpretation
|
|
74
|
+
*
|
|
75
|
+
* | Cedar answered | with | the rule answers |
|
|
76
|
+
* | --- | --- | --- |
|
|
77
|
+
* | `allow` | no errors | pass |
|
|
78
|
+
* | `deny` | determining `forbid` | fail |
|
|
79
|
+
* | `deny` | no determining policy | `onNoDeterminingPolicy` |
|
|
80
|
+
* | anything | evaluation errors | **fail, and log** |
|
|
81
|
+
*
|
|
82
|
+
* The last row is unconditional — an evaluation error is never an abstention.
|
|
83
|
+
* Cedar treats a policy that errors as not satisfied, so a `forbid` that
|
|
84
|
+
* errors stops forbidding and the top-level decision can read `allow`; the
|
|
85
|
+
* errors check runs first precisely so that a broken input fails closed.
|
|
86
|
+
*/
|
|
87
|
+
export declare class CedarPolicyRuleCollector implements RuleCollector {
|
|
88
|
+
private readonly rule;
|
|
89
|
+
constructor(config: CedarPolicyRuleCollectorConfig, options?: CedarPolicyRuleCollectorOptions);
|
|
90
|
+
collect(_context: CollectorContext): Promise<Rule[]>;
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=CedarPolicyRuleCollector.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CedarPolicyRuleCollector.d.mts","sourceRoot":"","sources":["../src/CedarPolicyRuleCollector.mts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACX,gBAAgB,EAChB,MAAM,EACN,IAAI,EACJ,aAAa,EACb,MAAM,iCAAiC,CAAC;AAUzC,+EAA+E;AAC/E,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,MAAM,CAAC;AAIrD,2DAA2D;AAC3D,MAAM,WAAW,8BAA8B;IAC9C,2EAA2E;IAC3E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gFAAgF;IAChF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;;;;;OASG;IACH,qBAAqB,CAAC,EAAE,mBAAmB,CAAC;IAC5C;;;;;;;OAOG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,mEAAmE;IACnE,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,mFAAmF;AACnF,MAAM,WAAW,+BAA+B;IAC/C,6EAA6E;IAC7E,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB;AAKD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,qBAAa,wBAAyB,YAAW,aAAa;IAC7D,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAO;gBAEhB,MAAM,EAAE,8BAA8B,EAAE,OAAO,CAAC,EAAE,+BAA+B;IA+CvF,OAAO,CAAC,QAAQ,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;CAK1D"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 1o1 Co. Ltd.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { preparsePolicySet, statefulIsAuthorized } from "@cedar-policy/cedar-wasm/nodejs";
|
|
4
|
+
import { createConsoleLogger } from "@o3co/auth.policy-verifier.core";
|
|
5
|
+
import { buildCedarRequest, resolveMapping, } from "./mapping.mjs";
|
|
6
|
+
import { loadPolicySource } from "./policySource.mjs";
|
|
7
|
+
const NO_DETERMINING_POLICIES = ["abstain", "deny"];
|
|
8
|
+
/** Distinguishes concurrently-constructed collectors' preparsed sets. */
|
|
9
|
+
let policySetCounter = 0;
|
|
10
|
+
/**
|
|
11
|
+
* Evaluates a Cedar policy set, co-resident, as one core `Rule`.
|
|
12
|
+
*
|
|
13
|
+
* ## Why one rule, not a translation
|
|
14
|
+
*
|
|
15
|
+
* Core's evaluator is OR within a group and AND across groups, with no global
|
|
16
|
+
* override; Cedar's forbid-overrides-permit is inexpressible in that algebra.
|
|
17
|
+
* So the policy set is never translated into core rules — the real Cedar
|
|
18
|
+
* evaluator runs, and its whole verdict enters AND-evaluation as a single
|
|
19
|
+
* group. Layered PDP: Cedar semantics inside the group, core semantics across
|
|
20
|
+
* groups, composing only toward strictness.
|
|
21
|
+
*
|
|
22
|
+
* ## Why evaluation happens in `verify`
|
|
23
|
+
*
|
|
24
|
+
* The attribute and rule pipelines run concurrently, so `collect` never sees
|
|
25
|
+
* the merged attributes — and the point of the design is that Cedar policies
|
|
26
|
+
* decide over what the attribute collectors gathered. `verify(attrs)` is where
|
|
27
|
+
* that map exists. Cedar evaluation is a deterministic, synchronous function
|
|
28
|
+
* of `(preparsed policy set, request)` with the request built from `attrs`
|
|
29
|
+
* alone, so the rule satisfies the purity contract exactly: the policy set id
|
|
30
|
+
* and mapping are fixed at boot, nothing of `CollectorContext` is retained,
|
|
31
|
+
* and equal attributes give equal answers. The rule object is built once, in
|
|
32
|
+
* the constructor — the hoisted form `metrics.test.mts` documents as the
|
|
33
|
+
* strongest compliance shape.
|
|
34
|
+
*
|
|
35
|
+
* The one deliberate softening: on the *error* branch the rule emits a log
|
|
36
|
+
* line (config `logEvaluationErrors`, default on). The decision itself remains
|
|
37
|
+
* a pure function of `attrs`; see the config doc for why silence there would
|
|
38
|
+
* cost more than the letter of "no side effects" buys.
|
|
39
|
+
*
|
|
40
|
+
* ## Answer interpretation
|
|
41
|
+
*
|
|
42
|
+
* | Cedar answered | with | the rule answers |
|
|
43
|
+
* | --- | --- | --- |
|
|
44
|
+
* | `allow` | no errors | pass |
|
|
45
|
+
* | `deny` | determining `forbid` | fail |
|
|
46
|
+
* | `deny` | no determining policy | `onNoDeterminingPolicy` |
|
|
47
|
+
* | anything | evaluation errors | **fail, and log** |
|
|
48
|
+
*
|
|
49
|
+
* The last row is unconditional — an evaluation error is never an abstention.
|
|
50
|
+
* Cedar treats a policy that errors as not satisfied, so a `forbid` that
|
|
51
|
+
* errors stops forbidding and the top-level decision can read `allow`; the
|
|
52
|
+
* errors check runs first precisely so that a broken input fails closed.
|
|
53
|
+
*/
|
|
54
|
+
export class CedarPolicyRuleCollector {
|
|
55
|
+
rule;
|
|
56
|
+
constructor(config, options) {
|
|
57
|
+
const raw = (config ?? {});
|
|
58
|
+
const ruleType = raw.ruleType === undefined ? "cedar" : raw.ruleType;
|
|
59
|
+
if (typeof ruleType !== "string" || ruleType.length === 0) {
|
|
60
|
+
throw new Error(`CedarPolicyRuleCollector: ruleType must be a non-empty string, got ${JSON.stringify(raw.ruleType)}`);
|
|
61
|
+
}
|
|
62
|
+
const onNoDeterminingPolicy = (raw.onNoDeterminingPolicy ?? "abstain");
|
|
63
|
+
if (!NO_DETERMINING_POLICIES.includes(onNoDeterminingPolicy)) {
|
|
64
|
+
throw new Error(`CedarPolicyRuleCollector: onNoDeterminingPolicy must be one of ${NO_DETERMINING_POLICIES.join(", ")}, got ${JSON.stringify(raw.onNoDeterminingPolicy)}`);
|
|
65
|
+
}
|
|
66
|
+
const rawLog = raw.logEvaluationErrors;
|
|
67
|
+
if (rawLog !== undefined && typeof rawLog !== "boolean") {
|
|
68
|
+
throw new Error(`CedarPolicyRuleCollector: logEvaluationErrors must be a boolean, got ${JSON.stringify(rawLog)}`);
|
|
69
|
+
}
|
|
70
|
+
const logEvaluationErrors = rawLog ?? true;
|
|
71
|
+
const mapping = resolveMapping(raw);
|
|
72
|
+
const source = loadPolicySource(raw);
|
|
73
|
+
// Boot-time compile. `preparsePolicySet` stores the compiled set in wasm
|
|
74
|
+
// memory under this id; the per-request call references it without
|
|
75
|
+
// re-parsing. The id is per-instance so two collectors never share a slot.
|
|
76
|
+
const policySetId = `auth.policy-verifier.cedar:${policySetCounter++}`;
|
|
77
|
+
const parsed = preparsePolicySet(policySetId, { staticPolicies: source.text });
|
|
78
|
+
if (parsed.type === "failure") {
|
|
79
|
+
const details = parsed.errors.map((error) => error.message).join("; ");
|
|
80
|
+
throw new Error(`CedarPolicyRuleCollector: policy set from ${source.description} failed to compile: ${details}`);
|
|
81
|
+
}
|
|
82
|
+
const logger = logEvaluationErrors
|
|
83
|
+
? (options?.logger ?? createConsoleLogger({ collector: "CedarPolicyRuleCollector" }))
|
|
84
|
+
: undefined;
|
|
85
|
+
this.rule = buildRule({ ruleType, onNoDeterminingPolicy, policySetId, mapping, logger });
|
|
86
|
+
}
|
|
87
|
+
async collect(_context) {
|
|
88
|
+
// Nothing is read from the context: everything the rule needs was fixed
|
|
89
|
+
// at boot, and everything request-shaped reaches it through `attrs`.
|
|
90
|
+
return [this.rule];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function buildRule(bound) {
|
|
94
|
+
const { ruleType, onNoDeterminingPolicy, policySetId, mapping, logger } = bound;
|
|
95
|
+
return {
|
|
96
|
+
ruleType,
|
|
97
|
+
code: "cedar_deny",
|
|
98
|
+
message: "Denied by Cedar policy",
|
|
99
|
+
verify(attrs) {
|
|
100
|
+
let request;
|
|
101
|
+
try {
|
|
102
|
+
request = buildCedarRequest(mapping, attrs);
|
|
103
|
+
}
|
|
104
|
+
catch (cause) {
|
|
105
|
+
logger?.error({ policySetId, reason: cause instanceof Error ? cause.message : String(cause) }, "cedar request could not be built from attributes — denying");
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
const answer = statefulIsAuthorized({ ...request, preparsedPolicySetId: policySetId });
|
|
109
|
+
if (answer.type !== "success") {
|
|
110
|
+
logger?.error({ policySetId, errors: answer.errors.map((error) => error.message) }, "cedar authorization call failed — denying");
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const { decision, diagnostics } = answer.response;
|
|
114
|
+
if (diagnostics.errors.length > 0) {
|
|
115
|
+
// Checked before the decision on purpose: an erroring `forbid` stops
|
|
116
|
+
// forbidding, so `decision` can read "allow" exactly when it is least
|
|
117
|
+
// trustworthy. Never an abstention.
|
|
118
|
+
logger?.error({
|
|
119
|
+
policySetId,
|
|
120
|
+
decision,
|
|
121
|
+
errors: diagnostics.errors.map((error) => `${error.policyId}: ${error.error.message}`),
|
|
122
|
+
}, "cedar policy evaluation raised errors — denying");
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
if (decision === "allow")
|
|
126
|
+
return true;
|
|
127
|
+
if (diagnostics.reason.length === 0)
|
|
128
|
+
return onNoDeterminingPolicy === "abstain";
|
|
129
|
+
return false;
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { AttributeCollector, Attributes, CollectorContext } from "@o3co/auth.policy-verifier.core";
|
|
2
|
+
/**
|
|
3
|
+
* Promotes the parsed request — `action` and the `Resource` fields — into
|
|
4
|
+
* attributes, so that a rule can decide over them without touching
|
|
5
|
+
* `CollectorContext`.
|
|
6
|
+
*
|
|
7
|
+
* `CedarPolicyRuleCollector` needs this: its rule builds the Cedar
|
|
8
|
+
* `(principal, action, resource, context)` request from the merged attribute
|
|
9
|
+
* map inside `verify`, and `verify` runs where the request no longer exists.
|
|
10
|
+
* Only primitives are copied — string values out of the context, never a
|
|
11
|
+
* reference into it — which is the legal side of the line the rule-purity
|
|
12
|
+
* conformance suite draws.
|
|
13
|
+
*
|
|
14
|
+
* `requestResourceId` is written only when the resource parser produced one;
|
|
15
|
+
* an absent id stays absent rather than becoming `""`, so downstream mapping
|
|
16
|
+
* can tell "no id" from "empty id".
|
|
17
|
+
*/
|
|
18
|
+
export declare class RequestFactsCollector implements AttributeCollector {
|
|
19
|
+
collect(context: CollectorContext): Promise<Attributes>;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=RequestFactsCollector.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequestFactsCollector.d.mts","sourceRoot":"","sources":["../src/RequestFactsCollector.mts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACX,kBAAkB,EAClB,UAAU,EACV,gBAAgB,EAChB,MAAM,iCAAiC,CAAC;AAQzC;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,qBAAsB,YAAW,kBAAkB;IACzD,OAAO,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,UAAU,CAAC;CAW7D"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 1o1 Co. Ltd.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { ATTR_REQUEST_ACTION, ATTR_REQUEST_RESOURCE_ID, ATTR_REQUEST_RESOURCE_RAW, ATTR_REQUEST_RESOURCE_TYPE, } from "./keys.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Promotes the parsed request — `action` and the `Resource` fields — into
|
|
6
|
+
* attributes, so that a rule can decide over them without touching
|
|
7
|
+
* `CollectorContext`.
|
|
8
|
+
*
|
|
9
|
+
* `CedarPolicyRuleCollector` needs this: its rule builds the Cedar
|
|
10
|
+
* `(principal, action, resource, context)` request from the merged attribute
|
|
11
|
+
* map inside `verify`, and `verify` runs where the request no longer exists.
|
|
12
|
+
* Only primitives are copied — string values out of the context, never a
|
|
13
|
+
* reference into it — which is the legal side of the line the rule-purity
|
|
14
|
+
* conformance suite draws.
|
|
15
|
+
*
|
|
16
|
+
* `requestResourceId` is written only when the resource parser produced one;
|
|
17
|
+
* an absent id stays absent rather than becoming `""`, so downstream mapping
|
|
18
|
+
* can tell "no id" from "empty id".
|
|
19
|
+
*/
|
|
20
|
+
export class RequestFactsCollector {
|
|
21
|
+
async collect(context) {
|
|
22
|
+
const attributes = new Map([
|
|
23
|
+
[ATTR_REQUEST_ACTION, context.action],
|
|
24
|
+
[ATTR_REQUEST_RESOURCE_TYPE, context.resource.resourceType],
|
|
25
|
+
[ATTR_REQUEST_RESOURCE_RAW, context.resource.raw],
|
|
26
|
+
]);
|
|
27
|
+
if (context.resource.resourceId !== undefined) {
|
|
28
|
+
attributes.set(ATTR_REQUEST_RESOURCE_ID, context.resource.resourceId);
|
|
29
|
+
}
|
|
30
|
+
return attributes;
|
|
31
|
+
}
|
|
32
|
+
}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { CedarPolicyRuleCollectorConfig, CedarPolicyRuleCollectorOptions, NoDeterminingPolicy, } from "./CedarPolicyRuleCollector.mjs";
|
|
2
|
+
export { CedarPolicyRuleCollector } from "./CedarPolicyRuleCollector.mjs";
|
|
3
|
+
export { ATTR_REQUEST_ACTION, ATTR_REQUEST_RESOURCE_ID, ATTR_REQUEST_RESOURCE_RAW, ATTR_REQUEST_RESOURCE_TYPE, } from "./keys.mjs";
|
|
4
|
+
export type { AttributeMapping, EntityMappingConfig } from "./mapping.mjs";
|
|
5
|
+
export { CedarInputError } from "./mapping.mjs";
|
|
6
|
+
export { cedarPolicyModule } from "./module.mjs";
|
|
7
|
+
export { RequestFactsCollector } from "./RequestFactsCollector.mjs";
|
|
8
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAGA,YAAY,EACX,8BAA8B,EAC9B,+BAA+B,EAC/B,mBAAmB,GACnB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,wBAAwB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,EACN,mBAAmB,EACnB,wBAAwB,EACxB,yBAAyB,EACzB,0BAA0B,GAC1B,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAC3E,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 1o1 Co. Ltd.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
export { CedarPolicyRuleCollector } from "./CedarPolicyRuleCollector.mjs";
|
|
4
|
+
export { ATTR_REQUEST_ACTION, ATTR_REQUEST_RESOURCE_ID, ATTR_REQUEST_RESOURCE_RAW, ATTR_REQUEST_RESOURCE_TYPE, } from "./keys.mjs";
|
|
5
|
+
export { CedarInputError } from "./mapping.mjs";
|
|
6
|
+
export { cedarPolicyModule } from "./module.mjs";
|
|
7
|
+
export { RequestFactsCollector } from "./RequestFactsCollector.mjs";
|
package/dist/keys.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const ATTR_REQUEST_ACTION: "requestAction";
|
|
2
|
+
export declare const ATTR_REQUEST_RESOURCE_TYPE: "requestResourceType";
|
|
3
|
+
export declare const ATTR_REQUEST_RESOURCE_ID: "requestResourceId";
|
|
4
|
+
export declare const ATTR_REQUEST_RESOURCE_RAW: "requestResourceRaw";
|
|
5
|
+
//# sourceMappingURL=keys.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.d.mts","sourceRoot":"","sources":["../src/keys.mts"],"names":[],"mappings":"AAYA,eAAO,MAAM,mBAAmB,EAAG,eAAwB,CAAC;AAC5D,eAAO,MAAM,0BAA0B,EAAG,qBAA8B,CAAC;AACzE,eAAO,MAAM,wBAAwB,EAAG,mBAA4B,CAAC;AACrE,eAAO,MAAM,yBAAyB,EAAG,oBAA6B,CAAC"}
|
package/dist/keys.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 1o1 Co. Ltd.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// Attribute keys written by `RequestFactsCollector` and read (by default) by
|
|
4
|
+
// `CedarPolicyRuleCollector`. They exist because the rule pipeline never sees
|
|
5
|
+
// `CollectorContext`: a rule is a function of the merged attributes alone, so
|
|
6
|
+
// the request facts a Cedar policy set decides over — action, resource type,
|
|
7
|
+
// resource id — must be promoted into attributes by a collector first. These
|
|
8
|
+
// are this package's vocabulary, not core's: core's `ATTR_*` constants are
|
|
9
|
+
// reserved for OAuth/OIDC/RBAC concepts (AGENTS.md "Core Vocabulary Scope"),
|
|
10
|
+
// and "the parsed request" is not one.
|
|
11
|
+
export const ATTR_REQUEST_ACTION = "requestAction";
|
|
12
|
+
export const ATTR_REQUEST_RESOURCE_TYPE = "requestResourceType";
|
|
13
|
+
export const ATTR_REQUEST_RESOURCE_ID = "requestResourceId";
|
|
14
|
+
export const ATTR_REQUEST_RESOURCE_RAW = "requestResourceRaw";
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { Context, Entities, EntityUid } from "@cedar-policy/cedar-wasm/nodejs";
|
|
2
|
+
import type { ReadonlyAttributes } from "@o3co/auth.policy-verifier.core";
|
|
3
|
+
/**
|
|
4
|
+
* Raised when the merged attributes cannot be shaped into a Cedar request —
|
|
5
|
+
* a required input is missing, or a value is present but malformed. The rule
|
|
6
|
+
* catches it and denies: an input the mapping cannot vouch for must never
|
|
7
|
+
* become an authorization the deployment did not write.
|
|
8
|
+
*/
|
|
9
|
+
export declare class CedarInputError extends Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* One synthesized entity attribute: either a plain value copied from an
|
|
14
|
+
* attribute, or an entity reference built from one (`resource.owner ==
|
|
15
|
+
* principal` needs `owner` to be an entity, not a string).
|
|
16
|
+
*/
|
|
17
|
+
export type AttributeMapping = string | {
|
|
18
|
+
attribute: string;
|
|
19
|
+
entityType: string;
|
|
20
|
+
};
|
|
21
|
+
/** Declarative config for one synthesized entity (principal or resource). */
|
|
22
|
+
export interface EntityMappingConfig {
|
|
23
|
+
attributes?: Record<string, AttributeMapping>;
|
|
24
|
+
parents?: Record<string, string>;
|
|
25
|
+
}
|
|
26
|
+
/** Validated mapping the collector resolves once at boot. */
|
|
27
|
+
export interface ResolvedMapping {
|
|
28
|
+
principalType: string;
|
|
29
|
+
principalIdAttribute: string;
|
|
30
|
+
principal: EntityMappingConfig;
|
|
31
|
+
actionType: string;
|
|
32
|
+
actionIdAttribute: string;
|
|
33
|
+
resourceTypeAttribute: string;
|
|
34
|
+
resourceIdAttribute: string;
|
|
35
|
+
resourceIdWhenAbsent: string;
|
|
36
|
+
resource: EntityMappingConfig;
|
|
37
|
+
context: Record<string, string>;
|
|
38
|
+
}
|
|
39
|
+
/** The request shape `statefulIsAuthorized` accepts, minus the preparsed id. */
|
|
40
|
+
export interface CedarRequest {
|
|
41
|
+
principal: EntityUid;
|
|
42
|
+
action: EntityUid;
|
|
43
|
+
resource: EntityUid;
|
|
44
|
+
context: Context;
|
|
45
|
+
entities: Entities;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Validates the mapping section of a `CedarPolicyRuleCollector` config entry
|
|
49
|
+
* and resolves defaults. Runs once at boot, inside the collector factory, so a
|
|
50
|
+
* shape mistake refuses to start rather than denying every request at runtime.
|
|
51
|
+
*/
|
|
52
|
+
export declare function resolveMapping(config: Record<string, unknown>): ResolvedMapping;
|
|
53
|
+
/**
|
|
54
|
+
* Builds the Cedar authorization request from the merged attributes.
|
|
55
|
+
*
|
|
56
|
+
* Pure: a deterministic function of `(mapping, attrs)`, no I/O, nothing read
|
|
57
|
+
* outside its arguments — it runs inside `Rule.verify` and is bound by the
|
|
58
|
+
* same contract. Throws {@link CedarInputError} when the attributes cannot
|
|
59
|
+
* supply the request; the caller turns that into a deny.
|
|
60
|
+
*
|
|
61
|
+
* Two deliberate asymmetries in how malformed input is treated:
|
|
62
|
+
*
|
|
63
|
+
* - A mapped **attribute** whose value is absent or unmappable is *omitted*
|
|
64
|
+
* from the synthesized entity. That is fail-closed on its own: any policy
|
|
65
|
+
* that reads the missing attribute raises a Cedar evaluation error, and the
|
|
66
|
+
* rule denies on evaluation errors regardless of the abstain knob.
|
|
67
|
+
* - A mapped **parent** whose value is present but malformed *throws*. Parent
|
|
68
|
+
* omission is not an error to Cedar — membership is simply absent — so a
|
|
69
|
+
* typo'd `groups` attribute would silently un-member the principal, and a
|
|
70
|
+
* `forbid (principal in Group::"banned")` policy would silently stop
|
|
71
|
+
* forbidding. That is the one place omission fails open, so it is the one
|
|
72
|
+
* place malformed input refuses instead. An *absent* parents attribute stays
|
|
73
|
+
* legitimate (a principal in no groups).
|
|
74
|
+
*/
|
|
75
|
+
export declare function buildCedarRequest(mapping: ResolvedMapping, attrs: ReadonlyAttributes): CedarRequest;
|
|
76
|
+
//# sourceMappingURL=mapping.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapping.d.mts","sourceRoot":"","sources":["../src/mapping.mts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAkB,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AACpG,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAQ1E;;;;;GAKG;AACH,qBAAa,eAAgB,SAAQ,KAAK;gBAC7B,OAAO,EAAE,MAAM;CAI3B;AAED;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC;AAElF,6EAA6E;AAC7E,MAAM,WAAW,mBAAmB;IACnC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;IAC9C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjC;AAED,6DAA6D;AAC7D,MAAM,WAAW,eAAe;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,SAAS,EAAE,mBAAmB,CAAC;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,mBAAmB,EAAE,MAAM,CAAC;IAC5B,oBAAoB,EAAE,MAAM,CAAC;IAC7B,QAAQ,EAAE,mBAAmB,CAAC;IAC9B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC;AAED,gFAAgF;AAChF,MAAM,WAAW,YAAY;IAC5B,SAAS,EAAE,SAAS,CAAC;IACrB,MAAM,EAAE,SAAS,CAAC;IAClB,QAAQ,EAAE,SAAS,CAAC;IACpB,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,QAAQ,CAAC;CACnB;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,eAAe,CAqB/E;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,iBAAiB,CAChC,OAAO,EAAE,eAAe,EACxB,KAAK,EAAE,kBAAkB,GACvB,YAAY,CA6Bd"}
|
package/dist/mapping.mjs
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 1o1 Co. Ltd.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { ATTR_USER_ID } from "@o3co/auth.policy-verifier.core";
|
|
4
|
+
import { ATTR_REQUEST_ACTION, ATTR_REQUEST_RESOURCE_ID, ATTR_REQUEST_RESOURCE_TYPE, } from "./keys.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* Raised when the merged attributes cannot be shaped into a Cedar request —
|
|
7
|
+
* a required input is missing, or a value is present but malformed. The rule
|
|
8
|
+
* catches it and denies: an input the mapping cannot vouch for must never
|
|
9
|
+
* become an authorization the deployment did not write.
|
|
10
|
+
*/
|
|
11
|
+
export class CedarInputError extends Error {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "CedarInputError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Validates the mapping section of a `CedarPolicyRuleCollector` config entry
|
|
19
|
+
* and resolves defaults. Runs once at boot, inside the collector factory, so a
|
|
20
|
+
* shape mistake refuses to start rather than denying every request at runtime.
|
|
21
|
+
*/
|
|
22
|
+
export function resolveMapping(config) {
|
|
23
|
+
const principal = section(config, "principal");
|
|
24
|
+
const action = section(config, "action");
|
|
25
|
+
const resource = section(config, "resource");
|
|
26
|
+
const context = config.context;
|
|
27
|
+
const resolved = {
|
|
28
|
+
principalType: optionalString(principal, "principal.type") ?? "User",
|
|
29
|
+
principalIdAttribute: optionalString(principal, "principal.idAttribute") ?? ATTR_USER_ID,
|
|
30
|
+
principal: entityMapping(principal, "principal"),
|
|
31
|
+
actionType: optionalString(action, "action.type") ?? "Action",
|
|
32
|
+
actionIdAttribute: optionalString(action, "action.idAttribute") ?? ATTR_REQUEST_ACTION,
|
|
33
|
+
resourceTypeAttribute: optionalString(resource, "resource.typeAttribute") ?? ATTR_REQUEST_RESOURCE_TYPE,
|
|
34
|
+
resourceIdAttribute: optionalString(resource, "resource.idAttribute") ?? ATTR_REQUEST_RESOURCE_ID,
|
|
35
|
+
resourceIdWhenAbsent: optionalString(resource, "resource.idWhenAbsent") ?? "",
|
|
36
|
+
resource: entityMapping(resource, "resource"),
|
|
37
|
+
context: stringRecord(context, "context"),
|
|
38
|
+
};
|
|
39
|
+
return resolved;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Builds the Cedar authorization request from the merged attributes.
|
|
43
|
+
*
|
|
44
|
+
* Pure: a deterministic function of `(mapping, attrs)`, no I/O, nothing read
|
|
45
|
+
* outside its arguments — it runs inside `Rule.verify` and is bound by the
|
|
46
|
+
* same contract. Throws {@link CedarInputError} when the attributes cannot
|
|
47
|
+
* supply the request; the caller turns that into a deny.
|
|
48
|
+
*
|
|
49
|
+
* Two deliberate asymmetries in how malformed input is treated:
|
|
50
|
+
*
|
|
51
|
+
* - A mapped **attribute** whose value is absent or unmappable is *omitted*
|
|
52
|
+
* from the synthesized entity. That is fail-closed on its own: any policy
|
|
53
|
+
* that reads the missing attribute raises a Cedar evaluation error, and the
|
|
54
|
+
* rule denies on evaluation errors regardless of the abstain knob.
|
|
55
|
+
* - A mapped **parent** whose value is present but malformed *throws*. Parent
|
|
56
|
+
* omission is not an error to Cedar — membership is simply absent — so a
|
|
57
|
+
* typo'd `groups` attribute would silently un-member the principal, and a
|
|
58
|
+
* `forbid (principal in Group::"banned")` policy would silently stop
|
|
59
|
+
* forbidding. That is the one place omission fails open, so it is the one
|
|
60
|
+
* place malformed input refuses instead. An *absent* parents attribute stays
|
|
61
|
+
* legitimate (a principal in no groups).
|
|
62
|
+
*/
|
|
63
|
+
export function buildCedarRequest(mapping, attrs) {
|
|
64
|
+
const principalId = requiredString(attrs, mapping.principalIdAttribute, "principal id");
|
|
65
|
+
const actionId = requiredString(attrs, mapping.actionIdAttribute, "action");
|
|
66
|
+
const resourceType = requiredString(attrs, mapping.resourceTypeAttribute, "resource type");
|
|
67
|
+
const resourceId = optionalAttrString(attrs, mapping.resourceIdAttribute, "resource id") ??
|
|
68
|
+
mapping.resourceIdWhenAbsent;
|
|
69
|
+
const principal = { type: mapping.principalType, id: principalId };
|
|
70
|
+
const resource = { type: resourceType, id: resourceId };
|
|
71
|
+
return {
|
|
72
|
+
principal,
|
|
73
|
+
action: { type: mapping.actionType, id: actionId },
|
|
74
|
+
resource,
|
|
75
|
+
context: buildContext(mapping.context, attrs),
|
|
76
|
+
entities: [
|
|
77
|
+
{
|
|
78
|
+
uid: principal,
|
|
79
|
+
attrs: buildEntityAttrs(mapping.principal.attributes, attrs),
|
|
80
|
+
parents: buildParents(mapping.principal.parents, attrs),
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
uid: resource,
|
|
84
|
+
attrs: buildEntityAttrs(mapping.resource.attributes, attrs),
|
|
85
|
+
parents: buildParents(mapping.resource.parents, attrs),
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Converts one attribute value to a Cedar value. `undefined` means "cannot be
|
|
92
|
+
* represented": non-integer numbers (Cedar `long` is integral), functions,
|
|
93
|
+
* objects, arrays with an unrepresentable element. Callers omit such values —
|
|
94
|
+
* see `buildCedarRequest` for why omission is the safe direction for
|
|
95
|
+
* attributes.
|
|
96
|
+
*/
|
|
97
|
+
function toCedarValue(value) {
|
|
98
|
+
if (typeof value === "string" || typeof value === "boolean")
|
|
99
|
+
return value;
|
|
100
|
+
if (typeof value === "number")
|
|
101
|
+
return Number.isSafeInteger(value) ? value : undefined;
|
|
102
|
+
if (Array.isArray(value)) {
|
|
103
|
+
const out = [];
|
|
104
|
+
for (const item of value) {
|
|
105
|
+
const converted = toCedarValue(item);
|
|
106
|
+
if (converted === undefined)
|
|
107
|
+
return undefined;
|
|
108
|
+
out.push(converted);
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
function buildEntityAttrs(mappings, attrs) {
|
|
115
|
+
const out = {};
|
|
116
|
+
if (mappings === undefined)
|
|
117
|
+
return out;
|
|
118
|
+
for (const [cedarName, mapping] of Object.entries(mappings)) {
|
|
119
|
+
if (typeof mapping === "string") {
|
|
120
|
+
const value = toCedarValue(attrs.get(mapping));
|
|
121
|
+
if (value !== undefined)
|
|
122
|
+
out[cedarName] = value;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const raw = attrs.get(mapping.attribute);
|
|
126
|
+
if (typeof raw === "string" && raw.length > 0) {
|
|
127
|
+
out[cedarName] = { __entity: { type: mapping.entityType, id: raw } };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
function buildParents(mappings, attrs) {
|
|
133
|
+
const parents = [];
|
|
134
|
+
if (mappings === undefined)
|
|
135
|
+
return parents;
|
|
136
|
+
for (const [entityType, attrKey] of Object.entries(mappings)) {
|
|
137
|
+
const raw = attrs.get(attrKey);
|
|
138
|
+
if (raw === undefined)
|
|
139
|
+
continue; // no memberships is a legitimate state
|
|
140
|
+
const ids = typeof raw === "string" ? [raw] : raw;
|
|
141
|
+
if (!Array.isArray(ids) || !ids.every((id) => typeof id === "string")) {
|
|
142
|
+
throw new CedarInputError(`parents attribute "${attrKey}" must be a string or string[], got ${describe(raw)}`);
|
|
143
|
+
}
|
|
144
|
+
for (const id of ids) {
|
|
145
|
+
parents.push({ type: entityType, id });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return parents;
|
|
149
|
+
}
|
|
150
|
+
function buildContext(mappings, attrs) {
|
|
151
|
+
const context = {};
|
|
152
|
+
for (const [cedarKey, attrKey] of Object.entries(mappings)) {
|
|
153
|
+
const value = toCedarValue(attrs.get(attrKey));
|
|
154
|
+
if (value !== undefined)
|
|
155
|
+
context[cedarKey] = value;
|
|
156
|
+
}
|
|
157
|
+
return context;
|
|
158
|
+
}
|
|
159
|
+
// --- boot-time config shape helpers -----------------------------------------
|
|
160
|
+
function section(config, key) {
|
|
161
|
+
const value = config[key];
|
|
162
|
+
if (value === undefined)
|
|
163
|
+
return {};
|
|
164
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
165
|
+
throw new Error(`CedarPolicyRuleCollector: ${key} must be a config object, got ${describe(value)}`);
|
|
166
|
+
}
|
|
167
|
+
return value;
|
|
168
|
+
}
|
|
169
|
+
function optionalString(section, path) {
|
|
170
|
+
const key = path.split(".").pop();
|
|
171
|
+
const value = section[key];
|
|
172
|
+
if (value === undefined)
|
|
173
|
+
return undefined;
|
|
174
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
175
|
+
throw new Error(`CedarPolicyRuleCollector: ${path} must be a non-empty string, got ${describe(value)}`);
|
|
176
|
+
}
|
|
177
|
+
return value;
|
|
178
|
+
}
|
|
179
|
+
function entityMapping(section, path) {
|
|
180
|
+
const attributes = section.attributes;
|
|
181
|
+
const parents = section.parents;
|
|
182
|
+
const resolved = {};
|
|
183
|
+
if (attributes !== undefined) {
|
|
184
|
+
if (typeof attributes !== "object" || attributes === null || Array.isArray(attributes)) {
|
|
185
|
+
throw new Error(`CedarPolicyRuleCollector: ${path}.attributes must be a config object, got ${describe(attributes)}`);
|
|
186
|
+
}
|
|
187
|
+
const out = {};
|
|
188
|
+
for (const [cedarName, mapping] of Object.entries(attributes)) {
|
|
189
|
+
if (typeof mapping === "string" && mapping.length > 0) {
|
|
190
|
+
out[cedarName] = mapping;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (typeof mapping === "object" &&
|
|
194
|
+
mapping !== null &&
|
|
195
|
+
!Array.isArray(mapping) &&
|
|
196
|
+
typeof mapping.attribute === "string" &&
|
|
197
|
+
typeof mapping.entityType === "string") {
|
|
198
|
+
const entry = mapping;
|
|
199
|
+
out[cedarName] = { attribute: entry.attribute, entityType: entry.entityType };
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
throw new Error(`CedarPolicyRuleCollector: ${path}.attributes.${cedarName} must be an attribute key or { attribute, entityType }, got ${describe(mapping)}`);
|
|
203
|
+
}
|
|
204
|
+
resolved.attributes = out;
|
|
205
|
+
}
|
|
206
|
+
if (parents !== undefined) {
|
|
207
|
+
resolved.parents = stringRecord(parents, `${path}.parents`);
|
|
208
|
+
}
|
|
209
|
+
return resolved;
|
|
210
|
+
}
|
|
211
|
+
function stringRecord(value, path) {
|
|
212
|
+
if (value === undefined)
|
|
213
|
+
return {};
|
|
214
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
215
|
+
throw new Error(`CedarPolicyRuleCollector: ${path} must be a config object, got ${describe(value)}`);
|
|
216
|
+
}
|
|
217
|
+
const out = {};
|
|
218
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
219
|
+
if (typeof entry !== "string" || entry.length === 0) {
|
|
220
|
+
throw new Error(`CedarPolicyRuleCollector: ${path}.${key} must be a non-empty attribute key, got ${describe(entry)}`);
|
|
221
|
+
}
|
|
222
|
+
out[key] = entry;
|
|
223
|
+
}
|
|
224
|
+
return out;
|
|
225
|
+
}
|
|
226
|
+
function requiredString(attrs, key, role) {
|
|
227
|
+
const value = attrs.get(key);
|
|
228
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
229
|
+
throw new CedarInputError(`${role} attribute "${key}" must be a non-empty string, got ${describe(value)}`);
|
|
230
|
+
}
|
|
231
|
+
return value;
|
|
232
|
+
}
|
|
233
|
+
function optionalAttrString(attrs, key, role) {
|
|
234
|
+
const value = attrs.get(key);
|
|
235
|
+
if (value === undefined)
|
|
236
|
+
return undefined;
|
|
237
|
+
if (typeof value !== "string") {
|
|
238
|
+
throw new CedarInputError(`${role} attribute "${key}" must be a string, got ${describe(value)}`);
|
|
239
|
+
}
|
|
240
|
+
return value;
|
|
241
|
+
}
|
|
242
|
+
function describe(value) {
|
|
243
|
+
if (value === null)
|
|
244
|
+
return "null";
|
|
245
|
+
if (Array.isArray(value))
|
|
246
|
+
return "array";
|
|
247
|
+
return typeof value;
|
|
248
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Module } from "@o3co/auth.policy-verifier.core";
|
|
2
|
+
/**
|
|
3
|
+
* `Module` that registers the Cedar policy rule collector and the request-facts
|
|
4
|
+
* attribute collector that feeds it. Import and pass to
|
|
5
|
+
* `createApp({ modules: [builtinCollectorsModule, cedarPolicyModule, ...] })`,
|
|
6
|
+
* then reference both in config:
|
|
7
|
+
*
|
|
8
|
+
* ```hocon
|
|
9
|
+
* attribute.collectors = [
|
|
10
|
+
* { collector = "RequestFactsCollector" }
|
|
11
|
+
* ]
|
|
12
|
+
* rule.collectors = [
|
|
13
|
+
* { collector = "CedarPolicyRuleCollector", policyDir = "config/policies" }
|
|
14
|
+
* ]
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Nothing loads unless this module is imported: the WASM evaluator is a
|
|
18
|
+
* dependency of this package, not of core or the server.
|
|
19
|
+
*/
|
|
20
|
+
export declare const cedarPolicyModule: Module;
|
|
21
|
+
//# sourceMappingURL=module.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.d.mts","sourceRoot":"","sources":["../src/module.mts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iCAAiC,CAAC;AAI9D;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,iBAAiB,EAAE,MAY/B,CAAC"}
|
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 1o1 Co. Ltd.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { CedarPolicyRuleCollector } from "./CedarPolicyRuleCollector.mjs";
|
|
4
|
+
import { RequestFactsCollector } from "./RequestFactsCollector.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* `Module` that registers the Cedar policy rule collector and the request-facts
|
|
7
|
+
* attribute collector that feeds it. Import and pass to
|
|
8
|
+
* `createApp({ modules: [builtinCollectorsModule, cedarPolicyModule, ...] })`,
|
|
9
|
+
* then reference both in config:
|
|
10
|
+
*
|
|
11
|
+
* ```hocon
|
|
12
|
+
* attribute.collectors = [
|
|
13
|
+
* { collector = "RequestFactsCollector" }
|
|
14
|
+
* ]
|
|
15
|
+
* rule.collectors = [
|
|
16
|
+
* { collector = "CedarPolicyRuleCollector", policyDir = "config/policies" }
|
|
17
|
+
* ]
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Nothing loads unless this module is imported: the WASM evaluator is a
|
|
21
|
+
* dependency of this package, not of core or the server.
|
|
22
|
+
*/
|
|
23
|
+
export const cedarPolicyModule = {
|
|
24
|
+
name: "cedar-policy",
|
|
25
|
+
async init(context) {
|
|
26
|
+
context.attributeCollectorRegistry.register("RequestFactsCollector", () => new RequestFactsCollector());
|
|
27
|
+
context.ruleCollectorRegistry.register("CedarPolicyRuleCollector", (config) => new CedarPolicyRuleCollector(config));
|
|
28
|
+
},
|
|
29
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Where a policy set came from, for boot-time error messages. */
|
|
2
|
+
export interface PolicySource {
|
|
3
|
+
/** Concatenated Cedar policy text, ready for `preparsePolicySet`. */
|
|
4
|
+
text: string;
|
|
5
|
+
/** Human description of the source (`inline` or the resolved directory). */
|
|
6
|
+
description: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Loads the Cedar policy text a `CedarPolicyRuleCollector` evaluates.
|
|
10
|
+
*
|
|
11
|
+
* Exactly one of `policyDir` / `policies` must be set. `policyDir` is the
|
|
12
|
+
* intended shape: the `*.cedar` files in that directory (sorted by name,
|
|
13
|
+
* concatenated) are byte-identical to what a Cedar agent would load, so the
|
|
14
|
+
* corpus stays lift-and-shift portable and the official `cedar` CLI can
|
|
15
|
+
* validate the same files in CI. `policies` inlines a small set directly in
|
|
16
|
+
* config.
|
|
17
|
+
*
|
|
18
|
+
* Every file is parse-checked individually before the set is accepted, so a
|
|
19
|
+
* syntax error is reported against the file that contains it rather than
|
|
20
|
+
* against an offset into an invisible concatenation. All of this runs at boot,
|
|
21
|
+
* inside the collector factory: a broken policy set refuses to start, it does
|
|
22
|
+
* not serve denials (two-boundary validation — config is checked before the
|
|
23
|
+
* first request, here because file contents cannot be checked by the config
|
|
24
|
+
* schema).
|
|
25
|
+
*
|
|
26
|
+
* A directory with zero `.cedar` files is allowed and yields the empty policy
|
|
27
|
+
* set: that is migration step one — the collector mounted, abstaining on every
|
|
28
|
+
* request, behavior unchanged until the first policy lands.
|
|
29
|
+
*
|
|
30
|
+
* Relative paths resolve against the working directory, matching how the
|
|
31
|
+
* standalone template addresses its `config/` tree.
|
|
32
|
+
*/
|
|
33
|
+
export declare function loadPolicySource(config: {
|
|
34
|
+
policyDir?: unknown;
|
|
35
|
+
policies?: unknown;
|
|
36
|
+
}): PolicySource;
|
|
37
|
+
//# sourceMappingURL=policySource.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policySource.d.mts","sourceRoot":"","sources":["../src/policySource.mts"],"names":[],"mappings":"AAOA,kEAAkE;AAClE,MAAM,WAAW,YAAY;IAC5B,qEAAqE;IACrE,IAAI,EAAE,MAAM,CAAC;IACb,4EAA4E;IAC5E,WAAW,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE;IACxC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB,GAAG,YAAY,CAkDf"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 1o1 Co. Ltd.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
import { checkParsePolicySet } from "@cedar-policy/cedar-wasm/nodejs";
|
|
6
|
+
/**
|
|
7
|
+
* Loads the Cedar policy text a `CedarPolicyRuleCollector` evaluates.
|
|
8
|
+
*
|
|
9
|
+
* Exactly one of `policyDir` / `policies` must be set. `policyDir` is the
|
|
10
|
+
* intended shape: the `*.cedar` files in that directory (sorted by name,
|
|
11
|
+
* concatenated) are byte-identical to what a Cedar agent would load, so the
|
|
12
|
+
* corpus stays lift-and-shift portable and the official `cedar` CLI can
|
|
13
|
+
* validate the same files in CI. `policies` inlines a small set directly in
|
|
14
|
+
* config.
|
|
15
|
+
*
|
|
16
|
+
* Every file is parse-checked individually before the set is accepted, so a
|
|
17
|
+
* syntax error is reported against the file that contains it rather than
|
|
18
|
+
* against an offset into an invisible concatenation. All of this runs at boot,
|
|
19
|
+
* inside the collector factory: a broken policy set refuses to start, it does
|
|
20
|
+
* not serve denials (two-boundary validation — config is checked before the
|
|
21
|
+
* first request, here because file contents cannot be checked by the config
|
|
22
|
+
* schema).
|
|
23
|
+
*
|
|
24
|
+
* A directory with zero `.cedar` files is allowed and yields the empty policy
|
|
25
|
+
* set: that is migration step one — the collector mounted, abstaining on every
|
|
26
|
+
* request, behavior unchanged until the first policy lands.
|
|
27
|
+
*
|
|
28
|
+
* Relative paths resolve against the working directory, matching how the
|
|
29
|
+
* standalone template addresses its `config/` tree.
|
|
30
|
+
*/
|
|
31
|
+
export function loadPolicySource(config) {
|
|
32
|
+
const { policyDir, policies } = config;
|
|
33
|
+
if (policyDir !== undefined && policies !== undefined) {
|
|
34
|
+
throw new Error("CedarPolicyRuleCollector: policyDir and policies are mutually exclusive — configure one");
|
|
35
|
+
}
|
|
36
|
+
if (policies !== undefined) {
|
|
37
|
+
if (typeof policies !== "string") {
|
|
38
|
+
throw new Error(`CedarPolicyRuleCollector: policies must be a string, got ${typeof policies}`);
|
|
39
|
+
}
|
|
40
|
+
assertParses(policies, "policies (inline)");
|
|
41
|
+
return { text: policies, description: "inline policies" };
|
|
42
|
+
}
|
|
43
|
+
if (policyDir === undefined) {
|
|
44
|
+
throw new Error("CedarPolicyRuleCollector: one of policyDir or policies is required");
|
|
45
|
+
}
|
|
46
|
+
if (typeof policyDir !== "string" || policyDir.length === 0) {
|
|
47
|
+
throw new Error(`CedarPolicyRuleCollector: policyDir must be a non-empty string, got ${typeof policyDir}`);
|
|
48
|
+
}
|
|
49
|
+
const dir = resolve(policyDir);
|
|
50
|
+
let names;
|
|
51
|
+
try {
|
|
52
|
+
names = readdirSync(dir);
|
|
53
|
+
}
|
|
54
|
+
catch (cause) {
|
|
55
|
+
throw new Error(`CedarPolicyRuleCollector: cannot read policyDir "${dir}": ${message(cause)}`);
|
|
56
|
+
}
|
|
57
|
+
const files = names.filter((name) => name.endsWith(".cedar")).sort();
|
|
58
|
+
const parts = [];
|
|
59
|
+
for (const name of files) {
|
|
60
|
+
const path = resolve(dir, name);
|
|
61
|
+
let text;
|
|
62
|
+
try {
|
|
63
|
+
text = readFileSync(path, "utf8");
|
|
64
|
+
}
|
|
65
|
+
catch (cause) {
|
|
66
|
+
throw new Error(`CedarPolicyRuleCollector: cannot read "${path}": ${message(cause)}`);
|
|
67
|
+
}
|
|
68
|
+
assertParses(text, path);
|
|
69
|
+
parts.push(text);
|
|
70
|
+
}
|
|
71
|
+
return { text: parts.join("\n"), description: dir };
|
|
72
|
+
}
|
|
73
|
+
/** Parse-checks one policy text, naming its source on failure. */
|
|
74
|
+
function assertParses(text, source) {
|
|
75
|
+
const answer = checkParsePolicySet({ staticPolicies: text });
|
|
76
|
+
if (answer.type === "failure") {
|
|
77
|
+
const details = answer.errors.map((error) => error.message).join("; ");
|
|
78
|
+
throw new Error(`CedarPolicyRuleCollector: ${source} failed to parse: ${details}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function message(cause) {
|
|
82
|
+
return cause instanceof Error ? cause.message : String(cause);
|
|
83
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@o3co/auth.policy-verifier.cedar",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22.0.0"
|
|
8
|
+
},
|
|
9
|
+
"main": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"types": "./dist/index.d.mts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/o3co/auth.policy-verifier.git",
|
|
25
|
+
"directory": "packages/cedar"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@cedar-policy/cedar-wasm": "4.12.0",
|
|
29
|
+
"@o3co/auth.policy-verifier.core": "0.6.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "^26.2.0",
|
|
33
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
34
|
+
"vitest": "^4.1.10"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"prebuild": "rimraf dist",
|
|
38
|
+
"build": "tsc",
|
|
39
|
+
"typecheck": "tsc --noEmit -p tsconfig.typecheck.json",
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"test:coverage": "vitest run --coverage"
|
|
42
|
+
}
|
|
43
|
+
}
|