@patronage/factory-ci 1.0.0-alpha.4 → 1.0.0-alpha.41
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 +713 -11
- package/dist/.build-fingerprint.json +3 -0
- package/dist/alchemy/index.d.ts +1174 -0
- package/dist/alchemy/index.js +2142 -0
- package/dist/bundle-alchemy-entry-CT5hCphA.js +112 -0
- package/dist/execute-alchemy-entry-B0lBN-YZ.js +160 -0
- package/dist/execute-alchemy-entry-B397GSk5.d.ts +134 -0
- package/dist/index.d.ts +1218 -93
- package/dist/index.js +2841 -333
- package/package.json +28 -10
- package/src/actions.ts +13 -0
- package/src/alchemy/child-diagnostics.ts +148 -0
- package/src/alchemy/credential-preflight.ts +428 -0
- package/src/alchemy/destroy-postcondition.ts +103 -0
- package/src/alchemy/evaluate-stack.ts +153 -0
- package/src/alchemy/first-deploy-authorization.ts +194 -0
- package/src/alchemy/index.ts +144 -0
- package/src/alchemy/lifecycle-failure.ts +174 -0
- package/src/alchemy/lifecycle.ts +1330 -0
- package/src/alchemy/local-emulation.ts +89 -0
- package/src/alchemy/plan-effects.ts +510 -0
- package/src/alchemy/retained-identity.ts +248 -0
- package/src/alchemy/secret-name-policy.ts +96 -0
- package/src/alchemy/stage-policy.ts +269 -0
- package/src/alchemy/state-tree.ts +168 -0
- package/src/alchemy/url-implies-auth.ts +264 -0
- package/src/alchemy-baseline.ts +75 -0
- package/src/bundle-alchemy-entry.ts +5 -0
- package/src/candidate-impact-workflow.ts +110 -0
- package/src/candidate-lifecycle.ts +29 -0
- package/src/cursor-oidc.ts +33 -0
- package/src/execute-alchemy-entry.ts +128 -8
- package/src/factory-workflow.ts +83 -40
- package/src/github-app-token.ts +131 -22
- package/src/github-transport.ts +186 -0
- package/src/impact-demand-condition.ts +67 -0
- package/src/index.ts +174 -0
- package/src/lifecycle-contract-lane.ts +113 -0
- package/src/merge-freeze-job.ts +253 -0
- package/src/pinned-action.ts +30 -0
- package/src/pr-status-hud-workflow.ts +96 -0
- package/src/preview-cleanup-topology.ts +115 -0
- package/src/preview-proof-inventory.ts +1092 -0
- package/src/preview-proof-lifecycle.ts +328 -0
- package/src/preview-receipt-workflow.ts +83 -0
- package/src/production-impact-workflow.ts +181 -0
- package/src/proof-reuse-gate.ts +297 -63
- package/src/proof-reuse-presentation.ts +131 -0
- package/src/push-identity-workflow.ts +463 -0
- package/src/runnable-cli.ts +27 -0
- package/src/verify-runner-policy.ts +290 -0
- package/src/vitest-profile-reader.test.ts +208 -0
- package/src/vitest-profile-reader.ts +225 -0
- package/src/vitest-profile.ts +631 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retained-identity asserts over Alchemy state snapshots (#991).
|
|
3
|
+
*
|
|
4
|
+
* A protected stage's value is the physical resources it already owns: a D1
|
|
5
|
+
* database id, an R2 bucket name, a Worker name. An Alchemy deploy that no
|
|
6
|
+
* longer recognises one of them does not fail — it creates a second one and
|
|
7
|
+
* leaves the first orphaned with the data still in it. The only cheap moment
|
|
8
|
+
* to catch that is before the deploy runs, by reading the recorded state and
|
|
9
|
+
* checking that every identity the project says it retains is still there and
|
|
10
|
+
* still the same.
|
|
11
|
+
*
|
|
12
|
+
* The project supplies the markers. This module carries no resource names.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Where in a recorded resource an identity field lives. */
|
|
16
|
+
export type RetainedIdentitySection = "attr" | "props";
|
|
17
|
+
|
|
18
|
+
/** One physical identity a project refuses to let move. */
|
|
19
|
+
export interface RetainedIdentityMarker {
|
|
20
|
+
/** The resource's fully qualified name in Alchemy state. */
|
|
21
|
+
readonly fqn: string;
|
|
22
|
+
/** The Alchemy resource type the recorded resource must report. */
|
|
23
|
+
readonly resourceType: string;
|
|
24
|
+
/** Which recorded section holds the identity field. */
|
|
25
|
+
readonly section: RetainedIdentitySection;
|
|
26
|
+
/** The identity field itself, such as `databaseId` or `bucketName`. */
|
|
27
|
+
readonly field: string;
|
|
28
|
+
/**
|
|
29
|
+
* The exact value the identity must hold. Omit only when a `before`
|
|
30
|
+
* snapshot supplies the expectation instead.
|
|
31
|
+
*/
|
|
32
|
+
readonly value?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* One resource record in the persisted-state document: the `(stack, stage,
|
|
37
|
+
* fqn)` address, and under `state` the persisted record itself.
|
|
38
|
+
*/
|
|
39
|
+
export interface StateSnapshotResource {
|
|
40
|
+
readonly stack?: unknown;
|
|
41
|
+
readonly stage?: unknown;
|
|
42
|
+
readonly fqn?: unknown;
|
|
43
|
+
readonly state?: {
|
|
44
|
+
readonly attr?: Record<string, unknown>;
|
|
45
|
+
readonly props?: Record<string, unknown>;
|
|
46
|
+
readonly fqn?: unknown;
|
|
47
|
+
readonly kind?: unknown;
|
|
48
|
+
readonly resourceType?: unknown;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Which stack and stage a snapshot is read for. */
|
|
53
|
+
export interface StateSnapshotScope {
|
|
54
|
+
/** Keep only records this stack owns. Omit when the document holds one. */
|
|
55
|
+
readonly stack?: string;
|
|
56
|
+
/** Keep only records this stage owns. Omit when the document holds one. */
|
|
57
|
+
readonly stage?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A recorded stage state: the whole persisted-state document.
|
|
62
|
+
*
|
|
63
|
+
* An evaluated stack graph is a different artifact with the same identities in
|
|
64
|
+
* it. A caller holding one projects it onto this shape at the call site;
|
|
65
|
+
* epic #985 node D owns that graph type, and this module deliberately does not
|
|
66
|
+
* import it so the two land independently.
|
|
67
|
+
*/
|
|
68
|
+
export interface StateSnapshot {
|
|
69
|
+
readonly resources: readonly StateSnapshotResource[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type RetainedIdentityViolationReason =
|
|
73
|
+
/** No recorded resource matches the marker's fqn, type, and shape. */
|
|
74
|
+
| "missing"
|
|
75
|
+
/** The recorded identity is not the value the marker names. */
|
|
76
|
+
| "unexpected"
|
|
77
|
+
/** The identity differs between the two snapshots. */
|
|
78
|
+
| "changed";
|
|
79
|
+
|
|
80
|
+
export interface RetainedIdentityViolation {
|
|
81
|
+
readonly marker: RetainedIdentityMarker;
|
|
82
|
+
readonly reason: RetainedIdentityViolationReason;
|
|
83
|
+
/** The identity the `after` snapshot records, when it records one. */
|
|
84
|
+
readonly after?: string;
|
|
85
|
+
/** The identity the `before` snapshot records, when one was given. */
|
|
86
|
+
readonly before?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Every retained identity that did not hold. */
|
|
90
|
+
export class RetainedIdentityError extends Error {
|
|
91
|
+
readonly violations: readonly RetainedIdentityViolation[];
|
|
92
|
+
|
|
93
|
+
constructor(violations: readonly RetainedIdentityViolation[]) {
|
|
94
|
+
const detail = violations
|
|
95
|
+
.map(({ marker, reason }) => `${marker.fqn}.${marker.field} (${reason})`)
|
|
96
|
+
.join(", ");
|
|
97
|
+
super(`Retained resource identities did not hold: ${detail}.`);
|
|
98
|
+
this.name = "RetainedIdentityError";
|
|
99
|
+
this.violations = violations;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Read a persisted-state document into a snapshot of one stage.
|
|
105
|
+
*
|
|
106
|
+
* The persisted-state document holds every record in scope
|
|
107
|
+
* (`{ resources: [{ stack, stage, fqn, state }] }`); a single-record document
|
|
108
|
+
* is not this shape. A document covering the whole estate holds one fqn once
|
|
109
|
+
* per stage, so a marker naming it would match whichever record sorted
|
|
110
|
+
* first — a `prod` identity asserted against a `dev` record. `scope` narrows
|
|
111
|
+
* the document to the stack and stage the markers are about.
|
|
112
|
+
*
|
|
113
|
+
* Throws on anything that is not a resource inventory, and on a scoped
|
|
114
|
+
* document that still records one fqn twice. A state this module cannot read
|
|
115
|
+
* unambiguously is never treated as a state with nothing in it, because that
|
|
116
|
+
* reading turns a broken read into a passing assert.
|
|
117
|
+
*/
|
|
118
|
+
export const parseStateSnapshot = (
|
|
119
|
+
text: string,
|
|
120
|
+
scope: StateSnapshotScope = {}
|
|
121
|
+
): StateSnapshot => {
|
|
122
|
+
let parsed: unknown;
|
|
123
|
+
try {
|
|
124
|
+
parsed = JSON.parse(text) as unknown;
|
|
125
|
+
} catch {
|
|
126
|
+
throw new TypeError("Alchemy state is not valid structured data.");
|
|
127
|
+
}
|
|
128
|
+
if (
|
|
129
|
+
typeof parsed !== "object" ||
|
|
130
|
+
parsed === null ||
|
|
131
|
+
!("resources" in parsed) ||
|
|
132
|
+
!Array.isArray(parsed.resources)
|
|
133
|
+
) {
|
|
134
|
+
throw new TypeError("Alchemy state does not contain a resource inventory.");
|
|
135
|
+
}
|
|
136
|
+
const recorded = (parsed.resources as readonly StateSnapshotResource[])
|
|
137
|
+
.filter(({ stack }) => scope.stack === undefined || stack === scope.stack)
|
|
138
|
+
.filter(({ stage }) => scope.stage === undefined || stage === scope.stage);
|
|
139
|
+
const repeated = recorded
|
|
140
|
+
.map(({ fqn }) => String(fqn))
|
|
141
|
+
.filter((fqn, index, all) => all.indexOf(fqn) !== index);
|
|
142
|
+
if (repeated.length > 0) {
|
|
143
|
+
throw new TypeError(
|
|
144
|
+
`Alchemy state records ${[...new Set(repeated)].toSorted().join(", ")} more than once: narrow the export to one stack and stage.`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return { resources: recorded };
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The identity a snapshot records for a marker, or `undefined` when the
|
|
152
|
+
* snapshot records no resource that matches the marker.
|
|
153
|
+
*/
|
|
154
|
+
const recordedIdentity = (
|
|
155
|
+
snapshot: StateSnapshot,
|
|
156
|
+
marker: RetainedIdentityMarker
|
|
157
|
+
): string | undefined => {
|
|
158
|
+
const recorded = snapshot.resources.find(({ fqn }) => fqn === marker.fqn);
|
|
159
|
+
const state = recorded?.state;
|
|
160
|
+
if (
|
|
161
|
+
state === undefined ||
|
|
162
|
+
state.fqn !== marker.fqn ||
|
|
163
|
+
(state.kind !== undefined && state.kind !== "resource") ||
|
|
164
|
+
state.resourceType !== marker.resourceType
|
|
165
|
+
) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
const value = state[marker.section]?.[marker.field];
|
|
169
|
+
return typeof value === "string" && value !== "" ? value : undefined;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const violationFor = (
|
|
173
|
+
marker: RetainedIdentityMarker,
|
|
174
|
+
after: StateSnapshot,
|
|
175
|
+
before: StateSnapshot | undefined
|
|
176
|
+
): RetainedIdentityViolation | undefined => {
|
|
177
|
+
const recordedAfter = recordedIdentity(after, marker);
|
|
178
|
+
if (recordedAfter === undefined) {
|
|
179
|
+
return { marker, reason: "missing" };
|
|
180
|
+
}
|
|
181
|
+
if (marker.value !== undefined && marker.value !== recordedAfter) {
|
|
182
|
+
return { after: recordedAfter, marker, reason: "unexpected" };
|
|
183
|
+
}
|
|
184
|
+
if (before === undefined) {
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
const recordedBefore = recordedIdentity(before, marker);
|
|
188
|
+
// A marker absent from `before` and present in `after` is a first record of
|
|
189
|
+
// that identity, not a moved one. An identity that was recorded and is now
|
|
190
|
+
// gone is already reported as missing above.
|
|
191
|
+
if (recordedBefore !== undefined && recordedBefore !== recordedAfter) {
|
|
192
|
+
return {
|
|
193
|
+
after: recordedAfter,
|
|
194
|
+
before: recordedBefore,
|
|
195
|
+
marker,
|
|
196
|
+
reason: "changed",
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
return undefined;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export interface AssertRetainedIdentitiesOptions {
|
|
203
|
+
/**
|
|
204
|
+
* The state the operation would run against. Every marker is checked
|
|
205
|
+
* against this snapshot.
|
|
206
|
+
*/
|
|
207
|
+
readonly after: StateSnapshot;
|
|
208
|
+
/**
|
|
209
|
+
* The state recorded before. Supply it to catch an identity that moved
|
|
210
|
+
* between the two. Omit only when every marker names its own `value`.
|
|
211
|
+
*/
|
|
212
|
+
readonly before?: StateSnapshot;
|
|
213
|
+
/** The identities the project retains. Must not be empty. */
|
|
214
|
+
readonly markers: readonly RetainedIdentityMarker[];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Throw `RetainedIdentityError` unless every retained identity still holds.
|
|
219
|
+
*
|
|
220
|
+
* A marker that names no `value` and has no `before` snapshot to compare
|
|
221
|
+
* against asserts nothing, so it is a `TypeError` rather than a pass. So is an
|
|
222
|
+
* empty marker list: a check with nothing in it must not read as a proof.
|
|
223
|
+
*/
|
|
224
|
+
export const assertRetainedIdentities = ({
|
|
225
|
+
after,
|
|
226
|
+
before,
|
|
227
|
+
markers,
|
|
228
|
+
}: AssertRetainedIdentitiesOptions): void => {
|
|
229
|
+
if (markers.length === 0) {
|
|
230
|
+
throw new TypeError(
|
|
231
|
+
"assertRetainedIdentities needs at least one retained identity marker."
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
for (const marker of markers) {
|
|
235
|
+
if (marker.value === undefined && before === undefined) {
|
|
236
|
+
throw new TypeError(
|
|
237
|
+
`Retained identity ${marker.fqn}.${marker.field} asserts nothing: give it a value or pass a before snapshot.`
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const violations = markers
|
|
243
|
+
.map((marker) => violationFor(marker, after, before))
|
|
244
|
+
.filter((violation) => violation !== undefined);
|
|
245
|
+
if (violations.length > 0) {
|
|
246
|
+
throw new RetainedIdentityError(violations);
|
|
247
|
+
}
|
|
248
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A secret-name redaction policy for `executeAlchemyEntry`'s `redact` hook.
|
|
3
|
+
*
|
|
4
|
+
* `executeAlchemyEntry` already applies a caller-supplied `redact` to captured
|
|
5
|
+
* stdout and stderr and states that this package supplies no policy. This
|
|
6
|
+
* builds one from names the project passes, and nothing more: there is no new
|
|
7
|
+
* mechanism and no secret-name list of our own.
|
|
8
|
+
*
|
|
9
|
+
* The policy is fail-closed at construction. A named secret that is absent, or
|
|
10
|
+
* too short to replace safely, throws while the policy is being built —
|
|
11
|
+
* before any output exists to redact. The alternative is a policy that quietly
|
|
12
|
+
* covers less than the caller believes it covers.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** What a redacted value is replaced with. */
|
|
16
|
+
export const SECRET_REDACTION_PLACEHOLDER = "[REDACTED]";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The shortest value that may be replaced. A short value occurs in ordinary
|
|
20
|
+
* output by chance, so replacing it would erase unrelated text and hide what
|
|
21
|
+
* the run actually did.
|
|
22
|
+
*/
|
|
23
|
+
export const MINIMUM_REDACTABLE_SECRET_LENGTH = 8;
|
|
24
|
+
|
|
25
|
+
export interface SecretNamePolicyOptions {
|
|
26
|
+
/**
|
|
27
|
+
* The names of the values to redact. The project owns this list; this
|
|
28
|
+
* package neither ships one nor infers one from a name pattern.
|
|
29
|
+
*/
|
|
30
|
+
readonly names: readonly string[];
|
|
31
|
+
/** Where those names are read from, normally the child's environment. */
|
|
32
|
+
readonly values: Readonly<Record<string, string | undefined>>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Builds the `redact` function `executeAlchemyEntry` takes.
|
|
37
|
+
*
|
|
38
|
+
* Every named value is replaced with {@link SECRET_REDACTION_PLACEHOLDER}.
|
|
39
|
+
* The value is trimmed first: an environment value routinely carries a
|
|
40
|
+
* trailing newline that the secret itself does not, and replacing the raw
|
|
41
|
+
* form would swallow the line break that follows the secret in the output.
|
|
42
|
+
* Longer values are replaced first, so a secret that contains another secret
|
|
43
|
+
* leaves no fragment behind.
|
|
44
|
+
*
|
|
45
|
+
* The policy replaces verbatim substrings of each named value only. A value
|
|
46
|
+
* that the output re-encodes passes through unredacted. URL-encoded,
|
|
47
|
+
* base64, and JSON-escaped forms are all such re-encodings. A project that
|
|
48
|
+
* prints a secret in one of those forms must not rely on this policy for it.
|
|
49
|
+
* See issue #1011.
|
|
50
|
+
*
|
|
51
|
+
* An empty `names` list yields a pass-through function only when `values` is
|
|
52
|
+
* also empty: a project with no secrets in its Alchemy output declares none,
|
|
53
|
+
* and gets no redaction. Empty names beside a populated `values` map is
|
|
54
|
+
* refused, because that is a caller who meant to declare something.
|
|
55
|
+
*/
|
|
56
|
+
export const secretNamePolicy = ({
|
|
57
|
+
names,
|
|
58
|
+
values,
|
|
59
|
+
}: SecretNamePolicyOptions): ((text: string) => string) => {
|
|
60
|
+
if (names.length === 0 && Object.keys(values).length > 0) {
|
|
61
|
+
// Handing the values in and declaring none of them is the shape a caller
|
|
62
|
+
// reaches by accident. A project that truly has no secrets passes no
|
|
63
|
+
// values either, and gets the pass-through.
|
|
64
|
+
throw new Error(
|
|
65
|
+
"secretNamePolicy needs at least one name when values are present"
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
const declared = [...new Set(names)].toSorted();
|
|
69
|
+
const absent = declared.filter((name) => !values[name]?.trim());
|
|
70
|
+
if (absent.length > 0) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
`Secret-name policy declares values that are absent: ${absent.join(", ")}`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
const short = declared.filter(
|
|
76
|
+
(name) =>
|
|
77
|
+
(values[name] ?? "").trim().length < MINIMUM_REDACTABLE_SECRET_LENGTH
|
|
78
|
+
);
|
|
79
|
+
if (short.length > 0) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
`Secret-name policy declares values shorter than ${MINIMUM_REDACTABLE_SECRET_LENGTH} characters, which cannot be redacted safely: ${short.join(", ")}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const secrets = [
|
|
86
|
+
...new Set(declared.map((name) => (values[name] ?? "").trim())),
|
|
87
|
+
].toSorted((left, right) => right.length - left.length);
|
|
88
|
+
|
|
89
|
+
return (text: string) => {
|
|
90
|
+
let redacted = text;
|
|
91
|
+
for (const secret of secrets) {
|
|
92
|
+
redacted = redacted.replaceAll(secret, SECRET_REDACTION_PLACEHOLDER);
|
|
93
|
+
}
|
|
94
|
+
return redacted;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stage and stack pairing, and protected-stage policy, from a project-supplied
|
|
3
|
+
* table (#991).
|
|
4
|
+
*
|
|
5
|
+
* Two projects wrote the same three questions twice — is this stage protected,
|
|
6
|
+
* is it disposable, and may this stack run it — and each answered them with its
|
|
7
|
+
* own stage names hard-coded into the answer. The names differ; the questions
|
|
8
|
+
* do not. This module owns the questions. A project supplies the table and gets
|
|
9
|
+
* decisions back, so no project or stack name appears here.
|
|
10
|
+
*
|
|
11
|
+
* One stage name does appear: `local`. It names the local emulator rather than
|
|
12
|
+
* any project's estate, every project spells it the same way, and the question
|
|
13
|
+
* it answers — is this the emulated stage — has one answer for the whole fleet.
|
|
14
|
+
* See {@link isLocalEmulationStage}.
|
|
15
|
+
*
|
|
16
|
+
* The disposable grammar this package already owns (`disposable-stage.ts`) is
|
|
17
|
+
* available as a matcher rather than assumed, because a project's disposable
|
|
18
|
+
* set is wider than that one grammar.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { isLocalPreviewStage } from "../disposable-stage.ts";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* How a table names a set of stages: an exact list, a pattern, or a predicate
|
|
25
|
+
* for a set the project resolves itself (a stage backed by a config file, say).
|
|
26
|
+
*/
|
|
27
|
+
export type StageMatcher =
|
|
28
|
+
| readonly string[]
|
|
29
|
+
| RegExp
|
|
30
|
+
| ((stage: string) => boolean);
|
|
31
|
+
|
|
32
|
+
/** Every stage the shared disposable grammar recognises. */
|
|
33
|
+
export const LOCAL_PREVIEW_STAGES: StageMatcher = (stage: string) =>
|
|
34
|
+
isLocalPreviewStage(stage);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Test a pattern without carrying state between calls.
|
|
38
|
+
*
|
|
39
|
+
* A `RegExp` with `g` or `y` advances `lastIndex` on every match, so the same
|
|
40
|
+
* pattern answers "yes" and then "no" for the same stage. A policy that
|
|
41
|
+
* alternates is worse than one that always refuses, so a flagged pattern is
|
|
42
|
+
* tested through a fresh copy whose `lastIndex` starts at zero. The copy
|
|
43
|
+
* carries the complete flags: `y` anchors the match at `lastIndex`, so
|
|
44
|
+
* dropping it would let `/prod/y` accept a stage named `pre-prod` that the
|
|
45
|
+
* project's own pattern rejects. The caller's own `RegExp` is never written
|
|
46
|
+
* to, so a frozen one still works.
|
|
47
|
+
*/
|
|
48
|
+
const statelessTest = (pattern: RegExp, stage: string): boolean =>
|
|
49
|
+
pattern.global || pattern.sticky
|
|
50
|
+
? new RegExp(pattern.source, pattern.flags).test(stage)
|
|
51
|
+
: pattern.test(stage);
|
|
52
|
+
|
|
53
|
+
const stageMatches = (matcher: StageMatcher, stage: string): boolean => {
|
|
54
|
+
if (typeof matcher === "function") {
|
|
55
|
+
return matcher(stage);
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(matcher)) {
|
|
58
|
+
return matcher.includes(stage);
|
|
59
|
+
}
|
|
60
|
+
return statelessTest(matcher as RegExp, stage);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** A matcher that accepts a stage any of `matchers` accepts. */
|
|
64
|
+
export const anyStage =
|
|
65
|
+
(...matchers: readonly StageMatcher[]): StageMatcher =>
|
|
66
|
+
(stage: string) =>
|
|
67
|
+
matchers.some((matcher) => stageMatches(matcher, stage));
|
|
68
|
+
|
|
69
|
+
/** One stack's claim on stages, in either or both directions. */
|
|
70
|
+
export interface StackStageOwnership {
|
|
71
|
+
/** The stack this entry describes. */
|
|
72
|
+
readonly stack: string;
|
|
73
|
+
/**
|
|
74
|
+
* The only stages this stack may run. Omit when the stack runs any known
|
|
75
|
+
* stage no other stack claims.
|
|
76
|
+
*/
|
|
77
|
+
readonly stages?: StageMatcher;
|
|
78
|
+
/**
|
|
79
|
+
* Stages no other stack may run. Omit when the stack claims none of its
|
|
80
|
+
* stages exclusively. Declaring the same matcher as `stages` and `claims`
|
|
81
|
+
* pairs a stack and its stages in both directions.
|
|
82
|
+
*/
|
|
83
|
+
readonly claims?: StageMatcher;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface StagePolicyTable {
|
|
87
|
+
readonly stages: {
|
|
88
|
+
/** Stages that hold real state and refuse destructive operations. */
|
|
89
|
+
readonly protected: StageMatcher;
|
|
90
|
+
/** Stages a cleanup sweep may create and delete freely. */
|
|
91
|
+
readonly disposable: StageMatcher;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* The project's whole stack registry. When present, a stack outside it is
|
|
95
|
+
* refused, so a typo cannot pass as an unconstrained stack. Omit to leave
|
|
96
|
+
* stack ownership unpoliced.
|
|
97
|
+
*/
|
|
98
|
+
readonly stacks?: readonly StackStageOwnership[];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type StagePolicyErrorCode =
|
|
102
|
+
/** The stage is neither protected nor disposable. */
|
|
103
|
+
| "UNKNOWN_STAGE"
|
|
104
|
+
/** The table classifies the stage as both protected and disposable. */
|
|
105
|
+
| "AMBIGUOUS_STAGE"
|
|
106
|
+
/** A destructive operation was asked for on a protected stage. */
|
|
107
|
+
| "PROTECTED_STAGE"
|
|
108
|
+
/** The stack is not in the table's registry. */
|
|
109
|
+
| "UNKNOWN_STACK"
|
|
110
|
+
/** The stack and the stage are not paired by the table. */
|
|
111
|
+
| "STAGE_NOT_OWNED";
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A policy refusal. The `code` is the stable part: a project maps it onto its
|
|
115
|
+
* own lifecycle failure envelope, and the message is for a human.
|
|
116
|
+
*/
|
|
117
|
+
export class StagePolicyError extends Error {
|
|
118
|
+
readonly code: StagePolicyErrorCode;
|
|
119
|
+
|
|
120
|
+
constructor(code: StagePolicyErrorCode, message: string) {
|
|
121
|
+
super(message);
|
|
122
|
+
this.name = "StagePolicyError";
|
|
123
|
+
this.code = code;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface StagePolicy {
|
|
128
|
+
/** Whether the stage holds real state. */
|
|
129
|
+
readonly isProtectedStage: (stage: string) => boolean;
|
|
130
|
+
/** Whether a cleanup sweep may delete the stage. */
|
|
131
|
+
readonly isDisposableStage: (stage: string) => boolean;
|
|
132
|
+
/** Whether the table classifies the stage at all. */
|
|
133
|
+
readonly isKnownStage: (stage: string) => boolean;
|
|
134
|
+
/** Throw unless the table classifies the stage. */
|
|
135
|
+
readonly assertKnownStage: (stage: string) => void;
|
|
136
|
+
/** Throw unless the stage is known and disposable. */
|
|
137
|
+
readonly assertDestructiveStage: (stage: string) => void;
|
|
138
|
+
/** Throw unless the table pairs the stack and the stage. */
|
|
139
|
+
readonly assertStackOwnsStage: (stack: string, stage: string) => void;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const assertTable = (table: StagePolicyTable): void => {
|
|
143
|
+
const seen = new Set<string>();
|
|
144
|
+
for (const entry of table.stacks ?? []) {
|
|
145
|
+
if (!entry.stack) {
|
|
146
|
+
throw new TypeError("Every stack ownership entry needs a stack name.");
|
|
147
|
+
}
|
|
148
|
+
if (seen.has(entry.stack)) {
|
|
149
|
+
throw new TypeError(
|
|
150
|
+
`Stack "${entry.stack}" appears twice in the stack registry.`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
seen.add(entry.stack);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Is this the stage that runs against the local emulator?
|
|
159
|
+
*
|
|
160
|
+
* True for the literal `local` and nothing else (#991). Two projects and HQ
|
|
161
|
+
* each answered this with their own rule — a refusal list, a constant, and
|
|
162
|
+
* `$(whoami)` — and all three meant the same single name, so the answer is one
|
|
163
|
+
* literal rather than a pattern or a table. A disposable preview stage such as
|
|
164
|
+
* `local-pr-1-abcdef0` shares the prefix but runs against a real account, so it
|
|
165
|
+
* is false here. The comparison is exact: no trim and no case folding, because
|
|
166
|
+
* a stage name reaches Alchemy exactly as it is spelled.
|
|
167
|
+
*
|
|
168
|
+
* This question is deliberately outside {@link stagePolicy}: a project's table
|
|
169
|
+
* classifies the stages of its own estate, and the emulated stage belongs to
|
|
170
|
+
* no estate.
|
|
171
|
+
*/
|
|
172
|
+
export const isLocalEmulationStage = (stage: string): boolean =>
|
|
173
|
+
stage === "local";
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Read a project's stage table and answer the stage questions from it.
|
|
177
|
+
*
|
|
178
|
+
* A stage the table classifies as both protected and disposable is a table
|
|
179
|
+
* defect, not a stage a caller may act on, so every query on it throws.
|
|
180
|
+
*/
|
|
181
|
+
export const stagePolicy = (table: StagePolicyTable): StagePolicy => {
|
|
182
|
+
assertTable(table);
|
|
183
|
+
|
|
184
|
+
const classify = (stage: string) => {
|
|
185
|
+
const isProtected = stageMatches(table.stages.protected, stage);
|
|
186
|
+
const isDisposable = stageMatches(table.stages.disposable, stage);
|
|
187
|
+
if (isProtected && isDisposable) {
|
|
188
|
+
throw new StagePolicyError(
|
|
189
|
+
"AMBIGUOUS_STAGE",
|
|
190
|
+
`Stage "${stage}" is declared both protected and disposable.`
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
return { isDisposable, isProtected };
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const isProtectedStage = (stage: string) => classify(stage).isProtected;
|
|
197
|
+
const isDisposableStage = (stage: string) => classify(stage).isDisposable;
|
|
198
|
+
const isKnownStage = (stage: string) => {
|
|
199
|
+
const { isDisposable, isProtected } = classify(stage);
|
|
200
|
+
return isProtected || isDisposable;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const assertKnownStage = (stage: string) => {
|
|
204
|
+
if (!isKnownStage(stage)) {
|
|
205
|
+
throw new StagePolicyError(
|
|
206
|
+
"UNKNOWN_STAGE",
|
|
207
|
+
`Unsupported Alchemy stage: ${stage}`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const assertDestructiveStage = (stage: string) => {
|
|
213
|
+
assertKnownStage(stage);
|
|
214
|
+
if (isProtectedStage(stage)) {
|
|
215
|
+
throw new StagePolicyError(
|
|
216
|
+
"PROTECTED_STAGE",
|
|
217
|
+
`Refusing destructive operation for protected stage: ${stage}`
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const assertStackOwnsStage = (stack: string, stage: string) => {
|
|
223
|
+
// Ownership is a question about a stage the table classifies. Asked about
|
|
224
|
+
// a stage it does not, an unconstrained stack answers "yours" and a stage
|
|
225
|
+
// typo passes the pairing check it was meant to fail.
|
|
226
|
+
assertKnownStage(stage);
|
|
227
|
+
const registry = table.stacks;
|
|
228
|
+
if (registry === undefined) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const entry = registry.find((candidate) => candidate.stack === stack);
|
|
232
|
+
if (entry === undefined) {
|
|
233
|
+
throw new StagePolicyError(
|
|
234
|
+
"UNKNOWN_STACK",
|
|
235
|
+
`Unsupported Alchemy stack: ${stack}`
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
// Another stack's claim is reported before this stack's own stage list,
|
|
239
|
+
// because it names the stack that does own the stage and so tells the
|
|
240
|
+
// caller more than "not yours" does.
|
|
241
|
+
const owner = registry.find(
|
|
242
|
+
(candidate) =>
|
|
243
|
+
candidate.stack !== stack &&
|
|
244
|
+
candidate.claims !== undefined &&
|
|
245
|
+
stageMatches(candidate.claims, stage)
|
|
246
|
+
);
|
|
247
|
+
if (owner !== undefined) {
|
|
248
|
+
throw new StagePolicyError(
|
|
249
|
+
"STAGE_NOT_OWNED",
|
|
250
|
+
`Stage ${stage} belongs only to stack ${owner.stack}, not ${stack}.`
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
if (entry.stages !== undefined && !stageMatches(entry.stages, stage)) {
|
|
254
|
+
throw new StagePolicyError(
|
|
255
|
+
"STAGE_NOT_OWNED",
|
|
256
|
+
`Stack ${stack} does not run stage ${stage}.`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
assertDestructiveStage,
|
|
263
|
+
assertKnownStage,
|
|
264
|
+
assertStackOwnsStage,
|
|
265
|
+
isDisposableStage,
|
|
266
|
+
isKnownStage,
|
|
267
|
+
isProtectedStage,
|
|
268
|
+
};
|
|
269
|
+
};
|