@huaweidevtools/aishell-dsh-deploy 0.0.18 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/lib/data/terraform-examples.zip +0 -0
- package/lib/index.js +1579 -556
- package/lib/types/account-view.d.ts +174 -0
- package/lib/types/design-render.d.ts +64 -0
- package/lib/types/diagram.d.ts +49 -0
- package/lib/types/layout.d.ts +145 -0
- package/lib/types/svg-render.d.ts +146 -0
- package/lib/types/traffic.d.ts +105 -0
- package/lib/types/version.d.ts +1 -1
- package/package.json +10 -11
- package/lib/types/d2-design.d.ts +0 -60
- package/lib/types/d2-engine.d.ts +0 -111
- package/lib/types/d2-inventory.d.ts +0 -82
package/lib/index.js
CHANGED
|
@@ -11,7 +11,6 @@ import { readStoredPair } from "@huaweidevtools/aishell-dsh-credentials-store/ac
|
|
|
11
11
|
import { verifyCredentials } from "@huaweidevtools/aishell-dsh-credentials-store/verify";
|
|
12
12
|
import { createHash, createHmac } from "node:crypto";
|
|
13
13
|
import { createRequire } from "node:module";
|
|
14
|
-
import { D2 } from "@terrastruct/d2";
|
|
15
14
|
import { JSONPath } from "jsonpath-plus";
|
|
16
15
|
import AdmZip from "adm-zip";
|
|
17
16
|
|
|
@@ -1692,7 +1691,7 @@ function withAnchoredPaths(args, ctx) {
|
|
|
1692
1691
|
* written by earlier releases carry it — so it is kept even though there is no
|
|
1693
1692
|
* server.
|
|
1694
1693
|
*/
|
|
1695
|
-
const SERVER_VERSION = "aishell-deploy 0.0.
|
|
1694
|
+
const SERVER_VERSION = "aishell-deploy 0.0.20";
|
|
1696
1695
|
|
|
1697
1696
|
//#endregion
|
|
1698
1697
|
//#region src/deployment-state.ts
|
|
@@ -3923,7 +3922,7 @@ async function handlePrice(deployment) {
|
|
|
3923
3922
|
period: "month",
|
|
3924
3923
|
hours: HOURS_PER_MONTH,
|
|
3925
3924
|
priced_at: pricedAt,
|
|
3926
|
-
regions: [...new Set(quotes.map((quote
|
|
3925
|
+
regions: [...new Set(quotes.map((quote) => quote.region))].sort(),
|
|
3927
3926
|
tf_hash: await computeTfHash(deployment),
|
|
3928
3927
|
plan_file: PLAN_FILE,
|
|
3929
3928
|
nothing_billable: nothingBillable,
|
|
@@ -4336,14 +4335,14 @@ function statesNothingBillable(quoted, unpriced) {
|
|
|
4336
4335
|
* total, which is `null` when there is none to compute.
|
|
4337
4336
|
*/
|
|
4338
4337
|
function quoteTotal(quotes) {
|
|
4339
|
-
const currencies = [...new Set(quotes.map((quote
|
|
4338
|
+
const currencies = [...new Set(quotes.map((quote) => quote.currency))].sort();
|
|
4340
4339
|
if (quotes.length === 0 || currencies.length > 1) return {
|
|
4341
4340
|
currencies,
|
|
4342
4341
|
total: null
|
|
4343
4342
|
};
|
|
4344
4343
|
return {
|
|
4345
4344
|
currencies,
|
|
4346
|
-
total: round(quotes.reduce((sum, quote
|
|
4345
|
+
total: round(quotes.reduce((sum, quote) => sum + quote.monthly, 0))
|
|
4347
4346
|
};
|
|
4348
4347
|
}
|
|
4349
4348
|
/**
|
|
@@ -5800,29 +5799,610 @@ function edgesOf(blocks, topology) {
|
|
|
5800
5799
|
}
|
|
5801
5800
|
|
|
5802
5801
|
//#endregion
|
|
5803
|
-
//#region src/
|
|
5804
|
-
/**
|
|
5805
|
-
|
|
5806
|
-
|
|
5807
|
-
|
|
5808
|
-
|
|
5809
|
-
|
|
5810
|
-
|
|
5811
|
-
|
|
5812
|
-
|
|
5813
|
-
|
|
5814
|
-
|
|
5815
|
-
|
|
5816
|
-
|
|
5817
|
-
|
|
5818
|
-
|
|
5819
|
-
|
|
5820
|
-
|
|
5821
|
-
|
|
5822
|
-
|
|
5823
|
-
|
|
5824
|
-
|
|
5802
|
+
//#region src/layout.ts
|
|
5803
|
+
/**
|
|
5804
|
+
* Horizontal space one character of a label takes.
|
|
5805
|
+
*
|
|
5806
|
+
* Measured against the font the diagram is drawn with: the renderer emits
|
|
5807
|
+
* `font-size: 16` with a sans-serif face, whose average advance is a little
|
|
5808
|
+
* over half an em. 9.6 is 0.6em, which is slightly generous — a box that is a
|
|
5809
|
+
* few pixels too wide reads as padding, and one that is too narrow truncates a
|
|
5810
|
+
* name.
|
|
5811
|
+
*/
|
|
5812
|
+
const CHAR_WIDTH = 9.6;
|
|
5813
|
+
/** Line height for a label at that size. */
|
|
5814
|
+
const LINE_HEIGHT = 22;
|
|
5815
|
+
/** Line height for a detail line, which is drawn a size smaller. */
|
|
5816
|
+
const DETAIL_LINE_HEIGHT = 17;
|
|
5817
|
+
/**
|
|
5818
|
+
* The text a box carries, with its count in brackets.
|
|
5819
|
+
*
|
|
5820
|
+
* `sg-postgresql [1]` rather than a separate figure: the count qualifies the
|
|
5821
|
+
* name, and putting it there keeps the box one line of identity instead of two
|
|
5822
|
+
* blocks a reader has to associate.
|
|
5823
|
+
*
|
|
5824
|
+
* @param label - the box's own label.
|
|
5825
|
+
* @param figure - the count, when there is one.
|
|
5826
|
+
* @returns the label with the count appended.
|
|
5827
|
+
*/
|
|
5828
|
+
function labelWithFigure(label, figure) {
|
|
5829
|
+
return figure === void 0 ? label : `${label} [${figure}]`;
|
|
5830
|
+
}
|
|
5831
|
+
/**
|
|
5832
|
+
* The height of a container's title band, including its detail lines.
|
|
5833
|
+
*
|
|
5834
|
+
* A container's own label sits at the top of the band and each detail line
|
|
5835
|
+
* under it, so the band has to be tall enough for both — plus the gap that
|
|
5836
|
+
* keeps the last line clear of the first row of children.
|
|
5837
|
+
*
|
|
5838
|
+
* `measure` sizes the box with this and `place` positions the children with it.
|
|
5839
|
+
* Those two must be the same number: a band sized for the label alone puts the
|
|
5840
|
+
* first row of children where the detail lines are drawn, and the lines land on
|
|
5841
|
+
* top of the child boxes.
|
|
5842
|
+
*
|
|
5843
|
+
* @param detail - the container's detail lines.
|
|
5844
|
+
* @returns the band height in pixels.
|
|
5845
|
+
*/
|
|
5846
|
+
function bandHeight(detail) {
|
|
5847
|
+
return detail.length === 0 ? TITLE_BAND : TITLE_BAND + detail.length * DETAIL_LINE_HEIGHT + SIBLING_GAP / 2;
|
|
5848
|
+
}
|
|
5849
|
+
/**
|
|
5850
|
+
* The width the widest detail line needs.
|
|
5851
|
+
*
|
|
5852
|
+
* @param detail - the lines under a container's label.
|
|
5853
|
+
* @returns the width in pixels, 0 when there are none.
|
|
5854
|
+
*/
|
|
5855
|
+
function textWidthOf(detail) {
|
|
5856
|
+
return detail.length === 0 ? 0 : Math.max(...detail.map((line) => textWidth(line)));
|
|
5857
|
+
}
|
|
5858
|
+
/** Space between a container's border and its contents. */
|
|
5859
|
+
const PADDING$1 = 16;
|
|
5860
|
+
/** Space between sibling boxes. */
|
|
5861
|
+
const SIBLING_GAP = 20;
|
|
5862
|
+
/**
|
|
5863
|
+
* The band at a container's top that holds its own label.
|
|
5864
|
+
*
|
|
5865
|
+
* 34, not the 26 that would fit a 13px line exactly: the label's baseline sits
|
|
5866
|
+
* 24 below the top edge, so text occupies roughly 18–30 and leaves 4px above
|
|
5867
|
+
* and 4px below. A band sized to the type exactly reads as pressed against the
|
|
5868
|
+
* frame, which is the thing this margin exists to prevent.
|
|
5869
|
+
*/
|
|
5870
|
+
const TITLE_BAND = 34;
|
|
5871
|
+
/** The narrowest a leaf box is allowed to be, so a short name is not a sliver. */
|
|
5872
|
+
const MIN_LEAF_WIDTH = 120;
|
|
5873
|
+
/**
|
|
5874
|
+
* The width a label needs.
|
|
5875
|
+
*
|
|
5876
|
+
* @param label - the text on the box.
|
|
5877
|
+
* @returns its width in pixels.
|
|
5878
|
+
*/
|
|
5879
|
+
function textWidth(label) {
|
|
5880
|
+
return label.length * CHAR_WIDTH;
|
|
5881
|
+
}
|
|
5882
|
+
/**
|
|
5883
|
+
* The extent of a set of boxes packed into a given number of columns.
|
|
5884
|
+
*
|
|
5885
|
+
* @param sizes - the child extents.
|
|
5886
|
+
* @param columns - how many share a row.
|
|
5887
|
+
* @returns the packed width and height.
|
|
5888
|
+
*/
|
|
5889
|
+
function pack(sizes, columns) {
|
|
5890
|
+
const rows = Math.ceil(sizes.length / columns);
|
|
5891
|
+
let width = 0;
|
|
5892
|
+
let height = 0;
|
|
5893
|
+
for (let row = 0; row < rows; row++) {
|
|
5894
|
+
const slice = sizes.slice(row * columns, (row + 1) * columns);
|
|
5895
|
+
width = Math.max(width, slice.reduce((sum, size) => sum + size.width, 0) + SIBLING_GAP * (slice.length - 1));
|
|
5896
|
+
height += Math.max(...slice.map((size) => size.height)) + (row < rows - 1 ? SIBLING_GAP : 0);
|
|
5897
|
+
}
|
|
5898
|
+
return {
|
|
5899
|
+
width,
|
|
5900
|
+
height
|
|
5901
|
+
};
|
|
5902
|
+
}
|
|
5903
|
+
/**
|
|
5904
|
+
* The column count that packs a set of boxes into the least elongated block.
|
|
5905
|
+
*
|
|
5906
|
+
* The measure is logarithmic, so a block twice as wide as it is tall scores the
|
|
5907
|
+
* same as one twice as tall as it is wide. A linear measure would call a wide
|
|
5908
|
+
* strip "close enough" whenever the target were small, which is the failure
|
|
5909
|
+
* this whole module exists to avoid.
|
|
5910
|
+
*
|
|
5911
|
+
* @param sizes - the child extents.
|
|
5912
|
+
* @returns the best column count, at least 1.
|
|
5913
|
+
*/
|
|
5914
|
+
function squarestColumns(sizes) {
|
|
5915
|
+
let best = 1;
|
|
5916
|
+
let bestError = Infinity;
|
|
5917
|
+
const limit = Math.min(sizes.length, 4);
|
|
5918
|
+
for (let columns = 1; columns <= limit; columns++) {
|
|
5919
|
+
const packed = pack(sizes, columns);
|
|
5920
|
+
const error = Math.abs(Math.log(packed.width / packed.height));
|
|
5921
|
+
if (error < bestError) {
|
|
5922
|
+
bestError = error;
|
|
5923
|
+
best = columns;
|
|
5924
|
+
}
|
|
5925
|
+
}
|
|
5926
|
+
return best;
|
|
5927
|
+
}
|
|
5928
|
+
/**
|
|
5929
|
+
* Measure a tree, bottom up.
|
|
5930
|
+
*
|
|
5931
|
+
* @param node - the node to measure.
|
|
5932
|
+
* @returns its extent and its children's, all positioned at the origin.
|
|
5933
|
+
*/
|
|
5934
|
+
function measure(node) {
|
|
5935
|
+
if (node.children.length === 0) {
|
|
5936
|
+
const detail$1 = node.detail ?? [];
|
|
5937
|
+
const label = labelWithFigure(node.label, node.figure);
|
|
5938
|
+
const widest = Math.max(textWidth(label), ...detail$1.map((line) => textWidth(line)));
|
|
5939
|
+
return {
|
|
5940
|
+
node,
|
|
5941
|
+
width: Math.max(widest + PADDING$1 * 2, MIN_LEAF_WIDTH),
|
|
5942
|
+
height: LINE_HEIGHT + PADDING$1 + detail$1.length * DETAIL_LINE_HEIGHT,
|
|
5943
|
+
columns: 1,
|
|
5944
|
+
children: []
|
|
5945
|
+
};
|
|
5946
|
+
}
|
|
5947
|
+
const children = equalWidth(node.children.map((child) => measure(child)));
|
|
5948
|
+
const columns = squarestColumns(children);
|
|
5949
|
+
const packed = pack(children, columns);
|
|
5950
|
+
const detail = node.detail ?? [];
|
|
5951
|
+
return {
|
|
5952
|
+
node,
|
|
5953
|
+
width: Math.max(packed.width, textWidthOf(detail)) + PADDING$1 * 2,
|
|
5954
|
+
height: packed.height + bandHeight(detail) + PADDING$1,
|
|
5955
|
+
columns,
|
|
5956
|
+
children
|
|
5957
|
+
};
|
|
5958
|
+
}
|
|
5959
|
+
/**
|
|
5960
|
+
* Give every sibling the width of the widest one.
|
|
5961
|
+
*
|
|
5962
|
+
* The width is applied to each child's OWN width, so a container among them
|
|
5963
|
+
* keeps room for its own contents; only the excess is added, never taken away.
|
|
5964
|
+
* Heights are left alone — a tall container beside a short leaf is a real
|
|
5965
|
+
* difference, and equalising it would pad every leaf to the size of a VPC.
|
|
5966
|
+
*
|
|
5967
|
+
* @param siblings - the measured children of one parent.
|
|
5968
|
+
* @returns the same children, widened to a common width.
|
|
5969
|
+
*/
|
|
5970
|
+
function equalWidth(siblings) {
|
|
5971
|
+
if (siblings.length < 2) return [...siblings];
|
|
5972
|
+
const widest = Math.max(...siblings.map((child) => child.width));
|
|
5973
|
+
return siblings.map((child) => child.width === widest ? child : {
|
|
5974
|
+
...child,
|
|
5975
|
+
width: widest
|
|
5976
|
+
});
|
|
5977
|
+
}
|
|
5978
|
+
/**
|
|
5979
|
+
* Position a measured tree, top down.
|
|
5980
|
+
*
|
|
5981
|
+
* @param measured - the tree to place.
|
|
5982
|
+
* @param x - the left edge to place it at.
|
|
5983
|
+
* @param y - the top edge to place it at.
|
|
5984
|
+
* @param depth - the nesting depth, for the caller's styling.
|
|
5985
|
+
* @returns the placed boxes.
|
|
5986
|
+
*/
|
|
5987
|
+
function place(measured, x, y, depth) {
|
|
5988
|
+
const self = {
|
|
5989
|
+
id: measured.node.id,
|
|
5990
|
+
label: measured.node.label,
|
|
5991
|
+
x,
|
|
5992
|
+
y,
|
|
5993
|
+
width: measured.width,
|
|
5994
|
+
height: measured.height,
|
|
5995
|
+
depth,
|
|
5996
|
+
existing: measured.node.existing === true,
|
|
5997
|
+
provider: measured.node.provider ?? "",
|
|
5998
|
+
detail: measured.node.detail ?? [],
|
|
5999
|
+
figure: measured.node.figure,
|
|
6000
|
+
children: []
|
|
6001
|
+
};
|
|
6002
|
+
if (measured.children.length === 0) return [self];
|
|
6003
|
+
const direct = [];
|
|
6004
|
+
const descendants = [];
|
|
6005
|
+
const rows = Math.ceil(measured.children.length / measured.columns);
|
|
6006
|
+
let cursorY = y + bandHeight(measured.node.detail ?? []);
|
|
6007
|
+
for (let row = 0; row < rows; row++) {
|
|
6008
|
+
const slice = measured.children.slice(row * measured.columns, (row + 1) * measured.columns);
|
|
6009
|
+
const rowWidth = slice.reduce((sum, child) => sum + child.width, 0) + SIBLING_GAP * (slice.length - 1);
|
|
6010
|
+
let cursorX = x + PADDING$1 + (measured.width - PADDING$1 * 2 - rowWidth) / 2;
|
|
6011
|
+
for (const child of slice) {
|
|
6012
|
+
const [self_, ...rest] = place(child, cursorX, cursorY, depth + 1);
|
|
6013
|
+
direct.push(self_);
|
|
6014
|
+
descendants.push(self_, ...rest);
|
|
6015
|
+
cursorX += child.width + SIBLING_GAP;
|
|
6016
|
+
}
|
|
6017
|
+
cursorY += Math.max(...slice.map((child) => child.height)) + SIBLING_GAP;
|
|
6018
|
+
}
|
|
6019
|
+
return [{
|
|
6020
|
+
...self,
|
|
6021
|
+
children: direct
|
|
6022
|
+
}, ...descendants];
|
|
6023
|
+
}
|
|
6024
|
+
/**
|
|
6025
|
+
* Lay out a tree of nested boxes.
|
|
6026
|
+
*
|
|
6027
|
+
* ## How the roots are arranged
|
|
6028
|
+
*
|
|
6029
|
+
* The roots are the same layer of the picture — a VPC, a band of shared
|
|
6030
|
+
* services — so they are packed beside each other rather than stacked. Which
|
|
6031
|
+
* The roots go in ONE row, however narrow the result. They are the top of the
|
|
6032
|
+
* picture — regions, usually — and a reader compares them left to right: which
|
|
6033
|
+
* region holds the VPC, which holds only a default group. Wrapping them onto
|
|
6034
|
+
* several rows puts two regions above two others with nothing saying they are
|
|
6035
|
+
* peers, and the eye reads that as a hierarchy that does not exist.
|
|
6036
|
+
*
|
|
6037
|
+
* Everything below a root is packed by the square rule, because there the
|
|
6038
|
+
* alternatives really are equivalent and the aspect decides.
|
|
6039
|
+
*
|
|
6040
|
+
* @param roots - the outermost boxes.
|
|
6041
|
+
* @param margin - space left around the whole drawing.
|
|
6042
|
+
* @returns the placed boxes and the canvas extent.
|
|
6043
|
+
*/
|
|
6044
|
+
function layoutTree(roots, margin = 40) {
|
|
6045
|
+
const measured = roots.map((root) => measure(root));
|
|
6046
|
+
const columns = measured.length;
|
|
6047
|
+
const packed = pack(measured, columns);
|
|
6048
|
+
const placed = [];
|
|
6049
|
+
const rows = Math.ceil(measured.length / columns);
|
|
6050
|
+
let cursorY = margin;
|
|
6051
|
+
for (let row = 0; row < rows; row++) {
|
|
6052
|
+
const slice = measured.slice(row * columns, (row + 1) * columns);
|
|
6053
|
+
const rowWidth = slice.reduce((sum, box) => sum + box.width, 0) + SIBLING_GAP * (slice.length - 1);
|
|
6054
|
+
let cursorX = margin + (packed.width - rowWidth) / 2;
|
|
6055
|
+
for (const box of slice) {
|
|
6056
|
+
placed.push(...place(box, cursorX, cursorY, 0));
|
|
6057
|
+
cursorX += box.width + SIBLING_GAP;
|
|
6058
|
+
}
|
|
6059
|
+
cursorY += Math.max(...slice.map((box) => box.height)) + SIBLING_GAP;
|
|
6060
|
+
}
|
|
6061
|
+
return {
|
|
6062
|
+
boxes: placed,
|
|
6063
|
+
roots: placed.filter((box) => box.depth === 0),
|
|
6064
|
+
width: packed.width + margin * 2,
|
|
6065
|
+
height: cursorY - SIBLING_GAP + margin
|
|
6066
|
+
};
|
|
6067
|
+
}
|
|
6068
|
+
|
|
6069
|
+
//#endregion
|
|
6070
|
+
//#region src/svg-render.ts
|
|
6071
|
+
/**
|
|
6072
|
+
* The hues a container is drawn in, by its nesting depth.
|
|
6073
|
+
*
|
|
6074
|
+
* Depth alone cannot say WHICH thing a box is — a security group and an ECS sit
|
|
6075
|
+
* at the same level — so this is not an identity, only a way to read the nesting
|
|
6076
|
+
* at a glance. Two colours alternate down the levels, which is enough to see
|
|
6077
|
+
* where one container ends and the next begins without turning the picture into
|
|
6078
|
+
* a palette.
|
|
6079
|
+
*
|
|
6080
|
+
* A resource's own service is a different question, answered by `serviceHue`.
|
|
6081
|
+
*/
|
|
6082
|
+
const CONTAINER_DEPTH_FILL = [
|
|
6083
|
+
"#e8eef7",
|
|
6084
|
+
"#dde7f5",
|
|
6085
|
+
"#d2dff2"
|
|
5825
6086
|
];
|
|
6087
|
+
const CONTAINER_DEPTH_STROKE = [
|
|
6088
|
+
"#9aa8bd",
|
|
6089
|
+
"#8294af",
|
|
6090
|
+
"#6b80a0"
|
|
6091
|
+
];
|
|
6092
|
+
/**
|
|
6093
|
+
* Convert HSL to a `#rrggbb` string.
|
|
6094
|
+
*
|
|
6095
|
+
* The conversion is done here rather than left to the renderer, because several
|
|
6096
|
+
* SVG rasterisers do not parse the space-separated CSS Color 4 syntax and fall
|
|
6097
|
+
* back to black without reporting anything.
|
|
6098
|
+
*
|
|
6099
|
+
* @param hue - 0–359.
|
|
6100
|
+
* @param saturation - 0–100.
|
|
6101
|
+
* @param lightness - 0–100.
|
|
6102
|
+
* @returns the colour as hex.
|
|
6103
|
+
*/
|
|
6104
|
+
function hslToHex(hue, saturation, lightness) {
|
|
6105
|
+
const s = saturation / 100;
|
|
6106
|
+
const l = lightness / 100;
|
|
6107
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
6108
|
+
const x = c * (1 - Math.abs(hue / 60 % 2 - 1));
|
|
6109
|
+
const m = l - c / 2;
|
|
6110
|
+
const [r, g, b] = hue < 60 ? [
|
|
6111
|
+
c,
|
|
6112
|
+
x,
|
|
6113
|
+
0
|
|
6114
|
+
] : hue < 120 ? [
|
|
6115
|
+
x,
|
|
6116
|
+
c,
|
|
6117
|
+
0
|
|
6118
|
+
] : hue < 180 ? [
|
|
6119
|
+
0,
|
|
6120
|
+
c,
|
|
6121
|
+
x
|
|
6122
|
+
] : hue < 240 ? [
|
|
6123
|
+
0,
|
|
6124
|
+
x,
|
|
6125
|
+
c
|
|
6126
|
+
] : hue < 300 ? [
|
|
6127
|
+
x,
|
|
6128
|
+
0,
|
|
6129
|
+
c
|
|
6130
|
+
] : [
|
|
6131
|
+
c,
|
|
6132
|
+
0,
|
|
6133
|
+
x
|
|
6134
|
+
];
|
|
6135
|
+
const channel = (value) => Math.round((value + m) * 255).toString(16).padStart(2, "0");
|
|
6136
|
+
return `#${channel(r)}${channel(g)}${channel(b)}`;
|
|
6137
|
+
}
|
|
6138
|
+
/**
|
|
6139
|
+
* The hue a service's boxes are tinted with.
|
|
6140
|
+
*
|
|
6141
|
+
* Keyed by the provider the inventory reports, which is a stable identity: every
|
|
6142
|
+
* `ecs` resource is the same service whatever it is called. A hash rather than a
|
|
6143
|
+
* table, for the reason the connector colours use one — a table would need a
|
|
6144
|
+
* line per service and would be wrong the first time an unfamiliar one appeared.
|
|
6145
|
+
*
|
|
6146
|
+
* @param provider - the service that reported the resource.
|
|
6147
|
+
* @returns a hue in degrees, 0–359.
|
|
6148
|
+
*/
|
|
6149
|
+
function serviceHue(provider) {
|
|
6150
|
+
let value = 0;
|
|
6151
|
+
for (const character of provider) value = value * 31 + character.charCodeAt(0) >>> 0;
|
|
6152
|
+
return value % 360;
|
|
6153
|
+
}
|
|
6154
|
+
/**
|
|
6155
|
+
* The fill and stroke for one box.
|
|
6156
|
+
*
|
|
6157
|
+
* @param role - whether the box holds other boxes.
|
|
6158
|
+
* @param provider - the service that reported it, for a resource.
|
|
6159
|
+
* @param depth - the nesting level, for a container.
|
|
6160
|
+
* @returns the two colours.
|
|
6161
|
+
*/
|
|
6162
|
+
function boxColours(role, provider, depth) {
|
|
6163
|
+
if (role === "container") {
|
|
6164
|
+
const level = Math.min(depth, CONTAINER_DEPTH_FILL.length - 1);
|
|
6165
|
+
return {
|
|
6166
|
+
fill: CONTAINER_DEPTH_FILL[level],
|
|
6167
|
+
stroke: CONTAINER_DEPTH_STROKE[level]
|
|
6168
|
+
};
|
|
6169
|
+
}
|
|
6170
|
+
const hue = serviceHue(provider);
|
|
6171
|
+
return {
|
|
6172
|
+
fill: hslToHex(hue, 45, 88),
|
|
6173
|
+
stroke: hslToHex(hue, 40, 55)
|
|
6174
|
+
};
|
|
6175
|
+
}
|
|
6176
|
+
/** Colours and metrics, kept in one place so both themes stay in step. */
|
|
6177
|
+
const TOKENS = {
|
|
6178
|
+
ink: "#1b1f24",
|
|
6179
|
+
muted: "#57606a",
|
|
6180
|
+
canvas: "#ffffff",
|
|
6181
|
+
containerFill: "#eef4ff",
|
|
6182
|
+
containerStroke: "#9cc0f5",
|
|
6183
|
+
resourceFill: "#f7f8fa",
|
|
6184
|
+
resourceStroke: "#c7ccd4",
|
|
6185
|
+
traffic: "#0969da",
|
|
6186
|
+
policy: "#8250df",
|
|
6187
|
+
labelBackground: "#ffffff"
|
|
6188
|
+
};
|
|
6189
|
+
/**
|
|
6190
|
+
* Escape text for XML content and attributes.
|
|
6191
|
+
*
|
|
6192
|
+
* @param text - the string to escape.
|
|
6193
|
+
* @returns the same text with the five XML metacharacters replaced.
|
|
6194
|
+
*/
|
|
6195
|
+
function escapeXml(text) {
|
|
6196
|
+
return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'");
|
|
6197
|
+
}
|
|
6198
|
+
/**
|
|
6199
|
+
* Where a connector meets a box's border, on the side facing the other box.
|
|
6200
|
+
*
|
|
6201
|
+
* The connector is drawn as an orthogonal path, so it leaves through a
|
|
6202
|
+
* horizontal edge when the boxes are stacked and through a vertical edge when
|
|
6203
|
+
* they are side by side. Picking the side from the relative position is what
|
|
6204
|
+
* keeps the first and last segment perpendicular to the border it touches —
|
|
6205
|
+
* a line meeting a box at an angle reads as passing behind it.
|
|
6206
|
+
*
|
|
6207
|
+
* @param box - the box whose border point is wanted.
|
|
6208
|
+
* @param towardX - the other box's centre, x.
|
|
6209
|
+
* @param towardY - the other box's centre, y.
|
|
6210
|
+
* @returns the point on the box's border.
|
|
6211
|
+
*/
|
|
6212
|
+
function borderPoint(box, towardX, towardY) {
|
|
6213
|
+
const cx = box.x + box.width / 2;
|
|
6214
|
+
const cy = box.y + box.height / 2;
|
|
6215
|
+
const dx = towardX - cx;
|
|
6216
|
+
const dy = towardY - cy;
|
|
6217
|
+
const horizontal = Math.abs(dx) / (box.width / 2 || 1);
|
|
6218
|
+
const vertical = Math.abs(dy) / (box.height / 2 || 1);
|
|
6219
|
+
if (horizontal > vertical) return {
|
|
6220
|
+
x: dx > 0 ? box.x + box.width : box.x,
|
|
6221
|
+
y: cy
|
|
6222
|
+
};
|
|
6223
|
+
return {
|
|
6224
|
+
x: cx,
|
|
6225
|
+
y: dy > 0 ? box.y + box.height : box.y
|
|
6226
|
+
};
|
|
6227
|
+
}
|
|
6228
|
+
/**
|
|
6229
|
+
* Draw one box: its rectangle, then its label.
|
|
6230
|
+
*
|
|
6231
|
+
* A container's label sits in its title band, at the top, left-aligned — the
|
|
6232
|
+
* convention every architecture diagram uses, and what makes a nested box read
|
|
6233
|
+
* as "this region contains the things below it" rather than as a floating card.
|
|
6234
|
+
*
|
|
6235
|
+
* A leaf's label is centred in its box.
|
|
6236
|
+
*
|
|
6237
|
+
* @param box - the placed box.
|
|
6238
|
+
* @param role - how to draw it.
|
|
6239
|
+
* @returns the markup for the box itself, not its children.
|
|
6240
|
+
*/
|
|
6241
|
+
function drawBox(box, role) {
|
|
6242
|
+
const isContainer = role === "container";
|
|
6243
|
+
const { fill, stroke } = boxColours(role, box.provider, box.depth);
|
|
6244
|
+
const radius = isContainer ? 6 : 4;
|
|
6245
|
+
const weight = isContainer ? 600 : 400;
|
|
6246
|
+
const dash = box.existing ? " stroke-dasharray=\"7 5\"" : "";
|
|
6247
|
+
const shape = `<rect x="${box.x}" y="${box.y}" width="${box.width}" height="${box.height}" rx="${radius}" fill="${fill}" stroke="${stroke}" stroke-width="1.5"${dash}/>`;
|
|
6248
|
+
if (isContainer) {
|
|
6249
|
+
const textX = box.x + 16;
|
|
6250
|
+
let textY = box.y + 18;
|
|
6251
|
+
let markup$1 = shape + `<text x="${textX}" y="${textY}" font-size="13" font-weight="${weight}" fill="${TOKENS.ink}">${escapeXml(box.label)}</text>`;
|
|
6252
|
+
for (const line of box.detail) {
|
|
6253
|
+
textY += DETAIL_LINE;
|
|
6254
|
+
markup$1 += `<text x="${textX}" y="${textY}" font-size="10" fill="${TOKENS.muted}" font-family="ui-monospace, SFMono-Regular, Menlo, monospace">${escapeXml(line)}</text>`;
|
|
6255
|
+
}
|
|
6256
|
+
return markup$1;
|
|
6257
|
+
}
|
|
6258
|
+
const centreX = box.x + box.width / 2;
|
|
6259
|
+
const lines = box.detail.length;
|
|
6260
|
+
const blockHeight = lines * DETAIL_LINE;
|
|
6261
|
+
const labelY = box.y + box.height / 2 + 4 - blockHeight / 2;
|
|
6262
|
+
let markup = shape + `<text x="${centreX}" y="${labelY}" font-size="12" text-anchor="middle" font-weight="${weight}" fill="${TOKENS.ink}">${escapeXml(labelWithFigure(box.label, box.figure))}</text>`;
|
|
6263
|
+
const leftX = box.x + PADDING;
|
|
6264
|
+
for (const [index, line] of box.detail.entries()) {
|
|
6265
|
+
const y = labelY + DETAIL_LINE * (index + 1);
|
|
6266
|
+
markup += `<text x="${leftX}" y="${y}" font-size="10" fill="${TOKENS.muted}" font-family="ui-monospace, SFMono-Regular, Menlo, monospace">${escapeXml(line)}</text>`;
|
|
6267
|
+
}
|
|
6268
|
+
return markup;
|
|
6269
|
+
}
|
|
6270
|
+
/** The vertical distance between detail lines, matching the layout's own. */
|
|
6271
|
+
const DETAIL_LINE = 17;
|
|
6272
|
+
/** The padding a leaf's left-aligned rules start from. */
|
|
6273
|
+
const PADDING = 16;
|
|
6274
|
+
/**
|
|
6275
|
+
* The points an orthogonal connector turns at.
|
|
6276
|
+
*
|
|
6277
|
+
* ## Why orthogonal and not straight
|
|
6278
|
+
*
|
|
6279
|
+
* A straight line between two boxes passes over whatever lies between them —
|
|
6280
|
+
* and in a packed box tree, that is the sibling boxes. Measured on a real
|
|
6281
|
+
* account: every one of twenty-seven connectors crossed a box that was not one
|
|
6282
|
+
* of its endpoints. An orthogonal path with one bend in the gap between rows
|
|
6283
|
+
* stays in the empty corridor the layout already leaves there.
|
|
6284
|
+
*
|
|
6285
|
+
* The path leaves along the axis its border point faces, turns once in the
|
|
6286
|
+
* corridor, and arrives along the same axis. Two segments per bend, so the
|
|
6287
|
+
* route is always three or four points and never doubles back.
|
|
6288
|
+
*
|
|
6289
|
+
* @param from - the source box.
|
|
6290
|
+
* @param to - the target box.
|
|
6291
|
+
* @returns the polyline's points, source first.
|
|
6292
|
+
*/
|
|
6293
|
+
function routePoints(from, to) {
|
|
6294
|
+
const fromCx = from.x + from.width / 2;
|
|
6295
|
+
const fromCy = from.y + from.height / 2;
|
|
6296
|
+
const toCx = to.x + to.width / 2;
|
|
6297
|
+
const toCy = to.y + to.height / 2;
|
|
6298
|
+
const start = borderPoint(from, toCx, toCy);
|
|
6299
|
+
const end = borderPoint(to, fromCx, fromCy);
|
|
6300
|
+
const startVertical = start.x === fromCx;
|
|
6301
|
+
const endVertical = end.x === toCx;
|
|
6302
|
+
if (startVertical && endVertical) {
|
|
6303
|
+
const midY = (start.y + end.y) / 2;
|
|
6304
|
+
return [
|
|
6305
|
+
start,
|
|
6306
|
+
{
|
|
6307
|
+
x: start.x,
|
|
6308
|
+
y: midY
|
|
6309
|
+
},
|
|
6310
|
+
{
|
|
6311
|
+
x: end.x,
|
|
6312
|
+
y: midY
|
|
6313
|
+
},
|
|
6314
|
+
end
|
|
6315
|
+
];
|
|
6316
|
+
}
|
|
6317
|
+
if (!startVertical && !endVertical) {
|
|
6318
|
+
const midX = (start.x + end.x) / 2;
|
|
6319
|
+
return [
|
|
6320
|
+
start,
|
|
6321
|
+
{
|
|
6322
|
+
x: midX,
|
|
6323
|
+
y: start.y
|
|
6324
|
+
},
|
|
6325
|
+
{
|
|
6326
|
+
x: midX,
|
|
6327
|
+
y: end.y
|
|
6328
|
+
},
|
|
6329
|
+
end
|
|
6330
|
+
];
|
|
6331
|
+
}
|
|
6332
|
+
return [
|
|
6333
|
+
start,
|
|
6334
|
+
{
|
|
6335
|
+
x: end.x,
|
|
6336
|
+
y: start.y
|
|
6337
|
+
},
|
|
6338
|
+
end
|
|
6339
|
+
];
|
|
6340
|
+
}
|
|
6341
|
+
/**
|
|
6342
|
+
* Draw one connector as an orthogonal path, with its label on the route.
|
|
6343
|
+
*
|
|
6344
|
+
* The two kinds are told apart by weight and dash: a traffic flow is solid and
|
|
6345
|
+
* heavier — it is where requests go — while a policy rule is dashed, because
|
|
6346
|
+
* nothing travels along it. Colour alone would not survive a black-and-white
|
|
6347
|
+
* print, and the difference matters enough to be readable there.
|
|
6348
|
+
*
|
|
6349
|
+
* @param edge - the connector.
|
|
6350
|
+
* @param from - the box it leaves.
|
|
6351
|
+
* @param to - the box it arrives at.
|
|
6352
|
+
* @returns the markup.
|
|
6353
|
+
*/
|
|
6354
|
+
function drawEdge(edge, from, to) {
|
|
6355
|
+
const points = routePoints(from, to);
|
|
6356
|
+
const path = points.map((point, index) => `${index === 0 ? "M" : "L"} ${point.x} ${point.y}`).join(" ");
|
|
6357
|
+
const traffic = edge.kind === "traffic";
|
|
6358
|
+
const stroke = traffic ? TOKENS.traffic : TOKENS.policy;
|
|
6359
|
+
const width = traffic ? 2 : 1.2;
|
|
6360
|
+
const dash = traffic ? "" : ` stroke-dasharray="5 4"`;
|
|
6361
|
+
const marker = traffic ? "arrowTraffic" : "arrowPolicy";
|
|
6362
|
+
const line = `<path d="${path}" fill="none" stroke="${stroke}" stroke-width="${width}"${dash} marker-end="url(#${marker})"/>`;
|
|
6363
|
+
if (edge.label === "") return line;
|
|
6364
|
+
const middle = points.length >= 4 ? {
|
|
6365
|
+
x: (points[1].x + points[2].x) / 2,
|
|
6366
|
+
y: (points[1].y + points[2].y) / 2
|
|
6367
|
+
} : {
|
|
6368
|
+
x: (points[0].x + points.at(-1).x) / 2,
|
|
6369
|
+
y: (points[0].y + points.at(-1).y) / 2
|
|
6370
|
+
};
|
|
6371
|
+
const width_ = edge.label.length * 7 + 8;
|
|
6372
|
+
const plaque = `<rect x="${middle.x - width_ / 2}" y="${middle.y - 9}" width="${width_}" height="18" rx="3" fill="${TOKENS.labelBackground}" fill-opacity="0.92"/>`;
|
|
6373
|
+
const text = `<text x="${middle.x}" y="${middle.y + 4}" font-size="11" text-anchor="middle" fill="${traffic ? TOKENS.ink : TOKENS.muted}" font-weight="${traffic ? 600 : 400}">${escapeXml(edge.label)}</text>`;
|
|
6374
|
+
return line + plaque + text;
|
|
6375
|
+
}
|
|
6376
|
+
/**
|
|
6377
|
+
* Render the diagram.
|
|
6378
|
+
*
|
|
6379
|
+
* The SVG declares `width` and `height` from the layout, not only a `viewBox`.
|
|
6380
|
+
* Without them a browser treats the file as a replaced element with no size and
|
|
6381
|
+
* lays it out at the CSS default of 300×150, scaling everything into that box —
|
|
6382
|
+
* which draws a readable diagram at about one pixel.
|
|
6383
|
+
*
|
|
6384
|
+
* @param diagram - the placed boxes and connectors.
|
|
6385
|
+
* @returns the SVG document, with no XML declaration so it embeds in HTML.
|
|
6386
|
+
*/
|
|
6387
|
+
function renderDiagram(diagram) {
|
|
6388
|
+
const byId = new Map(diagram.boxes.map((box) => [box.id, box]));
|
|
6389
|
+
const parts = [];
|
|
6390
|
+
parts.push(`<svg xmlns="http://www.w3.org/2000/svg" width="${diagram.width}" height="${diagram.height}" viewBox="0 0 ${diagram.width} ${diagram.height}" font-family="ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif">`);
|
|
6391
|
+
parts.push(`<defs><marker id="arrowTraffic" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M 0 0 L 10 5 L 0 10 z" fill="${TOKENS.traffic}"/></marker><marker id="arrowPolicy" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path d="M 0 0 L 10 5 L 0 10 z" fill="${TOKENS.policy}"/></marker></defs>`);
|
|
6392
|
+
parts.push(`<rect width="${diagram.width}" height="${diagram.height}" fill="${TOKENS.canvas}"/>`);
|
|
6393
|
+
for (const box of diagram.boxes) parts.push(drawBox(box, diagram.roles.get(box.id) ?? "resource"));
|
|
6394
|
+
for (const edge of diagram.edges) {
|
|
6395
|
+
const from = byId.get(edge.from);
|
|
6396
|
+
const to = byId.get(edge.to);
|
|
6397
|
+
if (from === void 0 || to === void 0) continue;
|
|
6398
|
+
parts.push(drawEdge(edge, from, to));
|
|
6399
|
+
}
|
|
6400
|
+
parts.push("</svg>");
|
|
6401
|
+
return parts.join("");
|
|
6402
|
+
}
|
|
6403
|
+
|
|
6404
|
+
//#endregion
|
|
6405
|
+
//#region src/existing-topology.ts
|
|
5826
6406
|
/**
|
|
5827
6407
|
* Every scalar the properties contain, keyed by its dotted path.
|
|
5828
6408
|
*
|
|
@@ -5880,641 +6460,1082 @@ function isJsonEncoded(text) {
|
|
|
5880
6460
|
const first = text.trimStart()[0];
|
|
5881
6461
|
return first === "[" || first === "{";
|
|
5882
6462
|
}
|
|
5883
|
-
|
|
5884
|
-
|
|
5885
|
-
|
|
5886
|
-
|
|
5887
|
-
|
|
5888
|
-
|
|
5889
|
-
|
|
5890
|
-
|
|
5891
|
-
|
|
5892
|
-
*
|
|
5893
|
-
*
|
|
5894
|
-
*
|
|
5895
|
-
|
|
5896
|
-
|
|
5897
|
-
|
|
5898
|
-
|
|
5899
|
-
|
|
5900
|
-
|
|
5901
|
-
|
|
5902
|
-
|
|
5903
|
-
|
|
5904
|
-
|
|
6463
|
+
|
|
6464
|
+
//#endregion
|
|
6465
|
+
//#region src/account-view.ts
|
|
6466
|
+
/**
|
|
6467
|
+
* Read a string-valued property, unwrapping the JSON encoding RMS uses.
|
|
6468
|
+
*
|
|
6469
|
+
* The inventory ships some ids as `"[\"…\"]"` — a JSON array inside a string —
|
|
6470
|
+
* and a plain comparison against an id never matches. One unwrap is enough:
|
|
6471
|
+
* the provider does not double-encode.
|
|
6472
|
+
*
|
|
6473
|
+
* @param value - the property value.
|
|
6474
|
+
* @returns the id it holds, or `undefined`.
|
|
6475
|
+
*/
|
|
6476
|
+
function idOf$1(value) {
|
|
6477
|
+
if (typeof value === "string") {
|
|
6478
|
+
const trimmed = value.trim();
|
|
6479
|
+
if (trimmed.startsWith("[")) try {
|
|
6480
|
+
const parsed = JSON.parse(trimmed);
|
|
6481
|
+
if (Array.isArray(parsed) && typeof parsed[0] === "string") return parsed[0];
|
|
6482
|
+
} catch {
|
|
6483
|
+
return trimmed;
|
|
6484
|
+
}
|
|
6485
|
+
return trimmed === "" ? void 0 : trimmed;
|
|
6486
|
+
}
|
|
6487
|
+
if (Array.isArray(value) && typeof value[0] === "string") return value[0];
|
|
6488
|
+
return void 0;
|
|
6489
|
+
}
|
|
6490
|
+
/**
|
|
6491
|
+
* The attribute names that mean "this is drawn inside that".
|
|
6492
|
+
*
|
|
6493
|
+
* Two kinds, and both are nesting:
|
|
6494
|
+
*
|
|
6495
|
+
* - **Placement** — `vpc_id`, `subnet_id`, `networkId`. Where a thing lives.
|
|
6496
|
+
* - **Attachment** — `serverID`, `host_id`, `clusterUID`, and an EIP's
|
|
6497
|
+
* `associateInstanceId`. What a thing is part of.
|
|
6498
|
+
*
|
|
6499
|
+
* Both answer the same question for the reader ("what does this belong to"),
|
|
6500
|
+
* and both are drawn the same way. Keeping one table rather than two is what
|
|
6501
|
+
* lets a service this build has never seen nest correctly: it only has to name
|
|
6502
|
+
* the relationship the way the others do.
|
|
6503
|
+
*
|
|
6504
|
+
* Ranked: a more specific container wins. A server names both its VPC and its
|
|
6505
|
+
* subnet, and the subnet is where it lives.
|
|
6506
|
+
*
|
|
6507
|
+
* ## Every entry points from the member to its container
|
|
6508
|
+
*
|
|
6509
|
+
* The direction is part of the meaning, not a detail. A balancer lists its EIPs
|
|
6510
|
+
* in `eips[].eip_id`, which reads like a container holding a member — but the
|
|
6511
|
+
* EIP is the one that names the balancer, through `associateInstanceId`, and
|
|
6512
|
+
* only the EIP's property says where it belongs. Reading the balancer's list as
|
|
6513
|
+
* placement put the balancer inside its own address.
|
|
6514
|
+
*
|
|
6515
|
+
* So an attribute belongs here only when it says "I am part of that". A
|
|
6516
|
+
* container that lists its members does not.
|
|
6517
|
+
*/
|
|
6518
|
+
const NESTS_IN = [
|
|
6519
|
+
{
|
|
6520
|
+
match: /vpc[._]?id$/i,
|
|
6521
|
+
rank: 1
|
|
6522
|
+
},
|
|
6523
|
+
{
|
|
6524
|
+
match: /(subnet|network)[._]?(id|uid)s?$/i,
|
|
6525
|
+
rank: 2
|
|
6526
|
+
},
|
|
6527
|
+
{
|
|
6528
|
+
match: /^(vnic\.)?deviceId$/i,
|
|
6529
|
+
rank: 3
|
|
6530
|
+
},
|
|
6531
|
+
{
|
|
6532
|
+
match: /(^|\.)(server|host)[._]?id$/i,
|
|
6533
|
+
rank: 4
|
|
6534
|
+
},
|
|
6535
|
+
{
|
|
6536
|
+
match: /cluster(uid|[._]?id)$/i,
|
|
6537
|
+
rank: 4
|
|
6538
|
+
},
|
|
6539
|
+
{
|
|
6540
|
+
match: /^associateInstanceId$/i,
|
|
6541
|
+
rank: 4
|
|
6542
|
+
},
|
|
6543
|
+
{
|
|
6544
|
+
match: /^loadbalancers?[._]id$/i,
|
|
6545
|
+
rank: 4
|
|
6546
|
+
}
|
|
6547
|
+
];
|
|
6548
|
+
/**
|
|
6549
|
+
* The attribute names that name a security group guarding this resource.
|
|
6550
|
+
*
|
|
6551
|
+
* These do NOT nest and do NOT become edges: a group is a property of the thing
|
|
6552
|
+
* it guards, so it is drawn as text inside that thing's box.
|
|
6553
|
+
*/
|
|
6554
|
+
const GUARDS_BY = [/security[._]?group[._]?id$/i, /secgroup$/i];
|
|
6555
|
+
/**
|
|
6556
|
+
* How many resources must name a container before it counts as one.
|
|
6557
|
+
*
|
|
6558
|
+
* Two is enough to tell "everything inside me points here" from "one thing
|
|
6559
|
+
* mentioned me". A NAT gateway is named by the single EIP attached to it and is
|
|
6560
|
+
* still a thing placed inside a VPC; a VPC is named by every subnet, server and
|
|
6561
|
+
* load balancer it holds and is a root.
|
|
6562
|
+
*/
|
|
6563
|
+
const CONTAINER_REFERENTS = 2;
|
|
6564
|
+
/**
|
|
6565
|
+
* How specifically an attribute names a container, 0 for none.
|
|
5905
6566
|
*
|
|
5906
6567
|
* @param attribute - a dotted property path.
|
|
5907
|
-
* @returns the rank
|
|
6568
|
+
* @returns the rank, higher being more specific.
|
|
5908
6569
|
*/
|
|
5909
|
-
function
|
|
5910
|
-
|
|
5911
|
-
const
|
|
5912
|
-
for (const { nouns, rank } of LOCATION_KINDS) for (const word of nouns) if (noun.endsWith(word)) return rank;
|
|
6570
|
+
function nestRank(attribute) {
|
|
6571
|
+
if (attribute === "description" || attribute.startsWith("description.")) return 0;
|
|
6572
|
+
for (const { match, rank } of NESTS_IN) if (match.test(attribute)) return rank;
|
|
5913
6573
|
return 0;
|
|
5914
6574
|
}
|
|
5915
6575
|
/**
|
|
5916
|
-
*
|
|
6576
|
+
* Whether an attribute names a security group.
|
|
5917
6577
|
*
|
|
5918
|
-
* @param
|
|
5919
|
-
* @returns
|
|
6578
|
+
* @param attribute - a dotted property path.
|
|
6579
|
+
* @returns whether it does.
|
|
5920
6580
|
*/
|
|
5921
|
-
function
|
|
5922
|
-
|
|
5923
|
-
|
|
5924
|
-
|
|
5925
|
-
|
|
5926
|
-
|
|
5927
|
-
|
|
5928
|
-
|
|
5929
|
-
|
|
5930
|
-
|
|
5931
|
-
const
|
|
5932
|
-
for (const [id, resource] of byId) flat.set(id, flattenProperties(resource.properties ?? {}));
|
|
5933
|
-
const raw = [];
|
|
5934
|
-
const seen = new Set();
|
|
5935
|
-
for (const [id, properties] of flat) for (const [attribute, value] of properties) {
|
|
5936
|
-
if (value === id) continue;
|
|
5937
|
-
if (!byId.has(value)) continue;
|
|
5938
|
-
const key = `${id}\u0000${value}`;
|
|
5939
|
-
if (seen.has(key)) continue;
|
|
5940
|
-
seen.add(key);
|
|
5941
|
-
raw.push({
|
|
5942
|
-
from: id,
|
|
5943
|
-
to: value,
|
|
5944
|
-
attribute,
|
|
5945
|
-
location: locationRank(attribute)
|
|
5946
|
-
});
|
|
5947
|
-
}
|
|
5948
|
-
const edges = raw.map((entry) => ({
|
|
5949
|
-
from: entry.from,
|
|
5950
|
-
to: entry.to,
|
|
5951
|
-
attribute: entry.attribute,
|
|
5952
|
-
contains: entry.location > 0
|
|
5953
|
-
}));
|
|
6581
|
+
function isGuardAttribute(attribute) {
|
|
6582
|
+
return GUARDS_BY.some((match) => match.test(attribute));
|
|
6583
|
+
}
|
|
6584
|
+
/**
|
|
6585
|
+
* Read the placement and attachment layer.
|
|
6586
|
+
*
|
|
6587
|
+
* @param resources - the account's resources.
|
|
6588
|
+
* @returns resource id → the id of its container.
|
|
6589
|
+
*/
|
|
6590
|
+
function containment(resources) {
|
|
6591
|
+
const byId = new Map(resources.map((resource) => [resource.id, resource]));
|
|
5954
6592
|
const parentOf = new Map();
|
|
5955
|
-
const
|
|
5956
|
-
|
|
5957
|
-
|
|
5958
|
-
if (
|
|
5959
|
-
|
|
5960
|
-
|
|
5961
|
-
|
|
5962
|
-
}
|
|
5963
|
-
for (const [
|
|
5964
|
-
const
|
|
5965
|
-
|
|
6593
|
+
const rankOf = new Map();
|
|
6594
|
+
/** Record a placement, keeping the most specific one. */
|
|
6595
|
+
const place$1 = (child, parent, rank) => {
|
|
6596
|
+
if (child === parent || !byId.has(parent)) return;
|
|
6597
|
+
if (rank <= (rankOf.get(child) ?? 0)) return;
|
|
6598
|
+
parentOf.set(child, parent);
|
|
6599
|
+
rankOf.set(child, rank);
|
|
6600
|
+
};
|
|
6601
|
+
for (const resource of resources) for (const [attribute, value] of flattenProperties(resource.properties ?? {})) {
|
|
6602
|
+
const rank = nestRank(attribute);
|
|
6603
|
+
if (rank === 0) continue;
|
|
6604
|
+
const target = idOf$1(value);
|
|
6605
|
+
if (target === void 0) continue;
|
|
6606
|
+
place$1(resource.id, target, rank);
|
|
6607
|
+
}
|
|
6608
|
+
const namedBy = new Map();
|
|
6609
|
+
for (const resource of resources) for (const [attribute, value] of flattenProperties(resource.properties ?? {})) {
|
|
6610
|
+
const rank = nestRank(attribute);
|
|
6611
|
+
if (rank === 0) continue;
|
|
6612
|
+
const target = idOf$1(value);
|
|
6613
|
+
if (target === void 0 || target === resource.id) continue;
|
|
6614
|
+
if (!byId.has(target)) continue;
|
|
6615
|
+
const listed = namedBy.get(target) ?? [];
|
|
6616
|
+
if (listed.some((entry) => entry.from === resource.id)) continue;
|
|
6617
|
+
namedBy.set(target, [...listed, {
|
|
6618
|
+
from: resource.id,
|
|
6619
|
+
rank
|
|
6620
|
+
}]);
|
|
6621
|
+
}
|
|
6622
|
+
for (const [named, referrers] of namedBy) {
|
|
6623
|
+
if (parentOf.has(named)) continue;
|
|
6624
|
+
if (referrers.length >= CONTAINER_REFERENTS) continue;
|
|
6625
|
+
const inherited = inheritedContainer(byId.get(named), resources, parentOf, namedBy);
|
|
6626
|
+
if (inherited === void 0 || inherited === named) continue;
|
|
6627
|
+
if (holdsTransitively(named, inherited, parentOf)) continue;
|
|
6628
|
+
const rank = referrers.reduce((best, referrer) => Math.max(best, referrer.rank), 0);
|
|
6629
|
+
place$1(named, inherited, rank);
|
|
6630
|
+
}
|
|
6631
|
+
for (const [child] of [...parentOf]) {
|
|
6632
|
+
const seen = new Set([child]);
|
|
6633
|
+
let cursor = parentOf.get(child);
|
|
5966
6634
|
while (cursor !== void 0) {
|
|
5967
|
-
if (
|
|
6635
|
+
if (seen.has(cursor)) {
|
|
5968
6636
|
parentOf.delete(child);
|
|
5969
6637
|
break;
|
|
5970
6638
|
}
|
|
5971
|
-
|
|
6639
|
+
seen.add(cursor);
|
|
5972
6640
|
cursor = parentOf.get(cursor);
|
|
5973
6641
|
}
|
|
5974
6642
|
}
|
|
5975
|
-
|
|
5976
|
-
for (const [child, parent] of parentOf) {
|
|
5977
|
-
const list = childrenOf.get(parent) ?? [];
|
|
5978
|
-
list.push(child);
|
|
5979
|
-
childrenOf.set(parent, list);
|
|
5980
|
-
}
|
|
5981
|
-
const factsOf = (resource) => {
|
|
5982
|
-
const properties = flat.get(resource.id) ?? new Map();
|
|
5983
|
-
const label = resource.name ?? "";
|
|
5984
|
-
const facts = [];
|
|
5985
|
-
for (const key of LABEL_ATTRIBUTES) {
|
|
5986
|
-
const value = properties.get(key);
|
|
5987
|
-
if (value === void 0 || value === "" || value === label) continue;
|
|
5988
|
-
if (facts.some((fact) => fact.value === value)) continue;
|
|
5989
|
-
facts.push({
|
|
5990
|
-
key,
|
|
5991
|
-
value
|
|
5992
|
-
});
|
|
5993
|
-
}
|
|
5994
|
-
return facts;
|
|
5995
|
-
};
|
|
5996
|
-
const ancestorsOf = (id) => {
|
|
5997
|
-
const chain = [];
|
|
5998
|
-
const seenChain = new Set([id]);
|
|
5999
|
-
let cursor = parentOf.get(id);
|
|
6000
|
-
while (cursor !== void 0 && !seenChain.has(cursor)) {
|
|
6001
|
-
seenChain.add(cursor);
|
|
6002
|
-
chain.push(cursor);
|
|
6003
|
-
cursor = parentOf.get(cursor);
|
|
6004
|
-
}
|
|
6005
|
-
return chain;
|
|
6006
|
-
};
|
|
6007
|
-
const build = (id) => {
|
|
6008
|
-
const resource = byId.get(id);
|
|
6009
|
-
return {
|
|
6010
|
-
id,
|
|
6011
|
-
provider: resource.provider,
|
|
6012
|
-
type: resource.type,
|
|
6013
|
-
label: resource.name !== void 0 && resource.name !== "" ? resource.name : `${resource.provider}/${resource.type} ${id.slice(0, 8)}`,
|
|
6014
|
-
facts: factsOf(resource),
|
|
6015
|
-
parent: parentOf.get(id),
|
|
6016
|
-
ancestors: ancestorsOf(id),
|
|
6017
|
-
children: (childrenOf.get(id) ?? []).map(build)
|
|
6018
|
-
};
|
|
6019
|
-
};
|
|
6020
|
-
const rootIds = [...byId.keys()].filter((id) => !parentOf.has(id));
|
|
6021
|
-
const depth = (id) => ancestorsOf(id).length;
|
|
6022
|
-
rootIds.sort((a, b) => {
|
|
6023
|
-
const da = depth(a), db = depth(b);
|
|
6024
|
-
if (da !== db) return db - da;
|
|
6025
|
-
return (byId.get(a).name ?? a).localeCompare(byId.get(b).name ?? b);
|
|
6026
|
-
});
|
|
6027
|
-
return {
|
|
6028
|
-
roots: rootIds.map(build),
|
|
6029
|
-
nodes: [...byId.keys()].map(build),
|
|
6030
|
-
edges,
|
|
6031
|
-
duplicates
|
|
6032
|
-
};
|
|
6643
|
+
return parentOf;
|
|
6033
6644
|
}
|
|
6034
|
-
|
|
6035
|
-
//#endregion
|
|
6036
|
-
//#region src/d2-engine.ts
|
|
6037
6645
|
/**
|
|
6038
|
-
*
|
|
6646
|
+
* Every container a resource's properties name, most specific first.
|
|
6039
6647
|
*
|
|
6040
|
-
*
|
|
6041
|
-
*
|
|
6042
|
-
*
|
|
6043
|
-
*
|
|
6044
|
-
*
|
|
6648
|
+
* A resource may state more than one placement, and they are all true: an EIP
|
|
6649
|
+
* attached to a NAT gateway names the gateway (it is drawn inside it) AND the
|
|
6650
|
+
* VPC it sits in. Only one of them can be the box it is drawn in, but the
|
|
6651
|
+
* others still say where it lives — which is what a resource that states no
|
|
6652
|
+
* placement of its own can borrow.
|
|
6045
6653
|
*
|
|
6046
|
-
*
|
|
6047
|
-
*
|
|
6048
|
-
* distinguishable blue/violet/teal.
|
|
6049
|
-
*
|
|
6050
|
-
* Both ids must be given. With no `dark-theme-id` D2 emits NO dark palette at
|
|
6051
|
-
* all, and every class would resolve in light colours on a dark ground.
|
|
6654
|
+
* @param resource - the resource.
|
|
6655
|
+
* @returns the container ids, ordered by how specific the attribute was.
|
|
6052
6656
|
*/
|
|
6053
|
-
|
|
6054
|
-
const
|
|
6657
|
+
function allContainers(resource) {
|
|
6658
|
+
const found = [];
|
|
6659
|
+
for (const [attribute, value] of flattenProperties(resource.properties ?? {})) {
|
|
6660
|
+
const rank = nestRank(attribute);
|
|
6661
|
+
if (rank === 0) continue;
|
|
6662
|
+
const target = idOf$1(value);
|
|
6663
|
+
if (target === void 0 || target === resource.id) continue;
|
|
6664
|
+
found.push({
|
|
6665
|
+
id: target,
|
|
6666
|
+
rank
|
|
6667
|
+
});
|
|
6668
|
+
}
|
|
6669
|
+
return found.sort((a, b) => b.rank - a.rank);
|
|
6670
|
+
}
|
|
6055
6671
|
/**
|
|
6056
|
-
* The
|
|
6672
|
+
* The container a resource is in, following the naming chain when needed.
|
|
6057
6673
|
*
|
|
6058
|
-
*
|
|
6059
|
-
* the generated source is the whole description of what was drawn — a reader
|
|
6060
|
-
* feeding it to the D2 CLI gets the same colours.
|
|
6674
|
+
* ## Why one hop is not enough
|
|
6061
6675
|
*
|
|
6062
|
-
*
|
|
6063
|
-
|
|
6064
|
-
|
|
6065
|
-
|
|
6066
|
-
"direction: down",
|
|
6067
|
-
"",
|
|
6068
|
-
"vars: {",
|
|
6069
|
-
" d2-config: {",
|
|
6070
|
-
` theme-id: ${LIGHT_THEME_ID}`,
|
|
6071
|
-
` dark-theme-id: ${DARK_THEME_ID}`,
|
|
6072
|
-
" }",
|
|
6073
|
-
"}",
|
|
6074
|
-
""
|
|
6075
|
-
];
|
|
6076
|
-
}
|
|
6077
|
-
/**
|
|
6078
|
-
* Compile D2 source and render it to an SVG.
|
|
6676
|
+
* A NAT gateway reports only its spec. The EIP attached to it knows two things:
|
|
6677
|
+
* that it belongs to the gateway, and that the gateway sits in a VPC. The
|
|
6678
|
+
* gateway's placement is therefore one link away — it is in whatever VPC the
|
|
6679
|
+
* thing that names it names.
|
|
6079
6680
|
*
|
|
6080
|
-
* @param
|
|
6081
|
-
* @
|
|
6082
|
-
* @
|
|
6083
|
-
*
|
|
6084
|
-
*
|
|
6681
|
+
* @param resource - the resource whose container is wanted.
|
|
6682
|
+
* @param resources - everything in the account.
|
|
6683
|
+
* @param parentOf - the placement resolved so far.
|
|
6684
|
+
* @param namedBy - which resources name each other.
|
|
6685
|
+
* @returns the container id, or `undefined` when the chain leads nowhere.
|
|
6085
6686
|
*/
|
|
6086
|
-
|
|
6087
|
-
const
|
|
6088
|
-
|
|
6089
|
-
|
|
6090
|
-
|
|
6091
|
-
|
|
6092
|
-
|
|
6093
|
-
|
|
6094
|
-
|
|
6095
|
-
|
|
6096
|
-
|
|
6687
|
+
function inheritedContainer(resource, resources, parentOf, namedBy) {
|
|
6688
|
+
const byId = new Map(resources.map((entry) => [entry.id, entry]));
|
|
6689
|
+
const seen = new Set([resource.id]);
|
|
6690
|
+
const queue = (namedBy.get(resource.id) ?? []).map((entry) => entry.from);
|
|
6691
|
+
while (queue.length > 0) {
|
|
6692
|
+
const id = queue.shift();
|
|
6693
|
+
if (seen.has(id)) continue;
|
|
6694
|
+
seen.add(id);
|
|
6695
|
+
const referrer = byId.get(id);
|
|
6696
|
+
if (referrer === void 0) continue;
|
|
6697
|
+
const placed = parentOf.get(id);
|
|
6698
|
+
if (placed !== void 0 && placed !== resource.id) return placed;
|
|
6699
|
+
const candidate = allContainers(referrer).find((entry) => entry.id !== resource.id);
|
|
6700
|
+
if (candidate !== void 0) return candidate.id;
|
|
6701
|
+
queue.push(...(namedBy.get(id) ?? []).map((entry) => entry.from));
|
|
6097
6702
|
}
|
|
6703
|
+
return void 0;
|
|
6098
6704
|
}
|
|
6099
6705
|
/**
|
|
6100
|
-
*
|
|
6706
|
+
* Whether `container` already sits inside `resource`.
|
|
6101
6707
|
*
|
|
6102
|
-
*
|
|
6103
|
-
*
|
|
6104
|
-
*
|
|
6708
|
+
* Used to refuse a placement that would invert the nesting: a VPC named by an
|
|
6709
|
+
* EIP whose gateway the VPC also holds must not then be placed inside that
|
|
6710
|
+
* gateway. The walk follows the placement chain upwards, and stops at a cycle
|
|
6711
|
+
* rather than looping.
|
|
6105
6712
|
*
|
|
6106
|
-
* @param
|
|
6713
|
+
* @param resource - the resource that would be the new container.
|
|
6714
|
+
* @param container - the box that would be placed inside it.
|
|
6715
|
+
* @param parentOf - the placement resolved so far.
|
|
6716
|
+
* @returns whether `container` is already below `resource`.
|
|
6107
6717
|
*/
|
|
6108
|
-
|
|
6109
|
-
const
|
|
6110
|
-
|
|
6111
|
-
|
|
6112
|
-
|
|
6718
|
+
function holdsTransitively(resource, container, parentOf) {
|
|
6719
|
+
const seen = new Set([container]);
|
|
6720
|
+
let cursor = parentOf.get(container);
|
|
6721
|
+
while (cursor !== void 0) {
|
|
6722
|
+
if (cursor === resource) return true;
|
|
6723
|
+
if (seen.has(cursor)) return false;
|
|
6724
|
+
seen.add(cursor);
|
|
6725
|
+
cursor = parentOf.get(cursor);
|
|
6726
|
+
}
|
|
6727
|
+
return false;
|
|
6113
6728
|
}
|
|
6114
6729
|
/**
|
|
6115
|
-
*
|
|
6730
|
+
* Read which security groups guard each resource.
|
|
6116
6731
|
*
|
|
6117
|
-
* @param
|
|
6118
|
-
* @returns
|
|
6732
|
+
* @param resources - the account's resources.
|
|
6733
|
+
* @returns resource id → the group NAMES, sorted so two runs agree.
|
|
6119
6734
|
*/
|
|
6120
|
-
function
|
|
6121
|
-
|
|
6735
|
+
function guards(resources) {
|
|
6736
|
+
const byId = new Map(resources.map((resource) => [resource.id, resource]));
|
|
6737
|
+
const found = new Map();
|
|
6738
|
+
for (const resource of resources) for (const [attribute, value] of flattenProperties(resource.properties ?? {})) {
|
|
6739
|
+
if (!isGuardAttribute(attribute)) continue;
|
|
6740
|
+
const target = idOf$1(value);
|
|
6741
|
+
if (target === void 0 || target === resource.id) continue;
|
|
6742
|
+
const group = byId.get(target);
|
|
6743
|
+
if (group === void 0) continue;
|
|
6744
|
+
const names = found.get(resource.id) ?? [];
|
|
6745
|
+
if (!names.includes(group.name ?? group.id)) names.push(group.name ?? group.id);
|
|
6746
|
+
found.set(resource.id, names);
|
|
6747
|
+
}
|
|
6748
|
+
for (const names of found.values()) names.sort();
|
|
6749
|
+
return found;
|
|
6122
6750
|
}
|
|
6123
6751
|
/**
|
|
6124
|
-
* The
|
|
6752
|
+
* The resources that use each security group.
|
|
6125
6753
|
*
|
|
6126
|
-
*
|
|
6127
|
-
* nests the node inside a box named after the part before the dot — inventing a
|
|
6128
|
-
* container that does not exist. Every character D2 reserves is replaced by `_`
|
|
6129
|
-
* for the same reason.
|
|
6754
|
+
* ## Why this is worth showing
|
|
6130
6755
|
*
|
|
6131
|
-
* A
|
|
6132
|
-
*
|
|
6133
|
-
*
|
|
6134
|
-
*
|
|
6135
|
-
*
|
|
6756
|
+
* A group's rules say what it admits; its users say whether it matters. On a
|
|
6757
|
+
* live account 13 groups exist and only 6 are used by anything — and one of
|
|
6758
|
+
* those is used by three resources. A reader who has to open each group to find
|
|
6759
|
+
* out whether it is live cannot tell a housekeeping leftover from the group
|
|
6760
|
+
* guarding the database.
|
|
6136
6761
|
*
|
|
6137
|
-
* @param
|
|
6138
|
-
* @returns
|
|
6762
|
+
* @param resources - the account's resources.
|
|
6763
|
+
* @returns group id → the ids of the resources that name it.
|
|
6139
6764
|
*/
|
|
6140
|
-
function
|
|
6141
|
-
|
|
6765
|
+
function groupUsers(resources) {
|
|
6766
|
+
const users = new Map();
|
|
6767
|
+
for (const resource of resources) for (const [attribute, value] of flattenProperties(resource.properties ?? {})) {
|
|
6768
|
+
if (!isGuardAttribute(attribute)) continue;
|
|
6769
|
+
const target = idOf$1(value);
|
|
6770
|
+
if (target === void 0 || target === resource.id) continue;
|
|
6771
|
+
const groupId = target;
|
|
6772
|
+
if (!resources.some((entry) => entry.id === groupId)) continue;
|
|
6773
|
+
const listed = users.get(groupId) ?? [];
|
|
6774
|
+
if (!listed.includes(resource.id)) listed.push(resource.id);
|
|
6775
|
+
users.set(groupId, listed);
|
|
6776
|
+
}
|
|
6777
|
+
return users;
|
|
6142
6778
|
}
|
|
6143
|
-
|
|
6144
|
-
//#endregion
|
|
6145
|
-
//#region src/d2-inventory.ts
|
|
6146
6779
|
/**
|
|
6147
|
-
*
|
|
6780
|
+
* Read the policy edges between security groups.
|
|
6148
6781
|
*
|
|
6149
|
-
*
|
|
6150
|
-
*
|
|
6782
|
+
* ## What makes this an edge
|
|
6783
|
+
*
|
|
6784
|
+
* A rule on group A whose `remote_group_id` is group B says: traffic from
|
|
6785
|
+
* anything carrying B may reach anything carrying A, on this port. That is a
|
|
6786
|
+
* statement about reachability between two groups, with a direction and a port
|
|
6787
|
+
* — the only relationship in the account that has all three.
|
|
6788
|
+
*
|
|
6789
|
+
* Two rules that say the same thing (a group's rules are often duplicated by
|
|
6790
|
+
* the provider) are collapsed: a reader comparing the picture to the console
|
|
6791
|
+
* should not have to work out which arrow is which duplicate.
|
|
6792
|
+
*
|
|
6793
|
+
* @param resources - the account's resources.
|
|
6794
|
+
* @returns the policy edges, sorted for a stable picture.
|
|
6151
6795
|
*/
|
|
6152
|
-
|
|
6153
|
-
[
|
|
6154
|
-
|
|
6155
|
-
|
|
6156
|
-
|
|
6157
|
-
|
|
6158
|
-
|
|
6159
|
-
|
|
6160
|
-
|
|
6161
|
-
|
|
6162
|
-
|
|
6163
|
-
|
|
6164
|
-
|
|
6165
|
-
|
|
6166
|
-
|
|
6167
|
-
|
|
6168
|
-
|
|
6169
|
-
|
|
6170
|
-
|
|
6171
|
-
|
|
6172
|
-
|
|
6173
|
-
|
|
6174
|
-
|
|
6175
|
-
|
|
6796
|
+
function policies(resources) {
|
|
6797
|
+
const groupNames = new Map(resources.filter((resource) => resource.type === "securityGroups").map((resource) => [resource.id, resource.name ?? resource.id]));
|
|
6798
|
+
const edges = [];
|
|
6799
|
+
const seen = new Set();
|
|
6800
|
+
for (const resource of resources) {
|
|
6801
|
+
if (resource.type !== "securityGroups") continue;
|
|
6802
|
+
const rules = resource.properties?.["security_group_rules"];
|
|
6803
|
+
if (!Array.isArray(rules)) continue;
|
|
6804
|
+
for (const rule of rules) {
|
|
6805
|
+
if (rule === null || typeof rule !== "object") continue;
|
|
6806
|
+
const fields = rule;
|
|
6807
|
+
const remote = idOf$1(fields["remote_group_id"]);
|
|
6808
|
+
if (remote === void 0 || remote === resource.id) continue;
|
|
6809
|
+
if (!groupNames.has(remote)) continue;
|
|
6810
|
+
const direction = typeof fields["direction"] === "string" ? fields["direction"] : "ingress";
|
|
6811
|
+
const protocol = typeof fields["protocol"] === "string" && fields["protocol"] !== "" ? fields["protocol"] : "any";
|
|
6812
|
+
const low = fields["port_range_min"];
|
|
6813
|
+
const high = fields["port_range_max"];
|
|
6814
|
+
const ports = typeof low === "number" || typeof low === "string" ? `${String(low)}${String(low) === String(high) ? "" : `-${String(high)}`}` : void 0;
|
|
6815
|
+
const key = `${remote}\u0000${resource.id}\u0000${direction}\u0000${protocol}\u0000${ports ?? ""}`;
|
|
6816
|
+
if (seen.has(key)) continue;
|
|
6817
|
+
seen.add(key);
|
|
6818
|
+
edges.push({
|
|
6819
|
+
from: remote,
|
|
6820
|
+
to: resource.id,
|
|
6821
|
+
direction,
|
|
6822
|
+
protocol,
|
|
6823
|
+
ports
|
|
6824
|
+
});
|
|
6825
|
+
}
|
|
6826
|
+
}
|
|
6827
|
+
edges.sort((a, b) => (groupNames.get(a.from) ?? "").localeCompare(groupNames.get(b.from) ?? "") || (groupNames.get(a.to) ?? "").localeCompare(groupNames.get(b.to) ?? ""));
|
|
6828
|
+
return edges;
|
|
6829
|
+
}
|
|
6176
6830
|
/**
|
|
6177
|
-
*
|
|
6831
|
+
* Whether a rule says anything a reader would not assume.
|
|
6178
6832
|
*
|
|
6179
|
-
* Three
|
|
6180
|
-
*
|
|
6181
|
-
*
|
|
6182
|
-
*
|
|
6833
|
+
* Three shapes are assumed and therefore dropped:
|
|
6834
|
+
*
|
|
6835
|
+
* - a rule admitting the group's own members (`remote_group_id` = itself),
|
|
6836
|
+
* which is the cloud's default for any group;
|
|
6837
|
+
* - an egress rule admitting everything, which nearly every group has;
|
|
6838
|
+
* - an exact duplicate of a rule already kept.
|
|
6839
|
+
*
|
|
6840
|
+
* @param rule - the provider's rule object.
|
|
6841
|
+
* @param groupId - the id of the group the rule belongs to.
|
|
6842
|
+
* @param nameOf - resolves a group id to its name.
|
|
6843
|
+
* @returns the summary, or `undefined` when the rule says nothing new.
|
|
6183
6844
|
*/
|
|
6184
|
-
|
|
6845
|
+
function summarizeRule(rule, groupId, nameOf) {
|
|
6846
|
+
const direction = typeof rule["direction"] === "string" ? rule["direction"] : "ingress";
|
|
6847
|
+
const protocol = typeof rule["protocol"] === "string" && rule["protocol"] !== "" ? rule["protocol"] : "any";
|
|
6848
|
+
const low = rule["port_range_min"];
|
|
6849
|
+
const high = rule["port_range_max"];
|
|
6850
|
+
const ports = typeof low === "number" || typeof low === "string" ? String(low) === String(high) ? String(low) : `${String(low)}-${String(high)}` : "all";
|
|
6851
|
+
const remoteGroup = idOf$1(rule["remote_group_id"]);
|
|
6852
|
+
const remoteCidr = typeof rule["remote_ip_prefix"] === "string" && rule["remote_ip_prefix"] !== "" ? rule["remote_ip_prefix"] : void 0;
|
|
6853
|
+
if (remoteGroup === groupId) return void 0;
|
|
6854
|
+
const source$1 = remoteGroup !== void 0 ? nameOf(remoteGroup) ?? remoteGroup : remoteCidr ?? "self";
|
|
6855
|
+
if (direction === "egress" && ports === "all" && protocol === "any") return void 0;
|
|
6856
|
+
return {
|
|
6857
|
+
direction,
|
|
6858
|
+
protocol,
|
|
6859
|
+
ports,
|
|
6860
|
+
source: source$1
|
|
6861
|
+
};
|
|
6862
|
+
}
|
|
6185
6863
|
/**
|
|
6186
|
-
* The
|
|
6864
|
+
* The rules worth showing for one security group, split by direction.
|
|
6187
6865
|
*
|
|
6188
|
-
* @param
|
|
6189
|
-
* @
|
|
6190
|
-
* @returns a D2 theme class reference.
|
|
6866
|
+
* @param resources - the account's resources.
|
|
6867
|
+
* @returns group id → its ingress and egress summaries, each de-duplicated.
|
|
6191
6868
|
*/
|
|
6192
|
-
function
|
|
6193
|
-
const
|
|
6194
|
-
|
|
6195
|
-
|
|
6196
|
-
|
|
6197
|
-
|
|
6198
|
-
|
|
6199
|
-
|
|
6869
|
+
function ruleSummaries(resources) {
|
|
6870
|
+
const nameOf = (id) => resources.find((resource) => resource.id === id)?.name;
|
|
6871
|
+
const found = new Map();
|
|
6872
|
+
for (const resource of resources) {
|
|
6873
|
+
if (resource.type !== "securityGroups") continue;
|
|
6874
|
+
const rules = resource.properties?.["security_group_rules"];
|
|
6875
|
+
if (!Array.isArray(rules)) continue;
|
|
6876
|
+
const bucket = {
|
|
6877
|
+
ingress: [],
|
|
6878
|
+
egress: []
|
|
6879
|
+
};
|
|
6880
|
+
const seen = new Set();
|
|
6881
|
+
for (const rule of rules) {
|
|
6882
|
+
if (rule === null || typeof rule !== "object") continue;
|
|
6883
|
+
const summary = summarizeRule(rule, resource.id, nameOf);
|
|
6884
|
+
if (summary === void 0) continue;
|
|
6885
|
+
const key = `${summary.direction}\u0000${summary.protocol}\u0000${summary.ports}\u0000${summary.source}`;
|
|
6886
|
+
if (seen.has(key)) continue;
|
|
6887
|
+
seen.add(key);
|
|
6888
|
+
if (summary.direction === "egress") bucket.egress.push(summary);
|
|
6889
|
+
else bucket.ingress.push(summary);
|
|
6890
|
+
}
|
|
6891
|
+
if (bucket.ingress.length > 0 || bucket.egress.length > 0) found.set(resource.id, bucket);
|
|
6892
|
+
}
|
|
6893
|
+
return found;
|
|
6200
6894
|
}
|
|
6201
6895
|
/**
|
|
6202
|
-
*
|
|
6896
|
+
* Read all three layers at once.
|
|
6203
6897
|
*
|
|
6204
|
-
* @param
|
|
6205
|
-
* @returns the
|
|
6898
|
+
* @param resources - the account's resources.
|
|
6899
|
+
* @returns the resolved view.
|
|
6206
6900
|
*/
|
|
6207
|
-
|
|
6208
|
-
const
|
|
6901
|
+
function accountView(resources) {
|
|
6902
|
+
const policyEdges = policies(resources);
|
|
6903
|
+
const policyGroups = new Set();
|
|
6904
|
+
for (const edge of policyEdges) {
|
|
6905
|
+
policyGroups.add(edge.from);
|
|
6906
|
+
policyGroups.add(edge.to);
|
|
6907
|
+
}
|
|
6209
6908
|
return {
|
|
6210
|
-
|
|
6211
|
-
|
|
6909
|
+
parentOf: containment(resources),
|
|
6910
|
+
guardsOf: guards(resources),
|
|
6911
|
+
policies: policyEdges,
|
|
6912
|
+
policyGroups
|
|
6212
6913
|
};
|
|
6213
6914
|
}
|
|
6915
|
+
|
|
6916
|
+
//#endregion
|
|
6917
|
+
//#region src/traffic.ts
|
|
6214
6918
|
/**
|
|
6215
|
-
*
|
|
6919
|
+
* Read a string-valued property, unwrapping the JSON encoding the inventory
|
|
6920
|
+
* uses for some ids.
|
|
6216
6921
|
*
|
|
6217
|
-
* @param
|
|
6218
|
-
* @returns
|
|
6922
|
+
* @param value - the property value.
|
|
6923
|
+
* @returns the id it holds, or `undefined`.
|
|
6219
6924
|
*/
|
|
6220
|
-
function
|
|
6221
|
-
|
|
6222
|
-
|
|
6223
|
-
|
|
6224
|
-
|
|
6225
|
-
|
|
6226
|
-
|
|
6227
|
-
|
|
6228
|
-
|
|
6229
|
-
return;
|
|
6925
|
+
function idOf(value) {
|
|
6926
|
+
if (typeof value === "string") {
|
|
6927
|
+
const trimmed = value.trim();
|
|
6928
|
+
if (trimmed !== "" && !trimmed.startsWith("[")) return trimmed;
|
|
6929
|
+
try {
|
|
6930
|
+
const parsed = JSON.parse(trimmed);
|
|
6931
|
+
return Array.isArray(parsed) && typeof parsed[0] === "string" ? parsed[0] : void 0;
|
|
6932
|
+
} catch {
|
|
6933
|
+
return void 0;
|
|
6230
6934
|
}
|
|
6231
|
-
|
|
6232
|
-
|
|
6233
|
-
|
|
6234
|
-
lines.push(`${indent}}`);
|
|
6235
|
-
};
|
|
6236
|
-
for (const root of topology.roots) write$1(root, 0);
|
|
6237
|
-
if (topology.roots.length > 0) lines.push("");
|
|
6238
|
-
for (const edge of drawnEdges(topology)) lines.push(`${nodeId(edge.from)} -> ${nodeId(edge.to)}: ${quote(edge.attribute)} { class: [${CONNECTION_CLASS$1}] }`);
|
|
6239
|
-
return lines.join("\n");
|
|
6935
|
+
}
|
|
6936
|
+
if (Array.isArray(value) && typeof value[0] === "string") return value[0];
|
|
6937
|
+
return void 0;
|
|
6240
6938
|
}
|
|
6241
6939
|
/**
|
|
6242
|
-
* The
|
|
6940
|
+
* The object an array-valued property's first entry holds.
|
|
6243
6941
|
*
|
|
6244
|
-
*
|
|
6245
|
-
*
|
|
6942
|
+
* The provider nests one-to-many links this way: `loadbalancers: [{ id }]`,
|
|
6943
|
+
* `eips: [{ eip_id, eip_address }]`. Reading the first entry is enough because a
|
|
6944
|
+
* listener belongs to exactly one balancer and an EIP has exactly one instance.
|
|
6246
6945
|
*
|
|
6247
|
-
*
|
|
6248
|
-
*
|
|
6249
|
-
|
|
6250
|
-
|
|
6251
|
-
|
|
6252
|
-
|
|
6253
|
-
|
|
6254
|
-
|
|
6255
|
-
|
|
6256
|
-
*
|
|
6257
|
-
* is visible from where the resource is drawn; a line from inside the
|
|
6258
|
-
* container out to it adds no fact and crosses the container's border to
|
|
6259
|
-
* say nothing. Five of those on the same account, all of them `vpc_id`.
|
|
6946
|
+
* @param value - the property value.
|
|
6947
|
+
* @returns the first entry, when it is an object.
|
|
6948
|
+
*/
|
|
6949
|
+
function firstOf(value) {
|
|
6950
|
+
if (!Array.isArray(value)) return void 0;
|
|
6951
|
+
const first = value[0];
|
|
6952
|
+
return first !== null && typeof first === "object" ? first : void 0;
|
|
6953
|
+
}
|
|
6954
|
+
/**
|
|
6955
|
+
* The cluster a listener fronts, when the listener says so.
|
|
6260
6956
|
*
|
|
6261
|
-
*
|
|
6262
|
-
*
|
|
6263
|
-
*
|
|
6957
|
+
* A listener created by the Kubernetes integration records its owner as JSON in
|
|
6958
|
+
* `description` — `{"cluster_id": "…", "service_id": "…"}`. That is the only
|
|
6959
|
+
* place the hop from the load balancer to the cluster is written down.
|
|
6264
6960
|
*
|
|
6265
|
-
* @param
|
|
6266
|
-
* @returns the
|
|
6961
|
+
* @param resource - a listener.
|
|
6962
|
+
* @returns the cluster id, or `undefined` when the listener is not owned by one.
|
|
6267
6963
|
*/
|
|
6268
|
-
function
|
|
6269
|
-
const
|
|
6270
|
-
|
|
6271
|
-
const
|
|
6272
|
-
|
|
6273
|
-
|
|
6274
|
-
|
|
6275
|
-
|
|
6276
|
-
|
|
6277
|
-
|
|
6278
|
-
|
|
6279
|
-
drawn.push(edge);
|
|
6964
|
+
function clusterBehind(resource) {
|
|
6965
|
+
const description = resource.properties?.["description"];
|
|
6966
|
+
if (typeof description !== "string") return void 0;
|
|
6967
|
+
const trimmed = description.trim();
|
|
6968
|
+
if (!trimmed.startsWith("{")) return void 0;
|
|
6969
|
+
try {
|
|
6970
|
+
const parsed = JSON.parse(trimmed);
|
|
6971
|
+
const cluster = parsed["cluster_id"];
|
|
6972
|
+
return typeof cluster === "string" && cluster !== "" ? cluster : void 0;
|
|
6973
|
+
} catch {
|
|
6974
|
+
return void 0;
|
|
6280
6975
|
}
|
|
6281
|
-
return drawn;
|
|
6282
6976
|
}
|
|
6283
6977
|
/**
|
|
6284
|
-
*
|
|
6285
|
-
*
|
|
6286
|
-
* The `r_` prefix is what keeps an id from colliding with D2's own reserved
|
|
6287
|
-
* words, and the base conversion handles every character D2 would otherwise
|
|
6288
|
-
* read as syntax.
|
|
6978
|
+
* Every path the inventory states.
|
|
6289
6979
|
*
|
|
6290
|
-
* @param
|
|
6291
|
-
* @returns
|
|
6980
|
+
* @param resources - the account's resources.
|
|
6981
|
+
* @returns the paths, each starting at the internet.
|
|
6292
6982
|
*/
|
|
6293
|
-
function
|
|
6294
|
-
|
|
6983
|
+
function trafficPaths(resources) {
|
|
6984
|
+
const byId = new Map(resources.map((resource) => [resource.id, resource]));
|
|
6985
|
+
const paths = [];
|
|
6986
|
+
for (const resource of resources) {
|
|
6987
|
+
if (resource.type !== "publicips") continue;
|
|
6988
|
+
const attached = idOf(resource.properties?.["associateInstanceId"]);
|
|
6989
|
+
if (attached === void 0 || !byId.has(attached)) continue;
|
|
6990
|
+
const address = typeof resource.properties?.["publicIpAddress"] === "string" ? resource.properties["publicIpAddress"] : resource.name;
|
|
6991
|
+
paths.push({ hops: [
|
|
6992
|
+
{
|
|
6993
|
+
kind: "internet",
|
|
6994
|
+
id: void 0,
|
|
6995
|
+
detail: void 0
|
|
6996
|
+
},
|
|
6997
|
+
{
|
|
6998
|
+
kind: "eip",
|
|
6999
|
+
id: resource.id,
|
|
7000
|
+
detail: address
|
|
7001
|
+
},
|
|
7002
|
+
{
|
|
7003
|
+
kind: "target",
|
|
7004
|
+
id: attached,
|
|
7005
|
+
detail: void 0
|
|
7006
|
+
}
|
|
7007
|
+
] });
|
|
7008
|
+
}
|
|
7009
|
+
for (const resource of resources) {
|
|
7010
|
+
if (resource.type !== "listeners") continue;
|
|
7011
|
+
const owner = firstOf(resource.properties?.["loadbalancers"]);
|
|
7012
|
+
const balancer = owner === void 0 ? void 0 : idOf(owner["id"]);
|
|
7013
|
+
if (balancer === void 0 || !byId.has(balancer)) continue;
|
|
7014
|
+
const protocol = typeof resource.properties?.["protocol"] === "string" ? resource.properties["protocol"] : void 0;
|
|
7015
|
+
const port = resource.properties?.["protocol_port"];
|
|
7016
|
+
const detail = protocol !== void 0 && (typeof port === "number" || typeof port === "string") ? `${protocol}:${String(port)}` : protocol;
|
|
7017
|
+
const cluster = clusterBehind(resource);
|
|
7018
|
+
const hops = [{
|
|
7019
|
+
kind: "balancer",
|
|
7020
|
+
id: balancer,
|
|
7021
|
+
detail: void 0
|
|
7022
|
+
}, {
|
|
7023
|
+
kind: "listener",
|
|
7024
|
+
id: resource.id,
|
|
7025
|
+
detail
|
|
7026
|
+
}];
|
|
7027
|
+
if (cluster !== void 0 && byId.has(cluster)) hops.push({
|
|
7028
|
+
kind: "target",
|
|
7029
|
+
id: cluster,
|
|
7030
|
+
detail: void 0
|
|
7031
|
+
});
|
|
7032
|
+
paths.push({ hops });
|
|
7033
|
+
}
|
|
7034
|
+
return paths;
|
|
7035
|
+
}
|
|
7036
|
+
/**
|
|
7037
|
+
* Continue a chain past its last box, through the policy that admits it.
|
|
7038
|
+
*
|
|
7039
|
+
* ## The gap this closes, and the limit of closing it
|
|
7040
|
+
*
|
|
7041
|
+
* A Kubernetes listener records the cluster it fronts, and nothing further: the
|
|
7042
|
+
* inventory does not say which pods talk to which database. What it DOES say is
|
|
7043
|
+
* that one group admits another on a port, and which resources each group
|
|
7044
|
+
* guards — so a cluster guarded by group A, where A's peer group B admits the
|
|
7045
|
+
* database, is a flow the account actually states.
|
|
7046
|
+
*
|
|
7047
|
+
* That is a hop through a rule rather than through a resource, and it is the
|
|
7048
|
+
* only continuation the data supports. It is added only when exactly one group
|
|
7049
|
+
* admits exactly one guarded target: with several candidates the chain would be
|
|
7050
|
+
* a guess, and a guess drawn as a flow is the defect this module exists to
|
|
7051
|
+
* avoid.
|
|
7052
|
+
*
|
|
7053
|
+
* @param resources - the account's resources.
|
|
7054
|
+
* @param view - the resolved layers.
|
|
7055
|
+
* @returns extra hops, keyed by the resource a chain currently ends at.
|
|
7056
|
+
*/
|
|
7057
|
+
function policyContinuations(resources, view) {
|
|
7058
|
+
const continuations = new Map();
|
|
7059
|
+
const byId = new Map(resources.map((resource) => [resource.id, resource]));
|
|
7060
|
+
const carrierOf = new Map();
|
|
7061
|
+
for (const [resourceId, names] of view.guardsOf) for (const name$1 of names) {
|
|
7062
|
+
const group = resources.find((resource) => resource.name === name$1 && resource.type === "securityGroups");
|
|
7063
|
+
if (group === void 0) continue;
|
|
7064
|
+
carrierOf.set(group.id, [...carrierOf.get(group.id) ?? [], resourceId]);
|
|
7065
|
+
}
|
|
7066
|
+
for (const policy of view.policies) {
|
|
7067
|
+
if (policy.direction !== "ingress") continue;
|
|
7068
|
+
const carriers = carrierOf.get(policy.from) ?? [];
|
|
7069
|
+
const destination = byId.get(policy.to);
|
|
7070
|
+
if (carriers.length !== 1) continue;
|
|
7071
|
+
if (destination === void 0) continue;
|
|
7072
|
+
const guardedResources = resources.filter((resource) => (view.guardsOf.get(resource.id) ?? []).includes(destination.name ?? "") && resource.id !== carriers[0]);
|
|
7073
|
+
if (guardedResources.length !== 1) continue;
|
|
7074
|
+
const target = guardedResources[0];
|
|
7075
|
+
continuations.set(carriers[0], {
|
|
7076
|
+
kind: "target",
|
|
7077
|
+
id: target.id,
|
|
7078
|
+
detail: policy.ports === void 0 ? void 0 : `${policy.protocol === "any" ? "tcp" : policy.protocol} ${policy.ports}`
|
|
7079
|
+
});
|
|
7080
|
+
}
|
|
7081
|
+
return continuations;
|
|
6295
7082
|
}
|
|
6296
7083
|
|
|
6297
7084
|
//#endregion
|
|
6298
|
-
//#region src/
|
|
6299
|
-
/**
|
|
6300
|
-
* The D2 palette classes per node kind.
|
|
6301
|
-
*
|
|
6302
|
-
* These are D2's OWN theme classes (`fill-B4`, `stroke-B1`, …), not custom
|
|
6303
|
-
* colours, and that is the whole reason for using them. A custom
|
|
6304
|
-
* `classes: { vpc: { style: { fill: "#EEF4FF" } } }` is emitted as a
|
|
6305
|
-
* hard-coded `fill="#EEF4FF"` on the element, so it stays light-blue on a dark
|
|
6306
|
-
* ground and the diagram becomes unreadable. The built-in classes are emitted
|
|
6307
|
-
* as class REFERENCES, and D2 writes both palettes into the SVG with the dark
|
|
6308
|
-
* half behind `@media (prefers-color-scheme: dark)` — so one file follows the
|
|
6309
|
-
* viewer's theme with no JavaScript.
|
|
6310
|
-
*
|
|
6311
|
-
* The hues are D2's accent ramps: `B` (blue) for network structure, `AA` (red)
|
|
6312
|
-
* for the one kind that is a warning by nature, and `N` (neutral) for anything
|
|
6313
|
-
* unrecognised. Lightness steps apart so a subnet reads as inside its VPC.
|
|
6314
|
-
*/
|
|
6315
|
-
const KIND_CLASSES = {
|
|
6316
|
-
vpc: "fill-B5, stroke-B3",
|
|
6317
|
-
subnet: "fill-B6, stroke-B4",
|
|
6318
|
-
"security-group": "fill-AA4, stroke-AA2",
|
|
6319
|
-
"security-group-rule": "fill-AA5, stroke-AA4",
|
|
6320
|
-
gateway: "fill-N6, stroke-N4",
|
|
6321
|
-
"load-balancer": "fill-B4, stroke-B2",
|
|
6322
|
-
compute: "fill-AB5, stroke-AB4",
|
|
6323
|
-
database: "fill-AB4, stroke-AB4"
|
|
6324
|
-
};
|
|
7085
|
+
//#region src/diagram.ts
|
|
6325
7086
|
/**
|
|
6326
|
-
* The
|
|
7087
|
+
* The lines a security group's rules become, inside the box they guard.
|
|
6327
7088
|
*
|
|
6328
|
-
*
|
|
6329
|
-
*
|
|
6330
|
-
* the
|
|
7089
|
+
* Ingress first, then egress, each rule on its own line. The verbs are
|
|
7090
|
+
* `in`/`out` rather than the provider's `ingress`/`egress`: a box is scanned,
|
|
7091
|
+
* and the direction is the first thing a reader checks.
|
|
6331
7092
|
*
|
|
6332
|
-
*
|
|
6333
|
-
*
|
|
6334
|
-
* node is not an ECS instance and a DDS cluster is not RDS, and a box captioned
|
|
6335
|
-
* "ECS" would claim it is. The node's own `name` and terraform type carry the
|
|
6336
|
-
* specific identity.
|
|
7093
|
+
* @param summaries - the rules worth showing for the groups guarding a box.
|
|
7094
|
+
* @returns the detail lines, or an empty list when there are none.
|
|
6337
7095
|
*/
|
|
6338
|
-
|
|
6339
|
-
|
|
6340
|
-
|
|
6341
|
-
|
|
6342
|
-
|
|
6343
|
-
|
|
6344
|
-
|
|
6345
|
-
|
|
6346
|
-
database: "database"
|
|
6347
|
-
};
|
|
7096
|
+
function ruleLines(summaries) {
|
|
7097
|
+
const lines = [];
|
|
7098
|
+
for (const bucket of summaries) {
|
|
7099
|
+
for (const rule of bucket.ingress) lines.push(`in ${rule.protocol} ${rule.ports} \u2190 ${rule.source}`);
|
|
7100
|
+
for (const rule of bucket.egress) lines.push(`out ${rule.protocol} ${rule.ports} \u2192 ${rule.source}`);
|
|
7101
|
+
}
|
|
7102
|
+
return lines;
|
|
7103
|
+
}
|
|
6348
7104
|
/**
|
|
6349
|
-
* The
|
|
7105
|
+
* The text a resource's box shows.
|
|
7106
|
+
*
|
|
7107
|
+
* The name first — that is what the console and every other tool call it by —
|
|
7108
|
+
* then the one fact that makes it recognisable: a subnet's CIDR, a server's
|
|
7109
|
+
* flavour. Everything else is left out, because a box whose label is a dump of
|
|
7110
|
+
* properties cannot be scanned.
|
|
6350
7111
|
*
|
|
6351
|
-
*
|
|
6352
|
-
*
|
|
6353
|
-
* them would need a translation step nobody asked for.
|
|
7112
|
+
* @param resource - the resource.
|
|
7113
|
+
* @returns the label.
|
|
6354
7114
|
*/
|
|
6355
|
-
|
|
6356
|
-
|
|
6357
|
-
|
|
6358
|
-
|
|
6359
|
-
}
|
|
7115
|
+
function labelOf(resource) {
|
|
7116
|
+
const name$1 = resource.name ?? resource.id.slice(0, 8);
|
|
7117
|
+
const properties = resource.properties ?? {};
|
|
7118
|
+
const detail = typeof properties["cidr"] === "string" ? properties["cidr"] : typeof properties["flavor"] === "string" ? properties["flavor"] : typeof properties["flavorCode"] === "string" ? properties["flavorCode"] : typeof properties["publicIpAddress"] === "string" ? properties["publicIpAddress"] : void 0;
|
|
7119
|
+
return detail === void 0 || detail === name$1 ? name$1 : `${name$1} ${detail}`;
|
|
7120
|
+
}
|
|
6360
7121
|
/**
|
|
6361
|
-
*
|
|
7122
|
+
* Build the node tree for one group of resources.
|
|
6362
7123
|
*
|
|
6363
|
-
*
|
|
6364
|
-
*
|
|
6365
|
-
*
|
|
6366
|
-
* the
|
|
7124
|
+
* @param members - the resources to include.
|
|
7125
|
+
* @param parentOf - the resolved containment.
|
|
7126
|
+
* @param guardsOf - the resolved guarding.
|
|
7127
|
+
* @param excluded - ids handled elsewhere (the policy band).
|
|
7128
|
+
* @returns the nodes, with the topmost ones returned rather than nested.
|
|
6367
7129
|
*/
|
|
6368
|
-
|
|
7130
|
+
function treeOf(members, parentOf, guardsOf, excluded, summaryByName) {
|
|
7131
|
+
const present = new Set(members.map((resource) => resource.id));
|
|
7132
|
+
const holds = new Set();
|
|
7133
|
+
for (const resource of members) {
|
|
7134
|
+
const parent = parentOf.get(resource.id);
|
|
7135
|
+
if (parent !== void 0 && present.has(parent)) holds.add(parent);
|
|
7136
|
+
}
|
|
7137
|
+
const nodes = new Map();
|
|
7138
|
+
const build = (resource) => {
|
|
7139
|
+
const existing = nodes.get(resource.id);
|
|
7140
|
+
if (existing !== void 0) return existing;
|
|
7141
|
+
const guards$1 = guardsOf.get(resource.id) ?? [];
|
|
7142
|
+
const label = guards$1.length === 0 ? labelOf(resource) : `${labelOf(resource)} · SG ${guards$1.join(", ")}`;
|
|
7143
|
+
const node = {
|
|
7144
|
+
id: resource.id,
|
|
7145
|
+
provider: resource.provider,
|
|
7146
|
+
label,
|
|
7147
|
+
lines: ruleLines(guards$1.map((name$1) => summaryByName.get(name$1)).filter((x) => x !== void 0)),
|
|
7148
|
+
role: holds.has(resource.id) ? "container" : "resource",
|
|
7149
|
+
children: []
|
|
7150
|
+
};
|
|
7151
|
+
nodes.set(resource.id, node);
|
|
7152
|
+
return node;
|
|
7153
|
+
};
|
|
7154
|
+
const roots = [];
|
|
7155
|
+
const nested = new Set();
|
|
7156
|
+
for (const resource of members) {
|
|
7157
|
+
if (excluded.has(resource.id)) continue;
|
|
7158
|
+
const parent = parentOf.get(resource.id);
|
|
7159
|
+
if (parent === void 0 || !present.has(parent)) {
|
|
7160
|
+
roots.push(build(resource));
|
|
7161
|
+
continue;
|
|
7162
|
+
}
|
|
7163
|
+
nodes.get(parent)?.children.push(build(resource));
|
|
7164
|
+
nested.add(resource.id);
|
|
7165
|
+
}
|
|
7166
|
+
for (const resource of members) {
|
|
7167
|
+
if (nested.has(resource.id) || excluded.has(resource.id)) continue;
|
|
7168
|
+
if (!roots.some((root) => root.id === resource.id)) roots.push(build(resource));
|
|
7169
|
+
}
|
|
7170
|
+
return roots;
|
|
7171
|
+
}
|
|
7172
|
+
/**
|
|
7173
|
+
* Convert the tree into the layout's shape.
|
|
7174
|
+
*
|
|
7175
|
+
* @param node - the tree node.
|
|
7176
|
+
* @returns the layout node.
|
|
6369
7177
|
/**
|
|
6370
|
-
*
|
|
7178
|
+
* Convert the tree into the layout's shape.
|
|
6371
7179
|
*
|
|
6372
|
-
* @param
|
|
6373
|
-
* @
|
|
6374
|
-
* `<to-address>`. Only addresses the topology knows are drawn.
|
|
6375
|
-
* @returns the D2 source and the SVG it renders to.
|
|
6376
|
-
* @throws when the generated D2 source does not compile. That is a defect in
|
|
6377
|
-
* this module rather than in the user's configuration — the generator is
|
|
6378
|
-
* what produced the text — so the message names the generated fragment.
|
|
7180
|
+
* @param node - the tree node.
|
|
7181
|
+
* @returns the layout node.
|
|
6379
7182
|
*/
|
|
6380
|
-
|
|
6381
|
-
const source$1 = toD2(topology, edges);
|
|
7183
|
+
function toLayoutNode$1(node) {
|
|
6382
7184
|
return {
|
|
6383
|
-
|
|
6384
|
-
|
|
7185
|
+
id: node.id,
|
|
7186
|
+
label: node.label,
|
|
7187
|
+
...node.provider === "" ? {} : { provider: node.provider },
|
|
7188
|
+
...node.lines.length === 0 ? {} : { detail: node.lines },
|
|
7189
|
+
...node.figure === void 0 ? {} : { figure: node.figure },
|
|
7190
|
+
children: node.children.map((child) => toLayoutNode$1(child))
|
|
6385
7191
|
};
|
|
6386
7192
|
}
|
|
6387
7193
|
/**
|
|
6388
|
-
*
|
|
6389
|
-
*
|
|
6390
|
-
* The shape of the output is fixed by what D2 accepts:
|
|
7194
|
+
* Build the drawable diagram for an account.
|
|
6391
7195
|
*
|
|
6392
|
-
*
|
|
6393
|
-
*
|
|
6394
|
-
* in every subnet. Facts go into the node's LABEL instead.
|
|
6395
|
-
* - Facts are separated by `·` rather than aligned into columns, because D2
|
|
6396
|
-
* treats a leading `|` as a block-string terminator.
|
|
6397
|
-
* - No `vars` block beyond the theme: D2 discards everything under it except
|
|
6398
|
-
* `d2-config`.
|
|
6399
|
-
*
|
|
6400
|
-
* @param topology - the derived picture.
|
|
6401
|
-
* @param edges - the relationships to draw.
|
|
6402
|
-
* @returns D2 source text.
|
|
7196
|
+
* @param resources - everything the inventory reported.
|
|
7197
|
+
* @returns the placed boxes, the policy edges, and each box's role.
|
|
6403
7198
|
*/
|
|
6404
|
-
function
|
|
6405
|
-
const
|
|
6406
|
-
const
|
|
6407
|
-
|
|
6408
|
-
|
|
6409
|
-
|
|
6410
|
-
|
|
6411
|
-
|
|
6412
|
-
|
|
6413
|
-
|
|
6414
|
-
|
|
6415
|
-
|
|
7199
|
+
function buildDiagram(resources) {
|
|
7200
|
+
const view = accountView(resources);
|
|
7201
|
+
const byId = new Map(resources.map((resource) => [resource.id, resource]));
|
|
7202
|
+
const summaries = ruleSummaries(resources);
|
|
7203
|
+
const summaryByName = new Map(resources.filter((resource) => summaries.has(resource.id)).map((resource) => [resource.name ?? resource.id, summaries.get(resource.id)]));
|
|
7204
|
+
const childrenOf = new Map();
|
|
7205
|
+
for (const [child, parent] of view.parentOf) childrenOf.set(parent, [...childrenOf.get(parent) ?? [], child]);
|
|
7206
|
+
const regions = new Map();
|
|
7207
|
+
for (const resource of resources) {
|
|
7208
|
+
const region = resource.region ?? "unknown";
|
|
7209
|
+
regions.set(region, [...regions.get(region) ?? [], resource]);
|
|
7210
|
+
}
|
|
7211
|
+
const claimed = new Set();
|
|
7212
|
+
/** Build one resource's node, with its guards shown inside it. */
|
|
7213
|
+
const nodeFor = (resource) => ({
|
|
7214
|
+
id: resource.id,
|
|
7215
|
+
provider: resource.provider,
|
|
7216
|
+
label: (() => {
|
|
7217
|
+
const guards$1 = view.guardsOf.get(resource.id) ?? [];
|
|
7218
|
+
return guards$1.length === 0 ? labelOf(resource) : `${labelOf(resource)} · SG ${guards$1.join(", ")}`;
|
|
7219
|
+
})(),
|
|
7220
|
+
lines: ruleLines((view.guardsOf.get(resource.id) ?? []).map((name$1) => summaryByName.get(name$1)).filter((x) => x !== void 0)),
|
|
7221
|
+
role: (childrenOf.get(resource.id) ?? []).length > 0 ? "container" : "resource",
|
|
7222
|
+
children: []
|
|
7223
|
+
});
|
|
7224
|
+
/** Attach every descendant of `parentId`, depth first. */
|
|
7225
|
+
const attachBelow = (parent, parentId) => {
|
|
7226
|
+
for (const childId of childrenOf.get(parentId) ?? []) {
|
|
7227
|
+
if (claimed.has(childId)) continue;
|
|
7228
|
+
claimed.add(childId);
|
|
7229
|
+
const resource = byId.get(childId);
|
|
7230
|
+
if (resource === void 0) continue;
|
|
7231
|
+
const node = nodeFor(resource);
|
|
7232
|
+
parent.children.push(node);
|
|
7233
|
+
attachBelow(node, childId);
|
|
7234
|
+
}
|
|
7235
|
+
};
|
|
7236
|
+
const regionTrees = [];
|
|
7237
|
+
for (const [region, members] of [...regions].sort((a, b) => a[0].localeCompare(b[0]))) {
|
|
7238
|
+
const inRegion = new Set(members.map((resource) => resource.id));
|
|
7239
|
+
const regionNode = {
|
|
7240
|
+
id: `region:${region}`,
|
|
7241
|
+
label: region,
|
|
7242
|
+
role: "container",
|
|
7243
|
+
provider: "",
|
|
7244
|
+
lines: [],
|
|
7245
|
+
children: []
|
|
7246
|
+
};
|
|
7247
|
+
for (const vpc of members.filter((resource) => resource.type === "vpcs")) {
|
|
7248
|
+
claimed.add(vpc.id);
|
|
7249
|
+
const vpcNode = {
|
|
7250
|
+
id: vpc.id,
|
|
7251
|
+
provider: vpc.provider,
|
|
7252
|
+
label: labelOf(vpc),
|
|
7253
|
+
role: "container",
|
|
7254
|
+
lines: [],
|
|
7255
|
+
children: []
|
|
7256
|
+
};
|
|
7257
|
+
attachBelow(vpcNode, vpc.id);
|
|
7258
|
+
regionNode.children.push(vpcNode);
|
|
6416
7259
|
}
|
|
6417
|
-
|
|
6418
|
-
if (label === void 0) throw new Error(`no label for connection kind "${edge.connection}" (${edge.from} -> ${edge.to})`);
|
|
6419
|
-
lines.push(`${d2Id(edge.from)} -> ${d2Id(edge.to)}: ${quote(label)} { class: [${CONNECTION_CLASS}] }`);
|
|
7260
|
+
regionTrees.push(regionNode);
|
|
6420
7261
|
}
|
|
6421
|
-
const
|
|
6422
|
-
|
|
6423
|
-
|
|
7262
|
+
const unclaimed = resources.filter((resource) => !claimed.has(resource.id) && resource.type !== "vpcs");
|
|
7263
|
+
/**
|
|
7264
|
+
* Which resources are shown as TEXT on something else rather than as a box.
|
|
7265
|
+
*
|
|
7266
|
+
* A security group and a bandwidth are properties of what they serve: the
|
|
7267
|
+
* value that names them is on the served resource, and the group's rules
|
|
7268
|
+
* already appear inside that resource's box. Drawing them again as boxes puts
|
|
7269
|
+
* the same object in two places, and a group that serves three things in one.
|
|
7270
|
+
*/
|
|
7271
|
+
const users = groupUsers(resources);
|
|
7272
|
+
/** The rule lines one security group's box carries, ingress first. */
|
|
7273
|
+
const groupLines = (group) => {
|
|
7274
|
+
const bucket = summaryByName.get(group.name ?? "");
|
|
7275
|
+
if (bucket === void 0) return [];
|
|
7276
|
+
const lines = [];
|
|
7277
|
+
for (const rule of bucket.ingress) lines.push(`in ${rule.protocol} ${rule.ports} \u2190 ${rule.source}`);
|
|
7278
|
+
for (const rule of bucket.egress) lines.push(`out ${rule.protocol} ${rule.ports} \u2192 ${rule.source}`);
|
|
7279
|
+
return lines;
|
|
7280
|
+
};
|
|
7281
|
+
/**
|
|
7282
|
+
* The figure a security group's box shows: how many resources use it.
|
|
7283
|
+
*
|
|
7284
|
+
* A single number, drawn centred and larger than the rules, because it answers
|
|
7285
|
+
* a different question — is this group alive — and a reader should be able to
|
|
7286
|
+
* tell live from leftover without reading a line of text.
|
|
7287
|
+
*/
|
|
7288
|
+
const groupFigure = (group) => String((users.get(group.id) ?? []).length);
|
|
7289
|
+
for (const regionNode of regionTrees) {
|
|
7290
|
+
const region = regionNode.label;
|
|
7291
|
+
const mine = unclaimed.filter((resource) => (resource.region ?? "unknown") === region);
|
|
7292
|
+
const groups = mine.filter((resource) => resource.type === "securityGroups");
|
|
7293
|
+
if (groups.length > 0) regionNode.children.push({
|
|
7294
|
+
id: `band:security:${region}`,
|
|
7295
|
+
label: "Security groups",
|
|
7296
|
+
role: "container",
|
|
7297
|
+
provider: "",
|
|
7298
|
+
lines: [],
|
|
7299
|
+
children: [...groups].sort((a, b) => (a.name ?? "").localeCompare(b.name ?? "")).map((group) => ({
|
|
7300
|
+
id: group.id,
|
|
7301
|
+
provider: group.provider,
|
|
7302
|
+
label: group.name ?? group.id,
|
|
7303
|
+
lines: groupLines(group),
|
|
7304
|
+
figure: groupFigure(group),
|
|
7305
|
+
role: "resource",
|
|
7306
|
+
children: []
|
|
7307
|
+
}))
|
|
7308
|
+
});
|
|
7309
|
+
const loose = mine.filter((resource) => resource.type !== "securityGroups" && resource.type !== "bandwidths");
|
|
7310
|
+
if (loose.length === 0) continue;
|
|
7311
|
+
const kids = treeOf(loose, view.parentOf, view.guardsOf, new Set(), summaryByName);
|
|
7312
|
+
const sorted = [...kids].sort((a, b) => a.label.localeCompare(b.label));
|
|
7313
|
+
regionNode.children.push({
|
|
7314
|
+
id: `band:loose:${region}`,
|
|
7315
|
+
label: "Unattached",
|
|
7316
|
+
role: "container",
|
|
7317
|
+
provider: "",
|
|
7318
|
+
lines: [],
|
|
7319
|
+
children: sorted
|
|
7320
|
+
});
|
|
7321
|
+
}
|
|
7322
|
+
const roots = regionTrees;
|
|
7323
|
+
const layout = layoutTree(roots.map((root) => toLayoutNode$1(root)));
|
|
7324
|
+
const roles = new Map();
|
|
7325
|
+
const note = (node) => {
|
|
7326
|
+
roles.set(node.id, node.role);
|
|
7327
|
+
for (const child of node.children) note(child);
|
|
7328
|
+
};
|
|
7329
|
+
for (const root of roots) note(root);
|
|
7330
|
+
const edges = [];
|
|
7331
|
+
view.policies;
|
|
7332
|
+
const ambiguous = new Set();
|
|
7333
|
+
for (const [child, parent] of view.parentOf) ambiguous.add(`${child}\u0000${parent}`);
|
|
7334
|
+
for (const path of trafficPaths(resources)) for (let index = 0; index < path.hops.length - 1; index++) {
|
|
7335
|
+
const from = path.hops[index];
|
|
7336
|
+
const to = path.hops[index + 1];
|
|
7337
|
+
if (from.id === void 0 || to.id === void 0) continue;
|
|
7338
|
+
if (!roles.has(from.id) || !roles.has(to.id)) continue;
|
|
7339
|
+
if (ambiguous.has(`${from.id}\u0000${to.id}`)) continue;
|
|
7340
|
+
if (ambiguous.has(`${to.id}\u0000${from.id}`)) continue;
|
|
7341
|
+
edges.push({
|
|
7342
|
+
from: from.id,
|
|
7343
|
+
to: to.id,
|
|
7344
|
+
label: to.detail ?? from.detail ?? "",
|
|
7345
|
+
kind: "traffic"
|
|
7346
|
+
});
|
|
7347
|
+
}
|
|
7348
|
+
for (const [from, hop] of policyContinuations(resources, view)) {
|
|
7349
|
+
if (hop.id === void 0) continue;
|
|
7350
|
+
if (!roles.has(from) || !roles.has(hop.id)) continue;
|
|
7351
|
+
edges.push({
|
|
7352
|
+
from,
|
|
7353
|
+
to: hop.id,
|
|
7354
|
+
label: hop.detail ?? "",
|
|
7355
|
+
kind: "traffic"
|
|
7356
|
+
});
|
|
7357
|
+
}
|
|
7358
|
+
return {
|
|
7359
|
+
width: layout.width,
|
|
7360
|
+
height: layout.height,
|
|
7361
|
+
boxes: layout.boxes,
|
|
7362
|
+
edges,
|
|
7363
|
+
roles
|
|
7364
|
+
};
|
|
6424
7365
|
}
|
|
6425
7366
|
/**
|
|
6426
|
-
*
|
|
7367
|
+
* Draw the account as a diagram.
|
|
6427
7368
|
*
|
|
6428
|
-
*
|
|
6429
|
-
*
|
|
6430
|
-
|
|
7369
|
+
* @param resources - everything the inventory reported.
|
|
7370
|
+
* @returns the SVG and a text record of what it shows.
|
|
7371
|
+
*/
|
|
7372
|
+
function renderAccountDiagram(resources) {
|
|
7373
|
+
const diagram = buildDiagram(resources);
|
|
7374
|
+
return {
|
|
7375
|
+
diagram,
|
|
7376
|
+
svg: renderDiagram(diagram),
|
|
7377
|
+
source: describe$1(diagram)
|
|
7378
|
+
};
|
|
7379
|
+
}
|
|
7380
|
+
/**
|
|
7381
|
+
* A readable text form of the diagram, written beside the SVG.
|
|
7382
|
+
*
|
|
7383
|
+
* It is not D2 any more and does not claim to be: it records the nesting and the
|
|
7384
|
+
* policy edges as indented text, which is what a reader opens it for.
|
|
6431
7385
|
*
|
|
6432
|
-
* @param
|
|
6433
|
-
* @
|
|
6434
|
-
* @returns the lines for this node and its subtree.
|
|
7386
|
+
* @param diagram - the built diagram.
|
|
7387
|
+
* @returns the text.
|
|
6435
7388
|
*/
|
|
6436
|
-
function
|
|
6437
|
-
const
|
|
6438
|
-
const
|
|
6439
|
-
|
|
6440
|
-
|
|
6441
|
-
|
|
6442
|
-
const
|
|
6443
|
-
if (
|
|
6444
|
-
|
|
6445
|
-
|
|
6446
|
-
`${indent}${d2Id(node.address)}: ${quote(label)} {`,
|
|
6447
|
-
`${indent} ${style}`,
|
|
6448
|
-
...extra.map((line) => `${indent} ${line.trim()}`),
|
|
6449
|
-
`${indent}}`
|
|
6450
|
-
];
|
|
7389
|
+
function describe$1(diagram) {
|
|
7390
|
+
const lines = [];
|
|
7391
|
+
const walk = (box, depth) => {
|
|
7392
|
+
lines.push(`${" ".repeat(depth)}${box.label}`);
|
|
7393
|
+
for (const child of box.children) walk(child, depth + 1);
|
|
7394
|
+
};
|
|
7395
|
+
for (const root of diagram.boxes.filter((box) => box.depth === 0)) walk(root, 0);
|
|
7396
|
+
if (diagram.edges.length > 0) {
|
|
7397
|
+
lines.push("", "policy:");
|
|
7398
|
+
for (const edge of diagram.edges) lines.push(` ${edge.from} -> ${edge.to} (${edge.label})`);
|
|
6451
7399
|
}
|
|
6452
|
-
return
|
|
6453
|
-
`${indent}${d2Id(node.address)}: ${quote(label)} {`,
|
|
6454
|
-
`${indent} ${style}`,
|
|
6455
|
-
...extra.map((line) => `${indent} ${line.trim()}`),
|
|
6456
|
-
...node.children.flatMap((child) => nodeLines(child, depth + 1)),
|
|
6457
|
-
`${indent}}`
|
|
6458
|
-
];
|
|
7400
|
+
return lines.join("\n");
|
|
6459
7401
|
}
|
|
7402
|
+
|
|
7403
|
+
//#endregion
|
|
7404
|
+
//#region src/design-render.ts
|
|
6460
7405
|
/**
|
|
6461
|
-
* The
|
|
7406
|
+
* The word a box is headed with, by kind.
|
|
6462
7407
|
*
|
|
6463
|
-
*
|
|
6464
|
-
*
|
|
6465
|
-
*
|
|
6466
|
-
*
|
|
6467
|
-
*
|
|
7408
|
+
* Written out rather than derived from the kind name because the kind is this
|
|
7409
|
+
* module's vocabulary (`security-group`) while the box is read by someone
|
|
7410
|
+
* looking at a network diagram and needs the words the cloud uses.
|
|
7411
|
+
*
|
|
7412
|
+
* `compute` and `database` cover several provider types, so their captions stay
|
|
7413
|
+
* generic: a CCE node is not an ECS instance, and a box captioned "ECS" would
|
|
7414
|
+
* claim it is.
|
|
7415
|
+
*/
|
|
7416
|
+
const KIND_CAPTION = {
|
|
7417
|
+
vpc: "VPC",
|
|
7418
|
+
subnet: "subnet",
|
|
7419
|
+
"security-group": "secgroup",
|
|
7420
|
+
"security-group-rule": "rule",
|
|
7421
|
+
gateway: "gateway",
|
|
7422
|
+
"load-balancer": "ELB",
|
|
7423
|
+
compute: "compute",
|
|
7424
|
+
database: "database"
|
|
7425
|
+
};
|
|
7426
|
+
/**
|
|
7427
|
+
* The text a design node's box shows.
|
|
6468
7428
|
*
|
|
6469
|
-
*
|
|
6470
|
-
*
|
|
7429
|
+
* A rule leads with its EFFECT rather than its name: `ingress 22 ← 0.0.0.0/0`
|
|
7430
|
+
* says what it admits, which is what a reviewer checks. Every other kind leads
|
|
7431
|
+
* with its cloud name, falling back to the terraform label — the label is what
|
|
7432
|
+
* addresses and tool results use, but the cloud name is what the console shows.
|
|
6471
7433
|
*
|
|
6472
|
-
* @param node - the node
|
|
7434
|
+
* @param node - the design node.
|
|
6473
7435
|
* @returns one line of text.
|
|
6474
7436
|
*/
|
|
6475
|
-
function
|
|
7437
|
+
function designLabel(node) {
|
|
6476
7438
|
const facts = new Map(node.facts.map((fact) => [fact.key, fact.value]));
|
|
6477
7439
|
if (node.kind === "security-group-rule") {
|
|
6478
7440
|
const direction = facts.get("direction") ?? "?";
|
|
6479
7441
|
const ports = facts.get("ports") ?? "all ports";
|
|
6480
7442
|
const remote = facts.get("remote_ip_prefix");
|
|
6481
7443
|
const arrow = direction === "egress" ? "→" : "←";
|
|
6482
|
-
|
|
6483
|
-
return `${direction} ${ports}${target}`;
|
|
7444
|
+
return remote === void 0 ? `${direction} ${ports}` : `${direction} ${ports} ${arrow} ${remote}`;
|
|
6484
7445
|
}
|
|
6485
|
-
const
|
|
6486
|
-
const head = `${
|
|
7446
|
+
const caption = KIND_CAPTION[node.kind] ?? node.kind;
|
|
7447
|
+
const head = `${caption} ${facts.get("name") ?? node.name}`;
|
|
6487
7448
|
const shown = node.facts.filter((fact) => fact.key !== "name").map((fact) => fact.value);
|
|
6488
7449
|
return shown.length === 0 ? head : `${head} ${shown.join(" · ")}`;
|
|
6489
7450
|
}
|
|
6490
7451
|
/**
|
|
6491
|
-
*
|
|
7452
|
+
* Convert a design node into the layout tree's shape.
|
|
6492
7453
|
*
|
|
6493
|
-
*
|
|
6494
|
-
*
|
|
6495
|
-
|
|
6496
|
-
|
|
7454
|
+
* @param node - the design node.
|
|
7455
|
+
* @returns the layout node.
|
|
7456
|
+
*/
|
|
7457
|
+
function toLayoutNode(node) {
|
|
7458
|
+
return {
|
|
7459
|
+
id: node.address,
|
|
7460
|
+
label: designLabel(node),
|
|
7461
|
+
provider: node.type.replace(/^huaweicloud_/, "").split("_")[0] ?? "",
|
|
7462
|
+
existing: node.existing,
|
|
7463
|
+
children: node.children.map((child) => toLayoutNode(child))
|
|
7464
|
+
};
|
|
7465
|
+
}
|
|
7466
|
+
/**
|
|
7467
|
+
* Build the drawable diagram for a design.
|
|
7468
|
+
*
|
|
7469
|
+
* @param topology - the derived design.
|
|
7470
|
+
* @param edges - the relationships to draw, already filtered to what nesting
|
|
7471
|
+
* does not express.
|
|
7472
|
+
* @returns the placed boxes, the connectors, and each box's role.
|
|
7473
|
+
*/
|
|
7474
|
+
function buildDesignDiagram(topology, edges) {
|
|
7475
|
+
const layout = layoutTree(topology.roots.map((root) => toLayoutNode(root)));
|
|
7476
|
+
const roles = new Map();
|
|
7477
|
+
const note = (node) => {
|
|
7478
|
+
roles.set(node.address, node.children.length > 0 ? "container" : "resource");
|
|
7479
|
+
for (const child of node.children) note(child);
|
|
7480
|
+
};
|
|
7481
|
+
for (const root of topology.roots) note(root);
|
|
7482
|
+
const boxEdges = [];
|
|
7483
|
+
for (const edge of edges) {
|
|
7484
|
+
if (!roles.has(edge.from) || !roles.has(edge.to)) continue;
|
|
7485
|
+
boxEdges.push({
|
|
7486
|
+
from: edge.from,
|
|
7487
|
+
to: edge.to,
|
|
7488
|
+
label: edge.connection === void 0 ? "" : CONNECTION_LABEL[edge.connection] ?? edge.connection,
|
|
7489
|
+
kind: "traffic"
|
|
7490
|
+
});
|
|
7491
|
+
}
|
|
7492
|
+
return {
|
|
7493
|
+
width: layout.width,
|
|
7494
|
+
height: layout.height,
|
|
7495
|
+
boxes: layout.boxes,
|
|
7496
|
+
edges: boxEdges,
|
|
7497
|
+
roles
|
|
7498
|
+
};
|
|
7499
|
+
}
|
|
7500
|
+
/** The words on a connection's arrow: the cloud's own term, not this module's. */
|
|
7501
|
+
const CONNECTION_LABEL = {
|
|
7502
|
+
peering: "peering",
|
|
7503
|
+
endpoint: "VPCEP",
|
|
7504
|
+
route: "route"
|
|
7505
|
+
};
|
|
7506
|
+
/**
|
|
7507
|
+
* Draw the design.
|
|
6497
7508
|
*
|
|
6498
|
-
*
|
|
6499
|
-
*
|
|
6500
|
-
*
|
|
7509
|
+
* @param topology - the derived design.
|
|
7510
|
+
* @param edges - the relationships to draw.
|
|
7511
|
+
* @returns the SVG and a text record of what it shows.
|
|
7512
|
+
*/
|
|
7513
|
+
function renderDesignDiagram(topology, edges) {
|
|
7514
|
+
const diagram = buildDesignDiagram(topology, edges);
|
|
7515
|
+
return {
|
|
7516
|
+
diagram,
|
|
7517
|
+
svg: renderDiagram(diagram),
|
|
7518
|
+
source: describe(diagram)
|
|
7519
|
+
};
|
|
7520
|
+
}
|
|
7521
|
+
/**
|
|
7522
|
+
* A readable text form of the design, written beside the SVG.
|
|
6501
7523
|
*
|
|
6502
|
-
* @param
|
|
6503
|
-
* @returns the
|
|
7524
|
+
* @param diagram - the built diagram.
|
|
7525
|
+
* @returns the text.
|
|
6504
7526
|
*/
|
|
6505
|
-
function
|
|
6506
|
-
|
|
6507
|
-
const
|
|
6508
|
-
|
|
6509
|
-
|
|
6510
|
-
}
|
|
6511
|
-
|
|
6512
|
-
|
|
6513
|
-
"
|
|
6514
|
-
|
|
6515
|
-
|
|
6516
|
-
|
|
6517
|
-
].join("\n");
|
|
7527
|
+
function describe(diagram) {
|
|
7528
|
+
const lines = [];
|
|
7529
|
+
const walk = (box, depth) => {
|
|
7530
|
+
lines.push(`${" ".repeat(depth)}${box.label}`);
|
|
7531
|
+
for (const child of box.children) walk(child, depth + 1);
|
|
7532
|
+
};
|
|
7533
|
+
for (const root of diagram.boxes.filter((box) => box.depth === 0)) walk(root, 0);
|
|
7534
|
+
if (diagram.edges.length > 0) {
|
|
7535
|
+
lines.push("", "connections:");
|
|
7536
|
+
for (const edge of diagram.edges) lines.push(` ${edge.from} -> ${edge.to} (${edge.label})`);
|
|
7537
|
+
}
|
|
7538
|
+
return lines.join("\n");
|
|
6518
7539
|
}
|
|
6519
7540
|
|
|
6520
7541
|
//#endregion
|
|
@@ -6646,7 +7667,7 @@ async function handleValidateDesign(deployment, existing) {
|
|
|
6646
7667
|
let svg;
|
|
6647
7668
|
let d2;
|
|
6648
7669
|
try {
|
|
6649
|
-
const rendered =
|
|
7670
|
+
const rendered = renderDesignDiagram(topology, edges);
|
|
6650
7671
|
svg = rendered.svg;
|
|
6651
7672
|
d2 = rendered.source;
|
|
6652
7673
|
} catch (error) {
|
|
@@ -6829,18 +7850,20 @@ function parseValidateJson(stdout) {
|
|
|
6829
7850
|
async function handleInventoryOnly(deployment, existing) {
|
|
6830
7851
|
const withProperties = existing.filter((resource) => resource.properties !== void 0 && Object.keys(resource.properties).length > 0);
|
|
6831
7852
|
if (withProperties.length === 0) return fail("none of the `existing` resources carried a `properties` object, so there are no relationships to derive — the diagram would be a list of unconnected boxes. Pass each resource's `properties` from list_existing_resources verbatim; that is where the ids that connect resources live.");
|
|
6832
|
-
const topology = deriveInventoryTopology(withProperties.map((resource) => ({
|
|
6833
|
-
id: resource.id,
|
|
6834
|
-
provider: resource.provider ?? "",
|
|
6835
|
-
type: resource.type,
|
|
6836
|
-
...resource.name === void 0 ? {} : { name: resource.name },
|
|
6837
|
-
...resource.region === void 0 ? {} : { region: resource.region },
|
|
6838
|
-
properties: resource.properties ?? {}
|
|
6839
|
-
})));
|
|
6840
7853
|
let svg;
|
|
6841
7854
|
let d2;
|
|
7855
|
+
let diagram;
|
|
6842
7856
|
try {
|
|
6843
|
-
const
|
|
7857
|
+
const resources = withProperties.map((resource) => ({
|
|
7858
|
+
id: resource.id,
|
|
7859
|
+
provider: resource.provider ?? "",
|
|
7860
|
+
type: resource.type,
|
|
7861
|
+
...resource.name === void 0 ? {} : { name: resource.name },
|
|
7862
|
+
...resource.region === void 0 ? {} : { region: resource.region },
|
|
7863
|
+
properties: resource.properties ?? {}
|
|
7864
|
+
}));
|
|
7865
|
+
const rendered = renderAccountDiagram(resources);
|
|
7866
|
+
diagram = rendered.diagram;
|
|
6844
7867
|
svg = rendered.svg;
|
|
6845
7868
|
d2 = rendered.source;
|
|
6846
7869
|
} catch (error) {
|
|
@@ -6867,22 +7890,22 @@ async function handleInventoryOnly(deployment, existing) {
|
|
|
6867
7890
|
state: "skipped",
|
|
6868
7891
|
detail: "There is no configuration to validate — this picture is of resources that already exist. Write a network .tf and call this tool again to have it checked."
|
|
6869
7892
|
},
|
|
6870
|
-
resources:
|
|
6871
|
-
id:
|
|
6872
|
-
type:
|
|
6873
|
-
label:
|
|
6874
|
-
|
|
7893
|
+
resources: diagram.boxes.map((box) => ({
|
|
7894
|
+
id: box.id,
|
|
7895
|
+
type: diagram.roles.get(box.id) === "container" ? "container" : "resource",
|
|
7896
|
+
label: box.label,
|
|
7897
|
+
depth: box.depth
|
|
6875
7898
|
})),
|
|
6876
|
-
edges:
|
|
7899
|
+
edges: diagram.edges.map((edge) => ({
|
|
6877
7900
|
from: edge.from,
|
|
6878
7901
|
to: edge.to,
|
|
6879
|
-
|
|
6880
|
-
|
|
7902
|
+
kind: edge.kind,
|
|
7903
|
+
label: edge.label
|
|
6881
7904
|
})),
|
|
6882
7905
|
summary: {
|
|
6883
|
-
drawn:
|
|
6884
|
-
edges:
|
|
6885
|
-
nested:
|
|
7906
|
+
drawn: diagram.boxes.length,
|
|
7907
|
+
edges: diagram.edges.length,
|
|
7908
|
+
nested: diagram.boxes.filter((box) => box.depth > 0).length,
|
|
6886
7909
|
...provided === 0 ? {} : { without_properties: provided },
|
|
6887
7910
|
validation: "skipped"
|
|
6888
7911
|
},
|