@intentius/chant-lexicon-aws 0.124.102 → 0.124.104

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.
@@ -1,5 +1,10 @@
1
1
  import { type AwsReadClientOptions } from "./api/read-client.js";
2
2
  import type { ResourceMetadata } from "@intentius/chant/lexicon";
3
+ /**
4
+ * The primary identifier's parts for a resource type, or undefined when the
5
+ * type is not a generated resource or has no primary identifier on record.
6
+ */
7
+ export declare function primaryIdentifierParts(entityType: string): readonly string[] | undefined;
3
8
  /**
4
9
  * The Cloud Control identifier the declared props spell, or undefined when the
5
10
  * type has no primary identifier on record or any part of it is absent or
@@ -33,6 +33,16 @@ function manifestByType() {
33
33
  }
34
34
  return byResourceType;
35
35
  }
36
+ /**
37
+ * The primary identifier's parts for a resource type, or undefined when the
38
+ * type is not a generated resource or has no primary identifier on record.
39
+ */
40
+ export function primaryIdentifierParts(entityType) {
41
+ const entry = manifestByType().get(entityType);
42
+ if (!entry || entry.kind !== "resource" || !entry.primaryIdentifier || entry.primaryIdentifier.length === 0)
43
+ return undefined;
44
+ return entry.primaryIdentifier;
45
+ }
36
46
  /**
37
47
  * The Cloud Control identifier the declared props spell, or undefined when the
38
48
  * type has no primary identifier on record or any part of it is absent or
@@ -5,6 +5,8 @@ export interface ImportManifestEntry {
5
5
  kind: string;
6
6
  /** GetAtt name -> CloudFormation attribute: the schema's `readOnlyProperties`. */
7
7
  attrs?: Record<string, string>;
8
+ /** The schema's top-level required properties. */
9
+ required?: string[];
8
10
  }
9
11
  /**
10
12
  * Whether `type` is a resource type this lexicon can write: the listed-type
@@ -16,6 +18,8 @@ export interface ImportManifestEntry {
16
18
  export declare function isImportableAwsType(type: string, manifest?: ReadonlyMap<string, ImportManifestEntry>): boolean;
17
19
  /** The properties a declaration of `type` can hold, from an indexed node's attrs. */
18
20
  export declare function awsImportProperties(type: string, attrs: Record<string, unknown>, manifest?: ReadonlyMap<string, ImportManifestEntry>): Record<string, unknown>;
21
+ /** The properties `type` requires that `properties` does not hold, sorted. */
22
+ export declare function awsMissingRequired(type: string, properties: Record<string, unknown>, manifest?: ReadonlyMap<string, ImportManifestEntry>): string[];
19
23
  /** `value` as a reference to `target`, or undefined. */
20
24
  export declare function awsImportReference(value: string, target: IndexImportTarget, manifest?: ReadonlyMap<string, ImportManifestEntry>): unknown | undefined;
21
25
  /** The resource attributes `chant import --index` writes on every resource it imports (#4536). */
@@ -18,6 +18,11 @@
18
18
  * `GetAtt(<id>, "Arn")` when it is the resource's ARN and the type has an
19
19
  * `Arn` attribute. Anything else stays a literal.
20
20
  * - A masked value becomes a `String` parameter with `NoEcho`.
21
+ * - A property the schema requires that the node does not hold becomes a
22
+ * `String` parameter to fill in. Cloud Control's `ListResources` returns
23
+ * part of a resource's model for some types (a role's listing has no
24
+ * `AssumeRolePolicyDocument`), and source that left the property out
25
+ * would build into a template CloudFormation refuses.
21
26
  */
22
27
  import { createRequire } from "module";
23
28
  import { moduleSourceUrl } from "@intentius/chant/module-source";
@@ -81,6 +86,10 @@ export function awsImportProperties(type, attrs, manifest = defaultManifest()) {
81
86
  }
82
87
  return out;
83
88
  }
89
+ /** The properties `type` requires that `properties` does not hold, sorted. */
90
+ export function awsMissingRequired(type, properties, manifest = defaultManifest()) {
91
+ return (manifest.get(type)?.required ?? []).filter((p) => properties[p] === undefined).sort();
92
+ }
84
93
  /** True when `type` exposes `Arn` to GetAtt, or when there is no registry to ask. */
