@theholocron/cli 5.0.0-alpha.539 → 5.0.0-alpha.540

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.mjs CHANGED
@@ -1765,19 +1765,44 @@ const runCiEntry = {
1765
1765
  // src/commands/secret-set/secret-set.entry.ts
1766
1766
  const secretSetEntry = {
1767
1767
  name: "new secret",
1768
- description: "Set a single secret via the configured `secrets` capability",
1768
+ description: "Set a single secret via the configured `secrets` capability, or hand a new one to a vault by file",
1769
1769
  positionals: [{
1770
1770
  key: "name",
1771
1771
  message: "Secret name:",
1772
1772
  type: "input"
1773
1773
  }],
1774
- options: [{
1775
- key: "scope",
1776
- flag: "--scope",
1777
- type: "input",
1778
- message: "Scope: repo (default), env=<name> or org=<name>:"
1779
- }],
1780
- skipOptions: ["from-stdin", "from-env"],
1774
+ options: [
1775
+ {
1776
+ key: "scope",
1777
+ flag: "--scope",
1778
+ type: "input",
1779
+ message: "Scope: repo (default), env=<name> or org=<name>:"
1780
+ },
1781
+ {
1782
+ key: "generate",
1783
+ flag: "--generate",
1784
+ type: "confirm",
1785
+ default: false,
1786
+ message: "Make the value (only for a secret the CLI generates, such as CRON_SECRET)?"
1787
+ },
1788
+ {
1789
+ key: "target",
1790
+ flag: "--target",
1791
+ type: "input",
1792
+ message: "Target: github (default) or file (the private import file for a vault):"
1793
+ },
1794
+ {
1795
+ key: "env",
1796
+ flag: "--env",
1797
+ type: "input",
1798
+ message: "With --target file: the vault Environment to import into (a layer such as prod, or a name):"
1799
+ }
1800
+ ],
1801
+ skipOptions: [
1802
+ "from-stdin",
1803
+ "from-env",
1804
+ "yes"
1805
+ ],
1781
1806
  supportsDryRun: true
1782
1807
  };
1783
1808
  // src/commands/secrets-list/secrets-list.entry.ts
@@ -28695,6 +28720,240 @@ const projectCreateCommand = {
28695
28720
  })).status === "fail") process.exitCode = 1;
28696
28721
  }
28697
28722
  };
