@suss/contract-terraform 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +205 -0
- package/README.md +57 -0
- package/dist/index.cjs +309 -0
- package/dist/index.d.cts +107 -0
- package/dist/index.d.ts +107 -0
- package/dist/index.js +277 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable by
|
|
79
|
+
such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and do not
|
|
117
|
+
modify the License. You may add Your own attribution notices
|
|
118
|
+
within Derivative Works that You distribute, alongside or as an
|
|
119
|
+
addendum to the NOTICE text from the Work, provided that such
|
|
120
|
+
additional attribution notices cannot be construed as modifying
|
|
121
|
+
the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor has
|
|
163
|
+
been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format please. Also add information on
|
|
185
|
+
how to contact you by electronic and paper mail.
|
|
186
|
+
|
|
187
|
+
If the program does not interact with users remotely through a computer
|
|
188
|
+
network, then your name and employer's name (e.g., your institution)
|
|
189
|
+
is sufficient to fill in the "name of copyright owner" field. Feel
|
|
190
|
+
free to omit the "name of employer" line if you are in no way affiliated
|
|
191
|
+
with any institution.
|
|
192
|
+
|
|
193
|
+
Copyright 2026 Nimbus Cloud AI LLC
|
|
194
|
+
|
|
195
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
196
|
+
you may not use this file except in compliance with the License.
|
|
197
|
+
You may obtain a copy of the License at
|
|
198
|
+
|
|
199
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
200
|
+
|
|
201
|
+
Unless required by applicable law or agreed to in writing, software
|
|
202
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
203
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
204
|
+
See the License for the specific language governing permissions and
|
|
205
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# @suss/contract-terraform
|
|
2
|
+
|
|
3
|
+
Reads the storage a Terraform configuration declares, so code can be checked against it.
|
|
4
|
+
|
|
5
|
+
## What this package is
|
|
6
|
+
|
|
7
|
+
A contract reader. It walks `.tf` files for `aws_dynamodb_table` resources and describes each one as a boundary, the same way `@suss/contract-cloudformation` describes a table a template declares.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
suss contract --from terraform infra/terraform/dynamodb -o tables.json
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The path may be one file or the directory a module lives in, since a module states its resources across several files.
|
|
14
|
+
|
|
15
|
+
## What it reads
|
|
16
|
+
|
|
17
|
+
```hcl
|
|
18
|
+
resource "aws_dynamodb_table" "orders" {
|
|
19
|
+
name = "${local.environment}-orders-v1"
|
|
20
|
+
hash_key = "order_id"
|
|
21
|
+
range_key = "placed_at"
|
|
22
|
+
|
|
23
|
+
attribute {
|
|
24
|
+
name = "order_id"
|
|
25
|
+
type = "S"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
global_secondary_index {
|
|
29
|
+
name = "by-customer-v1"
|
|
30
|
+
hash_key = "customer_id"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The resource label is the container, since that is what the rest of the configuration refers to. `name` is what the table is called once deployed, and it goes on the contract as the physical name.
|
|
36
|
+
|
|
37
|
+
That name is usually built at deploy time, and Terraform interpolates the same way CloudFormation does, so `"${local.environment}-orders-v1"` is recorded as the pattern `{local.environment}-orders-v1`. Code that builds the same name from its own variable records `{stage}-orders-v1`, and the two pair on the fixed text. This is what lets a service written in TypeScript meet a table declared in HCL.
|
|
38
|
+
|
|
39
|
+
Each secondary index becomes its own boundary, because a query through an index keys on that index's fields. `hash_key` and `range_key` say what identifies an item, partition key first, and the `attribute` blocks give each key its type.
|
|
40
|
+
|
|
41
|
+
A table declares its keys and lets every other attribute vary, so the contract says `fieldSet: "partial"` and the checker never calls an ordinary attribute unknown.
|
|
42
|
+
|
|
43
|
+
## What it will not tell you
|
|
44
|
+
|
|
45
|
+
- **Only DynamoDB tables.** An RDS instance, an S3 bucket, or an SQS queue declared in the same configuration is skipped.
|
|
46
|
+
- **A resource built with `for_each` or `count`** states one block for many tables, and this reads the block as written rather than working out what it expands to.
|
|
47
|
+
- **A name a variable supplies whole**, `name = var.table_name`, has no fixed text to pair on, so it records nothing rather than guessing.
|
|
48
|
+
|
|
49
|
+
## Where it fits in suss
|
|
50
|
+
|
|
51
|
+
Depends on `@suss/behavioral-ir` for the summaries it produces and `hcl2-parser` for reading HCL. The storage pass in `@suss/checker` pairs what code touches against what this declares.
|
|
52
|
+
|
|
53
|
+
## A way in that copies part of an item
|
|
54
|
+
|
|
55
|
+
A DynamoDB index does not carry the whole item. `projection_type = "INCLUDE"` copies the attributes it lists, `KEYS_ONLY` copies none, and both copy the index's keys and the table's. A reader asking that index for anything else gets nothing back for it, and the store raises no error, so the caller sees an item with fields missing and nothing says why.
|
|
56
|
+
|
|
57
|
+
So an index like that declares every field it will ever have, and its contract says `exhaustive` where the table's says `partial`. A pack states which attributes carry that, under `serves`, and a store without the idea leaves it out.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
terraformFileToSummaries: () => terraformFileToSummaries,
|
|
34
|
+
terraformToSummaries: () => terraformToSummaries
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
|
38
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
39
|
+
var import_hcl2_parser = __toESM(require("hcl2-parser"), 1);
|
|
40
|
+
var import_semver = __toESM(require("semver"), 1);
|
|
41
|
+
var import_behavioral_ir = require("@suss/behavioral-ir");
|
|
42
|
+
function terraformToSummaries(source, sourceFile, options) {
|
|
43
|
+
let parsed;
|
|
44
|
+
try {
|
|
45
|
+
const [read] = import_hcl2_parser.default.parseToObject(source);
|
|
46
|
+
parsed = read;
|
|
47
|
+
} catch {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
const document = asRecord(parsed);
|
|
51
|
+
if (document === null) {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
const constraints = providerConstraints(document);
|
|
55
|
+
const summaries = [];
|
|
56
|
+
for (const [resourceType, label, body] of resourcesIn(document)) {
|
|
57
|
+
for (const pack of options.packs) {
|
|
58
|
+
const pattern = patternFor(pack, resourceType, constraints);
|
|
59
|
+
if (pattern === void 0) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
summaries.push(
|
|
63
|
+
...summariesFor({ pattern, label, body, sourceFile, resourceType })
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return summaries;
|
|
68
|
+
}
|
|
69
|
+
function terraformFileToSummaries(target, options) {
|
|
70
|
+
const files = import_node_fs.default.statSync(target).isDirectory() ? import_node_fs.default.readdirSync(target).filter((name) => name.endsWith(".tf")).map((name) => import_node_path.default.join(target, name)) : [target];
|
|
71
|
+
return files.flatMap(
|
|
72
|
+
(file) => terraformToSummaries(import_node_fs.default.readFileSync(file, "utf8"), file, options)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
function providerConstraints(document) {
|
|
76
|
+
const constraints = /* @__PURE__ */ new Map();
|
|
77
|
+
for (const block of arrayOf(document.terraform)) {
|
|
78
|
+
for (const required of arrayOf(asRecord(block)?.required_providers)) {
|
|
79
|
+
const declared = asRecord(required);
|
|
80
|
+
if (declared === null) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
for (const [provider, spec] of Object.entries(declared)) {
|
|
84
|
+
for (const entry of arrayOf(spec)) {
|
|
85
|
+
const version = stringOf(asRecord(entry)?.version);
|
|
86
|
+
if (version !== null) {
|
|
87
|
+
constraints.set(provider, version);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return constraints;
|
|
94
|
+
}
|
|
95
|
+
function patternFor(pack, resourceType, constraints) {
|
|
96
|
+
const pinned = constraints.get(pack.provider);
|
|
97
|
+
return pack.resources.find((pattern) => {
|
|
98
|
+
if (pattern.resource !== resourceType) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if (pinned === void 0) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
return import_semver.default.intersects(pinned, pattern.providerVersions, {
|
|
106
|
+
loose: true
|
|
107
|
+
});
|
|
108
|
+
} catch {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
function summariesFor(opts) {
|
|
114
|
+
const { pattern } = opts;
|
|
115
|
+
if (pattern.boundary.kind === "message-bus") {
|
|
116
|
+
return [busSummary({ ...opts, boundary: pattern.boundary })];
|
|
117
|
+
}
|
|
118
|
+
const boundary = pattern.boundary;
|
|
119
|
+
const types = fieldTypes(opts.body, boundary);
|
|
120
|
+
return keyedShapes(opts.body, boundary).map(
|
|
121
|
+
(shape) => storageSummary({ ...opts, boundary, shape, types })
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
function storageSummary(opts) {
|
|
125
|
+
const { boundary, label, shape, types } = opts;
|
|
126
|
+
const address = `${opts.resourceType}.${label}`;
|
|
127
|
+
const declaredName = boundary.nameAttribute === void 0 ? null : stringOf(opts.body[boundary.nameAttribute]);
|
|
128
|
+
const physicalTable = declaredName === null ? null : (0, import_behavioral_ir.namePatternFromSub)(declaredName);
|
|
129
|
+
return {
|
|
130
|
+
kind: "library",
|
|
131
|
+
location: {
|
|
132
|
+
file: opts.sourceFile,
|
|
133
|
+
range: { start: 1, end: 1 },
|
|
134
|
+
exportName: null
|
|
135
|
+
},
|
|
136
|
+
identity: {
|
|
137
|
+
name: shape.accessPath === null ? address : `${address}#${shape.accessPath}`,
|
|
138
|
+
exportPath: null,
|
|
139
|
+
boundaryBinding: (0, import_behavioral_ir.storageBinding)({
|
|
140
|
+
recognition: "terraform",
|
|
141
|
+
storageSystem: boundary.storageSystem,
|
|
142
|
+
...boundary.transport !== void 0 ? { transport: boundary.transport } : {},
|
|
143
|
+
scope: "default",
|
|
144
|
+
container: label,
|
|
145
|
+
accessPath: shape.accessPath
|
|
146
|
+
})
|
|
147
|
+
},
|
|
148
|
+
inputs: [],
|
|
149
|
+
transitions: [],
|
|
150
|
+
gaps: [],
|
|
151
|
+
confidence: { source: "declared", level: "high" },
|
|
152
|
+
metadata: {
|
|
153
|
+
storageContract: {
|
|
154
|
+
// A way in that copies part of an item has every field it will
|
|
155
|
+
// ever have, whatever the container itself stores.
|
|
156
|
+
fieldSet: shape.serves === null ? boundary.fieldSet : "exhaustive",
|
|
157
|
+
...boundary.identifies === void 0 ? {} : {
|
|
158
|
+
identifies: { kind: "keyFields", fields: shape.keyFields },
|
|
159
|
+
fields: (shape.serves ?? shape.keyFields).map((field) => ({
|
|
160
|
+
name: field,
|
|
161
|
+
...types.has(field) ? { type: types.get(field) } : {},
|
|
162
|
+
...shape.keyFields.includes(field) ? { primary: true } : {}
|
|
163
|
+
}))
|
|
164
|
+
},
|
|
165
|
+
...physicalTable !== null ? { physicalTable } : {}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function busSummary(opts) {
|
|
171
|
+
const { boundary, label } = opts;
|
|
172
|
+
const declaredName = boundary.nameAttribute === void 0 ? null : stringOf(opts.body[boundary.nameAttribute]);
|
|
173
|
+
const physicalName = declaredName === null ? null : (0, import_behavioral_ir.namePatternFromSub)(declaredName);
|
|
174
|
+
return {
|
|
175
|
+
kind: "library",
|
|
176
|
+
location: {
|
|
177
|
+
file: opts.sourceFile,
|
|
178
|
+
range: { start: 1, end: 1 },
|
|
179
|
+
exportName: null
|
|
180
|
+
},
|
|
181
|
+
identity: {
|
|
182
|
+
name: `${opts.resourceType}.${label}`,
|
|
183
|
+
exportPath: null,
|
|
184
|
+
boundaryBinding: (0, import_behavioral_ir.messageBusBinding)({
|
|
185
|
+
recognition: "terraform",
|
|
186
|
+
messageBus: boundary.messageBus,
|
|
187
|
+
channel: label
|
|
188
|
+
})
|
|
189
|
+
},
|
|
190
|
+
inputs: [],
|
|
191
|
+
transitions: [],
|
|
192
|
+
gaps: [],
|
|
193
|
+
confidence: { source: "declared", level: "high" },
|
|
194
|
+
metadata: {
|
|
195
|
+
messageBus: {
|
|
196
|
+
...physicalName !== null ? { physicalName } : {},
|
|
197
|
+
...opts.body.fifo_queue === true ? { fifoQueue: true } : {},
|
|
198
|
+
...opts.body.fifo_topic === true ? { fifoTopic: true } : {}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function keyedShapes(body, boundary) {
|
|
204
|
+
if (boundary.identifies === void 0) {
|
|
205
|
+
return [{ accessPath: null, keyFields: [], serves: null }];
|
|
206
|
+
}
|
|
207
|
+
const ownKeys = keyFields(body, boundary.identifies);
|
|
208
|
+
const shapes = [
|
|
209
|
+
{ accessPath: null, keyFields: ownKeys, serves: null }
|
|
210
|
+
];
|
|
211
|
+
for (const block of boundary.accessPathBlocks ?? []) {
|
|
212
|
+
for (const declared of arrayOf(body[block])) {
|
|
213
|
+
const index = asRecord(declared);
|
|
214
|
+
const indexName = index === null ? null : stringOf(index.name);
|
|
215
|
+
if (index === null || indexName === null) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const indexKeys = keyFields(index, boundary.identifies);
|
|
219
|
+
shapes.push({
|
|
220
|
+
accessPath: indexName,
|
|
221
|
+
keyFields: indexKeys,
|
|
222
|
+
serves: servedFields(index, boundary, [...indexKeys, ...ownKeys])
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return shapes;
|
|
227
|
+
}
|
|
228
|
+
function servedFields(block, boundary, keys) {
|
|
229
|
+
const spec = boundary.serves;
|
|
230
|
+
if (spec === void 0) {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
const kind = stringOf(block[spec.kindAttribute]);
|
|
234
|
+
if (kind === null || kind === spec.everything) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
const listed = arrayOf(block[spec.fieldsAttribute]).flatMap(
|
|
238
|
+
(entry) => Array.isArray(entry) ? entry : [entry]
|
|
239
|
+
);
|
|
240
|
+
const named = listed.filter(
|
|
241
|
+
(entry) => typeof entry === "string"
|
|
242
|
+
);
|
|
243
|
+
return [.../* @__PURE__ */ new Set([...keys, ...named])];
|
|
244
|
+
}
|
|
245
|
+
function keyFields(block, attributes) {
|
|
246
|
+
return attributes.map((attribute) => stringOf(block[attribute])).filter((field) => field !== null);
|
|
247
|
+
}
|
|
248
|
+
function fieldTypes(body, boundary) {
|
|
249
|
+
const types = /* @__PURE__ */ new Map();
|
|
250
|
+
const spec = boundary.fieldTypes;
|
|
251
|
+
if (spec === void 0) {
|
|
252
|
+
return types;
|
|
253
|
+
}
|
|
254
|
+
for (const declared of arrayOf(body[spec.block])) {
|
|
255
|
+
const field = asRecord(declared);
|
|
256
|
+
if (field === null) {
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
const name = stringOf(field[spec.nameAttribute]);
|
|
260
|
+
const type = stringOf(field[spec.typeAttribute]);
|
|
261
|
+
if (name !== null && type !== null) {
|
|
262
|
+
types.set(name, type);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return types;
|
|
266
|
+
}
|
|
267
|
+
function resourcesIn(document) {
|
|
268
|
+
const found = [];
|
|
269
|
+
for (const resource of arrayOf(document.resource)) {
|
|
270
|
+
const byType = asRecord(resource);
|
|
271
|
+
if (byType === null) {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
for (const [resourceType, labelled] of Object.entries(byType)) {
|
|
275
|
+
for (const group of arrayOf(labelled)) {
|
|
276
|
+
const byLabel = asRecord(group);
|
|
277
|
+
if (byLabel === null) {
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
for (const [label, bodies] of Object.entries(byLabel)) {
|
|
281
|
+
for (const body of arrayOf(bodies)) {
|
|
282
|
+
const read = asRecord(body);
|
|
283
|
+
if (read !== null) {
|
|
284
|
+
found.push([resourceType, label, read]);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return found;
|
|
292
|
+
}
|
|
293
|
+
function arrayOf(value) {
|
|
294
|
+
if (value === void 0 || value === null) {
|
|
295
|
+
return [];
|
|
296
|
+
}
|
|
297
|
+
return Array.isArray(value) ? value : [value];
|
|
298
|
+
}
|
|
299
|
+
function asRecord(value) {
|
|
300
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
301
|
+
}
|
|
302
|
+
function stringOf(value) {
|
|
303
|
+
return typeof value === "string" ? value : null;
|
|
304
|
+
}
|
|
305
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
306
|
+
0 && (module.exports = {
|
|
307
|
+
terraformFileToSummaries,
|
|
308
|
+
terraformToSummaries
|
|
309
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { MessageBusTechnology, BehavioralSummary } from '@suss/behavioral-ir';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a pack says about the resources one Terraform provider declares.
|
|
5
|
+
*
|
|
6
|
+
* A provider states its own resource types, and a version of it states
|
|
7
|
+
* their shape, so both belong to a pack rather than to this reader. The
|
|
8
|
+
* reader walks HCL and matches; a pack says what `aws_dynamodb_table`
|
|
9
|
+
* is and which provider versions it is describing.
|
|
10
|
+
*
|
|
11
|
+
* Every entry is data rather than a function. A resource this shape
|
|
12
|
+
* cannot describe is a reason to widen it, rather than a reason for a
|
|
13
|
+
* pack to ship code the reader cannot see into.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** A store a caller addresses by container and key. */
|
|
17
|
+
interface StorageResource {
|
|
18
|
+
kind: "storage";
|
|
19
|
+
/** Which store this is: dynamodb, s3. */
|
|
20
|
+
storageSystem: string;
|
|
21
|
+
/** How a caller reaches it, when that is not the store's own name. */
|
|
22
|
+
transport?: string;
|
|
23
|
+
/** The attribute that says what the resource is called once deployed. */
|
|
24
|
+
nameAttribute?: string;
|
|
25
|
+
/** Whether the fields it declares are every field an item has. */
|
|
26
|
+
fieldSet: "exhaustive" | "partial" | "none";
|
|
27
|
+
/** The attributes that state what identifies an item, in key order. */
|
|
28
|
+
identifies?: string[];
|
|
29
|
+
/** Blocks that declare another way in, each keyed on its own fields. */
|
|
30
|
+
accessPathBlocks?: string[];
|
|
31
|
+
/** The block that gives each field a type, and its two attributes. */
|
|
32
|
+
fieldTypes?: {
|
|
33
|
+
block: string;
|
|
34
|
+
nameAttribute: string;
|
|
35
|
+
typeAttribute: string;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* How another way in says what it can serve. A DynamoDB index copies
|
|
39
|
+
* some of an item rather than all of it, and a reader asking for
|
|
40
|
+
* anything else gets nothing back and no error, so what it copies is
|
|
41
|
+
* every field it has rather than the ones somebody wrote down.
|
|
42
|
+
*/
|
|
43
|
+
serves?: {
|
|
44
|
+
/** The attribute that says which kind of copy it keeps. */
|
|
45
|
+
kindAttribute: string;
|
|
46
|
+
/** The attribute listing what it copies, for the kind that lists. */
|
|
47
|
+
fieldsAttribute: string;
|
|
48
|
+
/** The value of `kindAttribute` that means it copies the item. */
|
|
49
|
+
everything: string;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** A channel messages cross. */
|
|
53
|
+
interface MessageBusResource {
|
|
54
|
+
kind: "message-bus";
|
|
55
|
+
messageBus: MessageBusTechnology;
|
|
56
|
+
nameAttribute?: string;
|
|
57
|
+
}
|
|
58
|
+
type TerraformResource = StorageResource | MessageBusResource;
|
|
59
|
+
/** One resource type, as one version range of one provider declares it. */
|
|
60
|
+
interface TerraformResourcePattern {
|
|
61
|
+
/** The resource type, spelled the way the provider spells it. */
|
|
62
|
+
resource: string;
|
|
63
|
+
/**
|
|
64
|
+
* Which provider versions this describes, as a semver range. A
|
|
65
|
+
* configuration states its own constraint under `required_providers`,
|
|
66
|
+
* and an entry outside it is not read. A configuration that states no
|
|
67
|
+
* constraint is read by every entry, since nothing said otherwise.
|
|
68
|
+
*/
|
|
69
|
+
providerVersions: string;
|
|
70
|
+
/** What the resource is, once read. */
|
|
71
|
+
boundary: TerraformResource;
|
|
72
|
+
}
|
|
73
|
+
interface TerraformPack {
|
|
74
|
+
/** The pack's own name, for messages. */
|
|
75
|
+
name: string;
|
|
76
|
+
/**
|
|
77
|
+
* The provider these resources come from, as `required_providers`
|
|
78
|
+
* keys it: `aws`, `google`, `cloudflare`.
|
|
79
|
+
*/
|
|
80
|
+
provider: string;
|
|
81
|
+
resources: TerraformResourcePattern[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The boundaries a Terraform configuration declares.
|
|
86
|
+
*
|
|
87
|
+
* This reads HCL and matches what it finds against the packs a run
|
|
88
|
+
* loads. A pack says what `aws_dynamodb_table` is; nothing about any
|
|
89
|
+
* provider is written here.
|
|
90
|
+
*
|
|
91
|
+
* A name is usually built at deploy time, and Terraform interpolates
|
|
92
|
+
* the same way CloudFormation does, so `"${local.environment}-orders"`
|
|
93
|
+
* becomes the pattern `{local.environment}-orders` and pairs with code
|
|
94
|
+
* that builds the same name from its own variable. The README says how
|
|
95
|
+
* a block that HCL states once or many times is read.
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
interface TerraformReadOptions {
|
|
99
|
+
/** The packs that say what this configuration's resources are. */
|
|
100
|
+
packs: TerraformPack[];
|
|
101
|
+
}
|
|
102
|
+
/** Every boundary one Terraform configuration declares. */
|
|
103
|
+
declare function terraformToSummaries(source: string, sourceFile: string, options: TerraformReadOptions): BehavioralSummary[];
|
|
104
|
+
/** Read one `.tf` file, or every one directly inside a directory. */
|
|
105
|
+
declare function terraformFileToSummaries(target: string, options: TerraformReadOptions): BehavioralSummary[];
|
|
106
|
+
|
|
107
|
+
export { type MessageBusResource, type StorageResource, type TerraformPack, type TerraformReadOptions, type TerraformResource, type TerraformResourcePattern, terraformFileToSummaries, terraformToSummaries };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { MessageBusTechnology, BehavioralSummary } from '@suss/behavioral-ir';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a pack says about the resources one Terraform provider declares.
|
|
5
|
+
*
|
|
6
|
+
* A provider states its own resource types, and a version of it states
|
|
7
|
+
* their shape, so both belong to a pack rather than to this reader. The
|
|
8
|
+
* reader walks HCL and matches; a pack says what `aws_dynamodb_table`
|
|
9
|
+
* is and which provider versions it is describing.
|
|
10
|
+
*
|
|
11
|
+
* Every entry is data rather than a function. A resource this shape
|
|
12
|
+
* cannot describe is a reason to widen it, rather than a reason for a
|
|
13
|
+
* pack to ship code the reader cannot see into.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** A store a caller addresses by container and key. */
|
|
17
|
+
interface StorageResource {
|
|
18
|
+
kind: "storage";
|
|
19
|
+
/** Which store this is: dynamodb, s3. */
|
|
20
|
+
storageSystem: string;
|
|
21
|
+
/** How a caller reaches it, when that is not the store's own name. */
|
|
22
|
+
transport?: string;
|
|
23
|
+
/** The attribute that says what the resource is called once deployed. */
|
|
24
|
+
nameAttribute?: string;
|
|
25
|
+
/** Whether the fields it declares are every field an item has. */
|
|
26
|
+
fieldSet: "exhaustive" | "partial" | "none";
|
|
27
|
+
/** The attributes that state what identifies an item, in key order. */
|
|
28
|
+
identifies?: string[];
|
|
29
|
+
/** Blocks that declare another way in, each keyed on its own fields. */
|
|
30
|
+
accessPathBlocks?: string[];
|
|
31
|
+
/** The block that gives each field a type, and its two attributes. */
|
|
32
|
+
fieldTypes?: {
|
|
33
|
+
block: string;
|
|
34
|
+
nameAttribute: string;
|
|
35
|
+
typeAttribute: string;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* How another way in says what it can serve. A DynamoDB index copies
|
|
39
|
+
* some of an item rather than all of it, and a reader asking for
|
|
40
|
+
* anything else gets nothing back and no error, so what it copies is
|
|
41
|
+
* every field it has rather than the ones somebody wrote down.
|
|
42
|
+
*/
|
|
43
|
+
serves?: {
|
|
44
|
+
/** The attribute that says which kind of copy it keeps. */
|
|
45
|
+
kindAttribute: string;
|
|
46
|
+
/** The attribute listing what it copies, for the kind that lists. */
|
|
47
|
+
fieldsAttribute: string;
|
|
48
|
+
/** The value of `kindAttribute` that means it copies the item. */
|
|
49
|
+
everything: string;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** A channel messages cross. */
|
|
53
|
+
interface MessageBusResource {
|
|
54
|
+
kind: "message-bus";
|
|
55
|
+
messageBus: MessageBusTechnology;
|
|
56
|
+
nameAttribute?: string;
|
|
57
|
+
}
|
|
58
|
+
type TerraformResource = StorageResource | MessageBusResource;
|
|
59
|
+
/** One resource type, as one version range of one provider declares it. */
|
|
60
|
+
interface TerraformResourcePattern {
|
|
61
|
+
/** The resource type, spelled the way the provider spells it. */
|
|
62
|
+
resource: string;
|
|
63
|
+
/**
|
|
64
|
+
* Which provider versions this describes, as a semver range. A
|
|
65
|
+
* configuration states its own constraint under `required_providers`,
|
|
66
|
+
* and an entry outside it is not read. A configuration that states no
|
|
67
|
+
* constraint is read by every entry, since nothing said otherwise.
|
|
68
|
+
*/
|
|
69
|
+
providerVersions: string;
|
|
70
|
+
/** What the resource is, once read. */
|
|
71
|
+
boundary: TerraformResource;
|
|
72
|
+
}
|
|
73
|
+
interface TerraformPack {
|
|
74
|
+
/** The pack's own name, for messages. */
|
|
75
|
+
name: string;
|
|
76
|
+
/**
|
|
77
|
+
* The provider these resources come from, as `required_providers`
|
|
78
|
+
* keys it: `aws`, `google`, `cloudflare`.
|
|
79
|
+
*/
|
|
80
|
+
provider: string;
|
|
81
|
+
resources: TerraformResourcePattern[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The boundaries a Terraform configuration declares.
|
|
86
|
+
*
|
|
87
|
+
* This reads HCL and matches what it finds against the packs a run
|
|
88
|
+
* loads. A pack says what `aws_dynamodb_table` is; nothing about any
|
|
89
|
+
* provider is written here.
|
|
90
|
+
*
|
|
91
|
+
* A name is usually built at deploy time, and Terraform interpolates
|
|
92
|
+
* the same way CloudFormation does, so `"${local.environment}-orders"`
|
|
93
|
+
* becomes the pattern `{local.environment}-orders` and pairs with code
|
|
94
|
+
* that builds the same name from its own variable. The README says how
|
|
95
|
+
* a block that HCL states once or many times is read.
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
interface TerraformReadOptions {
|
|
99
|
+
/** The packs that say what this configuration's resources are. */
|
|
100
|
+
packs: TerraformPack[];
|
|
101
|
+
}
|
|
102
|
+
/** Every boundary one Terraform configuration declares. */
|
|
103
|
+
declare function terraformToSummaries(source: string, sourceFile: string, options: TerraformReadOptions): BehavioralSummary[];
|
|
104
|
+
/** Read one `.tf` file, or every one directly inside a directory. */
|
|
105
|
+
declare function terraformFileToSummaries(target: string, options: TerraformReadOptions): BehavioralSummary[];
|
|
106
|
+
|
|
107
|
+
export { type MessageBusResource, type StorageResource, type TerraformPack, type TerraformReadOptions, type TerraformResource, type TerraformResourcePattern, terraformFileToSummaries, terraformToSummaries };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import hcl2 from "hcl2-parser";
|
|
5
|
+
import semver from "semver";
|
|
6
|
+
import {
|
|
7
|
+
messageBusBinding,
|
|
8
|
+
namePatternFromSub,
|
|
9
|
+
storageBinding
|
|
10
|
+
} from "@suss/behavioral-ir";
|
|
11
|
+
function terraformToSummaries(source, sourceFile, options) {
|
|
12
|
+
let parsed;
|
|
13
|
+
try {
|
|
14
|
+
const [read] = hcl2.parseToObject(source);
|
|
15
|
+
parsed = read;
|
|
16
|
+
} catch {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
const document = asRecord(parsed);
|
|
20
|
+
if (document === null) {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
const constraints = providerConstraints(document);
|
|
24
|
+
const summaries = [];
|
|
25
|
+
for (const [resourceType, label, body] of resourcesIn(document)) {
|
|
26
|
+
for (const pack of options.packs) {
|
|
27
|
+
const pattern = patternFor(pack, resourceType, constraints);
|
|
28
|
+
if (pattern === void 0) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
summaries.push(
|
|
32
|
+
...summariesFor({ pattern, label, body, sourceFile, resourceType })
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return summaries;
|
|
37
|
+
}
|
|
38
|
+
function terraformFileToSummaries(target, options) {
|
|
39
|
+
const files = fs.statSync(target).isDirectory() ? fs.readdirSync(target).filter((name) => name.endsWith(".tf")).map((name) => path.join(target, name)) : [target];
|
|
40
|
+
return files.flatMap(
|
|
41
|
+
(file) => terraformToSummaries(fs.readFileSync(file, "utf8"), file, options)
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
function providerConstraints(document) {
|
|
45
|
+
const constraints = /* @__PURE__ */ new Map();
|
|
46
|
+
for (const block of arrayOf(document.terraform)) {
|
|
47
|
+
for (const required of arrayOf(asRecord(block)?.required_providers)) {
|
|
48
|
+
const declared = asRecord(required);
|
|
49
|
+
if (declared === null) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
for (const [provider, spec] of Object.entries(declared)) {
|
|
53
|
+
for (const entry of arrayOf(spec)) {
|
|
54
|
+
const version = stringOf(asRecord(entry)?.version);
|
|
55
|
+
if (version !== null) {
|
|
56
|
+
constraints.set(provider, version);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return constraints;
|
|
63
|
+
}
|
|
64
|
+
function patternFor(pack, resourceType, constraints) {
|
|
65
|
+
const pinned = constraints.get(pack.provider);
|
|
66
|
+
return pack.resources.find((pattern) => {
|
|
67
|
+
if (pattern.resource !== resourceType) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
if (pinned === void 0) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
return semver.intersects(pinned, pattern.providerVersions, {
|
|
75
|
+
loose: true
|
|
76
|
+
});
|
|
77
|
+
} catch {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function summariesFor(opts) {
|
|
83
|
+
const { pattern } = opts;
|
|
84
|
+
if (pattern.boundary.kind === "message-bus") {
|
|
85
|
+
return [busSummary({ ...opts, boundary: pattern.boundary })];
|
|
86
|
+
}
|
|
87
|
+
const boundary = pattern.boundary;
|
|
88
|
+
const types = fieldTypes(opts.body, boundary);
|
|
89
|
+
return keyedShapes(opts.body, boundary).map(
|
|
90
|
+
(shape) => storageSummary({ ...opts, boundary, shape, types })
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
function storageSummary(opts) {
|
|
94
|
+
const { boundary, label, shape, types } = opts;
|
|
95
|
+
const address = `${opts.resourceType}.${label}`;
|
|
96
|
+
const declaredName = boundary.nameAttribute === void 0 ? null : stringOf(opts.body[boundary.nameAttribute]);
|
|
97
|
+
const physicalTable = declaredName === null ? null : namePatternFromSub(declaredName);
|
|
98
|
+
return {
|
|
99
|
+
kind: "library",
|
|
100
|
+
location: {
|
|
101
|
+
file: opts.sourceFile,
|
|
102
|
+
range: { start: 1, end: 1 },
|
|
103
|
+
exportName: null
|
|
104
|
+
},
|
|
105
|
+
identity: {
|
|
106
|
+
name: shape.accessPath === null ? address : `${address}#${shape.accessPath}`,
|
|
107
|
+
exportPath: null,
|
|
108
|
+
boundaryBinding: storageBinding({
|
|
109
|
+
recognition: "terraform",
|
|
110
|
+
storageSystem: boundary.storageSystem,
|
|
111
|
+
...boundary.transport !== void 0 ? { transport: boundary.transport } : {},
|
|
112
|
+
scope: "default",
|
|
113
|
+
container: label,
|
|
114
|
+
accessPath: shape.accessPath
|
|
115
|
+
})
|
|
116
|
+
},
|
|
117
|
+
inputs: [],
|
|
118
|
+
transitions: [],
|
|
119
|
+
gaps: [],
|
|
120
|
+
confidence: { source: "declared", level: "high" },
|
|
121
|
+
metadata: {
|
|
122
|
+
storageContract: {
|
|
123
|
+
// A way in that copies part of an item has every field it will
|
|
124
|
+
// ever have, whatever the container itself stores.
|
|
125
|
+
fieldSet: shape.serves === null ? boundary.fieldSet : "exhaustive",
|
|
126
|
+
...boundary.identifies === void 0 ? {} : {
|
|
127
|
+
identifies: { kind: "keyFields", fields: shape.keyFields },
|
|
128
|
+
fields: (shape.serves ?? shape.keyFields).map((field) => ({
|
|
129
|
+
name: field,
|
|
130
|
+
...types.has(field) ? { type: types.get(field) } : {},
|
|
131
|
+
...shape.keyFields.includes(field) ? { primary: true } : {}
|
|
132
|
+
}))
|
|
133
|
+
},
|
|
134
|
+
...physicalTable !== null ? { physicalTable } : {}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function busSummary(opts) {
|
|
140
|
+
const { boundary, label } = opts;
|
|
141
|
+
const declaredName = boundary.nameAttribute === void 0 ? null : stringOf(opts.body[boundary.nameAttribute]);
|
|
142
|
+
const physicalName = declaredName === null ? null : namePatternFromSub(declaredName);
|
|
143
|
+
return {
|
|
144
|
+
kind: "library",
|
|
145
|
+
location: {
|
|
146
|
+
file: opts.sourceFile,
|
|
147
|
+
range: { start: 1, end: 1 },
|
|
148
|
+
exportName: null
|
|
149
|
+
},
|
|
150
|
+
identity: {
|
|
151
|
+
name: `${opts.resourceType}.${label}`,
|
|
152
|
+
exportPath: null,
|
|
153
|
+
boundaryBinding: messageBusBinding({
|
|
154
|
+
recognition: "terraform",
|
|
155
|
+
messageBus: boundary.messageBus,
|
|
156
|
+
channel: label
|
|
157
|
+
})
|
|
158
|
+
},
|
|
159
|
+
inputs: [],
|
|
160
|
+
transitions: [],
|
|
161
|
+
gaps: [],
|
|
162
|
+
confidence: { source: "declared", level: "high" },
|
|
163
|
+
metadata: {
|
|
164
|
+
messageBus: {
|
|
165
|
+
...physicalName !== null ? { physicalName } : {},
|
|
166
|
+
...opts.body.fifo_queue === true ? { fifoQueue: true } : {},
|
|
167
|
+
...opts.body.fifo_topic === true ? { fifoTopic: true } : {}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function keyedShapes(body, boundary) {
|
|
173
|
+
if (boundary.identifies === void 0) {
|
|
174
|
+
return [{ accessPath: null, keyFields: [], serves: null }];
|
|
175
|
+
}
|
|
176
|
+
const ownKeys = keyFields(body, boundary.identifies);
|
|
177
|
+
const shapes = [
|
|
178
|
+
{ accessPath: null, keyFields: ownKeys, serves: null }
|
|
179
|
+
];
|
|
180
|
+
for (const block of boundary.accessPathBlocks ?? []) {
|
|
181
|
+
for (const declared of arrayOf(body[block])) {
|
|
182
|
+
const index = asRecord(declared);
|
|
183
|
+
const indexName = index === null ? null : stringOf(index.name);
|
|
184
|
+
if (index === null || indexName === null) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const indexKeys = keyFields(index, boundary.identifies);
|
|
188
|
+
shapes.push({
|
|
189
|
+
accessPath: indexName,
|
|
190
|
+
keyFields: indexKeys,
|
|
191
|
+
serves: servedFields(index, boundary, [...indexKeys, ...ownKeys])
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return shapes;
|
|
196
|
+
}
|
|
197
|
+
function servedFields(block, boundary, keys) {
|
|
198
|
+
const spec = boundary.serves;
|
|
199
|
+
if (spec === void 0) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
const kind = stringOf(block[spec.kindAttribute]);
|
|
203
|
+
if (kind === null || kind === spec.everything) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
const listed = arrayOf(block[spec.fieldsAttribute]).flatMap(
|
|
207
|
+
(entry) => Array.isArray(entry) ? entry : [entry]
|
|
208
|
+
);
|
|
209
|
+
const named = listed.filter(
|
|
210
|
+
(entry) => typeof entry === "string"
|
|
211
|
+
);
|
|
212
|
+
return [.../* @__PURE__ */ new Set([...keys, ...named])];
|
|
213
|
+
}
|
|
214
|
+
function keyFields(block, attributes) {
|
|
215
|
+
return attributes.map((attribute) => stringOf(block[attribute])).filter((field) => field !== null);
|
|
216
|
+
}
|
|
217
|
+
function fieldTypes(body, boundary) {
|
|
218
|
+
const types = /* @__PURE__ */ new Map();
|
|
219
|
+
const spec = boundary.fieldTypes;
|
|
220
|
+
if (spec === void 0) {
|
|
221
|
+
return types;
|
|
222
|
+
}
|
|
223
|
+
for (const declared of arrayOf(body[spec.block])) {
|
|
224
|
+
const field = asRecord(declared);
|
|
225
|
+
if (field === null) {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const name = stringOf(field[spec.nameAttribute]);
|
|
229
|
+
const type = stringOf(field[spec.typeAttribute]);
|
|
230
|
+
if (name !== null && type !== null) {
|
|
231
|
+
types.set(name, type);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return types;
|
|
235
|
+
}
|
|
236
|
+
function resourcesIn(document) {
|
|
237
|
+
const found = [];
|
|
238
|
+
for (const resource of arrayOf(document.resource)) {
|
|
239
|
+
const byType = asRecord(resource);
|
|
240
|
+
if (byType === null) {
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
for (const [resourceType, labelled] of Object.entries(byType)) {
|
|
244
|
+
for (const group of arrayOf(labelled)) {
|
|
245
|
+
const byLabel = asRecord(group);
|
|
246
|
+
if (byLabel === null) {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
for (const [label, bodies] of Object.entries(byLabel)) {
|
|
250
|
+
for (const body of arrayOf(bodies)) {
|
|
251
|
+
const read = asRecord(body);
|
|
252
|
+
if (read !== null) {
|
|
253
|
+
found.push([resourceType, label, read]);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return found;
|
|
261
|
+
}
|
|
262
|
+
function arrayOf(value) {
|
|
263
|
+
if (value === void 0 || value === null) {
|
|
264
|
+
return [];
|
|
265
|
+
}
|
|
266
|
+
return Array.isArray(value) ? value : [value];
|
|
267
|
+
}
|
|
268
|
+
function asRecord(value) {
|
|
269
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
270
|
+
}
|
|
271
|
+
function stringOf(value) {
|
|
272
|
+
return typeof value === "string" ? value : null;
|
|
273
|
+
}
|
|
274
|
+
export {
|
|
275
|
+
terraformFileToSummaries,
|
|
276
|
+
terraformToSummaries
|
|
277
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@suss/contract-terraform",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Read the storage a Terraform configuration declares, for checking code against it.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"suss",
|
|
8
|
+
"behavioral-summary",
|
|
9
|
+
"terraform",
|
|
10
|
+
"dynamodb",
|
|
11
|
+
"storage"
|
|
12
|
+
],
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"author": "Nimbus Cloud AI LLC",
|
|
15
|
+
"homepage": "https://github.com/nimbuscloud-ai/suss/tree/main/packages/contract/terraform#readme",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/nimbuscloud-ai/suss.git",
|
|
19
|
+
"directory": "packages/contract/terraform"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/nimbuscloud-ai/suss/issues"
|
|
23
|
+
},
|
|
24
|
+
"main": "./dist/index.cjs",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js",
|
|
31
|
+
"require": "./dist/index.cjs"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsup",
|
|
41
|
+
"test": "vitest run",
|
|
42
|
+
"test:coverage": "vitest run --coverage",
|
|
43
|
+
"lint": "biome check .",
|
|
44
|
+
"lint:fix": "biome check --write .",
|
|
45
|
+
"typecheck": "tsc --noEmit"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@suss/behavioral-ir": "0.7.0",
|
|
49
|
+
"hcl2-parser": "^1.0.3",
|
|
50
|
+
"semver": "^7.6.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/semver": "*",
|
|
54
|
+
"tsup": "*",
|
|
55
|
+
"typescript": "*",
|
|
56
|
+
"vitest": "*"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
}
|
|
61
|
+
}
|