85
94
  function hasArnAttribute(type, manifest) {
86
95
  if (manifest.size === 0)
@@ -111,7 +120,14 @@ export function createAwsIndexImport(manifest) {
111
120
  // (#4536, ruled on choudoufu#2284 Q4): CloudFormation deletes at stack
112
121
  // level and reads no per-resource marker, so an adopted object is never
113
122
  // deleted on the stack's word alone. Import requires a DeletionPolicy.
114
- return { type: node.kind, properties: awsImportProperties(node.kind, node.attrs ?? {}, m()), attributes: { ...ADOPTED_RESOURCE_ATTRIBUTES } };
123
+ const properties = awsImportProperties(node.kind, node.attrs ?? {}, m());
124
+ const missing = awsMissingRequired(node.kind, properties, m());
125
+ return {
126
+ type: node.kind,
127
+ properties,
128
+ attributes: { ...ADOPTED_RESOURCE_ATTRIBUTES },
129
+ ...(missing.length > 0 ? { missing } : {}),
130
+ };
115
131
  },
116
132
  reference: (value, target) => awsImportReference(value, target, m()),
117
133
  secretParameter(name, context) {
@@ -122,6 +138,13 @@ export function createAwsIndexImport(manifest) {
122
138
  constraints: { NoEcho: true },
123
139
  };
124
140
  },
141
+ requiredParameter(name, context) {
142
+ return {
143
+ name,
144
+ type: "String",
145
+ description: `${context.path} of ${context.node.id}, required and not in the index listing`,
146
+ };
147
+ },
125
148
  parameterReference: (name) => ({ __intrinsic: "Ref", name }),
126
149
  typeLabel(node) {
127
150
  const last = node.kind.split("::").pop() ?? "Resource";
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "algorithm": "sha256",
3
3
  "artifacts": {
4
- "manifest.json": "19a010a93d22f2d3a649c448d1059236d02b864c20d39f462748716e2567b193",
5
- "meta.json": "5a61f147f0c79262967f826c69a53b8b21fe6c41c6142e6c6c235b595482357f",
4
+ "manifest.json": "ddbc53f1ebd4efc8c7dacf2241417c6b7a330224549ae2cbe2a3f84b2481b13a",
5
+ "meta.json": "7de63d55427041907cd0fbdc504fc496d381e2bfebeae2fe9d48f1847ed29faf",
6
6
  "types/index.d.ts": "8651f9ae166668f4311eea1fe61c3fe235f345947951c1d23d1df24a4fd3384f",
7
7
  "rules/hardcoded-region.ts": "957eadcaa9f39cc17d21437565d7a94607d9b082cf28918b178fa067f07498a9",
8
8
  "rules/iam-wildcard.ts": "10f5065fa2bbf2c8ce23dfb475a56df33f280df87784e44fec641b2584cbf333",
@@ -75,5 +75,5 @@
75
75
  "skills/chant-aws-eks.md": "8789255709ff004ad0a875fd5999edcdc66fc6e33d710db058d9f42703bcfdfe",
76
76
  "skills/chant-aws-carve-terraform.md": "a6f528c814024422744118abbfd9bbf26f3d60c599e90c3f186f8b76e1e38a51"
77
77
  },
78
- "composite": "2156ccec2f3285c3a5f41835a19fa6148ad36087ce1e67aa918d8a026a616fc6"
78
+ "composite": "4b4254374ba7c2da9da3950ab7a20461fd2e9b815158be029a7a65ad7f6830ea"
79
79
  }
@@ -0,0 +1,10 @@
1
+ import type { RefRule } from "@intentius/chant/graph-refs";
2
+ /** JSON text as a value: an object passes through, a string is parsed, and one that is URL-encoded is decoded first. */
3
+ export declare function parseJsonText(v: unknown): unknown;
4
+ /** Where each kind keeps a policy document, as a path from its attrs. */
5
+ export declare const POLICY_DOCUMENTS: ReadonlyArray<{
6
+ from: string;
7
+ path: string;
8
+ }>;
9
+ /** The catalog's rules that read JSON text. */
10
+ export declare function jsonTextRules(): RefRule[];
@@ -0,0 +1,114 @@
1
+ /** The values at `path` (`a.b`, `arr[].c`) of `obj`, objects and strings kept as they are. */
2
+ function rawAt(obj, path) {
3
+ let cur = [obj];
4
+ for (const step of path.split(".")) {
5
+ const isArray = step.endsWith("[]");
6
+ const key = isArray ? step.slice(0, -2) : step;
7
+ const next = [];
8
+ for (const c of cur) {
9
+ if (!c || typeof c !== "object")
10
+ continue;
11
+ const v = c[key];
12
+ if (v === undefined)
13
+ continue;
14
+ if (isArray) {
15
+ if (Array.isArray(v))
16
+ next.push(...v);
17
+ }
18
+ else {
19
+ next.push(v);
20
+ }
21
+ }
22
+ cur = next;
23
+ }
24
+ return cur;
25
+ }
26
+ /** JSON text as a value: an object passes through, a string is parsed, and one that is URL-encoded is decoded first. */
27
+ export function parseJsonText(v) {
28
+ if (typeof v !== "string")
29
+ return v;
30
+ for (const text of [v, safeDecode(v)]) {
31
+ if (text === undefined)
32
+ continue;
33
+ try {
34
+ return JSON.parse(text);
35
+ }
36
+ catch {
37
+ /* try the next spelling */
38
+ }
39
+ }
40
+ return undefined;
41
+ }
42
+ function safeDecode(s) {
43
+ try {
44
+ return decodeURIComponent(s);
45
+ }
46
+ catch {
47
+ return undefined;
48
+ }
49
+ }
50
+ const strings = (v) => (typeof v === "string" ? [v] : Array.isArray(v) ? v.filter((x) => typeof x === "string") : []);
51
+ /** The ARN-shaped strings of a list of values; wildcards and plain names are left to match nothing. */
52
+ const arns = (vs) => [...new Set(vs.filter((s) => s.startsWith("arn:")))];
53
+ const redrivePaths = ["RedrivePolicy", "Attributes.RedrivePolicy"];
54
+ function redriveRule(base) {
55
+ return {
56
+ from: "AWS::SQS::Queue",
57
+ path: `${base}.deadLetterTargetArn`,
58
+ targetKind: "AWS::SQS::Queue",
59
+ relation: "reference",
60
+ label: "dead-letter queue",
61
+ viaAttr: "RedrivePolicy",
62
+ optional: true,
63
+ join(holder, find) {
64
+ const out = [];
65
+ for (const doc of rawAt(holder.attrs, base)) {
66
+ for (const arn of arns(strings(parseJsonText(doc)?.deadLetterTargetArn))) {
67
+ out.push({ value: arn, targets: find(arn).filter((n) => n.kind === "AWS::SQS::Queue") });
68
+ }
69
+ }
70
+ return out;
71
+ },
72
+ };
73
+ }
74
+ /** Where each kind keeps a policy document, as a path from its attrs. */
75
+ export const POLICY_DOCUMENTS = [
76
+ { from: "AWS::IAM::Role", path: "AssumeRolePolicyDocument" },
77
+ { from: "AWS::IAM::Role", path: "Policies[].PolicyDocument" },
78
+ { from: "AWS::IAM::ManagedPolicy", path: "PolicyDocument" },
79
+ { from: "AWS::IAM::Policy", path: "PolicyDocument" },
80
+ { from: "AWS::SQS::QueuePolicy", path: "PolicyDocument" },
81
+ { from: "AWS::S3::BucketPolicy", path: "PolicyDocument" },
82
+ { from: "AWS::SQS::Queue", path: "Policy" },
83
+ { from: "AWS::SQS::Queue", path: "Attributes.Policy" },
84
+ ];
85
+ const POLICY_VALUES = [
86
+ { path: "Statement[].Resource", label: "grants on" },
87
+ { path: "Statement[].Principal.AWS", label: "trusts" },
88
+ ];
89
+ function policyRules() {
90
+ return POLICY_DOCUMENTS.flatMap(({ from, path: docPath }) => POLICY_VALUES.map(({ path: valuePath, label }) => ({
91
+ from,
92
+ path: `${docPath}.${valuePath}`,
93
+ relation: "reference",
94
+ label,
95
+ optional: true,
96
+ join(holder, find) {
97
+ const out = [];
98
+ for (const doc of rawAt(holder.attrs, docPath)) {
99
+ // A statement may be one object where the document would have a list.
100
+ const parsed = parseJsonText(doc);
101
+ const statements = parsed && typeof parsed === "object" ? parsed.Statement : undefined;
102
+ const list = Array.isArray(statements) ? statements : statements ? [statements] : [];
103
+ for (const arn of arns(list.flatMap((s) => rawAt(s, valuePath.replace(/^Statement\[\]\./, "")).flatMap(strings)))) {
104
+ out.push({ value: arn, targets: find(arn) });
105
+ }
106
+ }
107
+ return out;
108
+ },
109
+ })));
110
+ }
111
+ /** The catalog's rules that read JSON text. */
112
+ export function jsonTextRules() {
113
+ return [...redrivePaths.map(redriveRule), ...policyRules()];
114
+ }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aws",
3
- "version": "0.124.102",
3
+ "version": "0.124.104",
4
4
  "chantVersion": ">=0.1.0",
5
5
  "namespace": "AWS",
6
6
  "intrinsics": [