28723
+ // src/commands/generated-secrets/generated-secrets.ts
28724
+ /**
28725
+ * The secrets the CLI may make up (spec `tech-rotate-generated-secrets`). A name is here only when its value is random
28726
+ * and nobody else issues or holds a copy: a vendor's API token, or a signing secret the vendor also stores, must not be
28727
+ * invented, so anything not listed is refused by `generateSecretValue()`.
28728
+ */
28729
+ const GENERATED_SECRETS = { CRON_SECRET: {
28730
+ bytes: 32,
28731
+ encoding: "hex",
28732
+ note: "the bearer Vercel's cron sends to Sentinel's /api/scheduled"
28733
+ } };
28734
+ /** The recipe for `name`, or `undefined` when the CLI does not make that secret. */
28735
+ function generatedSecret(name) {
28736
+ return Object.hasOwn(GENERATED_SECRETS, name) ? GENERATED_SECRETS[name] : void 0;
28737
+ }
28738
+ /**
28739
+ * A fresh value for a secret in {@link GENERATED_SECRETS}. The value is never printed or logged by the caller.
28740
+ *
28741
+ * @throws {Error} When `name` is not one the CLI generates (the message lists the ones it does).
28742
+ */
28743
+ function generateSecretValue(name, random = randomBytes) {
28744
+ const recipe = generatedSecret(name);
28745
+ if (!recipe) throw new Error(`${name} is not a secret the CLI generates (${Object.keys(GENERATED_SECRETS).join(", ")}): a token a vendor issues cannot be made up`);
28746
+ return Buffer.from(random(recipe.bytes)).toString(recipe.encoding);
28747
+ }
28748
+ // src/commands/secret-handoff/secret-handoff.ts
28749
+ /**
28750
+ * Hand one made-up secret to a person for import into a vault. The CLI cannot write to a 1Password Environment, so the
28751
+ * hand-off is the private import file, the way `export tokens` and the heartbeat hand-off do it
28752
+ * (`.holocron/.private/.credentials`, mode 0600, never overwritten, refused unless git says it ignores it, refused on
28753
+ * Windows, deleted once the import is done). The value goes only to that file: nothing here prints, logs or returns it.
28754
+ */
28755
+ async function handOffSecret(input) {
28756
+ const { print, name } = input;
28757
+ const fail = (reason, ...message) => {
28758
+ for (const line of message) print(line);
28759
+ return {
28760
+ status: "fail",
28761
+ reason
28762
+ };
28763
+ };
28764
+ const line = dotenvLine(name, input.value);
28765
+ if (line === null) return fail("unsupported", `${name}: its value is not one a dotenv file can carry`);
28766
+ if ((input.platform ?? process.platform) === "win32") return fail("windows", "cannot make the file private on Windows: its mode bits are not enforced");
28767
+ const target = resolveCredentialsFile(input.base, void 0, input.makeDir);
28768
+ const file = target.path;
28769
+ const git = (input.isIgnored ?? isGitIgnored)(file);
28770
+ if (git === "tracked" || git === "unknown") return fail("not-ignored", git === "tracked" ? `refusing to write ${name} to ${file}: git does not ignore it, so it could be committed` : `refusing to write ${name} to ${file}: git could not say whether it ignores it`, ignoreAdvice(target.isDefault, "choose a path outside the repository"));
28771
+ let identity;
28772
+ try {
28773
+ identity = (input.writeFile ?? writeCredentialsFile)(file, `${line}\n`, false);
28774
+ } catch (err) {
28775
+ return fail("write", `could not write ${file}: ${errorMessage(err)}`, "an existing file is never overwritten: finish importing it, delete it, then run the command again");
28776
+ }
28777
+ print(`wrote ${name} for import`);
28778
+ const where = input.environment ? `the \`${input.environment}\` Environment` : "the Environment that holds it";
28779
+ return await finishCredentialsFile({
28780
+ path: file,
28781
+ identity,
28782
+ print,
28783
+ holds: `the new value of ${name}`,
28784
+ importHint: `import it in the 1Password app: Developer, View Environments, ${where}, Import .env file (it replaces the old value)`,
28785
+ keep: input.keep,
28786
+ interactive: input.interactive,
28787
+ shred: input.shred,
28788
+ waitForImport: input.waitForImport
28789
+ }) ? { status: "ok" } : {
28790
+ status: "fail",
28791
+ reason: "file-left"
28792
+ };
28793
+ }
28794
+ // src/commands/secret-import-check/secret-import-check.ts
28795
+ /**
28796
+ * Check that a hand-imported secret really changed in the vault (spec `tech-rotate-generated-secrets`). The Environment
28797
+ * is read in memory before the file is written and again after the wait, and only a per-run salted hash of the value is
28798
+ * kept between the two reads, so nothing here holds, prints or returns a value. A forgotten import is then caught by
28799
+ * this comparison, not by a 401 later.
28800
+ */
28801
+ /** A fresh salt for one run, so a fingerprint means nothing outside it. */
28802
+ function newSalt() {
28803
+ return randomBytes(16).toString("hex");
28804
+ }
28805
+ function fingerprint(salt, value) {
28806
+ return createHash("sha256").update(salt).update("\0").update(value).digest("hex");
28807
+ }
28808
+ /**
28809
+ * Reads `name` from the vault Environment `environmentId` and reduces it to a fingerprint. A vault with no way to read an
28810
+ * Environment, or a read that throws, is `unreadable` with the reason (never the thrown value's text beyond its message).
28811
+ */
28812
+ async function readEnvironmentSecret(vault, environmentId, name, salt) {
28813
+ if (!vault) return {
28814
+ status: "unreadable",
28815
+ why: "no vault provider is configured"
28816
+ };
28817
+ try {
28818
+ let value;
28819
+ if (vault.readEnvironmentEntries) value = (await vault.readEnvironmentEntries(environmentId)).find((e) => e.name === name)?.value;
28820
+ else if (vault.readEnvironment) {
28821
+ const values = await vault.readEnvironment(environmentId);
28822
+ value = Object.hasOwn(values, name) ? values[name] : void 0;
28823
+ } else return {
28824
+ status: "unreadable",
28825
+ why: `${vault.providerName} cannot read an Environment`
28826
+ };
28827
+ if (value === void 0) return {
28828
+ status: "read",
28829
+ fingerprint: void 0,
28830
+ missing: true
28831
+ };
28832
+ return {
28833
+ status: "read",
28834
+ fingerprint: value === "" ? void 0 : fingerprint(salt, value),
28835
+ missing: false
28836
+ };
28837
+ } catch (err) {
28838
+ return {
28839
+ status: "unreadable",
28840
+ why: errorMessage(err)
28841
+ };
28842
+ }
28843
+ }
28844
+ /** Whether the Environment holds a new, non-blank value after the import, from the reads before and after it. */
28845
+ function judgeImport(name, environment, before, after) {
28846
+ if (after.status === "unreadable") return {
28847
+ status: "skip",
28848
+ message: `could not read ${environment} to check the import: ${after.why}`
28849
+ };
28850
+ if (after.missing) return {
28851
+ status: "fail",
28852
+ message: `${environment} has no ${name}: it was not imported`
28853
+ };
28854
+ if (after.fingerprint === void 0) return {
28855
+ status: "fail",
28856
+ message: `${environment} holds a blank ${name}`
28857
+ };
28858
+ if (before.status === "read" && before.fingerprint === after.fingerprint) return {
28859
+ status: "fail",
28860
+ message: `${environment} still holds the old value of ${name}: it was not imported`
28861
+ };
28862
+ if (before.status === "unreadable") return {
28863
+ status: "ok",
28864
+ message: `${environment} holds ${name} (the old value could not be read, so a change is not proved)`
28865
+ };
28866
+ return {
28867
+ status: "ok",
28868
+ message: `${environment} holds a new ${name}`
28869
+ };
28870
+ }
28871
+ // src/commands/secret-set/secret-to-file/secret-to-file.ts
28872
+ /**
28873
+ * `new secret --target file`: hand a value to the private import file for a vault, check the import took, and name what
28874
+ * comes next (spec `tech-rotate-generated-secrets`). The value is never printed, logged or returned. The CLI cannot write
28875
+ * to a 1Password Environment, so the vault side is a person's import; with an Environment named the command reads it before
28876
+ * and after and says whether the value changed.
28877
+ */
28878
+ async function runSecretToFile(input, value, print, loader, logger = getLogger()) {
28879
+ const { name } = input;
28880
+ const dryRun = input.context.dryRun ?? false;
28881
+ const { repoRoot } = input.context;
28882
+ const interactive = input.interactive ?? process.stdin.isTTY === true;
28883
+ const environment = input.environment ? await (input.resolveEnvironment ?? ((id, root) => resolveEnvironmentId(id, { repoRoot: root })))(input.environment, repoRoot) : void 0;
28884
+ const into = environment ? ` for the \`${environment}\` Environment` : "";
28885
+ print(`Holocron secret set — ${name} to the private import file${into}${dryRun ? " (dry-run)" : ""}`);
28886
+ if (dryRun) {
28887
+ const message = `would write ${name} (<${value.length} chars>) to the private import file and wait for the import`;
28888
+ print(` … ${message}`);
28889
+ return {
28890
+ status: "dry-run",
28891
+ name,
28892
+ message
28893
+ };
28894
+ }
28895
+ if (!input.yes) {
28896
+ if (!input.confirm) throw new NonInteractiveError("pass --yes to write the secret without a terminal");
28897
+ if (!await input.confirm(`Write a new ${name} to the private import file${into}?`)) {
28898
+ print(" cancelled; nothing written");
28899
+ return {
28900
+ status: "cancelled",
28901
+ name
28902
+ };
28903
+ }
28904
+ }
28905
+ let vault;
28906
+ if (environment) {
28907
+ await loader.load();
28908
+ vault = loader.has("vault") ? loader.get("vault") : void 0;
28909
+ }
28910
+ const salt = newSalt();
28911
+ const before = environment ? await readEnvironmentSecret(vault, environment, name, salt) : void 0;
28912
+ const handed = await (input.handOff ?? handOffSecret)({
28913
+ name,
28914
+ value,
28915
+ base: repoRoot,
28916
+ print,
28917
+ interactive,
28918
+ ...environment ? { environment } : {}
28919
+ });
28920
+ if (handed.status === "fail") {
28921
+ const message = `${name} was not handed over (${handed.reason ?? "failed"})`;
28922
+ print(` ✗ ${message}`);
28923
+ logger.warn({
28924
+ key: name,
28925
+ reason: handed.reason,
28926
+ status: "fail"
28927
+ }, `secret set: ${name}`);
28928
+ return {
28929
+ status: "fail",
28930
+ name,
28931
+ message
28932
+ };
28933
+ }
28934
+ if (environment && before) {
28935
+ const verdict = judgeImport(name, environment, before, await readEnvironmentSecret(vault, environment, name, salt));
28936
+ print(` ${verdict.status === "ok" ? "✓" : verdict.status === "fail" ? "✗" : "·"} ${verdict.message}`);
28937
+ logger.info({
28938
+ key: name,
28939
+ status: verdict.status
28940
+ }, `secret import check: ${name}`);
28941
+ if (verdict.status === "fail") return {
28942
+ status: "fail",
28943
+ name,
28944
+ message: verdict.message
28945
+ };
28946
+ }
28947
+ print(` next: holocron sync secrets${input.environment ? ` --env ${input.environment}` : " --env <environment>"} (with the same --cwd), then redeploy: a deployment only gets variables when it is deployed`);
28948
+ logger.info({
28949
+ key: name,
28950
+ status: "ok"
28951
+ }, `secret set: ${name}`);
28952
+ return {
28953
+ status: "ok",
28954
+ name
28955
+ };
28956
+ }
28698
28957
  // src/commands/secret-set/secret-set.ts
