@intentius/chant-lexicon-aws 0.45.0 → 0.49.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.
Files changed (147) hide show
  1. package/dist/api/read-client.d.ts +10 -2
  2. package/dist/api/read-client.d.ts.map +1 -1
  3. package/dist/components/cloud-executor.d.ts.map +1 -1
  4. package/dist/composites/agentcore-agent.d.ts +26 -1
  5. package/dist/composites/agentcore-agent.d.ts.map +1 -1
  6. package/dist/composites/bucket-deployment.d.ts +82 -0
  7. package/dist/composites/bucket-deployment.d.ts.map +1 -0
  8. package/dist/composites/dynamodb-table.d.ts +45 -0
  9. package/dist/composites/dynamodb-table.d.ts.map +1 -0
  10. package/dist/composites/ec2-instance-bundle.d.ts +61 -0
  11. package/dist/composites/ec2-instance-bundle.d.ts.map +1 -0
  12. package/dist/composites/ecr-repository.d.ts +49 -0
  13. package/dist/composites/ecr-repository.d.ts.map +1 -0
  14. package/dist/composites/fargate-service.d.ts +16 -0
  15. package/dist/composites/fargate-service.d.ts.map +1 -1
  16. package/dist/composites/index.d.ts +16 -2
  17. package/dist/composites/index.d.ts.map +1 -1
  18. package/dist/composites/lambda-function.d.ts +4 -4
  19. package/dist/composites/lambda-function.d.ts.map +1 -1
  20. package/dist/composites/monitoring-stack.d.ts +99 -0
  21. package/dist/composites/monitoring-stack.d.ts.map +1 -0
  22. package/dist/composites/nlb-service.d.ts +48 -0
  23. package/dist/composites/nlb-service.d.ts.map +1 -0
  24. package/dist/composites/step-functions-workflow.d.ts +36 -0
  25. package/dist/composites/step-functions-workflow.d.ts.map +1 -0
  26. package/dist/deep-observe.d.ts +45 -0
  27. package/dist/deep-observe.d.ts.map +1 -1
  28. package/dist/deep-topology.d.ts +104 -0
  29. package/dist/deep-topology.d.ts.map +1 -0
  30. package/dist/effect-receipt-row.d.ts +92 -0
  31. package/dist/effect-receipt-row.d.ts.map +1 -0
  32. package/dist/index.d.ts +6 -2
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/integrity.json +9 -4
  35. package/dist/lint/audit-catalog.d.ts.map +1 -1
  36. package/dist/lint/post-synth/cf-refs.d.ts +24 -0
  37. package/dist/lint/post-synth/cf-refs.d.ts.map +1 -1
  38. package/dist/lint/post-synth/index.d.ts.map +1 -1
  39. package/dist/lint/post-synth/waw059.d.ts +36 -0
  40. package/dist/lint/post-synth/waw059.d.ts.map +1 -0
  41. package/dist/lint/post-synth/waw060.d.ts +16 -0
  42. package/dist/lint/post-synth/waw060.d.ts.map +1 -0
  43. package/dist/lint/post-synth/waw061.d.ts +19 -0
  44. package/dist/lint/post-synth/waw061.d.ts.map +1 -0
  45. package/dist/lint/post-synth/waw062.d.ts +19 -0
  46. package/dist/lint/post-synth/waw062.d.ts.map +1 -0
  47. package/dist/lint/post-synth/waw063.d.ts +35 -0
  48. package/dist/lint/post-synth/waw063.d.ts.map +1 -0
  49. package/dist/manifest.json +1 -1
  50. package/dist/okf/index.md +5 -0
  51. package/dist/okf/rules/WAW059.md +25 -0
  52. package/dist/okf/rules/WAW060.md +17 -0
  53. package/dist/okf/rules/WAW061.md +17 -0
  54. package/dist/okf/rules/WAW062.md +27 -0
  55. package/dist/okf/rules/WAW063.md +19 -0
  56. package/dist/okf/types/Action.md +2 -0
  57. package/dist/okf/types/AutoScalingGroup.md +1 -0
  58. package/dist/okf/types/Bucket.md +2 -0
  59. package/dist/okf/types/ECRRepository.md +1 -0
  60. package/dist/okf/types/Function.md +1 -0
  61. package/dist/okf/types/GlobalTable.md +4 -0
  62. package/dist/okf/types/IamPolicy.md +3 -0
  63. package/dist/okf/types/InstanceProfile.md +4 -0
  64. package/dist/okf/types/LaunchTemplate.md +1 -0
  65. package/dist/okf/types/LoadBalancer.md +1 -0
  66. package/dist/okf/types/ManagedPolicy.md +4 -0
  67. package/dist/okf/types/Map.md +2 -0
  68. package/dist/okf/types/Queue.md +1 -0
  69. package/dist/okf/types/Role.md +3 -0
  70. package/dist/okf/types/SecurityGroup.md +1 -0
  71. package/dist/okf/types/StateMachine.md +4 -0
  72. package/dist/okf/types/Subnet.md +4 -0
  73. package/dist/okf/types/Table.md +2 -0
  74. package/dist/okf/types/Type.md +5 -0
  75. package/dist/okf/types/Vpc.md +1 -0
  76. package/dist/op/activities/aws-apply.d.ts +48 -5
  77. package/dist/op/activities/aws-apply.d.ts.map +1 -1
  78. package/dist/op/activities/index.d.ts +11 -4
  79. package/dist/op/activities/index.d.ts.map +1 -1
  80. package/dist/ownership.d.ts +18 -0
  81. package/dist/ownership.d.ts.map +1 -1
  82. package/dist/plugin.d.ts.map +1 -1
  83. package/dist/receipt-store.d.ts +109 -0
  84. package/dist/receipt-store.d.ts.map +1 -0
  85. package/dist/rules/cf-refs.ts +62 -0
  86. package/dist/rules/waw059.ts +353 -0
  87. package/dist/rules/waw060.ts +91 -0
  88. package/dist/rules/waw061.ts +67 -0
  89. package/dist/rules/waw062.ts +0 -0
  90. package/dist/rules/waw063.ts +0 -0
  91. package/dist/serializer.d.ts.map +1 -1
  92. package/dist/skills/chant-aws-carve-terraform.md +26 -23
  93. package/dist/teardown.d.ts +85 -0
  94. package/dist/teardown.d.ts.map +1 -0
  95. package/package.json +3 -3
  96. package/src/api/read-client.test.ts +9 -0
  97. package/src/api/read-client.ts +15 -2
  98. package/src/components/cloud-executor.ts +10 -1
  99. package/src/composites/agentcore-agent.ts +35 -13
  100. package/src/composites/bucket-deployment.test.ts +84 -0
  101. package/src/composites/bucket-deployment.ts +153 -0
  102. package/src/composites/composites.test.ts +325 -0
  103. package/src/composites/dynamodb-table.test.ts +154 -0
  104. package/src/composites/dynamodb-table.ts +124 -0
  105. package/src/composites/ec2-instance-bundle.ts +134 -0
  106. package/src/composites/ecr-repository.test.ts +118 -0
  107. package/src/composites/ecr-repository.ts +117 -0
  108. package/src/composites/fargate-service.ts +19 -3
  109. package/src/composites/index.ts +21 -2
  110. package/src/composites/monitoring-stack.test.ts +130 -0
  111. package/src/composites/monitoring-stack.ts +164 -0
  112. package/src/composites/nlb-service.ts +117 -0
  113. package/src/composites/step-functions-workflow.ts +127 -0
  114. package/src/deep-observe.test.ts +201 -1
  115. package/src/deep-observe.ts +204 -0
  116. package/src/deep-topology.test.ts +392 -0
  117. package/src/deep-topology.ts +237 -0
  118. package/src/effect-receipt-row.test.ts +164 -0
  119. package/src/effect-receipt-row.ts +147 -0
  120. package/src/index.ts +28 -7
  121. package/src/lifecycle-integration.test.ts +4 -0
  122. package/src/lint/audit-catalog.ts +11 -0
  123. package/src/lint/post-synth/cf-refs.ts +62 -0
  124. package/src/lint/post-synth/index.ts +10 -0
  125. package/src/lint/post-synth/waw059.test.ts +309 -0
  126. package/src/lint/post-synth/waw059.ts +353 -0
  127. package/src/lint/post-synth/waw060.test.ts +131 -0
  128. package/src/lint/post-synth/waw060.ts +91 -0
  129. package/src/lint/post-synth/waw061.test.ts +99 -0
  130. package/src/lint/post-synth/waw061.ts +67 -0
  131. package/src/lint/post-synth/waw062.test.ts +109 -0
  132. package/src/lint/post-synth/waw062.ts +0 -0
  133. package/src/lint/post-synth/waw063.test.ts +220 -0
  134. package/src/lint/post-synth/waw063.ts +0 -0
  135. package/src/op/activities/aws-apply.test.ts +111 -0
  136. package/src/op/activities/aws-apply.ts +100 -6
  137. package/src/op/activities/index.ts +20 -3
  138. package/src/ownership.test.ts +24 -1
  139. package/src/ownership.ts +37 -0
  140. package/src/plugin.ts +59 -10
  141. package/src/receipt-store.test.ts +204 -0
  142. package/src/receipt-store.ts +315 -0
  143. package/src/serializer-ownership.test.ts +18 -0
  144. package/src/serializer.ts +114 -3
  145. package/src/skills/chant-aws-carve-terraform.md +26 -23
  146. package/src/teardown.test.ts +258 -0
  147. package/src/teardown.ts +276 -0
