@smoothbricks/nx-plugin 0.4.15 → 0.4.16
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 +17 -0
- package/dist/deploy-policy.d.ts +26 -0
- package/dist/deploy-policy.d.ts.map +1 -0
- package/dist/deploy-policy.js +36 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +153 -14
- package/dist/managed-assets.d.ts +3 -0
- package/dist/managed-assets.d.ts.map +1 -0
- package/dist/managed-assets.js +3 -0
- package/dist/managed-files/ci-workflow.d.ts +215 -0
- package/dist/managed-files/ci-workflow.d.ts.map +1 -0
- package/dist/managed-files/ci-workflow.js +1082 -0
- package/dist/managed-files/context.d.ts +51 -0
- package/dist/managed-files/context.d.ts.map +1 -0
- package/dist/managed-files/context.js +206 -0
- package/dist/managed-files/files.d.ts +53 -0
- package/dist/managed-files/files.d.ts.map +1 -0
- package/dist/managed-files/files.js +248 -0
- package/dist/managed-files/generator.d.ts +16 -0
- package/dist/managed-files/generator.d.ts.map +1 -0
- package/dist/managed-files/generator.js +41 -0
- package/dist/managed-files/github-runs-on.d.ts +19 -0
- package/dist/managed-files/github-runs-on.d.ts.map +1 -0
- package/dist/managed-files/github-runs-on.js +48 -0
- package/dist/managed-files/managed-content.d.ts +45 -0
- package/dist/managed-files/managed-content.d.ts.map +1 -0
- package/dist/managed-files/managed-content.js +102 -0
- package/dist/managed-files/managed-format.d.ts +37 -0
- package/dist/managed-files/managed-format.d.ts.map +1 -0
- package/dist/managed-files/managed-format.js +94 -0
- package/dist/managed-files/paths.d.ts +8 -0
- package/dist/managed-files/paths.d.ts.map +1 -0
- package/dist/managed-files/paths.js +46 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +5 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.js +38 -0
- package/dist/managed-files/private-npm-policy.d.ts +7 -0
- package/dist/managed-files/private-npm-policy.d.ts.map +1 -0
- package/dist/managed-files/private-npm-policy.js +51 -0
- package/dist/managed-files/publish-workflow.d.ts +150 -0
- package/dist/managed-files/publish-workflow.d.ts.map +1 -0
- package/dist/managed-files/publish-workflow.js +989 -0
- package/dist/managed-files/tree.d.ts +30 -0
- package/dist/managed-files/tree.d.ts.map +1 -0
- package/dist/managed-files/tree.js +91 -0
- package/dist/platform-targets.d.ts +5 -0
- package/dist/platform-targets.d.ts.map +1 -0
- package/dist/platform-targets.js +4 -0
- package/dist/typecheck-test-policy.d.ts +5 -9
- package/dist/typecheck-test-policy.d.ts.map +1 -1
- package/dist/typecheck-test-policy.js +69 -99
- package/dist/workspace-config-policy.d.ts +2 -3
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +8 -3
- package/dist/workspace-manifest.d.ts +291 -0
- package/dist/workspace-manifest.d.ts.map +1 -0
- package/dist/workspace-manifest.js +6 -0
- package/dist/workspace-package-policy.d.ts +13 -0
- package/dist/workspace-package-policy.d.ts.map +1 -0
- package/dist/workspace-package-policy.js +24 -0
- package/generators.json +5 -0
- package/managed/raw/envrc +20 -0
- package/managed/raw/git-format-staged.yml +82 -0
- package/managed/raw/gitattributes +8 -0
- package/managed/raw/tooling/devenv +83 -0
- package/managed/raw/tooling/direnv/devenv.smoo.nix +422 -0
- package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +251 -0
- package/managed/raw/tooling/direnv/merge-newer-pins.sh +48 -0
- package/managed/raw/tooling/direnv/repo-path +34 -0
- package/managed/raw/tooling/direnv/secret-references.ts +636 -0
- package/managed/raw/tooling/direnv/setup-environment.ts +585 -0
- package/managed/raw/tooling/direnv/toolchain-stamp.ts +88 -0
- package/managed/raw/tooling/git-hooks/commit-msg.sh +9 -0
- package/managed/raw/tooling/git-hooks/post-commit.sh +71 -0
- package/managed/raw/tooling/git-hooks/pre-commit.sh +18 -0
- package/managed/raw/tooling/git-hooks/pre-push.sh +48 -0
- package/managed/raw/tsconfig.lib.json +28 -0
- package/managed/templates/github/actions/cache-nix-devenv/action.yml +45 -0
- package/managed/templates/github/actions/cache-node-modules/action.yml +17 -0
- package/managed/templates/github/actions/cache-nx/action.yml +21 -0
- package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +19 -0
- package/managed/templates/github/actions/save-nix-devenv/action.yml +32 -0
- package/managed/templates/github/actions/setup-devenv/action.yml +241 -0
- package/managed/templates/github/workflows/managed-files.yml +127 -0
- package/package.json +89 -5
- package/src/bun-bootstrap.test.ts +0 -6
- package/src/deploy-policy.ts +40 -0
- package/src/index.test.ts +225 -2
- package/src/index.ts +180 -14
- package/src/managed-assets.ts +4 -0
- package/src/managed-files/ci-workflow.ts +1365 -0
- package/src/managed-files/context.ts +240 -0
- package/src/managed-files/files.ts +324 -0
- package/src/managed-files/generator.test.ts +151 -0
- package/src/managed-files/generator.ts +60 -0
- package/src/managed-files/github-runs-on.ts +51 -0
- package/src/managed-files/managed-content.test.ts +147 -0
- package/src/managed-files/managed-content.ts +121 -0
- package/src/managed-files/managed-format.ts +102 -0
- package/src/managed-files/paths.ts +44 -0
- package/src/managed-files/pr-preview-cleanup-workflow.ts +44 -0
- package/src/managed-files/private-npm-policy.ts +62 -0
- package/src/managed-files/publish-workflow.ts +1523 -0
- package/src/managed-files/schema.json +8 -0
- package/src/managed-files/tree-permissions.test.ts +109 -0
- package/src/managed-files/tree.test.ts +123 -0
- package/src/managed-files/tree.ts +118 -0
- package/src/platform-targets.ts +4 -0
- package/src/typecheck-test-policy.test.ts +28 -3
- package/src/typecheck-test-policy.ts +46 -121
- package/src/workspace-config-policy.test.ts +14 -2
- package/src/workspace-config-policy.ts +9 -3
- package/src/workspace-manifest.ts +327 -0
- package/src/workspace-package-policy.ts +32 -0
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider-neutral local secret resolution for smoo-managed repositories.
|
|
3
|
+
*
|
|
4
|
+
* Reads the root package.json `smoo.secrets` map and `smoo.remoteCache` block
|
|
5
|
+
* — the bootstrap twins of `PackageSecretCommand`, `PackageSmooConfig.secrets`
|
|
6
|
+
* and `PackageRemoteCacheConfig` in packages/cli/src/lib/json.ts. This file is
|
|
7
|
+
* a managed raw script: it runs from `tooling/direnv/setup-environment.ts`
|
|
8
|
+
* BEFORE `bun install`, when no workspace package and no Typia transform exist
|
|
9
|
+
* yet, so both shapes are hand-validated here against exactly what json.ts
|
|
10
|
+
* declares. Keep them aligned; smoo's Typia validation fails the manifest at
|
|
11
|
+
* generation time for anything this file would reject at runtime.
|
|
12
|
+
*
|
|
13
|
+
* THE RULE: shell entry resolves the `shell` group and nothing else. Every
|
|
14
|
+
* other group belongs to the command that needs it:
|
|
15
|
+
* `smoo secrets run <group> <command...>`.
|
|
16
|
+
*
|
|
17
|
+
* Shell entry happens on every direnv reload and every `devenv shell --
|
|
18
|
+
* <command>`, and a provider authorises per requesting process lineage, which
|
|
19
|
+
* is new each time — so a provider command placed there is a credential
|
|
20
|
+
* prompt on every one of them: dozens an hour for a developer, one more for
|
|
21
|
+
* every scripted subprocess. A credential whose only consumer is a deliberate
|
|
22
|
+
* command therefore belongs to that command.
|
|
23
|
+
*
|
|
24
|
+
* A group is NOT a fourth thing to declare. It is derived from the
|
|
25
|
+
* declarations a repository already carries, and an explicit `group` on the
|
|
26
|
+
* entry is only for what those cannot say. Derivation, per variable:
|
|
27
|
+
*
|
|
28
|
+
* - `nx-cache` — the variable `smoo.remoteCache.tokenSecret` names. A remote
|
|
29
|
+
* cache is an optimization: Nx reads NX_SELF_HOSTED_REMOTE_CACHE_SERVER
|
|
30
|
+
* and _ACCESS_TOKEN from the environment it runs in — CI injects them, a
|
|
31
|
+
* developer exports the token once in the terminal that wants the cache —
|
|
32
|
+
* and every nested shell inherits them. Absent, Nx keeps to its local
|
|
33
|
+
* cache.
|
|
34
|
+
* - `registry` — a variable `.npmrc` interpolates as `${VAR}`. An installed
|
|
35
|
+
* checkout contacts no registry at all (measured: `bun install --dry-run`
|
|
36
|
+
* succeeds in 14ms with the token absent), while a request that does
|
|
37
|
+
* contact one answers 401 without it.
|
|
38
|
+
* - `shell` — everything else: what the shell itself is for, resolved at
|
|
39
|
+
* entry because that is when it is needed.
|
|
40
|
+
*
|
|
41
|
+
* An explicit `group` wins over the derivation, and no group is privileged in
|
|
42
|
+
* code: a repository may name one of its own and run it the same way. Only
|
|
43
|
+
* the shape is validated, so a new group needs no new smoo release.
|
|
44
|
+
*
|
|
45
|
+
* Routing per declared variable, in first-match order:
|
|
46
|
+
*
|
|
47
|
+
* 1. An existing nonempty environment value wins; no command runs.
|
|
48
|
+
* 2. For the requested group, and for a group the request depends on (see
|
|
49
|
+
* below), a context that cannot run a provider command at all decides
|
|
50
|
+
* before the group does:
|
|
51
|
+
* a. CI (`CI` set): variables must be injected from the CI secret store;
|
|
52
|
+
* provider commands never run there. Missing variables refuse with
|
|
53
|
+
* injected-secret guidance instead of a confusing auth failure later.
|
|
54
|
+
* b. A `registry` variable inside a cowshed workspace
|
|
55
|
+
* (`COWSHED_WORKSPACE_TOKEN` set) refuses with names-only
|
|
56
|
+
* gateway-enrollment guidance: such a workspace holds no provider
|
|
57
|
+
* session, and the gateway exists to be the thing that does.
|
|
58
|
+
* 3. A variable outside the requested group is deferred — named in the
|
|
59
|
+
* result, not resolved, and not a failure.
|
|
60
|
+
* 4. Otherwise the command argv executes directly, without a shell; stdout
|
|
61
|
+
* with one terminal newline trimmed becomes the value.
|
|
62
|
+
*
|
|
63
|
+
* A request DEPENDS ON a group when the operation being resolved for reads
|
|
64
|
+
* that group's own declaration. Shell entry runs `bun install`, and
|
|
65
|
+
* `bun install` reads `.npmrc` — so `registry` is an input to shell entry
|
|
66
|
+
* that shell entry deliberately does not resolve. That dependency is what
|
|
67
|
+
* keeps rule 2 honest in a context which cannot honour a deferral: CI has no
|
|
68
|
+
* later command to run and a cowshed workspace has no provider session, so
|
|
69
|
+
* both refuse by name rather than promising that `smoo secrets run` would
|
|
70
|
+
* fix it. Nothing shell entry runs reads `smoo.remoteCache`, so an
|
|
71
|
+
* `nx-cache` variable is deferred in every context and blocks nothing.
|
|
72
|
+
*
|
|
73
|
+
* A deferred variable is a value in the result rather than a silent omission,
|
|
74
|
+
* and there is no fallback that resolves it anyway: a fallback would
|
|
75
|
+
* reinstate the prompt this rule removes, at the least predictable moment.
|
|
76
|
+
* The caller names the variable and the command that supplies it when the
|
|
77
|
+
* operation that needed it fails.
|
|
78
|
+
*
|
|
79
|
+
* Failures aggregate so a single direnv reload surfaces every problem.
|
|
80
|
+
* Error text names variables, groups and exit codes only: secret values,
|
|
81
|
+
* provider stdout/stderr, and command arguments are never echoed.
|
|
82
|
+
* A resolved value still reaches the environment of the install its caller
|
|
83
|
+
* runs next, so `maskSecretValues` redacts those values out of any captured
|
|
84
|
+
* child output that caller replays.
|
|
85
|
+
*
|
|
86
|
+
* Zero prompts, as an option and not a recommendation: a declared `command`
|
|
87
|
+
* may point at the OS keychain instead of at the provider. On macOS, populate
|
|
88
|
+
* the item once from the 1Password item (`security add-generic-password -s
|
|
89
|
+
* <name> -a <account> -w`) and declare
|
|
90
|
+
* `["security", "find-generic-password", "-w", "-s", "<name>"]`. macOS
|
|
91
|
+
* authorises a keychain item per calling binary, so the first approval covers
|
|
92
|
+
* every later read by that binary: rule 4 then answers without prompting and
|
|
93
|
+
* the deferral above never has to be paid. 1Password stays the source of
|
|
94
|
+
* truth and the keychain is a session cache of it — but the trade-off is
|
|
95
|
+
* exactly that, a second copy of the credential now lives on disk, outside
|
|
96
|
+
* the provider's rotation and revocation, and nothing here refreshes it.
|
|
97
|
+
*/
|
|
98
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
99
|
+
import { join } from 'node:path';
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Which operation resolves a declared secret. Open by construction: the three
|
|
103
|
+
* below are the DERIVED defaults, not the permitted values, so a repository
|
|
104
|
+
* that declares `"group": "deploy"` needs no smoo release to run it.
|
|
105
|
+
*/
|
|
106
|
+
export type SecretGroup = string;
|
|
107
|
+
|
|
108
|
+
/** Resolved at shell entry, because that is the operation that needs it. */
|
|
109
|
+
export const SHELL_GROUP: SecretGroup = 'shell';
|
|
110
|
+
|
|
111
|
+
/** Derived for a variable `.npmrc` interpolates as `${VAR}`. */
|
|
112
|
+
export const REGISTRY_GROUP: SecretGroup = 'registry';
|
|
113
|
+
|
|
114
|
+
/** Derived for the variable `smoo.remoteCache.tokenSecret` names. */
|
|
115
|
+
export const NX_CACHE_GROUP: SecretGroup = 'nx-cache';
|
|
116
|
+
|
|
117
|
+
export interface SecretSpec {
|
|
118
|
+
/** Executed directly, without a shell; stdout supplies the secret value. */
|
|
119
|
+
readonly command: readonly [string, ...string[]];
|
|
120
|
+
/**
|
|
121
|
+
* Overrides the derived group. Declared only for what the repository's own
|
|
122
|
+
* declarations cannot say — a credential no `.npmrc` reference and no cache
|
|
123
|
+
* token identifies, or one whose derivation is deliberately overridden.
|
|
124
|
+
*/
|
|
125
|
+
readonly group?: SecretGroup;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A declared secret with its group settled: explicit if stated, derived otherwise. */
|
|
129
|
+
export interface GroupedSecret {
|
|
130
|
+
readonly name: string;
|
|
131
|
+
/** The group that resolves it: `spec.group` when declared, `derivedGroup` otherwise. */
|
|
132
|
+
readonly group: SecretGroup;
|
|
133
|
+
/**
|
|
134
|
+
* What this repository's own declarations imply, kept beside the effective
|
|
135
|
+
* group so an override is a visible fact rather than a silent one. Equal to
|
|
136
|
+
* `group` unless the entry declares a different one.
|
|
137
|
+
*/
|
|
138
|
+
readonly derivedGroup: SecretGroup;
|
|
139
|
+
readonly spec: SecretSpec;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export type SecretCommandRunner = (argv: readonly string[]) => Promise<string>;
|
|
143
|
+
|
|
144
|
+
export interface SecretResolutionRequest {
|
|
145
|
+
/** Every declared secret of the repository, each with its group settled. */
|
|
146
|
+
readonly secrets: readonly GroupedSecret[];
|
|
147
|
+
/** The one group to resolve. No default: the wrong one is a credential prompt. */
|
|
148
|
+
readonly group: SecretGroup;
|
|
149
|
+
/** Environment consulted for existing values and mode flags; read, never written. */
|
|
150
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
151
|
+
/** Overrides the default direct-argv runner (test seam). */
|
|
152
|
+
readonly runCommand?: SecretCommandRunner;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export interface SecretResolutionOptions {
|
|
156
|
+
/** Repository root holding package.json and .npmrc. */
|
|
157
|
+
readonly root: string;
|
|
158
|
+
/** The one group to resolve. No default: the wrong one is a credential prompt. */
|
|
159
|
+
readonly group: SecretGroup;
|
|
160
|
+
/** Environment to consult; defaults to process.env. Read, never written. */
|
|
161
|
+
readonly env?: Record<string, string | undefined>;
|
|
162
|
+
/** Overrides the default direct-argv runner (test seam). */
|
|
163
|
+
readonly runCommand?: SecretCommandRunner;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** A declared secret this request declined to resolve, and how to supply it. */
|
|
167
|
+
export interface DeferredSecret {
|
|
168
|
+
readonly name: string;
|
|
169
|
+
readonly group: SecretGroup;
|
|
170
|
+
readonly guidance: string;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The resolution as a value: what belongs in a child's environment, and what
|
|
175
|
+
* was deliberately left out of it. Every declared secret appears in exactly
|
|
176
|
+
* one of `values`, `deferred`, or neither — the environment already carried
|
|
177
|
+
* it.
|
|
178
|
+
*/
|
|
179
|
+
export interface SecretResolution {
|
|
180
|
+
readonly values: Readonly<Record<string, string>>;
|
|
181
|
+
readonly deferred: readonly DeferredSecret[];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The env name pattern every shell accepts, so the same regex classifies
|
|
186
|
+
* declared names and `.npmrc` references without echoing values.
|
|
187
|
+
*/
|
|
188
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A group label's shape, and only its shape. It is one argv word of
|
|
192
|
+
* `smoo secrets run <group> <command...>`, so whitespace would make the
|
|
193
|
+
* command unwritable; every other value is the repository's business.
|
|
194
|
+
*/
|
|
195
|
+
const GROUP_NAME = /^\S+$/;
|
|
196
|
+
|
|
197
|
+
/** The `.npmrc` interpolations Bun substitutes while reading auth and registry config. */
|
|
198
|
+
const NPMRC_ENV_REFERENCE = /\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The command that resolves one group for exactly one child process. Every
|
|
202
|
+
* deferral names it, so what a developer reads is the command to run next
|
|
203
|
+
* rather than a description of one.
|
|
204
|
+
*/
|
|
205
|
+
const ON_DEMAND_COMMAND = 'smoo secrets run';
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Groups an operation depends on without resolving them, keyed by the group
|
|
209
|
+
* it does resolve. Shell entry runs `bun install`, which reads `.npmrc` —
|
|
210
|
+
* the declaration that derives `registry`. This is a dependency edge, not an
|
|
211
|
+
* exclusion: it makes CI and a cowshed workspace refuse a registry credential
|
|
212
|
+
* they cannot supply later, where a group nothing depends on stays silent.
|
|
213
|
+
*/
|
|
214
|
+
const DEPENDENT_GROUPS: Readonly<Record<string, readonly SecretGroup[]>> = {
|
|
215
|
+
[SHELL_GROUP]: [REGISTRY_GROUP],
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/** Groups a request for `group` needs supplied but does not resolve itself. */
|
|
219
|
+
export function dependentGroups(group: SecretGroup): readonly SecretGroup[] {
|
|
220
|
+
return DEPENDENT_GROUPS[group] ?? [];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
224
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function isNonemptyEnvValue(value: string | undefined): value is string {
|
|
228
|
+
return value !== undefined && value.length > 0;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Validates the `smoo.secrets` map out of an already-parsed package.json.
|
|
233
|
+
* Absent `smoo` or `smoo.secrets` is an empty map; a malformed declaration
|
|
234
|
+
* throws naming the offending variable — never a value.
|
|
235
|
+
*/
|
|
236
|
+
export function parseSmooSecrets(packageJson: unknown): Readonly<Record<string, SecretSpec>> {
|
|
237
|
+
if (!isRecord(packageJson)) return {};
|
|
238
|
+
const smoo = packageJson.smoo;
|
|
239
|
+
if (!isRecord(smoo)) return {};
|
|
240
|
+
const secrets = smoo.secrets;
|
|
241
|
+
if (secrets === undefined) return {};
|
|
242
|
+
if (!isRecord(secrets)) {
|
|
243
|
+
throw new Error('smoo.secrets must map environment variable names to { command: [string, ...] }');
|
|
244
|
+
}
|
|
245
|
+
const parsed: Record<string, SecretSpec> = {};
|
|
246
|
+
for (const [name, spec] of Object.entries(secrets)) {
|
|
247
|
+
parsed[name] = parseSecretSpec(name, spec);
|
|
248
|
+
}
|
|
249
|
+
return parsed;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function parseSecretSpec(name: string, spec: unknown): SecretSpec {
|
|
253
|
+
if (!ENV_NAME.test(name)) {
|
|
254
|
+
throw new Error(
|
|
255
|
+
`smoo.secrets: "${name}" is not a valid environment variable name (expected [A-Za-z_][A-Za-z0-9_]*)`,
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
if (!isRecord(spec)) {
|
|
259
|
+
throw new Error(`smoo.secrets.${name}: each entry must be an object with a command array`);
|
|
260
|
+
}
|
|
261
|
+
const raw = spec.command;
|
|
262
|
+
if (!Array.isArray(raw) || raw.length === 0) {
|
|
263
|
+
throw new Error(`smoo.secrets.${name}.command: must be an array of at least one string`);
|
|
264
|
+
}
|
|
265
|
+
for (const part of raw) {
|
|
266
|
+
if (typeof part !== 'string') {
|
|
267
|
+
throw new Error(`smoo.secrets.${name}.command: must be an array of at least one string`);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const [first, ...rest] = raw;
|
|
271
|
+
const group = spec.group;
|
|
272
|
+
if (group === undefined) {
|
|
273
|
+
return { command: [first, ...rest] };
|
|
274
|
+
}
|
|
275
|
+
if (typeof group !== 'string' || !GROUP_NAME.test(group)) {
|
|
276
|
+
throw new Error(
|
|
277
|
+
`smoo.secrets.${name}.group: must be a nonempty label without whitespace, so \`${ON_DEMAND_COMMAND} <group>\` can name it`,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
return { command: [first, ...rest], group };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The declared remote cache as a local shell needs it. `internalServer` is
|
|
285
|
+
* deliberately absent: it is the address a managed runner reaches inside its
|
|
286
|
+
* own network, and a shell that loads this file is by definition outside it,
|
|
287
|
+
* so a developer machine always takes the public `server`.
|
|
288
|
+
*/
|
|
289
|
+
export interface RemoteCacheSpec {
|
|
290
|
+
readonly server: string;
|
|
291
|
+
/** Variable holding the cache token: an ambient value, or a `smoo.secrets` entry in group `nx-cache`. */
|
|
292
|
+
readonly tokenSecret: string;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Validates the `smoo.remoteCache` block out of an already-parsed
|
|
297
|
+
* package.json. Absent `smoo` or `smoo.remoteCache` is no cache; a malformed
|
|
298
|
+
* declaration throws naming the field.
|
|
299
|
+
*/
|
|
300
|
+
export function parseSmooRemoteCache(packageJson: unknown): RemoteCacheSpec | null {
|
|
301
|
+
if (!isRecord(packageJson)) return null;
|
|
302
|
+
const smoo = packageJson.smoo;
|
|
303
|
+
if (!isRecord(smoo)) return null;
|
|
304
|
+
const remoteCache = smoo.remoteCache;
|
|
305
|
+
if (remoteCache === undefined) return null;
|
|
306
|
+
if (!isRecord(remoteCache)) {
|
|
307
|
+
throw new Error('smoo.remoteCache must be an object with { server, tokenSecret }');
|
|
308
|
+
}
|
|
309
|
+
const server = remoteCache.server;
|
|
310
|
+
const tokenSecret = remoteCache.tokenSecret;
|
|
311
|
+
// A trailing slash is refused rather than trimmed: Nx appends
|
|
312
|
+
// `/v1/cache/<hash>`, so the doubled slash is a route that answers 404
|
|
313
|
+
// forever, and silently repairing the manifest here would leave managed CI
|
|
314
|
+
// — which reads the same field — disagreeing with this shell.
|
|
315
|
+
if (typeof server !== 'string' || server.length === 0 || server.endsWith('/')) {
|
|
316
|
+
throw new Error(
|
|
317
|
+
'smoo.remoteCache.server must be an origin with no trailing slash, e.g. https://nx-cache.example.net',
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
if (typeof tokenSecret !== 'string' || !ENV_NAME.test(tokenSecret)) {
|
|
321
|
+
throw new Error('smoo.remoteCache.tokenSecret must name the environment variable holding the cache token');
|
|
322
|
+
}
|
|
323
|
+
return { server, tokenSecret };
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** Every `${VAR}` referenced from `.npmrc` text; an absent file contributes nothing. */
|
|
327
|
+
export function registryAuthEnvNames(npmrcText: string | null): ReadonlySet<string> {
|
|
328
|
+
const names = new Set<string>();
|
|
329
|
+
if (npmrcText === null) return names;
|
|
330
|
+
for (const match of npmrcText.matchAll(NPMRC_ENV_REFERENCE)) {
|
|
331
|
+
names.add(match[1]);
|
|
332
|
+
}
|
|
333
|
+
return names;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** What a repository's own declarations say about one variable, before any override. */
|
|
337
|
+
export interface GroupDerivationContext {
|
|
338
|
+
/** Env names referenced from `.npmrc`: which declared secrets a registry request needs. */
|
|
339
|
+
readonly registryIntentEnvs: ReadonlySet<string>;
|
|
340
|
+
/** The variable `smoo.remoteCache.tokenSecret` names, or null when no cache is declared. */
|
|
341
|
+
readonly cacheTokenEnv: string | null;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* The group a repository's existing declarations already imply for one
|
|
346
|
+
* variable. The cache token is checked first: a variable that is both the
|
|
347
|
+
* declared cache token and `.npmrc`-referenced was excluded from shell entry
|
|
348
|
+
* outright before groups existed, and `nx-cache` is the group that keeps that
|
|
349
|
+
* true.
|
|
350
|
+
*/
|
|
351
|
+
export function deriveSecretGroup(name: string, context: GroupDerivationContext): SecretGroup {
|
|
352
|
+
if (context.cacheTokenEnv === name) return NX_CACHE_GROUP;
|
|
353
|
+
if (context.registryIntentEnvs.has(name)) return REGISTRY_GROUP;
|
|
354
|
+
return SHELL_GROUP;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** Every declared secret with its group settled, in declaration order. */
|
|
358
|
+
export function groupSecrets(
|
|
359
|
+
secrets: Readonly<Record<string, SecretSpec>>,
|
|
360
|
+
context: GroupDerivationContext,
|
|
361
|
+
): readonly GroupedSecret[] {
|
|
362
|
+
return Object.entries(secrets).map(([name, spec]) => {
|
|
363
|
+
const derivedGroup = deriveSecretGroup(name, context);
|
|
364
|
+
return { name, group: spec.group ?? derivedGroup, derivedGroup, spec };
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
type SecretOutcome =
|
|
369
|
+
| { readonly name: string; readonly group: SecretGroup; readonly kind: 'env-wins' }
|
|
370
|
+
| { readonly name: string; readonly group: SecretGroup; readonly kind: 'resolved'; readonly value: string }
|
|
371
|
+
| { readonly name: string; readonly group: SecretGroup; readonly kind: 'deferred'; readonly guidance: string }
|
|
372
|
+
| { readonly name: string; readonly group: SecretGroup; readonly kind: 'failed'; readonly guidance: string };
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* One variable's routing, the four rules in the header applied in order. It
|
|
376
|
+
* is a value rather than a throw so each caller presents the result in its
|
|
377
|
+
* own terms: an install refuses, the remote cache turns itself off, and a
|
|
378
|
+
* deferred credential is named only if something then needs it.
|
|
379
|
+
*/
|
|
380
|
+
async function routeSecret(
|
|
381
|
+
secret: GroupedSecret,
|
|
382
|
+
context: {
|
|
383
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
384
|
+
readonly group: SecretGroup;
|
|
385
|
+
readonly dependsOn: readonly SecretGroup[];
|
|
386
|
+
readonly run: SecretCommandRunner;
|
|
387
|
+
},
|
|
388
|
+
): Promise<SecretOutcome> {
|
|
389
|
+
const { env } = context;
|
|
390
|
+
const { name, group } = secret;
|
|
391
|
+
if (isNonemptyEnvValue(env[name])) {
|
|
392
|
+
return { name, group, kind: 'env-wins' };
|
|
393
|
+
}
|
|
394
|
+
const requested = group === context.group;
|
|
395
|
+
if (requested || context.dependsOn.includes(group)) {
|
|
396
|
+
if (isNonemptyEnvValue(env.CI)) {
|
|
397
|
+
return {
|
|
398
|
+
name,
|
|
399
|
+
group,
|
|
400
|
+
kind: 'failed',
|
|
401
|
+
guidance:
|
|
402
|
+
'CI does not run secret provider commands — inject this variable into the job environment from the CI secret store',
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
if (group === REGISTRY_GROUP && isNonemptyEnvValue(env.COWSHED_WORKSPACE_TOKEN)) {
|
|
406
|
+
return {
|
|
407
|
+
name,
|
|
408
|
+
group,
|
|
409
|
+
kind: 'failed',
|
|
410
|
+
guidance:
|
|
411
|
+
'a registry credential absent in this cowshed workspace — enroll registry credentials through the cowshed gateway; gateway-managed workspaces never resolve a registry credential via a local provider command',
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
if (!requested) {
|
|
416
|
+
return {
|
|
417
|
+
name,
|
|
418
|
+
group,
|
|
419
|
+
kind: 'deferred',
|
|
420
|
+
guidance: `in group \`${group}\`, which a \`${context.group}\` request does not resolve — run \`${ON_DEMAND_COMMAND} ${group} <command>\` to supply it to that one command`,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
try {
|
|
424
|
+
const value = (await context.run(secret.spec.command)).replace(/\r?\n$/, '');
|
|
425
|
+
if (value.length === 0) {
|
|
426
|
+
return {
|
|
427
|
+
name,
|
|
428
|
+
group,
|
|
429
|
+
kind: 'failed',
|
|
430
|
+
guidance:
|
|
431
|
+
'provider command produced no output — run it locally to see why (its arguments and output are never logged here)',
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
return { name, group, kind: 'resolved', value };
|
|
435
|
+
} catch (error) {
|
|
436
|
+
return { name, group, kind: 'failed', guidance: describeCommandFailure(error) };
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export async function resolveSecrets(request: SecretResolutionRequest): Promise<SecretResolution> {
|
|
441
|
+
const context = {
|
|
442
|
+
env: request.env,
|
|
443
|
+
group: request.group,
|
|
444
|
+
dependsOn: dependentGroups(request.group),
|
|
445
|
+
run: request.runCommand ?? runSecretCommand,
|
|
446
|
+
};
|
|
447
|
+
const outcomes = await Promise.all(request.secrets.map(async (secret) => routeSecret(secret, context)));
|
|
448
|
+
const failures = outcomes.filter(
|
|
449
|
+
(outcome): outcome is Extract<SecretOutcome, { kind: 'failed' }> => outcome.kind === 'failed',
|
|
450
|
+
);
|
|
451
|
+
if (failures.length > 0) {
|
|
452
|
+
throw new Error(
|
|
453
|
+
[
|
|
454
|
+
'smoo secret resolution failed:',
|
|
455
|
+
...failures.map((failure) => `- ${failure.name} (${failure.group}): ${failure.guidance}`),
|
|
456
|
+
].join('\n'),
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
const values: Record<string, string> = {};
|
|
460
|
+
const deferred: DeferredSecret[] = [];
|
|
461
|
+
for (const outcome of outcomes) {
|
|
462
|
+
if (outcome.kind === 'resolved') {
|
|
463
|
+
values[outcome.name] = outcome.value;
|
|
464
|
+
} else if (outcome.kind === 'deferred') {
|
|
465
|
+
deferred.push({ name: outcome.name, group: outcome.group, guidance: outcome.guidance });
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return { values, deferred };
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
class SecretCommandExitError extends Error {
|
|
472
|
+
constructor(readonly exitCode: number) {
|
|
473
|
+
super('secret provider command failed');
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
class SecretCommandStartError extends Error {
|
|
478
|
+
constructor() {
|
|
479
|
+
super('secret provider command could not be started');
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function describeCommandFailure(error: unknown): string {
|
|
484
|
+
if (error instanceof SecretCommandExitError) {
|
|
485
|
+
return `provider command exited with code ${error.exitCode}; its arguments and output are suppressed — run it locally to debug`;
|
|
486
|
+
}
|
|
487
|
+
if (error instanceof SecretCommandStartError) {
|
|
488
|
+
return 'provider command could not be started — check that its program exists and is executable, then run it locally to debug';
|
|
489
|
+
}
|
|
490
|
+
// Foreign runner errors may carry argv or provider output; only the generic
|
|
491
|
+
// suppression note survives.
|
|
492
|
+
return 'provider command failed — its arguments and output are suppressed; run it locally to debug';
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** The program name is committed config, but an argument may embed a credential, so argv is never echoed. */
|
|
496
|
+
function startSecretCommand(argv: readonly string[]) {
|
|
497
|
+
try {
|
|
498
|
+
return Bun.spawn({
|
|
499
|
+
cmd: [...argv],
|
|
500
|
+
stdin: 'ignore',
|
|
501
|
+
stdout: 'pipe',
|
|
502
|
+
// Provider stderr can quote arguments or the secret itself; it is never surfaced.
|
|
503
|
+
stderr: 'ignore',
|
|
504
|
+
});
|
|
505
|
+
} catch {
|
|
506
|
+
throw new SecretCommandStartError();
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const runSecretCommand: SecretCommandRunner = async (argv) => {
|
|
511
|
+
const child = startSecretCommand(argv);
|
|
512
|
+
const stdout = await new Response(child.stdout).text();
|
|
513
|
+
const exitCode = await child.exited;
|
|
514
|
+
if (exitCode !== 0) {
|
|
515
|
+
throw new SecretCommandExitError(exitCode);
|
|
516
|
+
}
|
|
517
|
+
return stdout;
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
/** '*', the byte a redacted secret leaves behind. */
|
|
521
|
+
const MASK_BYTE = 0x2a;
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* A copy of captured child output with every resolved secret value replaced
|
|
525
|
+
* by the same number of `*` bytes.
|
|
526
|
+
*
|
|
527
|
+
* setup-environment.ts puts the values this file resolves into the
|
|
528
|
+
* environment of the install it then runs, and replays that child's captured
|
|
529
|
+
* stdout/stderr verbatim when it fails. Whatever the child echoes — a prepare
|
|
530
|
+
* script printing its environment, a registry URL carrying an inline
|
|
531
|
+
* credential — would otherwise walk straight past the suppression the rest of
|
|
532
|
+
* this file maintains. Redaction is byte-exact and same-length, so the
|
|
533
|
+
* failure being reported stays legible and every offset in it still lines up.
|
|
534
|
+
* Output with nothing to redact is returned as it came in, so the path that
|
|
535
|
+
* has no secrets to hide copies nothing.
|
|
536
|
+
*/
|
|
537
|
+
export function maskSecretValues(output: Uint8Array, values: Iterable<string>): Uint8Array {
|
|
538
|
+
const encoder = new TextEncoder();
|
|
539
|
+
let masked: Uint8Array | undefined;
|
|
540
|
+
for (const value of values) {
|
|
541
|
+
const needle = encoder.encode(value);
|
|
542
|
+
if (needle.length === 0 || needle.length > output.length) {
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
for (let from = 0; from + needle.length <= output.length; ) {
|
|
546
|
+
const at = indexOfBytes(masked ?? output, needle, from);
|
|
547
|
+
if (at < 0) {
|
|
548
|
+
break;
|
|
549
|
+
}
|
|
550
|
+
// `new Uint8Array`, not `.slice()`: a captured child's output arrives as
|
|
551
|
+
// a node Buffer, whose `slice` is `subarray` — a view. Masking through
|
|
552
|
+
// one would rewrite the caller's own captured bytes.
|
|
553
|
+
masked ??= new Uint8Array(output);
|
|
554
|
+
masked.fill(MASK_BYTE, at, at + needle.length);
|
|
555
|
+
from = at + needle.length;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return masked ?? output;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/** First occurrence of `needle` in `haystack` at or after `from`, or -1. */
|
|
562
|
+
function indexOfBytes(haystack: Uint8Array, needle: Uint8Array, from: number): number {
|
|
563
|
+
const first = needle[0];
|
|
564
|
+
if (first === undefined) {
|
|
565
|
+
return -1;
|
|
566
|
+
}
|
|
567
|
+
const last = haystack.length - needle.length;
|
|
568
|
+
for (let at = haystack.indexOf(first, from); at >= 0 && at <= last; at = haystack.indexOf(first, at + 1)) {
|
|
569
|
+
let matches = true;
|
|
570
|
+
for (let offset = 1; offset < needle.length; offset += 1) {
|
|
571
|
+
if (haystack[at + offset] !== needle[offset]) {
|
|
572
|
+
matches = false;
|
|
573
|
+
break;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
if (matches) {
|
|
577
|
+
return at;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return -1;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Every declared secret of the repository at `root`, each with its group
|
|
585
|
+
* settled from that repository's own declarations. The listing
|
|
586
|
+
* `smoo secrets run` prints and the routing below read the same value, so the
|
|
587
|
+
* groups a developer is offered are exactly the groups that resolve.
|
|
588
|
+
*/
|
|
589
|
+
export function readSecretGroups(root: string): readonly GroupedSecret[] {
|
|
590
|
+
const packageJson = readPackageJson(root);
|
|
591
|
+
return groupSecrets(parseSmooSecrets(packageJson), {
|
|
592
|
+
registryIntentEnvs: registryAuthEnvNames(readNpmrcText(root)),
|
|
593
|
+
cacheTokenEnv: parseSmooRemoteCache(packageJson)?.tokenSecret ?? null,
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* The declared secrets of `options.group`, resolved before the thing that
|
|
599
|
+
* needs them runs. Everything outside that group comes back deferred: named,
|
|
600
|
+
* unresolved, and not a failure — except where rule 2 applies, which is a
|
|
601
|
+
* context that could not honour the deferral anyway.
|
|
602
|
+
*/
|
|
603
|
+
export async function resolveSecretEnvironment(options: SecretResolutionOptions): Promise<SecretResolution> {
|
|
604
|
+
return resolveSecrets({
|
|
605
|
+
secrets: readSecretGroups(options.root),
|
|
606
|
+
group: options.group,
|
|
607
|
+
env: options.env ?? process.env,
|
|
608
|
+
runCommand: options.runCommand,
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
function readPackageJson(root: string): unknown {
|
|
613
|
+
const packageJsonPath = join(root, 'package.json');
|
|
614
|
+
let text: string;
|
|
615
|
+
try {
|
|
616
|
+
text = readFileSync(packageJsonPath, 'utf8');
|
|
617
|
+
} catch (error) {
|
|
618
|
+
throw new Error(
|
|
619
|
+
`smoo secrets: cannot read ${packageJsonPath} (${error instanceof Error ? error.message : String(error)})`,
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
let parsed: unknown;
|
|
623
|
+
try {
|
|
624
|
+
parsed = JSON.parse(text);
|
|
625
|
+
} catch (error) {
|
|
626
|
+
throw new Error(
|
|
627
|
+
`smoo secrets: ${packageJsonPath} is not valid JSON (${error instanceof Error ? error.message : String(error)})`,
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
return parsed;
|
|
631
|
+
}
|
|
632
|
+
function readNpmrcText(root: string): string | null {
|
|
633
|
+
const npmrcPath = join(root, '.npmrc');
|
|
634
|
+
if (!existsSync(npmrcPath)) return null;
|
|
635
|
+
return readFileSync(npmrcPath, 'utf8');
|
|
636
|
+
}
|