28699
28958
  /**
28700
28959
  * `holocron new secret` — one-shot single-secret push.
@@ -28719,13 +28978,21 @@ async function runSecretSet(input) {
28719
28978
  const print = input.print ?? ((line) => console.log(line));
28720
28979
  const logger = input.logger ?? getLogger();
28721
28980
  const loader = input.loader ?? new PluginLoader(input.loaded.resolved, input.context);
28981
+ if (input.generate && (input.value || input.fromStdin || input.fromEnv)) throw new Error("--generate makes the value: do not also pass a value, --from-stdin or --from-env");
28982
+ if (input.target === "file") {
28983
+ const value = input.generate ? generateSecretValue(input.name, input.random) : await resolveValue(input);
28984
+ if (!value) throw new Error(noValueMessage(input.name));
28985
+ return runSecretToFile(input, value, print, loader, logger);
28986
+ }
28987
+ if (input.environment) throw new Error("--env belongs to --target file: a GitHub secret is named by --scope");
28722
28988
  await loader.load();
28723
28989
  assertPluginsResolvable(loader, "new secret");
28724
28990
  const dryRun = input.context.dryRun ?? false;
28725
28991
  const scope = input.scope ?? { kind: "repo" };
28726
28992
  if (!loader.has("secrets")) throw new Error("`secrets` capability is not configured — add a `secrets` provider to holocron.config.json");
28727
- const value = await resolveValue(input);
28728
- if (!value) throw new Error(`no value for secret \`${input.name}\` — pass it positionally, via --from-stdin, --from-env <NAME>, or set $${input.name} in the environment`);
28993
+ const value = input.generate ? generateSecretValue(input.name, input.random) : await resolveValue(input);
28994
+ if (!value) throw new Error(noValueMessage(input.name));
28995
+ if (input.generate) print(" the value is made now and not shown, so it cannot be copied anywhere else");
28729
28996
  print(`Holocron secret set — ${input.name} (${describeScope(scope)})${dryRun ? " (dry-run)" : ""}`);
28730
28997
  if (dryRun) {
28731
28998
  const message = `would: secrets.setSecret(${describeScope(scope)}, ${input.name}, <${value.length} chars>)`;
@@ -28768,6 +29035,9 @@ async function runSecretSet(input) {
28768
29035
  };
28769
29036
  }
28770
29037
  }
29038
+ function noValueMessage(name) {
29039
+ return `no value for secret \`${name}\` — pass it positionally, via --from-stdin, --from-env <NAME>, or set $${name} in the environment`;
29040
+ }
28771
29041
  async function resolveValue(input) {
28772
29042
  if (input.value) return input.value;
28773
29043
  if (input.fromStdin) return (await (input.readStdin ?? defaultReadStdin)()).replace(/\r?\n$/, "");
@@ -28805,11 +29075,27 @@ const secretSetArgs = (y) => y.positional("name", {
28805
29075
  type: "string",
28806
29076
  default: "repo",
28807
29077
  describe: "Scope: \"repo\" (default), \"env=<name>\", or \"org=<name>\""
29078
+ }).option("target", {
29079
+ type: "string",
29080
+ choices: ["github", "file"],
29081
+ default: "github",
29082
+ describe: "Where the value goes: `github` (the `secrets` capability) or `file` (the private import file, for a vault the CLI cannot write)"
29083
+ }).option("generate", {
29084
+ type: "boolean",
29085
+ default: false,
29086
+ describe: "Make the value with a random source (only for a secret the CLI generates, such as CRON_SECRET)"
29087
+ }).option("env", {
29088
+ type: "string",
29089
+ describe: "With --target file: the vault Environment the file is imported into (a layer word or a name), so the import is checked"
29090
+ }).option("yes", {
29091
+ type: "boolean",
29092
+ default: false,
29093
+ describe: "With --target file: write without asking (required without a terminal)"
28808
29094
  });
28809
29095
  // src/commands/secret-set/secret-set.command.ts
28810
29096
  const secretSetCommand = {
28811
29097
  command: "secret [name] [value]",
28812
- describe: "Set a single secret via the configured `secrets` capability",
29098
+ describe: "Set a single secret via the configured `secrets` capability, or hand a new one to a vault by file",
28813
29099
  builder: secretSetArgs,
28814
29100
  handler: async (argv) => {
28815
29101
  const tokens = tokenContext(argv.token);
@@ -28831,7 +29117,15 @@ const secretSetCommand = {
28831
29117
  ...argv.value ? { value: argv.value } : {},
28832
29118
  ...argv.fromStdin ? { fromStdin: true } : {},
28833
29119
  ...argv.fromEnv ? { fromEnv: argv.fromEnv } : {},
28834
- scope
29120
+ scope,
29121
+ target: argv.target,
29122
+ ...argv.generate ? { generate: true } : {},
29123
+ ...argv.env ? { environment: argv.env } : {},
29124
+ ...argv.yes ? { yes: true } : {},
29125
+ ...process.stdin.isTTY === true ? { confirm: (message) => confirm({
29126
+ message,
29127
+ default: false
29128
+ }) } : {}
28835
29129
  })).status === "fail") process.exitCode = 1;
28836
29130
  }
28837
29131
  };