@@ -0,0 +1,276 @@
1
+ /**
2
+ * Env teardown for the aws lexicon (chant #1222) — both halves of the
3
+ * `teardownOwned` / `executeTeardown` capability pair, at STACK granularity.
4
+ *
5
+ * Per-resource selection is impossible here by construction: the thin read
6
+ * (`describeResources`) is sourced from `describe-stack-resources`, which
7
+ * returns no tags, so no resource-level marker can be read on the teardown
8
+ * path. The stack is aws's ownership boundary anyway — the applier deploys
9
+ * whole stacks and deletes ride CloudFormation — so teardown enumerates the
10
+ * environment's stacks and verifies the marker on each STACK's own tags
11
+ * (`DescribeStacks` → `Tags`), stamped there by the apply paths from the
12
+ * template's `Metadata["chant:ownership"]` block.
13
+ *
14
+ * Which stacks are the environment's: the project's declared `stacks[]` when
15
+ * it is a multi-stack project, else the single-stack convention — the explicit
16
+ * `stack` option, else the stack named after the environment (the same rule
17
+ * `describeResources` and `exportResources` apply, see chant #932).
18
+ *
19
+ * Verification is tag-reading, never name-trusting. A resolved stack whose
20
+ * tags carry exactly the requested identity (managed-by + stack + env) is a
21
+ * candidate of type `AWS::CloudFormation::Stack`. A stack carrying a DIFFERENT
22
+ * marker identity verifiably belongs to another project or env — out of scope,
23
+ * silently, the way another env's resources are. A stack carrying NO marker at
24
+ * all is unknowable — a legacy chant stack from before stack tagging, or a
25
+ * foreign stack that happens to hold the env's name — and is reported as a
26
+ * hole (`filtered` / unverified-ownership), never deleted. An absent stack is
27
+ * knowledge, not a hole: nothing to tear down.
28
+ *
29
+ * Execution re-reads each candidate stack's tags immediately before deleting
30
+ * (the enumeration is a moment old at best, and a delete is not undoable),
31
+ * then rides the existing Query-API delete — `awsDelete`, DeleteStack polled
32
+ * to DELETE_COMPLETE. An identity that no longer matches is `not-prunable:
33
+ * unverified-ownership`; an already-absent stack is `deleted` (teardown is
34
+ * idempotent).
35
+ */
36
+
37
+ import type {
38
+ TeardownCandidate,
39
+ TeardownEnumeration,
40
+ TeardownExecution,
41
+ TeardownHole,
42
+ TeardownOutcome,
43
+ } from "@intentius/chant/lexicon";
44
+ import { readOwnership, type OwnershipMarker } from "@intentius/chant/ownership";
45
+ import { AWS_TAG_OWNERSHIP_KEYS } from "./ownership";
46
+ import {
47
+ cfnQuery,
48
+ xmlLeaves,
49
+ xmlMembers,
50
+ AwsReadError,
51
+ type AwsReadClientOptions,
52
+ } from "./api/read-client";
53
+ import { awsDelete, type AwsHttp } from "./op/activities/aws-apply";
54
+
55
+ /** The one candidate type this lexicon's teardown produces: whole stacks. */
56
+ export const STACK_TYPE = "AWS::CloudFormation::Stack";
57
+
58
+ export interface AwsTeardownOptions {
59
+ environment: string;
60
+ /** The identity to select on: this project's ownership stack + the env being torn down. */
61
+ marker: OwnershipMarker;
62
+ /** Explicit deployed stack name (single-stack override). */
63
+ stack?: string;
64
+ /** Region that stack is deployed in. */
65
+ region?: string;
66
+ /** A multi-stack project's declared stacks (chant.config `stacks[]`). */
67
+ stacks?: Array<{ name: string; region?: string }>;
68
+ }
69
+
70
+ /** Injectable transports/timeouts, so tests double the Query API (no network). */
71
+ export interface AwsTeardownDeps {
72
+ /** Options for the read client's `DescribeStacks` calls (http injection, endpoint). */
73
+ read?: AwsReadClientOptions;
74
+ /** The applier transport `awsDelete` polls DeleteStack through. */
75
+ applyHttp?: AwsHttp;
76
+ /** DeleteStack settle timeout in ms (default `awsDelete`'s 300000). */
77
+ timeoutMs?: number;
78
+ /** DeleteStack poll interval in ms (default `awsDelete`'s 3000). */
79
+ intervalMs?: number;
80
+ }
81
+
82
+ /** The stacks an env teardown resolves to: `stacks[]`, else the single-stack convention. */
83
+ export function resolveTeardownStacks(
84
+ options: AwsTeardownOptions,
85
+ ): Array<{ name: string; region?: string }> {
86
+ if (options.stacks && options.stacks.length > 0) return options.stacks;
87
+ return [
88
+ {
89
+ name: options.stack ?? options.environment,
90
+ ...(options.region ? { region: options.region } : {}),
91
+ },
92
+ ];
93
+ }
94
+
95
+ /** One live stack, as DescribeStacks answers for it. */
96
+ interface LiveStack {
97
+ stackId?: string;
98
+ status?: string;
99
+ /** The stack's own tags, as a flat map. */
100
+ tags: Record<string, string>;
101
+ }
102
+
103
+ /** A DescribeStacks miss — CloudFormation's "does not exist" ValidationError. */
104
+ function isStackMissingError(err: unknown): boolean {
105
+ return err instanceof AwsReadError && /does not exist/i.test(err.message);
106
+ }
107
+
108
+ /** `DescribeStacks` for one stack; `undefined` when the stack does not exist. */
109
+ async function describeStack(
110
+ name: string,
111
+ region: string | undefined,
112
+ read: AwsReadClientOptions,
113
+ ): Promise<LiveStack | undefined> {
114
+ let xml: string;
115
+ try {
116
+ xml = await cfnQuery("DescribeStacks", { StackName: name }, {
117
+ ...read,
118
+ ...(region ? { region } : {}),
119
+ });
120
+ } catch (err) {
121
+ if (isStackMissingError(err)) return undefined;
122
+ throw err;
123
+ }
124
+ // One StackName queried → one stack member; `xmlLeaves` keeps the first
125
+ // occurrence of each scalar, which is that stack's StackId/StackStatus.
126
+ const leaves = xmlLeaves(xml);
127
+ const tags: Record<string, string> = {};
128
+ for (const m of xmlMembers(xml, "Tags")) {
129
+ if (m.Key) tags[m.Key] = m.Value ?? "";
130
+ }
131
+ return {
132
+ ...(leaves.StackId ? { stackId: leaves.StackId } : {}),
133
+ ...(leaves.StackStatus ? { status: leaves.StackStatus } : {}),
134
+ tags,
135
+ };
136
+ }
137
+
138
+ /** True when a live stack's own tags carry exactly the requested identity. */
139
+ function matchesMarker(tags: Record<string, string>, marker: OwnershipMarker): boolean {
140
+ const read = readOwnership(tags, AWS_TAG_OWNERSHIP_KEYS);
141
+ return read !== undefined && read.stack === marker.stack && read.env === marker.env;
142
+ }
143
+
144
+ /**
145
+ * Enumerate the environment's marker-verified stacks — the aws half of
146
+ * `chant lifecycle teardown <env>` planning. Read-only.
147
+ */
148
+ export async function teardownOwned(
149
+ options: AwsTeardownOptions,
150
+ deps: AwsTeardownDeps = {},
151
+ ): Promise<TeardownEnumeration> {
152
+ const read = deps.read ?? {};
153
+ const candidates: TeardownCandidate[] = [];
154
+ const holes: TeardownHole[] = [];
155
+
156
+ for (const ref of resolveTeardownStacks(options)) {
157
+ let live: LiveStack | undefined;
158
+ try {
159
+ live = await describeStack(ref.name, ref.region, read);
160
+ } catch (err) {
161
+ holes.push({
162
+ name: ref.name,
163
+ type: STACK_TYPE,
164
+ reason: "read-failed",
165
+ detail: err instanceof Error ? err.message : String(err),
166
+ });
167
+ continue;
168
+ }
169
+ // Absent is knowledge: the env-resolved stack does not exist, so there is
170
+ // nothing to tear down and nothing unknown about it.
171
+ if (live === undefined) continue;
172
+
173
+ const identity = readOwnership(live.tags, AWS_TAG_OWNERSHIP_KEYS);
174
+ if (identity !== undefined && identity.stack === options.marker.stack && identity.env === options.marker.env) {
175
+ candidates.push({
176
+ name: ref.name,
177
+ type: STACK_TYPE,
178
+ ...(live.stackId ? { physicalId: live.stackId } : {}),
179
+ marker: identity,
180
+ });
181
+ continue;
182
+ }
183
+ if (identity !== undefined) {
184
+ // A marker for a DIFFERENT identity is verifiably someone else's
185
+ // (another project's stack, another env's deployment of a shared stack
186
+ // name) — out of scope, the way any foreign-env resource is.
187
+ continue;
188
+ }
189
+ // No marker at all: a legacy chant stack deployed before stack tagging, or
190
+ // a foreign stack under the env's name — unknowable either way. Loud, and
191
+ // never deleted (#1089): unverified ownership reads as "do not touch",
192
+ // not as "clean".
193
+ holes.push({
194
+ name: ref.name,
195
+ type: STACK_TYPE,
196
+ reason: "filtered",
197
+ detail:
198
+ `unverified-ownership: the stack exists but its tags carry no chant ownership marker ` +
199
+ `(expected ${AWS_TAG_OWNERSHIP_KEYS.managedBy} + ${AWS_TAG_OWNERSHIP_KEYS.stack}=${options.marker.stack}` +
200
+ `${options.marker.env ? ` + ${AWS_TAG_OWNERSHIP_KEYS.env}=${options.marker.env}` : ""}) — ` +
201
+ `a chant stack deployed before stack tagging, or a foreign stack; re-deploy to stamp it, it will not be deleted`,
202
+ });
203
+ }
204
+
205
+ return { candidates, ...(holes.length > 0 ? { holes } : {}) };
206
+ }
207
+
208
+ /**
209
+ * Delete the handed-over stacks: re-verify each stack's own tags, then
210
+ * DeleteStack via `awsDelete`, polled to DELETE_COMPLETE. One outcome per
211
+ * candidate, always.
212
+ */
213
+ export async function executeTeardown(
214
+ options: AwsTeardownOptions & { candidates: TeardownCandidate[] },
215
+ deps: AwsTeardownDeps = {},
216
+ ): Promise<TeardownExecution> {
217
+ const read = deps.read ?? {};
218
+ const regionByStack = new Map<string, string | undefined>(
219
+ resolveTeardownStacks(options).map((s) => [s.name, s.region]),
220
+ );
221
+
222
+ const outcomes: TeardownOutcome[] = [];
223
+ for (const candidate of options.candidates) {
224
+ outcomes.push(await deleteStackCandidate(candidate, regionByStack.get(candidate.name), options.marker, read, deps));
225
+ }
226
+ return { outcomes };
227
+ }
228
+
229
+ async function deleteStackCandidate(
230
+ candidate: TeardownCandidate,
231
+ region: string | undefined,
232
+ marker: OwnershipMarker,
233
+ read: AwsReadClientOptions,
234
+ deps: AwsTeardownDeps,
235
+ ): Promise<TeardownOutcome> {
236
+ const base = {
237
+ name: candidate.name,
238
+ type: STACK_TYPE,
239
+ ...(candidate.physicalId ? { physicalId: candidate.physicalId } : {}),
240
+ };
241
+
242
+ // Re-read and re-verify the stack's own tags right before deleting: the
243
+ // enumeration is a moment old at best, and a DeleteStack is not undoable.
244
+ let live: LiveStack | undefined;
245
+ try {
246
+ live = await describeStack(candidate.name, region, read);
247
+ } catch (err) {
248
+ return { ...base, outcome: "failed", detail: err instanceof Error ? err.message : String(err) };
249
+ }
250
+ if (live === undefined || live.status === "DELETE_COMPLETE") {
251
+ return { ...base, outcome: "deleted", detail: "already absent" };
252
+ }
253
+ if (!matchesMarker(live.tags, marker)) {
254
+ return {
255
+ ...base,
256
+ outcome: "not-prunable",
257
+ detail: "unverified-ownership: the live stack's tags no longer carry the requested marker identity",
258
+ };
259
+ }
260
+
261
+ try {
262
+ await awsDelete(
263
+ {
264
+ stackName: candidate.name,
265
+ ...(region ? { region } : {}),
266
+ ...(deps.timeoutMs !== undefined ? { timeoutMs: deps.timeoutMs } : {}),
267
+ ...(deps.intervalMs !== undefined ? { intervalMs: deps.intervalMs } : {}),
268
+ },
269
+ undefined,
270
+ deps.applyHttp,
271
+ );
272
+ } catch (err) {
273
+ return { ...base, outcome: "failed", detail: err instanceof Error ? err.message : String(err) };
274
+ }
275
+ return { ...base, outcome: "deleted" };
276
+ }