@intentius/chant-lexicon-aws 0.124.96 → 0.124.98
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/dist/import/parser.d.ts.map +1 -1
- package/dist/import/parser.js +14 -1
- package/dist/integrity.json +3 -3
- package/dist/lint/post-synth/ext001.d.ts.map +1 -1
- package/dist/lint/post-synth/ext001.js +179 -30
- package/dist/manifest.json +1 -1
- package/dist/rules/ext001.ts +183 -28
- package/package.json +2 -2
- package/src/import/parser.test.ts +35 -0
- package/src/import/parser.ts +15 -1
- package/src/import/roundtrip-build.test.ts +66 -0
- package/src/lint/post-synth/ext001.test.ts +114 -0
- package/src/lint/post-synth/ext001.ts +183 -28
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/import/parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,UAAU,EAKX,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,IAAI,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/import/parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,UAAU,EAKX,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,IAAI,MAAM,SAAS,CAAC;AAoH3B,eAAO,MAAM,SAAS,aAGpB,CAAC;AAkEH;;;;GAIG;AACH,qBAAa,QAAS,SAAQ,eAAgB,YAAW,cAAc;IACrE;;OAEG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CA4BjC;IAED;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CASrC;IAEH,OAAO,CAAC,6BAA6B;IAUrC;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB,CAAS;IAEtC,OAAO,CAAC,eAAe;IAWvB;;OAEG;IACH,OAAO,CAAC,YAAY;IAYpB;;OAEG;IACH,OAAO,CAAC,eAAe;IAgBvB;;OAEG;IACH,OAAO,CAAC,cAAc;IAkBtB;;OAEG;IACH,OAAO,CAAC,eAAe;IAUvB;;OAEG;IACH,SAAS,CAAC,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,GAAG,IAAI,CAuJtG;CACF"}
|
package/dist/import/parser.js
CHANGED
|
@@ -102,7 +102,20 @@ const cfnYamlTypes = [
|
|
|
102
102
|
construct: (data) => ({ Condition: data }),
|
|
103
103
|
}),
|
|
104
104
|
];
|
|
105
|
-
|
|
105
|
+
/**
|
|
106
|
+
* js-yaml's default schema without the YAML 1.1 timestamp type. With it, an
|
|
107
|
+
* unquoted `Version: 2012-10-17` is read as a `Date`, which the generator then
|
|
108
|
+
* writes as `{}` (#5076). CloudFormation reads such a value as the string it
|
|
109
|
+
* was written as, so the schema has no implicit timestamp resolver and a
|
|
110
|
+
* date-like scalar (or a full ISO timestamp) stays a string. Merge keys and
|
|
111
|
+
* the other default types are kept.
|
|
112
|
+
*/
|
|
113
|
+
// js-yaml exports its built-in types at runtime; @types/js-yaml does not declare them.
|
|
114
|
+
const builtin = yaml.types;
|
|
115
|
+
export const CF_SCHEMA = yaml.CORE_SCHEMA.extend({
|
|
116
|
+
implicit: [builtin.merge],
|
|
117
|
+
explicit: [builtin.binary, builtin.omap, builtin.pairs, builtin.set, ...cfnYamlTypes],
|
|
118
|
+
});
|
|
106
119
|
/** Parameter keys import carries besides Type, Description and Default. */
|
|
107
120
|
const PARAMETER_CONSTRAINT_KEYS = [
|
|
108
121
|
"AllowedValues",
|
package/dist/integrity.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"algorithm": "sha256",
|
|
3
3
|
"artifacts": {
|
|
4
|
-
"manifest.json": "
|
|
4
|
+
"manifest.json": "6da2a643ae404ce49bcc52ec53a877f9fe824e4247e22a9cc533290baf142225",
|
|
5
5
|
"meta.json": "5a61f147f0c79262967f826c69a53b8b21fe6c41c6142e6c6c235b595482357f",
|
|
6
6
|
"types/index.d.ts": "8651f9ae166668f4311eea1fe61c3fe235f345947951c1d23d1df24a4fd3384f",
|
|
7
7
|
"rules/hardcoded-region.ts": "957eadcaa9f39cc17d21437565d7a94607d9b082cf28918b178fa067f07498a9",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"rules/s3-encryption.ts": "6611f3977d40314c31e6d4af99829a25ea71a529a6dfcaff425549c8aa54c29a",
|
|
10
10
|
"rules/cf-refs.ts": "3fa8d144e430ae00204b159f1c8c5bf78576c52f984cc4f656eb366d7423f819",
|
|
11
11
|
"rules/cor020.ts": "915bbb4c083c7b3b84b93e8e891b5ffffe5a138fdccda32ecfdda91b8685798c",
|
|
12
|
-
"rules/ext001.ts": "
|
|
12
|
+
"rules/ext001.ts": "273841481c234e0b6b7ea49926061b1eceb901dc579784dbeba0f6b35ce9e9d3",
|
|
13
13
|
"rules/waw010.ts": "41c58aaedb8f9fe266f4b40f1a076f762796427fdf9b317b8a4a50c713e45571",
|
|
14
14
|
"rules/waw011.ts": "fda06d63b71c37324dd23d519c2430c25d5fcb39a71eeed69caa76665dcae50d",
|
|
15
15
|
"rules/waw013.ts": "f9b37b955917415a2eb5f4aed7f28b76aff0a6abdb05228dcbb8bc77bc67672a",
|
|
@@ -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": "
|
|
78
|
+
"composite": "9510308531583c15cb88f6fb1d1d7cf5a3bacdb1751e5a62d5f4c0d09213a865"
|
|
79
79
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ext001.d.ts","sourceRoot":"","sources":["../../../src/lint/post-synth/ext001.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAG9G,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,SAAS,GAAG,oBAAoB,GAAG,aAAa,GAAG,cAAc,CAAC;IACxE,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;
|
|
1
|
+
{"version":3,"file":"ext001.d.ts","sourceRoot":"","sources":["../../../src/lint/post-synth/ext001.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAG9G,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,SAAS,GAAG,oBAAoB,GAAG,aAAa,GAAG,cAAc,CAAC;IACxE,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AA0UD;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,GAAG,EAAE,gBAAgB,EACrB,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,mBAAmB,EAAE,CAAC,GAChD,mBAAmB,EAAE,CAkBvB;AAED,eAAO,MAAM,MAAM,EAAE,cAOpB,CAAC"}
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
import { readFileSync } from "fs";
|
|
14
14
|
import { join } from "path";
|
|
15
15
|
import { moduleSourceDir } from "@intentius/chant/module-source";
|
|
16
|
-
import { parseCFTemplate } from "./cf-refs.js";
|
|
16
|
+
import { isIntrinsic, parseCFTemplate } from "./cf-refs.js";
|
|
17
17
|
/**
|
|
18
18
|
* Load lexicon JSON to get constraints per resource type.
|
|
19
19
|
*/
|
|
@@ -35,43 +35,192 @@ function loadLexiconConstraints() {
|
|
|
35
35
|
}
|
|
36
36
|
return map;
|
|
37
37
|
}
|
|
38
|
+
/** Keywords that describe a schema and never change what it accepts. */
|
|
39
|
+
const ANNOTATION_KEYWORDS = new Set([
|
|
40
|
+
"title", "description", "$comment", "$schema", "$id", "examples", "default", "deprecated",
|
|
41
|
+
]);
|
|
42
|
+
function sameJson(a, b) {
|
|
43
|
+
return a === b || JSON.stringify(a) === JSON.stringify(b);
|
|
44
|
+
}
|
|
45
|
+
/** Combine the verdicts of keywords that must all hold. */
|
|
46
|
+
function all(verdicts) {
|
|
47
|
+
if (verdicts.includes(false))
|
|
48
|
+
return false;
|
|
49
|
+
if (verdicts.includes(undefined))
|
|
50
|
+
return undefined;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
function matchesType(type, value) {
|
|
54
|
+
switch (type) {
|
|
55
|
+
case "string": return typeof value === "string";
|
|
56
|
+
case "boolean": return typeof value === "boolean";
|
|
57
|
+
case "null": return value === null;
|
|
58
|
+
case "array": return Array.isArray(value);
|
|
59
|
+
case "object": return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
60
|
+
case "number": return typeof value === "number" ? true : typeof value === "string" ? undefined : false;
|
|
61
|
+
case "integer":
|
|
62
|
+
if (typeof value === "number")
|
|
63
|
+
return Number.isInteger(value);
|
|
64
|
+
return typeof value === "string" ? undefined : false;
|
|
65
|
+
default: return undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
38
68
|
/**
|
|
39
|
-
*
|
|
69
|
+
* Evaluate a JSON schema against a present value. Supports the keywords the
|
|
70
|
+
* generated extension constraints use. Any other keyword makes the answer
|
|
71
|
+
* undecidable, and so does a value that is a CloudFormation intrinsic
|
|
72
|
+
* (Ref, Fn::*) where the keyword needs the concrete value.
|
|
73
|
+
*
|
|
74
|
+
* `topLevel` is set for the resource's own Properties object. There a property
|
|
75
|
+
* named by a schema but absent from the resource does not satisfy it, which is
|
|
76
|
+
* how this check has always read conditions (cfn-lint pairs them with
|
|
77
|
+
* `required`); a `false` schema is the one exception and means "absent".
|
|
40
78
|
*/
|
|
41
|
-
function
|
|
42
|
-
if (
|
|
79
|
+
function evalSchema(schema, value, topLevel = false) {
|
|
80
|
+
if (schema === true)
|
|
81
|
+
return true;
|
|
82
|
+
if (schema === false)
|
|
43
83
|
return false;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
84
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema))
|
|
85
|
+
return undefined;
|
|
86
|
+
const s = schema;
|
|
87
|
+
const concrete = !isIntrinsic(value);
|
|
88
|
+
const verdicts = [];
|
|
89
|
+
for (const [keyword, arg] of Object.entries(s)) {
|
|
90
|
+
if (ANNOTATION_KEYWORDS.has(keyword))
|
|
91
|
+
continue;
|
|
92
|
+
switch (keyword) {
|
|
93
|
+
case "const":
|
|
94
|
+
verdicts.push(concrete ? sameJson(value, arg) : undefined);
|
|
95
|
+
break;
|
|
96
|
+
case "enum":
|
|
97
|
+
verdicts.push(!Array.isArray(arg) ? undefined : concrete ? arg.some((e) => sameJson(value, e)) : undefined);
|
|
98
|
+
break;
|
|
99
|
+
case "pattern": {
|
|
100
|
+
if (typeof arg !== "string" || !concrete || typeof value !== "string") {
|
|
101
|
+
verdicts.push(undefined);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
verdicts.push(new RegExp(arg).test(value));
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
verdicts.push(undefined); // not a JavaScript regular expression
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
55
111
|
}
|
|
56
|
-
|
|
57
|
-
if (!
|
|
58
|
-
|
|
112
|
+
case "type": {
|
|
113
|
+
if (!concrete) {
|
|
114
|
+
verdicts.push(undefined);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
const types = Array.isArray(arg) ? arg : [arg];
|
|
118
|
+
const results = types.map((t) => (typeof t === "string" ? matchesType(t, value) : undefined));
|
|
119
|
+
verdicts.push(results.includes(true) ? true : results.includes(undefined) ? undefined : false);
|
|
120
|
+
break;
|
|
59
121
|
}
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
122
|
+
case "not": {
|
|
123
|
+
const v = evalSchema(arg, value);
|
|
124
|
+
verdicts.push(v === undefined ? undefined : !v);
|
|
125
|
+
break;
|
|
64
126
|
}
|
|
127
|
+
case "allOf":
|
|
128
|
+
verdicts.push(Array.isArray(arg) ? all(arg.map((a) => evalSchema(a, value))) : undefined);
|
|
129
|
+
break;
|
|
130
|
+
case "anyOf": {
|
|
131
|
+
if (!Array.isArray(arg)) {
|
|
132
|
+
verdicts.push(undefined);
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
const results = arg.map((a) => evalSchema(a, value));
|
|
136
|
+
verdicts.push(results.includes(true) ? true : results.includes(undefined) ? undefined : false);
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case "oneOf": {
|
|
140
|
+
if (!Array.isArray(arg)) {
|
|
141
|
+
verdicts.push(undefined);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
const results = arg.map((a) => evalSchema(a, value));
|
|
145
|
+
const yes = results.filter((r) => r === true).length;
|
|
146
|
+
const unknown = results.filter((r) => r === undefined).length;
|
|
147
|
+
verdicts.push(yes > 1 ? false : unknown === 0 ? yes === 1 : undefined);
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case "required": {
|
|
151
|
+
if (!Array.isArray(arg)) {
|
|
152
|
+
verdicts.push(undefined);
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
156
|
+
verdicts.push(concrete ? true : undefined);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
verdicts.push(arg.every((r) => r in value));
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
case "properties": {
|
|
163
|
+
if (!arg || typeof arg !== "object" || Array.isArray(arg)) {
|
|
164
|
+
verdicts.push(undefined);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
168
|
+
verdicts.push(concrete ? true : undefined);
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
const obj = value;
|
|
172
|
+
for (const [name, sub] of Object.entries(arg)) {
|
|
173
|
+
const present = name in obj;
|
|
174
|
+
if (sub === false)
|
|
175
|
+
verdicts.push(!present);
|
|
176
|
+
else if (!present)
|
|
177
|
+
verdicts.push(topLevel ? false : true);
|
|
178
|
+
else
|
|
179
|
+
verdicts.push(evalSchema(sub, obj[name]));
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
case "minItems":
|
|
184
|
+
case "maxItems": {
|
|
185
|
+
if (typeof arg !== "number" || !concrete) {
|
|
186
|
+
verdicts.push(undefined);
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
if (!Array.isArray(value)) {
|
|
190
|
+
verdicts.push(true);
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
verdicts.push(keyword === "minItems" ? value.length >= arg : value.length <= arg);
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
case "contains": {
|
|
197
|
+
if (!concrete) {
|
|
198
|
+
verdicts.push(undefined);
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
if (!Array.isArray(value)) {
|
|
202
|
+
verdicts.push(true);
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
const results = value.map((item) => evalSchema(arg, item));
|
|
206
|
+
verdicts.push(results.includes(true) ? true : results.includes(undefined) ? undefined : false);
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
case "additionalProperties":
|
|
210
|
+
verdicts.push(arg === true ? true : undefined);
|
|
211
|
+
break;
|
|
212
|
+
default:
|
|
213
|
+
verdicts.push(undefined); // a keyword this check does not evaluate
|
|
65
214
|
}
|
|
66
215
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return true;
|
|
216
|
+
return all(verdicts);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Check if a JSON schema "if" condition matches resource properties. A
|
|
220
|
+
* condition that cannot be decided does not match.
|
|
221
|
+
*/
|
|
222
|
+
function matchesCondition(condition, properties) {
|
|
223
|
+
return evalSchema(condition, properties, true) === true;
|
|
75
224
|
}
|
|
76
225
|
/**
|
|
77
226
|
* Check if a JSON schema "then" requirement holds for resource properties.
|
package/dist/manifest.json
CHANGED
package/dist/rules/ext001.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { readFileSync } from "fs";
|
|
|
15
15
|
import { join } from "path";
|
|
16
16
|
import { moduleSourceDir } from "@intentius/chant/module-source";
|
|
17
17
|
import type { PostSynthCheck, PostSynthContext, PostSynthDiagnostic } from "@intentius/chant/lint/post-synth";
|
|
18
|
-
import { parseCFTemplate, type CFResource } from "./cf-refs";
|
|
18
|
+
import { isIntrinsic, parseCFTemplate, type CFResource } from "./cf-refs";
|
|
19
19
|
|
|
20
20
|
export interface ExtensionConstraint {
|
|
21
21
|
name: string;
|
|
@@ -54,40 +54,195 @@ function loadLexiconConstraints(): Map<string, ExtensionConstraint[]> {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
57
|
+
* Three-valued result of evaluating a JSON schema against a value: true (the
|
|
58
|
+
* schema holds), false (it does not), undefined (it cannot be decided from the
|
|
59
|
+
* template alone). A condition fires a finding only on `true`, so an
|
|
60
|
+
* undecidable one stays silent. A wrong finding costs more than a missed one
|
|
61
|
+
* here, and an error-severity rule stops the build.
|
|
58
62
|
*/
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
63
|
+
type Verdict = boolean | undefined;
|
|
64
|
+
|
|
65
|
+
/** Keywords that describe a schema and never change what it accepts. */
|
|
66
|
+
const ANNOTATION_KEYWORDS = new Set([
|
|
67
|
+
"title", "description", "$comment", "$schema", "$id", "examples", "default", "deprecated",
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
function sameJson(a: unknown, b: unknown): boolean {
|
|
71
|
+
return a === b || JSON.stringify(a) === JSON.stringify(b);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Combine the verdicts of keywords that must all hold. */
|
|
75
|
+
function all(verdicts: Verdict[]): Verdict {
|
|
76
|
+
if (verdicts.includes(false)) return false;
|
|
77
|
+
if (verdicts.includes(undefined)) return undefined;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function matchesType(type: string, value: unknown): Verdict {
|
|
82
|
+
switch (type) {
|
|
83
|
+
case "string": return typeof value === "string";
|
|
84
|
+
case "boolean": return typeof value === "boolean";
|
|
85
|
+
case "null": return value === null;
|
|
86
|
+
case "array": return Array.isArray(value);
|
|
87
|
+
case "object": return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
88
|
+
case "number": return typeof value === "number" ? true : typeof value === "string" ? undefined : false;
|
|
89
|
+
case "integer":
|
|
90
|
+
if (typeof value === "number") return Number.isInteger(value);
|
|
91
|
+
return typeof value === "string" ? undefined : false;
|
|
92
|
+
default: return undefined;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Evaluate a JSON schema against a present value. Supports the keywords the
|
|
98
|
+
* generated extension constraints use. Any other keyword makes the answer
|
|
99
|
+
* undecidable, and so does a value that is a CloudFormation intrinsic
|
|
100
|
+
* (Ref, Fn::*) where the keyword needs the concrete value.
|
|
101
|
+
*
|
|
102
|
+
* `topLevel` is set for the resource's own Properties object. There a property
|
|
103
|
+
* named by a schema but absent from the resource does not satisfy it, which is
|
|
104
|
+
* how this check has always read conditions (cfn-lint pairs them with
|
|
105
|
+
* `required`); a `false` schema is the one exception and means "absent".
|
|
106
|
+
*/
|
|
107
|
+
function evalSchema(schema: unknown, value: unknown, topLevel = false): Verdict {
|
|
108
|
+
if (schema === true) return true;
|
|
109
|
+
if (schema === false) return false;
|
|
110
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema)) return undefined;
|
|
111
|
+
const s = schema as Record<string, unknown>;
|
|
112
|
+
const concrete = !isIntrinsic(value);
|
|
113
|
+
const verdicts: Verdict[] = [];
|
|
114
|
+
|
|
115
|
+
for (const [keyword, arg] of Object.entries(s)) {
|
|
116
|
+
if (ANNOTATION_KEYWORDS.has(keyword)) continue;
|
|
117
|
+
switch (keyword) {
|
|
118
|
+
case "const":
|
|
119
|
+
verdicts.push(concrete ? sameJson(value, arg) : undefined);
|
|
120
|
+
break;
|
|
121
|
+
case "enum":
|
|
122
|
+
verdicts.push(!Array.isArray(arg) ? undefined : concrete ? arg.some((e) => sameJson(value, e)) : undefined);
|
|
123
|
+
break;
|
|
124
|
+
case "pattern": {
|
|
125
|
+
if (typeof arg !== "string" || !concrete || typeof value !== "string") {
|
|
126
|
+
verdicts.push(undefined);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
verdicts.push(new RegExp(arg).test(value));
|
|
131
|
+
} catch {
|
|
132
|
+
verdicts.push(undefined); // not a JavaScript regular expression
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
72
135
|
}
|
|
73
|
-
|
|
74
|
-
if (!
|
|
136
|
+
case "type": {
|
|
137
|
+
if (!concrete) {
|
|
138
|
+
verdicts.push(undefined);
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
const types = Array.isArray(arg) ? arg : [arg];
|
|
142
|
+
const results = types.map((t) => (typeof t === "string" ? matchesType(t, value) : undefined));
|
|
143
|
+
verdicts.push(results.includes(true) ? true : results.includes(undefined) ? undefined : false);
|
|
144
|
+
break;
|
|
75
145
|
}
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
|
|
146
|
+
case "not": {
|
|
147
|
+
const v = evalSchema(arg, value);
|
|
148
|
+
verdicts.push(v === undefined ? undefined : !v);
|
|
149
|
+
break;
|
|
79
150
|
}
|
|
151
|
+
case "allOf":
|
|
152
|
+
verdicts.push(Array.isArray(arg) ? all(arg.map((a) => evalSchema(a, value))) : undefined);
|
|
153
|
+
break;
|
|
154
|
+
case "anyOf": {
|
|
155
|
+
if (!Array.isArray(arg)) {
|
|
156
|
+
verdicts.push(undefined);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
const results = arg.map((a) => evalSchema(a, value));
|
|
160
|
+
verdicts.push(results.includes(true) ? true : results.includes(undefined) ? undefined : false);
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
case "oneOf": {
|
|
164
|
+
if (!Array.isArray(arg)) {
|
|
165
|
+
verdicts.push(undefined);
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
const results = arg.map((a) => evalSchema(a, value));
|
|
169
|
+
const yes = results.filter((r) => r === true).length;
|
|
170
|
+
const unknown = results.filter((r) => r === undefined).length;
|
|
171
|
+
verdicts.push(yes > 1 ? false : unknown === 0 ? yes === 1 : undefined);
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
case "required": {
|
|
175
|
+
if (!Array.isArray(arg)) {
|
|
176
|
+
verdicts.push(undefined);
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
180
|
+
verdicts.push(concrete ? true : undefined);
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
verdicts.push(arg.every((r) => r in (value as Record<string, unknown>)));
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
case "properties": {
|
|
187
|
+
if (!arg || typeof arg !== "object" || Array.isArray(arg)) {
|
|
188
|
+
verdicts.push(undefined);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
192
|
+
verdicts.push(concrete ? true : undefined);
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
const obj = value as Record<string, unknown>;
|
|
196
|
+
for (const [name, sub] of Object.entries(arg as Record<string, unknown>)) {
|
|
197
|
+
const present = name in obj;
|
|
198
|
+
if (sub === false) verdicts.push(!present);
|
|
199
|
+
else if (!present) verdicts.push(topLevel ? false : true);
|
|
200
|
+
else verdicts.push(evalSchema(sub, obj[name]));
|
|
201
|
+
}
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case "minItems":
|
|
205
|
+
case "maxItems": {
|
|
206
|
+
if (typeof arg !== "number" || !concrete) {
|
|
207
|
+
verdicts.push(undefined);
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
if (!Array.isArray(value)) {
|
|
211
|
+
verdicts.push(true);
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
verdicts.push(keyword === "minItems" ? value.length >= arg : value.length <= arg);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case "contains": {
|
|
218
|
+
if (!concrete) {
|
|
219
|
+
verdicts.push(undefined);
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
if (!Array.isArray(value)) {
|
|
223
|
+
verdicts.push(true);
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
const results = value.map((item) => evalSchema(arg, item));
|
|
227
|
+
verdicts.push(results.includes(true) ? true : results.includes(undefined) ? undefined : false);
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
case "additionalProperties":
|
|
231
|
+
verdicts.push(arg === true ? true : undefined);
|
|
232
|
+
break;
|
|
233
|
+
default:
|
|
234
|
+
verdicts.push(undefined); // a keyword this check does not evaluate
|
|
80
235
|
}
|
|
81
236
|
}
|
|
237
|
+
return all(verdicts);
|
|
238
|
+
}
|
|
82
239
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return true;
|
|
240
|
+
/**
|
|
241
|
+
* Check if a JSON schema "if" condition matches resource properties. A
|
|
242
|
+
* condition that cannot be decided does not match.
|
|
243
|
+
*/
|
|
244
|
+
function matchesCondition(condition: unknown, properties: Record<string, unknown>): boolean {
|
|
245
|
+
return evalSchema(condition, properties, true) === true;
|
|
91
246
|
}
|
|
92
247
|
|
|
93
248
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intentius/chant-lexicon-aws",
|
|
3
|
-
"version": "0.124.
|
|
3
|
+
"version": "0.124.98",
|
|
4
4
|
"description": "AWS CloudFormation lexicon for chant — declarative IaC in TypeScript",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://intentius.io/chant",
|
|
@@ -83,6 +83,6 @@
|
|
|
83
83
|
"typescript": "^5.9.3"
|
|
84
84
|
},
|
|
85
85
|
"peerDependencies": {
|
|
86
|
-
"@intentius/chant": "^0.124.
|
|
86
|
+
"@intentius/chant": "^0.124.98"
|
|
87
87
|
}
|
|
88
88
|
}
|
|
@@ -292,4 +292,39 @@ describe("CFParser conditions and outputs (#2069)", () => {
|
|
|
292
292
|
const ir = parser.parse(template);
|
|
293
293
|
expect(ir.warnings).toBeUndefined();
|
|
294
294
|
});
|
|
295
|
+
|
|
296
|
+
describe("YAML dates (#5076)", () => {
|
|
297
|
+
const read = (yamlText: string) => parser.parse(yamlText);
|
|
298
|
+
|
|
299
|
+
test("an unquoted date reads as the string it was written as", () => {
|
|
300
|
+
const ir = read("AWSTemplateFormatVersion: 2010-09-09\nResources:\n Q:\n Type: AWS::SQS::Queue\n Properties:\n Doc:\n Version: 2012-10-17\n");
|
|
301
|
+
expect(ir.metadata?.version).toBe("2010-09-09");
|
|
302
|
+
expect(ir.resources[0].properties.Doc).toEqual({ Version: "2012-10-17" });
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test("a date-like string in a list, a tag argument and a nested map stays a string", () => {
|
|
306
|
+
const ir = read(`Resources:
|
|
307
|
+
Q:
|
|
308
|
+
Type: AWS::SQS::Queue
|
|
309
|
+
Properties:
|
|
310
|
+
Dates: [2012-10-17, 2024-02-29]
|
|
311
|
+
Name: !Sub "x-\${A}"
|
|
312
|
+
Nested:
|
|
313
|
+
Join: !Join ["-", [a, 2012-10-17]]
|
|
314
|
+
`);
|
|
315
|
+
const props = ir.resources[0].properties as any;
|
|
316
|
+
expect(props.Dates).toEqual(["2012-10-17", "2024-02-29"]);
|
|
317
|
+
expect(props.Nested.Join).toMatchObject({ delimiter: "-", values: ["a", "2012-10-17"] });
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test("a full timestamp stays a string, with its time and zone as written", () => {
|
|
321
|
+
const ir = read("Resources:\n Q:\n Type: AWS::SQS::Queue\n Properties:\n A: 2001-12-14T21:59:43.10-05:00\n B: 2001-12-14 21:59:43\n C: 2002-12-14T00:00:00Z\n");
|
|
322
|
+
expect(ir.resources[0].properties).toEqual({ A: "2001-12-14T21:59:43.10-05:00", B: "2001-12-14 21:59:43", C: "2002-12-14T00:00:00Z" });
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
test("merge keys and anchors still resolve", () => {
|
|
326
|
+
const ir = read("Resources:\n Q:\n Type: AWS::SQS::Queue\n Properties:\n A: &a {X: 1}\n B:\n <<: *a\n Y: 2\n");
|
|
327
|
+
expect((ir.resources[0].properties as any).B).toEqual({ X: 1, Y: 2 });
|
|
328
|
+
});
|
|
329
|
+
});
|
|
295
330
|
});
|
package/src/import/parser.ts
CHANGED
|
@@ -112,7 +112,21 @@ const cfnYamlTypes = [
|
|
|
112
112
|
}),
|
|
113
113
|
];
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
/**
|
|
116
|
+
* js-yaml's default schema without the YAML 1.1 timestamp type. With it, an
|
|
117
|
+
* unquoted `Version: 2012-10-17` is read as a `Date`, which the generator then
|
|
118
|
+
* writes as `{}` (#5076). CloudFormation reads such a value as the string it
|
|
119
|
+
* was written as, so the schema has no implicit timestamp resolver and a
|
|
120
|
+
* date-like scalar (or a full ISO timestamp) stays a string. Merge keys and
|
|
121
|
+
* the other default types are kept.
|
|
122
|
+
*/
|
|
123
|
+
// js-yaml exports its built-in types at runtime; @types/js-yaml does not declare them.
|
|
124
|
+
const builtin = (yaml as unknown as { types: Record<"merge" | "binary" | "omap" | "pairs" | "set", yaml.Type> }).types;
|
|
125
|
+
|
|
126
|
+
export const CF_SCHEMA = yaml.CORE_SCHEMA.extend({
|
|
127
|
+
implicit: [builtin.merge],
|
|
128
|
+
explicit: [builtin.binary, builtin.omap, builtin.pairs, builtin.set, ...cfnYamlTypes],
|
|
129
|
+
});
|
|
116
130
|
|
|
117
131
|
/**
|
|
118
132
|
* CloudFormation template structure
|
|
@@ -320,3 +320,69 @@ describe("Mappings and Fn::FindInMap", () => {
|
|
|
320
320
|
expect(() => generator.generate(parser.parse(JSON.stringify(original)))).toThrow(/cannot generate Fn::Transform/);
|
|
321
321
|
});
|
|
322
322
|
});
|
|
323
|
+
|
|
324
|
+
describe("YAML dates (#5076)", () => {
|
|
325
|
+
const policy = `Resources:
|
|
326
|
+
Role:
|
|
327
|
+
Type: AWS::IAM::Role
|
|
328
|
+
Properties:
|
|
329
|
+
AssumeRolePolicyDocument:
|
|
330
|
+
Version: 2012-10-17
|
|
331
|
+
Statement:
|
|
332
|
+
- Effect: Allow
|
|
333
|
+
Principal:
|
|
334
|
+
Service: ec2.amazonaws.com
|
|
335
|
+
Action: sts:AssumeRole
|
|
336
|
+
Policies:
|
|
337
|
+
- PolicyName: read
|
|
338
|
+
PolicyDocument:
|
|
339
|
+
Version: 2012-10-17
|
|
340
|
+
Statement:
|
|
341
|
+
- Effect: Allow
|
|
342
|
+
Action: s3:GetObject
|
|
343
|
+
Resource: !Sub "arn:aws:s3:::\${Bucket}/*"
|
|
344
|
+
Bucket:
|
|
345
|
+
Type: AWS::S3::Bucket
|
|
346
|
+
Properties:
|
|
347
|
+
BucketName: !Sub
|
|
348
|
+
- "logs-\${Day}"
|
|
349
|
+
- Day: 2024-01-15
|
|
350
|
+
`;
|
|
351
|
+
|
|
352
|
+
test("an unquoted Version: 2012-10-17 builds as the string, not {}", async () => {
|
|
353
|
+
const { template, source } = await roundTrip(policy);
|
|
354
|
+
expect(source).toContain('Version: "2012-10-17"');
|
|
355
|
+
const props = template.Resources.Role.Properties;
|
|
356
|
+
expect(props.AssumeRolePolicyDocument.Version).toBe("2012-10-17");
|
|
357
|
+
expect(props.Policies[0].PolicyDocument.Version).toBe("2012-10-17");
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
test("a date inside an Fn::Sub variable map is substituted as written", async () => {
|
|
361
|
+
const { template } = await roundTrip(policy);
|
|
362
|
+
expect(template.Resources.Bucket.Properties.BucketName).toEqual({ "Fn::Sub": "logs-2024-01-15" });
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
test("a date or timestamp as a parameter default, mapping value and output builds as written", async () => {
|
|
366
|
+
const { template } = await roundTrip(`AWSTemplateFormatVersion: 2010-09-09
|
|
367
|
+
Parameters:
|
|
368
|
+
Since:
|
|
369
|
+
Type: String
|
|
370
|
+
Default: 2001-12-14t21:59:43.10-05:00
|
|
371
|
+
Mappings:
|
|
372
|
+
Releases:
|
|
373
|
+
stable:
|
|
374
|
+
Date: 2024-01-15
|
|
375
|
+
Resources:
|
|
376
|
+
Queue:
|
|
377
|
+
Type: AWS::SQS::Queue
|
|
378
|
+
Properties:
|
|
379
|
+
QueueName: q
|
|
380
|
+
Outputs:
|
|
381
|
+
Stamp:
|
|
382
|
+
Value: 2024-01-15 10:00:00
|
|
383
|
+
`);
|
|
384
|
+
expect(template.Parameters.Since.Default).toBe("2001-12-14t21:59:43.10-05:00");
|
|
385
|
+
expect(template.Mappings.Releases.stable.Date).toBe("2024-01-15");
|
|
386
|
+
expect(template.Outputs.Stamp.Value).toBe("2024-01-15 10:00:00");
|
|
387
|
+
});
|
|
388
|
+
});
|
|
@@ -249,3 +249,117 @@ describe("EXT001: Extension Constraint Violation", () => {
|
|
|
249
249
|
expect(diags).toHaveLength(0);
|
|
250
250
|
});
|
|
251
251
|
});
|
|
252
|
+
|
|
253
|
+
describe("EXT001: schema keywords in a condition", () => {
|
|
254
|
+
// The S3 constraint as the generated lexicon carries it: OwnershipControls is
|
|
255
|
+
// required unless AccessControl is one of the three private canned ACLs.
|
|
256
|
+
const s3Constraint: ExtensionConstraint = {
|
|
257
|
+
name: "ownershipcontrols",
|
|
258
|
+
type: "if_then",
|
|
259
|
+
condition: {
|
|
260
|
+
properties: {
|
|
261
|
+
AccessControl: { not: { enum: ["Private", "BucketOwnerFullControl", "BucketOwnerRead"] }, type: "string" },
|
|
262
|
+
},
|
|
263
|
+
required: ["AccessControl"],
|
|
264
|
+
},
|
|
265
|
+
requirement: { properties: { OwnershipControls: { required: ["Rules"] } }, required: ["OwnershipControls"] },
|
|
266
|
+
};
|
|
267
|
+
const s3 = new Map([["AWS::S3::Bucket", [s3Constraint]]]);
|
|
268
|
+
const bucket = (props: Record<string, unknown>) =>
|
|
269
|
+
makeCtx({ Resources: { B: { Type: "AWS::S3::Bucket", Properties: props } } });
|
|
270
|
+
|
|
271
|
+
test.each(["Private", "BucketOwnerFullControl", "BucketOwnerRead"])(
|
|
272
|
+
"not enum: AccessControl %s is exempt",
|
|
273
|
+
(acl) => {
|
|
274
|
+
expect(checkExtensionConstraints(bucket({ AccessControl: acl }), s3)).toHaveLength(0);
|
|
275
|
+
},
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
test.each(["PublicRead", "PublicReadWrite", "AuthenticatedRead", "LogDeliveryWrite"])(
|
|
279
|
+
"still flags: AccessControl %s without OwnershipControls",
|
|
280
|
+
(acl) => {
|
|
281
|
+
const diags = checkExtensionConstraints(bucket({ AccessControl: acl }), s3);
|
|
282
|
+
expect(diags).toHaveLength(1);
|
|
283
|
+
expect(diags[0].message).toContain("OwnershipControls");
|
|
284
|
+
},
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
test("still satisfied: public ACL with OwnershipControls", () => {
|
|
288
|
+
const ctx = bucket({ AccessControl: "PublicRead", OwnershipControls: { Rules: [{ ObjectOwnership: "BucketOwnerPreferred" }] } });
|
|
289
|
+
expect(checkExtensionConstraints(ctx, s3)).toHaveLength(0);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("no finding without AccessControl", () => {
|
|
293
|
+
expect(checkExtensionConstraints(bucket({ BucketName: "x" }), s3)).toHaveLength(0);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test("undecidable value stays silent: Ref and Fn::If", () => {
|
|
297
|
+
expect(checkExtensionConstraints(bucket({ AccessControl: { Ref: "Acl" } }), s3)).toHaveLength(0);
|
|
298
|
+
const cond = { "Fn::If": ["Public", "PublicRead", "Private"] };
|
|
299
|
+
expect(checkExtensionConstraints(bucket({ AccessControl: cond }), s3)).toHaveLength(0);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test("a keyword the check does not evaluate keeps the finding silent", () => {
|
|
303
|
+
const map = new Map<string, ExtensionConstraint[]>([
|
|
304
|
+
["AWS::S3::Bucket", [{
|
|
305
|
+
name: "unknown-keyword",
|
|
306
|
+
type: "if_then",
|
|
307
|
+
condition: { properties: { AccessControl: { format: "something" } }, required: ["AccessControl"] },
|
|
308
|
+
requirement: { required: ["OwnershipControls"] },
|
|
309
|
+
}]],
|
|
310
|
+
]);
|
|
311
|
+
expect(checkExtensionConstraints(bucket({ AccessControl: "PublicRead" }), map)).toHaveLength(0);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test("type narrows a condition", () => {
|
|
315
|
+
const map = new Map<string, ExtensionConstraint[]>([
|
|
316
|
+
["AWS::S3::Bucket", [{
|
|
317
|
+
name: "typed",
|
|
318
|
+
type: "if_then",
|
|
319
|
+
condition: { properties: { AccessControl: { type: "string" } }, required: ["AccessControl"] },
|
|
320
|
+
requirement: { required: ["OwnershipControls"] },
|
|
321
|
+
}]],
|
|
322
|
+
]);
|
|
323
|
+
expect(checkExtensionConstraints(bucket({ AccessControl: "PublicRead" }), map)).toHaveLength(1);
|
|
324
|
+
expect(checkExtensionConstraints(bucket({ AccessControl: ["x"] }), map)).toHaveLength(0);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("anyOf, oneOf, allOf and a false property schema", () => {
|
|
328
|
+
const rule = (condition: unknown) =>
|
|
329
|
+
new Map<string, ExtensionConstraint[]>([
|
|
330
|
+
["AWS::ElasticLoadBalancingV2::LoadBalancer", [{
|
|
331
|
+
name: "c", type: "if_then", condition, requirement: { required: ["Subnets"] },
|
|
332
|
+
}]],
|
|
333
|
+
]);
|
|
334
|
+
const lb = (props: Record<string, unknown>) =>
|
|
335
|
+
makeCtx({ Resources: { L: { Type: "AWS::ElasticLoadBalancingV2::LoadBalancer", Properties: props } } });
|
|
336
|
+
// The generated shape: type is application, or absent.
|
|
337
|
+
const oneOf = rule({
|
|
338
|
+
oneOf: [
|
|
339
|
+
{ properties: { Type: { const: "application" } }, required: ["Type"] },
|
|
340
|
+
{ properties: { Type: false } },
|
|
341
|
+
],
|
|
342
|
+
});
|
|
343
|
+
expect(checkExtensionConstraints(lb({ Type: "application" }), oneOf)).toHaveLength(1);
|
|
344
|
+
expect(checkExtensionConstraints(lb({ Name: "n" }), oneOf)).toHaveLength(1);
|
|
345
|
+
expect(checkExtensionConstraints(lb({ Type: "network" }), oneOf)).toHaveLength(0);
|
|
346
|
+
|
|
347
|
+
const anyOf = rule({ anyOf: [{ required: ["A"] }, { required: ["B"] }] });
|
|
348
|
+
expect(checkExtensionConstraints(lb({ B: 1 }), anyOf)).toHaveLength(1);
|
|
349
|
+
expect(checkExtensionConstraints(lb({ C: 1 }), anyOf)).toHaveLength(0);
|
|
350
|
+
|
|
351
|
+
const allOf = rule({ allOf: [{ required: ["A"] }, { not: { required: ["B"] } }] });
|
|
352
|
+
expect(checkExtensionConstraints(lb({ A: 1 }), allOf)).toHaveLength(1);
|
|
353
|
+
expect(checkExtensionConstraints(lb({ A: 1, B: 1 }), allOf)).toHaveLength(0);
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
describe("EXT001: generated lexicon constraints", () => {
|
|
358
|
+
const bucket = (props: Record<string, unknown>) =>
|
|
359
|
+
makeCtx({ Resources: { B: { Type: "AWS::S3::Bucket", Properties: props } } });
|
|
360
|
+
|
|
361
|
+
test("AccessControl Private is not flagged; PublicRead without OwnershipControls is", () => {
|
|
362
|
+
expect(ext001.check(bucket({ AccessControl: "Private" }))).toHaveLength(0);
|
|
363
|
+
expect(ext001.check(bucket({ AccessControl: "PublicRead" }))).toHaveLength(1);
|
|
364
|
+
});
|
|
365
|
+
});
|
|
@@ -15,7 +15,7 @@ import { readFileSync } from "fs";
|
|
|
15
15
|
import { join } from "path";
|
|
16
16
|
import { moduleSourceDir } from "@intentius/chant/module-source";
|
|
17
17
|
import type { PostSynthCheck, PostSynthContext, PostSynthDiagnostic } from "@intentius/chant/lint/post-synth";
|
|
18
|
-
import { parseCFTemplate, type CFResource } from "./cf-refs";
|
|
18
|
+
import { isIntrinsic, parseCFTemplate, type CFResource } from "./cf-refs";
|
|
19
19
|
|
|
20
20
|
export interface ExtensionConstraint {
|
|
21
21
|
name: string;
|
|
@@ -54,40 +54,195 @@ function loadLexiconConstraints(): Map<string, ExtensionConstraint[]> {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
57
|
+
* Three-valued result of evaluating a JSON schema against a value: true (the
|
|
58
|
+
* schema holds), false (it does not), undefined (it cannot be decided from the
|
|
59
|
+
* template alone). A condition fires a finding only on `true`, so an
|
|
60
|
+
* undecidable one stays silent. A wrong finding costs more than a missed one
|
|
61
|
+
* here, and an error-severity rule stops the build.
|
|
58
62
|
*/
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
63
|
+
type Verdict = boolean | undefined;
|
|
64
|
+
|
|
65
|
+
/** Keywords that describe a schema and never change what it accepts. */
|
|
66
|
+
const ANNOTATION_KEYWORDS = new Set([
|
|
67
|
+
"title", "description", "$comment", "$schema", "$id", "examples", "default", "deprecated",
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
function sameJson(a: unknown, b: unknown): boolean {
|
|
71
|
+
return a === b || JSON.stringify(a) === JSON.stringify(b);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Combine the verdicts of keywords that must all hold. */
|
|
75
|
+
function all(verdicts: Verdict[]): Verdict {
|
|
76
|
+
if (verdicts.includes(false)) return false;
|
|
77
|
+
if (verdicts.includes(undefined)) return undefined;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function matchesType(type: string, value: unknown): Verdict {
|
|
82
|
+
switch (type) {
|
|
83
|
+
case "string": return typeof value === "string";
|
|
84
|
+
case "boolean": return typeof value === "boolean";
|
|
85
|
+
case "null": return value === null;
|
|
86
|
+
case "array": return Array.isArray(value);
|
|
87
|
+
case "object": return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
88
|
+
case "number": return typeof value === "number" ? true : typeof value === "string" ? undefined : false;
|
|
89
|
+
case "integer":
|
|
90
|
+
if (typeof value === "number") return Number.isInteger(value);
|
|
91
|
+
return typeof value === "string" ? undefined : false;
|
|
92
|
+
default: return undefined;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Evaluate a JSON schema against a present value. Supports the keywords the
|
|
98
|
+
* generated extension constraints use. Any other keyword makes the answer
|
|
99
|
+
* undecidable, and so does a value that is a CloudFormation intrinsic
|
|
100
|
+
* (Ref, Fn::*) where the keyword needs the concrete value.
|
|
101
|
+
*
|
|
102
|
+
* `topLevel` is set for the resource's own Properties object. There a property
|
|
103
|
+
* named by a schema but absent from the resource does not satisfy it, which is
|
|
104
|
+
* how this check has always read conditions (cfn-lint pairs them with
|
|
105
|
+
* `required`); a `false` schema is the one exception and means "absent".
|
|
106
|
+
*/
|
|
107
|
+
function evalSchema(schema: unknown, value: unknown, topLevel = false): Verdict {
|
|
108
|
+
if (schema === true) return true;
|
|
109
|
+
if (schema === false) return false;
|
|
110
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema)) return undefined;
|
|
111
|
+
const s = schema as Record<string, unknown>;
|
|
112
|
+
const concrete = !isIntrinsic(value);
|
|
113
|
+
const verdicts: Verdict[] = [];
|
|
114
|
+
|
|
115
|
+
for (const [keyword, arg] of Object.entries(s)) {
|
|
116
|
+
if (ANNOTATION_KEYWORDS.has(keyword)) continue;
|
|
117
|
+
switch (keyword) {
|
|
118
|
+
case "const":
|
|
119
|
+
verdicts.push(concrete ? sameJson(value, arg) : undefined);
|
|
120
|
+
break;
|
|
121
|
+
case "enum":
|
|
122
|
+
verdicts.push(!Array.isArray(arg) ? undefined : concrete ? arg.some((e) => sameJson(value, e)) : undefined);
|
|
123
|
+
break;
|
|
124
|
+
case "pattern": {
|
|
125
|
+
if (typeof arg !== "string" || !concrete || typeof value !== "string") {
|
|
126
|
+
verdicts.push(undefined);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
verdicts.push(new RegExp(arg).test(value));
|
|
131
|
+
} catch {
|
|
132
|
+
verdicts.push(undefined); // not a JavaScript regular expression
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
72
135
|
}
|
|
73
|
-
|
|
74
|
-
if (!
|
|
136
|
+
case "type": {
|
|
137
|
+
if (!concrete) {
|
|
138
|
+
verdicts.push(undefined);
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
const types = Array.isArray(arg) ? arg : [arg];
|
|
142
|
+
const results = types.map((t) => (typeof t === "string" ? matchesType(t, value) : undefined));
|
|
143
|
+
verdicts.push(results.includes(true) ? true : results.includes(undefined) ? undefined : false);
|
|
144
|
+
break;
|
|
75
145
|
}
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
|
|
146
|
+
case "not": {
|
|
147
|
+
const v = evalSchema(arg, value);
|
|
148
|
+
verdicts.push(v === undefined ? undefined : !v);
|
|
149
|
+
break;
|
|
79
150
|
}
|
|
151
|
+
case "allOf":
|
|
152
|
+
verdicts.push(Array.isArray(arg) ? all(arg.map((a) => evalSchema(a, value))) : undefined);
|
|
153
|
+
break;
|
|
154
|
+
case "anyOf": {
|
|
155
|
+
if (!Array.isArray(arg)) {
|
|
156
|
+
verdicts.push(undefined);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
const results = arg.map((a) => evalSchema(a, value));
|
|
160
|
+
verdicts.push(results.includes(true) ? true : results.includes(undefined) ? undefined : false);
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
case "oneOf": {
|
|
164
|
+
if (!Array.isArray(arg)) {
|
|
165
|
+
verdicts.push(undefined);
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
const results = arg.map((a) => evalSchema(a, value));
|
|
169
|
+
const yes = results.filter((r) => r === true).length;
|
|
170
|
+
const unknown = results.filter((r) => r === undefined).length;
|
|
171
|
+
verdicts.push(yes > 1 ? false : unknown === 0 ? yes === 1 : undefined);
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
case "required": {
|
|
175
|
+
if (!Array.isArray(arg)) {
|
|
176
|
+
verdicts.push(undefined);
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
180
|
+
verdicts.push(concrete ? true : undefined);
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
verdicts.push(arg.every((r) => r in (value as Record<string, unknown>)));
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
case "properties": {
|
|
187
|
+
if (!arg || typeof arg !== "object" || Array.isArray(arg)) {
|
|
188
|
+
verdicts.push(undefined);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
192
|
+
verdicts.push(concrete ? true : undefined);
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
const obj = value as Record<string, unknown>;
|
|
196
|
+
for (const [name, sub] of Object.entries(arg as Record<string, unknown>)) {
|
|
197
|
+
const present = name in obj;
|
|
198
|
+
if (sub === false) verdicts.push(!present);
|
|
199
|
+
else if (!present) verdicts.push(topLevel ? false : true);
|
|
200
|
+
else verdicts.push(evalSchema(sub, obj[name]));
|
|
201
|
+
}
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case "minItems":
|
|
205
|
+
case "maxItems": {
|
|
206
|
+
if (typeof arg !== "number" || !concrete) {
|
|
207
|
+
verdicts.push(undefined);
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
if (!Array.isArray(value)) {
|
|
211
|
+
verdicts.push(true);
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
verdicts.push(keyword === "minItems" ? value.length >= arg : value.length <= arg);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case "contains": {
|
|
218
|
+
if (!concrete) {
|
|
219
|
+
verdicts.push(undefined);
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
if (!Array.isArray(value)) {
|
|
223
|
+
verdicts.push(true);
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
const results = value.map((item) => evalSchema(arg, item));
|
|
227
|
+
verdicts.push(results.includes(true) ? true : results.includes(undefined) ? undefined : false);
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
case "additionalProperties":
|
|
231
|
+
verdicts.push(arg === true ? true : undefined);
|
|
232
|
+
break;
|
|
233
|
+
default:
|
|
234
|
+
verdicts.push(undefined); // a keyword this check does not evaluate
|
|
80
235
|
}
|
|
81
236
|
}
|
|
237
|
+
return all(verdicts);
|
|
238
|
+
}
|
|
82
239
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return true;
|
|
240
|
+
/**
|
|
241
|
+
* Check if a JSON schema "if" condition matches resource properties. A
|
|
242
|
+
* condition that cannot be decided does not match.
|
|
243
|
+
*/
|
|
244
|
+
function matchesCondition(condition: unknown, properties: Record<string, unknown>): boolean {
|
|
245
|
+
return evalSchema(condition, properties, true) === true;
|
|
91
246
|
}
|
|
92
247
|
|
|
93
248
|
/**
|