@intentius/chant-lexicon-cedar 0.44.9 → 0.44.12
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/README.md +48 -2
- package/dist/agentcore/embed.d.ts +189 -0
- package/dist/agentcore/embed.d.ts.map +1 -0
- package/dist/agentcore/enforcement.d.ts +76 -0
- package/dist/agentcore/enforcement.d.ts.map +1 -0
- package/dist/agentcore/scan.d.ts +46 -0
- package/dist/agentcore/scan.d.ts.map +1 -0
- package/dist/avp/client.d.ts +85 -8
- package/dist/avp/client.d.ts.map +1 -1
- package/dist/codegen/docs-dogwood.d.ts +21 -0
- package/dist/codegen/docs-dogwood.d.ts.map +1 -0
- package/dist/codegen/docs.d.ts.map +1 -1
- package/dist/dogwood/cli.d.ts +41 -0
- package/dist/dogwood/cli.d.ts.map +1 -1
- package/dist/dogwood/index.d.ts +10 -4
- package/dist/dogwood/index.d.ts.map +1 -1
- package/dist/dogwood/replay-activity.d.ts +196 -0
- package/dist/dogwood/replay-activity.d.ts.map +1 -0
- package/dist/dogwood/replay-op.d.ts +165 -0
- package/dist/dogwood/replay-op.d.ts.map +1 -0
- package/dist/dogwood/serialize.d.ts +20 -0
- package/dist/dogwood/serialize.d.ts.map +1 -1
- package/dist/dogwood/trace.d.ts +215 -0
- package/dist/dogwood/trace.d.ts.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/integrity.json +5 -3
- package/dist/lint/audit-catalog.d.ts.map +1 -1
- package/dist/lint/post-synth/dwdc013.d.ts +33 -0
- package/dist/lint/post-synth/dwdc013.d.ts.map +1 -0
- package/dist/lint/post-synth/index.d.ts.map +1 -1
- package/dist/manifest.json +1 -1
- package/dist/okf/index.md +1 -0
- package/dist/okf/rules/DWDC013.md +15 -0
- package/dist/okf/types/Policy.md +1 -0
- package/dist/op/activities/index.d.ts +18 -0
- package/dist/op/activities/index.d.ts.map +1 -0
- package/dist/plugin.d.ts.map +1 -1
- package/dist/rules/dwdc013.ts +64 -0
- package/dist/skills/chant-cedar-dogwood.md +327 -0
- package/package.json +7 -2
- package/src/agentcore/embed.test.ts +254 -0
- package/src/agentcore/embed.ts +399 -0
- package/src/agentcore/enforcement.test.ts +43 -0
- package/src/agentcore/enforcement.ts +92 -0
- package/src/agentcore/scan.ts +119 -0
- package/src/avp/OWNERSHIP.md +38 -0
- package/src/avp/client.test.ts +271 -0
- package/src/avp/client.ts +150 -16
- package/src/codegen/docs-dogwood.ts +1119 -0
- package/src/codegen/docs.ts +66 -1
- package/src/dogwood/cli.test.ts +122 -1
- package/src/dogwood/cli.ts +122 -1
- package/src/dogwood/index.ts +74 -1
- package/src/dogwood/replay-activity.test.ts +481 -0
- package/src/dogwood/replay-activity.ts +506 -0
- package/src/dogwood/replay-op.ts +242 -0
- package/src/dogwood/serialize.ts +37 -0
- package/src/dogwood/trace.test.ts +231 -0
- package/src/dogwood/trace.ts +471 -0
- package/src/index.ts +52 -0
- package/src/lint/audit-catalog.ts +8 -0
- package/src/lint/post-synth/dwd-post-synth.test.ts +119 -1
- package/src/lint/post-synth/dwdc013.ts +64 -0
- package/src/lint/post-synth/dwde-post-synth.test.ts +1 -1
- package/src/lint/post-synth/index.ts +2 -0
- package/src/op/activities/index.ts +27 -0
- package/src/plugin.test.ts +3 -2
- package/src/plugin.ts +30 -0
- package/src/skills/chant-cedar-dogwood.md +327 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed embedding of a cedar or dogwood policy into
|
|
3
|
+
* `AWS::BedrockAgentCore::Policy` (#1660).
|
|
4
|
+
*
|
|
5
|
+
* The aws lexicon keeps the deployment vehicle, exactly as it does for AVP
|
|
6
|
+
* (#1652, `../avp/embed.ts`). What differs is the shape of the vehicle:
|
|
7
|
+
* AgentCore's `Definition` is a two-arm `oneOf`, not the single `Static` arm
|
|
8
|
+
* `AWS::VerifiedPermissions::Policy` has —
|
|
9
|
+
*
|
|
10
|
+
* ```jsonc
|
|
11
|
+
* "PolicyDefinition": {
|
|
12
|
+
* "oneOf": [{ "required": ["Cedar"] }, { "required": ["Policy"] }]
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* — where `Cedar.Statement` is plain Cedar and `Policy.Statement` is the
|
|
17
|
+
* language-agnostic arm. That second arm is what makes AgentCore the
|
|
18
|
+
* deployment target for the dogwood dialect: a `.dw` policy is a Cedar policy
|
|
19
|
+
* with `when temporal { … }` clauses, which the `Cedar` arm has no business
|
|
20
|
+
* accepting, and the `Policy` arm exists precisely so a policy engine can be
|
|
21
|
+
* handed something that is not plain Cedar.
|
|
22
|
+
*
|
|
23
|
+
* A project that has both lexicons installed writes
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* new BedrockAgentCorePolicy({
|
|
27
|
+
* PolicyEngineId: engine.ref(),
|
|
28
|
+
* Name: "writeNeedsApproval",
|
|
29
|
+
* ...agentCoreStagedPolicy("writeNeedsApproval", writeNeedsApproval, "log-only"),
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* and the statement is the same text `chant build` writes to `policies.dw`,
|
|
34
|
+
* rendered by the same renderer, with `@id` intact.
|
|
35
|
+
*
|
|
36
|
+
* ### Why no dependency, and why the example uses plain objects
|
|
37
|
+
*
|
|
38
|
+
* Same rule as `../avp/embed.ts` and for the same reason: a cedar → aws
|
|
39
|
+
* dependency would invert the epic's decision that Cedar is vendor-neutral and
|
|
40
|
+
* make the cedar lexicon unbuildable without the aws one. Nothing here imports
|
|
41
|
+
* `@intentius/chant-lexicon-aws`. The seam is the data shape, which is stable
|
|
42
|
+
* CloudFormation, and the prop names below are the generated class's own
|
|
43
|
+
* (`PolicyEngineId`, `Name`, `Definition`, `EnforcementMode`, `Description`),
|
|
44
|
+
* so substituting the real class is a one-line edit. `examples/agentcore-policy/`
|
|
45
|
+
* demonstrates the pairing with a plain-object stand-in, because the shipped
|
|
46
|
+
* cedar examples build against the cedar serializer alone and an `AWS::*` entity
|
|
47
|
+
* declared in that tree would have no serializer to emit it.
|
|
48
|
+
*
|
|
49
|
+
* ### What this module refuses
|
|
50
|
+
*
|
|
51
|
+
* Templates. `AWS::VerifiedPermissions::Policy` has a `TemplateLinked` arm;
|
|
52
|
+
* AgentCore's `Definition` union does not, so a statement carrying a
|
|
53
|
+
* `?principal` or `?resource` slot has nowhere to go and would be rejected at
|
|
54
|
+
* deploy time with an error about the policy text rather than about the slot.
|
|
55
|
+
* {@link agentCoreStatement} throws instead, at authoring time, naming the
|
|
56
|
+
* declaration.
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
import { isDeclarable, type Declarable } from "@intentius/chant/declarable";
|
|
60
|
+
import {
|
|
61
|
+
cedarPolicyRecords,
|
|
62
|
+
getProps,
|
|
63
|
+
renderPolicyText,
|
|
64
|
+
resolvePolicyId,
|
|
65
|
+
CEDAR_POLICY_TYPE,
|
|
66
|
+
type CedarPolicyProps,
|
|
67
|
+
} from "../serializer";
|
|
68
|
+
import { dogwoodPolicyRecords, renderTemporalPolicyText } from "../dogwood/serialize";
|
|
69
|
+
import { DOGWOOD_POLICY_TYPE, type TemporalPolicyProps } from "../dogwood/policy";
|
|
70
|
+
import { enforcementMode, type AgentCoreEnforcementMode, type AgentCoreStage } from "./enforcement";
|
|
71
|
+
|
|
72
|
+
// ── The CloudFormation shapes ─────────────────────────────────────
|
|
73
|
+
|
|
74
|
+
/** `Definition.Cedar` — the plain-Cedar arm of the definition union. */
|
|
75
|
+
export interface AgentCoreCedarDefinition {
|
|
76
|
+
Statement: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** `Definition.Policy` — the language-agnostic arm, which carries `.dw` text. */
|
|
80
|
+
export interface AgentCoreLanguageDefinition {
|
|
81
|
+
Statement: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The `Definition` property of `AWS::BedrockAgentCore::Policy`.
|
|
86
|
+
*
|
|
87
|
+
* A union rather than a record with two optional keys, because upstream's
|
|
88
|
+
* schema is a `oneOf`: a definition carrying both arms is rejected, and a type
|
|
89
|
+
* that can express it is a type that lets a caller build one.
|
|
90
|
+
*/
|
|
91
|
+
export type AgentCorePolicyDefinition =
|
|
92
|
+
| { Cedar: AgentCoreCedarDefinition; Policy?: never }
|
|
93
|
+
| { Policy: AgentCoreLanguageDefinition; Cedar?: never };
|
|
94
|
+
|
|
95
|
+
/** The props of `AWS::BedrockAgentCore::Policy` this module can fill. */
|
|
96
|
+
export interface AgentCorePolicyResource {
|
|
97
|
+
PolicyEngineId: string;
|
|
98
|
+
Name: string;
|
|
99
|
+
Definition: AgentCorePolicyDefinition;
|
|
100
|
+
EnforcementMode?: AgentCoreEnforcementMode;
|
|
101
|
+
Description?: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* A policy paired with its rollout stage — everything but the engine id.
|
|
106
|
+
*
|
|
107
|
+
* Spreadable into the generated class, which is the point: the caller supplies
|
|
108
|
+
* `PolicyEngineId` (an AttrRef this module has no type for) and this supplies
|
|
109
|
+
* the three props that come from the declaration.
|
|
110
|
+
*/
|
|
111
|
+
export interface AgentCoreStagedPolicy {
|
|
112
|
+
Name: string;
|
|
113
|
+
Definition: AgentCorePolicyDefinition;
|
|
114
|
+
EnforcementMode: AgentCoreEnforcementMode;
|
|
115
|
+
Description?: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ── Upstream's own limits ─────────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
/** `Statement` minLength, on both arms of the union. */
|
|
121
|
+
export const AGENTCORE_STATEMENT_MIN = 35;
|
|
122
|
+
|
|
123
|
+
/** `Statement` maxLength, on both arms of the union. */
|
|
124
|
+
export const AGENTCORE_STATEMENT_MAX = 10000;
|
|
125
|
+
|
|
126
|
+
/** `Name` — an identifier, and create-only, so a rename replaces the policy. */
|
|
127
|
+
export const AGENTCORE_NAME_PATTERN = /^[A-Za-z][A-Za-z0-9_]*$/;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* `?principal` / `?resource` — Cedar's two template slots.
|
|
131
|
+
*
|
|
132
|
+
* Scanned over the whole statement rather than only the scope positions, which
|
|
133
|
+
* is where a slot can legally appear. Inside a `temporal { … }` clause the `?`
|
|
134
|
+
* sigil means a macro parameter, so in principle a macro parameter named
|
|
135
|
+
* `?principal` would read as a slot here — but macro parameters are legal only
|
|
136
|
+
* inside a macro *definition* body, and this module renders policies, never
|
|
137
|
+
* definitions. There is nowhere in a policy statement for the pattern to mean
|
|
138
|
+
* anything else.
|
|
139
|
+
*/
|
|
140
|
+
const TEMPLATE_SLOT = /\?(?:principal|resource)\b/;
|
|
141
|
+
|
|
142
|
+
// ── Options ───────────────────────────────────────────────────────
|
|
143
|
+
|
|
144
|
+
/** What a caller can hand the embedding: one policy, or a whole build's worth. */
|
|
145
|
+
export type AgentCorePolicySource =
|
|
146
|
+
| Declarable
|
|
147
|
+
| CedarPolicyProps
|
|
148
|
+
| TemporalPolicyProps
|
|
149
|
+
| Record<string, unknown>
|
|
150
|
+
| Map<string, Declarable>;
|
|
151
|
+
|
|
152
|
+
export interface AgentCoreEmbedOptions {
|
|
153
|
+
/** Override the policy id. Defaults to the serializer's own derivation. */
|
|
154
|
+
policyId?: string;
|
|
155
|
+
/**
|
|
156
|
+
* Force an arm of the `Definition` union.
|
|
157
|
+
*
|
|
158
|
+
* The default reads the policy: temporal clauses take the `Policy` arm, plain
|
|
159
|
+
* Cedar takes the `Cedar` arm. Forcing `"dogwood"` on plain Cedar is
|
|
160
|
+
* legitimate — dogwood embeds Cedar, so the `Policy` arm accepts it, and a
|
|
161
|
+
* policy set that will grow temporal clauses need not change arms later.
|
|
162
|
+
* Forcing `"cedar"` on temporal text is not, and throws.
|
|
163
|
+
*/
|
|
164
|
+
language?: "cedar" | "dogwood";
|
|
165
|
+
/** `Description` on the resource. Free prose; AgentCore does nothing with it. */
|
|
166
|
+
description?: string;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ── Rendering ─────────────────────────────────────────────────────
|
|
170
|
+
|
|
171
|
+
/** The dialect keys `TemporalPolicyProps` adds to `CedarPolicyProps`. */
|
|
172
|
+
const DOGWOOD_ONLY_PROPS = ["whenTemporal", "unlessTemporal", "whenGuardrails", "unlessGuardrails"] as const;
|
|
173
|
+
|
|
174
|
+
function hasTemporalProps(props: Record<string, unknown>): boolean {
|
|
175
|
+
return DOGWOOD_ONLY_PROPS.some((key) => {
|
|
176
|
+
const value = props[key];
|
|
177
|
+
return Array.isArray(value) ? value.length > 0 : value !== undefined;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** One rendered statement, and which arm it belongs in. */
|
|
182
|
+
interface RenderedStatement {
|
|
183
|
+
text: string;
|
|
184
|
+
/** True when the text is `.dw` rather than plain Cedar. */
|
|
185
|
+
temporal: boolean;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function renderRecord(name: string, props: Record<string, unknown>, options: AgentCoreEmbedOptions): RenderedStatement {
|
|
189
|
+
const temporal = hasTemporalProps(props);
|
|
190
|
+
const id = options.policyId ?? resolvePolicyId(name, props);
|
|
191
|
+
return { text: temporal ? renderTemporalPolicyText(id, props) : renderPolicyText(id, props), temporal };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Every policy in a build, as one statement.
|
|
196
|
+
*
|
|
197
|
+
* Cedar policies first, then temporal ones, both in declaration order, so the
|
|
198
|
+
* text is stable across builds. Any temporal policy in the set puts the whole
|
|
199
|
+
* statement in the `Policy` arm — which is correct rather than a compromise: a
|
|
200
|
+
* `.dw` file holds plain Cedar policies too, and splitting the set across two
|
|
201
|
+
* AgentCore policies to keep one of them in the `Cedar` arm would give the two
|
|
202
|
+
* halves separate `EnforcementMode` dials for no reason.
|
|
203
|
+
*/
|
|
204
|
+
function renderPolicySet(entities: Map<string, Declarable>): RenderedStatement {
|
|
205
|
+
const parts: string[] = [];
|
|
206
|
+
for (const { id, props } of cedarPolicyRecords(entities)) parts.push(renderPolicyText(id, props));
|
|
207
|
+
|
|
208
|
+
const temporalRecords = dogwoodPolicyRecords(entities);
|
|
209
|
+
for (const { id, props } of temporalRecords) parts.push(renderTemporalPolicyText(id, props));
|
|
210
|
+
|
|
211
|
+
return { text: parts.join("\n\n"), temporal: temporalRecords.length > 0 };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function render(
|
|
215
|
+
name: string,
|
|
216
|
+
source: AgentCorePolicySource,
|
|
217
|
+
options: AgentCoreEmbedOptions,
|
|
218
|
+
): RenderedStatement {
|
|
219
|
+
if (source instanceof Map) return renderPolicySet(source);
|
|
220
|
+
|
|
221
|
+
if (isDeclarable(source)) {
|
|
222
|
+
if (source.entityType !== CEDAR_POLICY_TYPE && source.entityType !== DOGWOOD_POLICY_TYPE) {
|
|
223
|
+
throw new Error(
|
|
224
|
+
`cedar: "${name}" is a ${source.entityType}, which is not a policy — AWS::BedrockAgentCore::Policy embeds ${CEDAR_POLICY_TYPE} or ${DOGWOOD_POLICY_TYPE}.`,
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
// Read straight off the entity: no reference walk, because a single
|
|
228
|
+
// Declarable carries no map of the names its siblings were declared under.
|
|
229
|
+
// A scope that names another declared entity needs the policy-set form,
|
|
230
|
+
// which walks references the way the serializer does.
|
|
231
|
+
const props = getProps(source);
|
|
232
|
+
const temporal = source.entityType === DOGWOOD_POLICY_TYPE || hasTemporalProps(props);
|
|
233
|
+
const id = options.policyId ?? resolvePolicyId(name, props);
|
|
234
|
+
return { text: temporal ? renderTemporalPolicyText(id, props) : renderPolicyText(id, props), temporal };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return renderRecord(name, source as Record<string, unknown>, options);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function assertEmbeddable(name: string, statement: RenderedStatement): void {
|
|
241
|
+
if (statement.text.length === 0) {
|
|
242
|
+
throw new Error(`cedar: "${name}" rendered no policy text, so there is nothing for AgentCore to evaluate.`);
|
|
243
|
+
}
|
|
244
|
+
if (TEMPLATE_SLOT.test(statement.text)) {
|
|
245
|
+
throw new Error(
|
|
246
|
+
`cedar: the statement for "${name}" carries a Cedar template slot — AWS::BedrockAgentCore::Policy takes a static statement, and its Definition union has no template-linked arm. Write the ?principal/?resource slot as a concrete entity, or deploy the template through AWS::VerifiedPermissions::Policy instead.`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
if (statement.text.length < AGENTCORE_STATEMENT_MIN) {
|
|
250
|
+
throw new Error(
|
|
251
|
+
`cedar: the statement for "${name}" is ${statement.text.length} characters and AgentCore's minimum is ${AGENTCORE_STATEMENT_MIN}.`,
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
if (statement.text.length > AGENTCORE_STATEMENT_MAX) {
|
|
255
|
+
throw new Error(
|
|
256
|
+
`cedar: the statement for "${name}" is ${statement.text.length} characters and AgentCore's maximum is ${AGENTCORE_STATEMENT_MAX} — split it across policies, each with its own EnforcementMode.`,
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// ── The seam ──────────────────────────────────────────────────────
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The policy text for one AgentCore policy — the exact string a `Statement`
|
|
265
|
+
* wants, on whichever arm it lands.
|
|
266
|
+
*
|
|
267
|
+
* `name` is the chant entity name; the `@id` annotation the statement carries
|
|
268
|
+
* is derived from it the same way the serializer derives it, so the statement
|
|
269
|
+
* in the policy engine can be matched back to the declaration that produced it.
|
|
270
|
+
*/
|
|
271
|
+
export function agentCoreStatement(
|
|
272
|
+
name: string,
|
|
273
|
+
source: AgentCorePolicySource,
|
|
274
|
+
options: AgentCoreEmbedOptions = {},
|
|
275
|
+
): string {
|
|
276
|
+
const statement = render(name, source, options);
|
|
277
|
+
assertEmbeddable(name, statement);
|
|
278
|
+
return statement.text;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The `Definition` property of `AWS::BedrockAgentCore::Policy`.
|
|
283
|
+
*
|
|
284
|
+
* The arm is chosen by what the policy is: a `Dogwood::TemporalPolicy`, or any
|
|
285
|
+
* props carrying a temporal clause, lands in `Definition.Policy`; plain Cedar
|
|
286
|
+
* lands in `Definition.Cedar`. `options.language` overrides in the one
|
|
287
|
+
* direction that is safe (see {@link AgentCoreEmbedOptions.language}).
|
|
288
|
+
*/
|
|
289
|
+
export function agentCorePolicyDefinition(
|
|
290
|
+
name: string,
|
|
291
|
+
source: AgentCorePolicySource,
|
|
292
|
+
options: AgentCoreEmbedOptions = {},
|
|
293
|
+
): AgentCorePolicyDefinition {
|
|
294
|
+
const statement = render(name, source, options);
|
|
295
|
+
assertEmbeddable(name, statement);
|
|
296
|
+
|
|
297
|
+
if (options.language === "cedar" && statement.temporal) {
|
|
298
|
+
throw new Error(
|
|
299
|
+
`cedar: "${name}" has temporal clauses, so it cannot go in Definition.Cedar — that arm is plain Cedar, and AgentCore's Cedar parser has no \`when temporal\` rule. Drop \`language: "cedar"\` and it lands in Definition.Policy.`,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const dogwood = options.language === "dogwood" || (options.language === undefined && statement.temporal);
|
|
304
|
+
return dogwood ? { Policy: { Statement: statement.text } } : { Cedar: { Statement: statement.text } };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* A policy and its rollout stage, ready to spread into the generated class.
|
|
309
|
+
*
|
|
310
|
+
* The staged-rollout pattern in one call, which is the point (#1660, and the
|
|
311
|
+
* loomster#171 showcase seam): shipping log-only and promoting to enforcing is
|
|
312
|
+
* a one-token diff, not a hand-edited `EnforcementMode` string somewhere else
|
|
313
|
+
* in the template. See ./enforcement.ts for why observe-before-enforce is the
|
|
314
|
+
* default reading of a temporal policy rather than an optional nicety.
|
|
315
|
+
*/
|
|
316
|
+
export function agentCoreStagedPolicy(
|
|
317
|
+
name: string,
|
|
318
|
+
source: AgentCorePolicySource,
|
|
319
|
+
stage: AgentCoreStage,
|
|
320
|
+
options: AgentCoreEmbedOptions = {},
|
|
321
|
+
): AgentCoreStagedPolicy {
|
|
322
|
+
return {
|
|
323
|
+
Name: agentCorePolicyName(name),
|
|
324
|
+
Definition: agentCorePolicyDefinition(name, source, options),
|
|
325
|
+
EnforcementMode: enforcementMode(stage),
|
|
326
|
+
...(options.description ? { Description: options.description } : {}),
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Every required prop of `AWS::BedrockAgentCore::Policy`, plus the stage.
|
|
332
|
+
*
|
|
333
|
+
* `PolicyEngineId` is a string here rather than an AttrRef because this module
|
|
334
|
+
* does not know the aws lexicon's reference types. A project passes
|
|
335
|
+
* `engine.ref()` in place of the literal and TypeScript is satisfied by the
|
|
336
|
+
* generated class's own prop type, not by this one.
|
|
337
|
+
*/
|
|
338
|
+
export function agentCorePolicyResource(
|
|
339
|
+
name: string,
|
|
340
|
+
source: AgentCorePolicySource,
|
|
341
|
+
policyEngineId: string,
|
|
342
|
+
options: AgentCoreEmbedOptions & { stage?: AgentCoreStage } = {},
|
|
343
|
+
): AgentCorePolicyResource {
|
|
344
|
+
const { stage, ...embed } = options;
|
|
345
|
+
return {
|
|
346
|
+
PolicyEngineId: policyEngineId,
|
|
347
|
+
Name: agentCorePolicyName(name),
|
|
348
|
+
Definition: agentCorePolicyDefinition(name, source, embed),
|
|
349
|
+
...(stage ? { EnforcementMode: enforcementMode(stage) } : {}),
|
|
350
|
+
...(embed.description ? { Description: embed.description } : {}),
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Every policy in a build, as one AgentCore `Definition` each, keyed by chant
|
|
356
|
+
* entity name.
|
|
357
|
+
*
|
|
358
|
+
* The whole-set form, and the one that walks references between declared
|
|
359
|
+
* entities — that is what `cedarPolicyRecords`/`dogwoodPolicyRecords` do. One
|
|
360
|
+
* definition per policy rather than one merged statement, because
|
|
361
|
+
* `EnforcementMode` is a per-policy dial and merging would throw it away; hand
|
|
362
|
+
* the map to {@link agentCorePolicyDefinition} instead when a single AgentCore
|
|
363
|
+
* policy really should carry the whole set.
|
|
364
|
+
*/
|
|
365
|
+
export function agentCorePolicySet(
|
|
366
|
+
entities: Map<string, Declarable>,
|
|
367
|
+
options: AgentCoreEmbedOptions = {},
|
|
368
|
+
): Record<string, AgentCorePolicyDefinition> {
|
|
369
|
+
const out: Record<string, AgentCorePolicyDefinition> = {};
|
|
370
|
+
|
|
371
|
+
for (const { name, id, props } of cedarPolicyRecords(entities)) {
|
|
372
|
+
out[name] = agentCorePolicyDefinition(name, props, { ...options, policyId: id });
|
|
373
|
+
}
|
|
374
|
+
for (const { name, id, props } of dogwoodPolicyRecords(entities)) {
|
|
375
|
+
out[name] = agentCorePolicyDefinition(name, props, { ...options, policyId: id, language: "dogwood" });
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* The chant entity name as an AgentCore `Name`.
|
|
383
|
+
*
|
|
384
|
+
* `Name` is create-only and matches `^[A-Za-z][A-Za-z0-9_]*$` — no hyphens,
|
|
385
|
+
* which is why it is not the kebab-case policy id the `@id` annotation carries.
|
|
386
|
+
* Checked rather than coerced: silently rewriting a name that is create-only
|
|
387
|
+
* would mean a later rename replaces the policy without anyone having asked.
|
|
388
|
+
*/
|
|
389
|
+
export function agentCorePolicyName(name: string): string {
|
|
390
|
+
if (!AGENTCORE_NAME_PATTERN.test(name)) {
|
|
391
|
+
throw new Error(
|
|
392
|
+
`cedar: "${name}" is not a legal AWS::BedrockAgentCore::Policy Name — it must match ${AGENTCORE_NAME_PATTERN.source} (letters, digits and underscores, starting with a letter).`,
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
if (name.length > 48) {
|
|
396
|
+
throw new Error(`cedar: the AgentCore policy Name "${name}" is ${name.length} characters and the maximum is 48.`);
|
|
397
|
+
}
|
|
398
|
+
return name;
|
|
399
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
AGENTCORE_ENFORCEMENT,
|
|
4
|
+
describeStage,
|
|
5
|
+
enforcementMode,
|
|
6
|
+
enforcementStage,
|
|
7
|
+
isEnforcing,
|
|
8
|
+
} from "./enforcement";
|
|
9
|
+
|
|
10
|
+
describe("EnforcementMode", () => {
|
|
11
|
+
it("carries the two wire values AWS::BedrockAgentCore::Policy declares", () => {
|
|
12
|
+
// The CloudFormation enum, verbatim: ["ACTIVE", "LOG_ONLY"], default ACTIVE.
|
|
13
|
+
expect(Object.values(AGENTCORE_ENFORCEMENT).sort()).toEqual(["ACTIVE", "LOG_ONLY"]);
|
|
14
|
+
expect(AGENTCORE_ENFORCEMENT.logOnly).toBe("LOG_ONLY");
|
|
15
|
+
expect(AGENTCORE_ENFORCEMENT.enforce).toBe("ACTIVE");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("maps a stage onto the wire value", () => {
|
|
19
|
+
expect(enforcementMode("log-only")).toBe("LOG_ONLY");
|
|
20
|
+
expect(enforcementMode("enforce")).toBe("ACTIVE");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("round-trips a stage through the wire value", () => {
|
|
24
|
+
for (const stage of ["log-only", "enforce"] as const) {
|
|
25
|
+
expect(enforcementStage(enforcementMode(stage))).toBe(stage);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("reads an absent EnforcementMode as enforcing, which is AWS's default", () => {
|
|
30
|
+
expect(enforcementStage(undefined)).toBe("enforce");
|
|
31
|
+
expect(isEnforcing(undefined)).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("says which stage reaches the gateway", () => {
|
|
35
|
+
expect(isEnforcing("ACTIVE")).toBe(true);
|
|
36
|
+
expect(isEnforcing("LOG_ONLY")).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("explains a stage the same way everywhere", () => {
|
|
40
|
+
expect(describeStage("log-only")).toMatch(/logged and do not affect the response/);
|
|
41
|
+
expect(describeStage("enforce")).toMatch(/reaches the gateway/);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `EnforcementMode` — the dial that makes a policy rollout observable before it
|
|
3
|
+
* is binding (#1660).
|
|
4
|
+
*
|
|
5
|
+
* `AWS::BedrockAgentCore::Policy` carries an optional `EnforcementMode` with
|
|
6
|
+
* exactly two values, and the CloudFormation schema says what they mean:
|
|
7
|
+
*
|
|
8
|
+
* > Whether the policy contributes to the enforce decision returned to Gateway.
|
|
9
|
+
* > LOG_ONLY policies are still evaluated but their decisions are observed
|
|
10
|
+
* > only, allowing customers to validate a policy against real traffic before
|
|
11
|
+
* > promoting it.
|
|
12
|
+
*
|
|
13
|
+
* That is observe-before-enforce, in the substrate, per policy — the same shape
|
|
14
|
+
* chant already has at the ops layer, and the reason epic #1646 picked
|
|
15
|
+
* AgentCore as the deployment target for the temporal dialect rather than
|
|
16
|
+
* inventing a staging mechanism. A temporal policy is the case that needs it
|
|
17
|
+
* most: `formerly within 24h …` cannot be reasoned about from the source alone,
|
|
18
|
+
* because whether it fires depends on traffic nobody has replayed yet.
|
|
19
|
+
*
|
|
20
|
+
* ### The two names, and why chant does not reuse AWS's
|
|
21
|
+
*
|
|
22
|
+
* The wire values are `LOG_ONLY` and `ACTIVE`. `ACTIVE` is a poor name for
|
|
23
|
+
* "enforcing" — it reads as "not disabled", and `LOG_ONLY` is active too; it is
|
|
24
|
+
* evaluated on every request. So the authoring vocabulary here is
|
|
25
|
+
* `"log-only"` / `"enforce"`, and {@link enforcementMode} is the one place the
|
|
26
|
+
* translation happens. The wire values are exported as well, for a caller that
|
|
27
|
+
* has one in hand from a live read.
|
|
28
|
+
*
|
|
29
|
+
* ### The showcase seam (loomster#171)
|
|
30
|
+
*
|
|
31
|
+
* loomster#171 wants a staged policy rollout it can demonstrate end to end:
|
|
32
|
+
* ship the policy log-only, watch what it would have denied, promote it. This
|
|
33
|
+
* module is that seam. The whole rollout is one argument —
|
|
34
|
+
* `agentCoreStagedPolicy(name, policy, stage)` — so the promotion is a
|
|
35
|
+
* one-token diff a reviewer can see, rather than a hand-edited string in a
|
|
36
|
+
* CloudFormation resource. Anything more elaborate (which environments are
|
|
37
|
+
* promoted, who decides) belongs to the caller's own config; this module's job
|
|
38
|
+
* is to make the two states nameable and the transition legible.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The wire values `EnforcementMode` takes, keyed by the stage they implement.
|
|
43
|
+
*
|
|
44
|
+
* A typed constant pair rather than a bare union so a caller reads
|
|
45
|
+
* `AGENTCORE_ENFORCEMENT.logOnly` at the call site and cannot typo the string.
|
|
46
|
+
*/
|
|
47
|
+
export const AGENTCORE_ENFORCEMENT = {
|
|
48
|
+
/** Evaluated on every request; its decision is observed, never returned. */
|
|
49
|
+
logOnly: "LOG_ONLY",
|
|
50
|
+
/** Contributes to the decision Gateway returns. AWS's own default. */
|
|
51
|
+
enforce: "ACTIVE",
|
|
52
|
+
} as const;
|
|
53
|
+
|
|
54
|
+
/** The `EnforcementMode` property value, as CloudFormation spells it. */
|
|
55
|
+
export type AgentCoreEnforcementMode = (typeof AGENTCORE_ENFORCEMENT)[keyof typeof AGENTCORE_ENFORCEMENT];
|
|
56
|
+
|
|
57
|
+
/** The stage of a rollout, in the vocabulary the seam is authored in. */
|
|
58
|
+
export type AgentCoreStage = "log-only" | "enforce";
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The `EnforcementMode` a stage deploys as.
|
|
62
|
+
*
|
|
63
|
+
* Omitting `EnforcementMode` entirely is legal and means `ACTIVE` — AWS's
|
|
64
|
+
* schema declares that default. This helper never returns `undefined`, so a
|
|
65
|
+
* policy that went through it says which stage it is in on the face of the
|
|
66
|
+
* template, and a diff that promotes one shows the change.
|
|
67
|
+
*/
|
|
68
|
+
export function enforcementMode(stage: AgentCoreStage): AgentCoreEnforcementMode {
|
|
69
|
+
return stage === "log-only" ? AGENTCORE_ENFORCEMENT.logOnly : AGENTCORE_ENFORCEMENT.enforce;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The stage a wire value came from — the inverse of {@link enforcementMode}. */
|
|
73
|
+
export function enforcementStage(mode: AgentCoreEnforcementMode | undefined): AgentCoreStage {
|
|
74
|
+
return mode === AGENTCORE_ENFORCEMENT.logOnly ? "log-only" : "enforce";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** True when the policy's decision reaches Gateway rather than only the log. */
|
|
78
|
+
export function isEnforcing(mode: AgentCoreEnforcementMode | undefined): boolean {
|
|
79
|
+
return enforcementStage(mode) === "enforce";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* One line of prose for a stage, for a description or a plan summary.
|
|
84
|
+
*
|
|
85
|
+
* Kept here rather than at the call sites so every surface that explains a
|
|
86
|
+
* staged policy explains it the same way.
|
|
87
|
+
*/
|
|
88
|
+
export function describeStage(stage: AgentCoreStage): string {
|
|
89
|
+
return stage === "log-only"
|
|
90
|
+
? "Evaluated against real traffic; decisions are logged and do not affect the response."
|
|
91
|
+
: "Enforcing: this policy's decision reaches the gateway.";
|
|
92
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finding embedded AgentCore policy statements in a build's emitted output
|
|
3
|
+
* (#1660).
|
|
4
|
+
*
|
|
5
|
+
* The DWD walls read emitted *text* rather than the in-memory model, for the
|
|
6
|
+
* reason `../dogwood/scan.ts` sets out: `chant audit` runs over artifacts chant
|
|
7
|
+
* did not write, and a wall that only fires on chant's own output is not a
|
|
8
|
+
* wall. An embedded statement is the same situation one lexicon further out —
|
|
9
|
+
* the statement lands in whatever the aws lexicon emitted, so this reads the
|
|
10
|
+
* emitted template rather than reaching for a resource class the cedar lexicon
|
|
11
|
+
* deliberately does not import.
|
|
12
|
+
*
|
|
13
|
+
* Structural, not CloudFormation-specific. The thing being looked for is an
|
|
14
|
+
* object with a `Definition.Policy.Statement` string, which is AgentCore's
|
|
15
|
+
* language-agnostic arm wherever it appears — in a CFN template's
|
|
16
|
+
* `Resources.<id>.Properties`, in a plan JSON, in a fixture. The nearest
|
|
17
|
+
* enclosing key is carried along as the name to blame, which for a CFN template
|
|
18
|
+
* is the logical id.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { PostSynthContext } from "@intentius/chant/lint/post-synth";
|
|
22
|
+
import { isRecord } from "../policy-text";
|
|
23
|
+
|
|
24
|
+
/** One `Definition.Policy.Statement` found in an emitted artifact. */
|
|
25
|
+
export interface EmbeddedAgentCoreStatement {
|
|
26
|
+
/** The lexicon whose output carried it. */
|
|
27
|
+
lexicon: string;
|
|
28
|
+
/** The filename, or the lexicon's primary output when it had no name. */
|
|
29
|
+
source: string;
|
|
30
|
+
/** The nearest enclosing key — a CloudFormation logical id, in practice. */
|
|
31
|
+
logicalId?: string;
|
|
32
|
+
/** The statement text, verbatim. */
|
|
33
|
+
statement: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The `Cedar` arm as well, for a check that needs to tell the two apart. */
|
|
37
|
+
export interface EmbeddedAgentCoreDefinition extends EmbeddedAgentCoreStatement {
|
|
38
|
+
arm: "Cedar" | "Policy";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function walk(
|
|
42
|
+
node: unknown,
|
|
43
|
+
logicalId: string | undefined,
|
|
44
|
+
found: Array<{ logicalId?: string; arm: "Cedar" | "Policy"; statement: string }>,
|
|
45
|
+
): void {
|
|
46
|
+
if (Array.isArray(node)) {
|
|
47
|
+
for (const item of node) walk(item, logicalId, found);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (!isRecord(node)) return;
|
|
51
|
+
|
|
52
|
+
const definition = node.Definition;
|
|
53
|
+
if (isRecord(definition)) {
|
|
54
|
+
for (const arm of ["Cedar", "Policy"] as const) {
|
|
55
|
+
const value = definition[arm];
|
|
56
|
+
if (isRecord(value) && typeof value.Statement === "string") {
|
|
57
|
+
found.push({ ...(logicalId ? { logicalId } : {}), arm, statement: value.Statement });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const [key, value] of Object.entries(node)) {
|
|
63
|
+
// `Properties` is CloudFormation's own wrapper, so it is not the name to
|
|
64
|
+
// blame — the logical id one level up is.
|
|
65
|
+
walk(value, key === "Properties" ? logicalId : key, found);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Every emitted text in a build, with the lexicon and filename that carried it. */
|
|
70
|
+
function emittedTexts(ctx: PostSynthContext): Array<{ lexicon: string; source: string; text: string }> {
|
|
71
|
+
const out: Array<{ lexicon: string; source: string; text: string }> = [];
|
|
72
|
+
for (const [lexicon, output] of ctx.outputs) {
|
|
73
|
+
if (typeof output === "string") {
|
|
74
|
+
if (output.length > 0) out.push({ lexicon, source: lexicon, text: output });
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (typeof output.primary === "string" && output.primary.length > 0) {
|
|
78
|
+
out.push({ lexicon, source: lexicon, text: output.primary });
|
|
79
|
+
}
|
|
80
|
+
for (const [filename, content] of Object.entries(output.files ?? {})) {
|
|
81
|
+
if (typeof content === "string") out.push({ lexicon, source: filename, text: content });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Every AgentCore policy definition embedded in a build's emitted output.
|
|
89
|
+
*
|
|
90
|
+
* Non-JSON output is skipped rather than reported: most emitted files are not
|
|
91
|
+
* JSON, and a parse failure here says nothing about whether a statement is
|
|
92
|
+
* there.
|
|
93
|
+
*/
|
|
94
|
+
export function embeddedAgentCoreDefinitions(ctx: PostSynthContext): EmbeddedAgentCoreDefinition[] {
|
|
95
|
+
const results: EmbeddedAgentCoreDefinition[] = [];
|
|
96
|
+
|
|
97
|
+
for (const { lexicon, source, text } of emittedTexts(ctx)) {
|
|
98
|
+
const trimmed = text.trimStart();
|
|
99
|
+
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) continue;
|
|
100
|
+
|
|
101
|
+
let parsed: unknown;
|
|
102
|
+
try {
|
|
103
|
+
parsed = JSON.parse(text);
|
|
104
|
+
} catch {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const found: Array<{ logicalId?: string; arm: "Cedar" | "Policy"; statement: string }> = [];
|
|
109
|
+
walk(parsed, undefined, found);
|
|
110
|
+
for (const hit of found) results.push({ lexicon, source, ...hit });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return results;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Only the language-agnostic arm — the one a `.dw` statement travels in. */
|
|
117
|
+
export function embeddedAgentCorePolicyStatements(ctx: PostSynthContext): EmbeddedAgentCoreStatement[] {
|
|
118
|
+
return embeddedAgentCoreDefinitions(ctx).filter((d) => d.arm === "Policy");
|
|
119
|
+
}
|