@secretenvs/cli 1.7.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +13 -13
  2. package/dist/index.js +59 -164
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -6,19 +6,19 @@ are bundled into the build, so the packed tarball installs on its own.
6
6
 
7
7
  ## Commands
8
8
 
9
- | Command | What it does |
10
- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
11
- | `secretenvs login [--url <baseUrl>] [--token]` | Prompts for email and password (hidden), plus a 2FA code if the account has it on; stores an API token. With `--token`, asks for an API token you created in the web app instead (for accounts that sign in through SIAM). |
12
- | `secretenvs logout` | Deletes the stored credentials. |
13
- | `secretenvs whoami` | Shows the server, account and token prefix, and checks the token still works. |
14
- | `secretenvs projects` | Lists accessible projects (slug, role, name). |
15
- | `secretenvs envs -p <project>` | Lists the environments of a project. |
16
- | `secretenvs list -p <project> -e <env> [--reveal]` | Lists keys and their source (`env` / `shared`). Values are masked. |
17
- | `secretenvs set KEY=VALUE -p <project> -e <env>` | Creates or updates an environment variable (needs EDITOR). |
18
- | `secretenvs history KEY -p <project> -e <env> [--shared] [--limit]` | Lists versions: number, change, author, time. Values are never shown. |
19
- | `secretenvs rollback KEY --to <n> -p <project> -e <env> [--shared] [--yes]` | Restores version `n` as a **new** version (needs EDITOR). Asks to confirm. |
20
- | `secretenvs pull -p <project> -e <env> [--out .env] [--format dotenv\|json] [--force]` | Writes the resolved variables to a file (mode `0600`). |
21
- | `secretenvs run -p <project> -e <env> -- <command...>` | Runs a command with the variables injected into its environment. |
9
+ | Command | What it does |
10
+ | -------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
11
+ | `secretenvs login [--url <baseUrl>]` | Asks for an API token you created in the web app (Settings > API tokens), checks it with the server and stores it. There is no email and password login: accounts sign in through SIAM. |
12
+ | `secretenvs logout` | Deletes the stored credentials. |
13
+ | `secretenvs whoami` | Shows the server, account and token prefix, and checks the token still works. |
14
+ | `secretenvs projects` | Lists accessible projects (slug, role, name). |
15
+ | `secretenvs envs -p <project>` | Lists the environments of a project. |
16
+ | `secretenvs list -p <project> -e <env> [--reveal]` | Lists keys and their source (`env` / `shared`). Values are masked. |
17
+ | `secretenvs set KEY=VALUE -p <project> -e <env>` | Creates or updates an environment variable (needs EDITOR). |
18
+ | `secretenvs history KEY -p <project> -e <env> [--shared] [--limit]` | Lists versions: number, change, author, time. Values are never shown. |
19
+ | `secretenvs rollback KEY --to <n> -p <project> -e <env> [--shared] [--yes]` | Restores version `n` as a **new** version (needs EDITOR). Asks to confirm. |
20
+ | `secretenvs pull -p <project> -e <env> [--out .env] [--format dotenv\|json] [--force]` | Writes the resolved variables to a file (mode `0600`). |
21
+ | `secretenvs run -p <project> -e <env> -- <command...>` | Runs a command with the variables injected into its environment. |
22
22
 
23
23
  Every command supports `--help`. Failures print a message to stderr and exit
24
24
  with a non-zero code. `run` exits with the code of the command it started.
package/dist/index.js CHANGED
@@ -24,7 +24,7 @@ function defaultDeps() {
24
24
  var spawnCommand = (command, args, options) => crossSpawn(command, args, options);
25
25
 
26
26
  // src/program.ts
27
- import { Command, CommanderError } from "commander";
27
+ import { Command, CommanderError, Option } from "commander";
28
28
 
29
29
  // src/commands.ts
30
30
  import { chmod as chmod2, writeFile as writeFile2 } from "fs/promises";
@@ -41,13 +41,8 @@ var MAX_KEY_LENGTH = 128;
41
41
  var MAX_VALUE_BYTES = 64 * 1024;
42
42
  var MAX_NAME_LENGTH = 64;
43
43
  var MAX_BULK_VARIABLES = 500;
44
- var MIN_PASSWORD_LENGTH = 12;
45
- var MAX_PASSWORD_LENGTH = 256;
46
44
  var MAX_VERSIONS_LIMIT = 100;
47
- var CSRF_HEADER_NAME = "x-requested-with";
48
- var CSRF_HEADER_VALUE = "secretenvs-web";
49
45
  var TOKEN_PREFIX = "svt_";
50
- var MAX_AUTH_CODE_LENGTH = 32;
51
46
  var ProjectRole = z.enum(["OWNER", "ADMIN", "EDITOR", "VIEWER"]);
52
47
  var TokenAccess = z.enum(["READ", "READ_WRITE"]);
53
48
  var VersionChangeType = z.enum(["CREATED", "UPDATED", "ROLLBACK"]);
@@ -55,7 +50,6 @@ var ErrorCode = z.enum([
55
50
  "VALIDATION_ERROR",
56
51
  "BAD_REQUEST",
57
52
  "UNAUTHENTICATED",
58
- "MFA_REQUIRED",
59
53
  "FORBIDDEN",
60
54
  "NOT_FOUND",
61
55
  "CONFLICT",
@@ -77,47 +71,19 @@ var variableValue = z2.string().refine(
77
71
  var secretExpiry = isoDate.refine((value) => Date.parse(value) > Date.now(), {
78
72
  message: "Expiry must be in the future"
79
73
  }).nullable();
80
- var password2 = z2.string().min(MIN_PASSWORD_LENGTH).max(MAX_PASSWORD_LENGTH);
81
- var RegisterInput = z2.object({ email, name, password: password2 }).strict();
82
- var authCode = z2.string().trim().min(1, "Enter your code").max(MAX_AUTH_CODE_LENGTH);
83
- var LoginInput = z2.object({
84
- email,
85
- password: z2.string().min(1).max(MAX_PASSWORD_LENGTH),
86
- code: authCode.optional()
87
- }).strict();
88
74
  var UserDto = z2.object({
89
75
  id,
90
76
  email,
91
77
  name,
92
78
  createdAt: isoDate,
93
- twoFactorEnabled: z2.boolean(),
94
- // Name the user gave the authenticator app at setup; null while 2FA is off.
95
- twoFactorAppName: z2.string().nullable(),
96
79
  // Whether the account may use the /admin pages (set directly in the database).
97
- isSuperAdmin: z2.boolean(),
98
- // Linked to a SIAM account: name and email follow SIAM, so they are not edited here.
99
- managedBySiam: z2.boolean(),
100
- // Has a password of its own. Accounts created through SIAM do not, and SIAM handles their
101
- // password and two-factor authentication.
102
- hasPassword: z2.boolean()
80
+ isSuperAdmin: z2.boolean()
103
81
  }).strict();
104
82
  var AuthOptionsDto = z2.object({
105
- // Sign-in through SIAM is configured on this server.
106
- siam: z2.boolean(),
107
- // Where people manage their SIAM account; null when it is not configured or SIAM is off.
83
+ // Where people manage their SIAM account (name, email, password, two-factor); null when the
84
+ // server does not say.
108
85
  profileUrl: z2.string().nullable()
109
86
  }).strict();
110
- var TwoFactorSetupDto = z2.object({ secret: z2.string(), otpauthUri: z2.string() }).strict();
111
- var TwoFactorSetupInput = z2.object({ password: z2.string().min(1).max(MAX_PASSWORD_LENGTH) }).strict();
112
- var TwoFactorEnableInput = z2.object({
113
- appName: z2.string().trim().min(1, "Enter the app name").max(MAX_NAME_LENGTH),
114
- code: z2.string().trim().regex(/^\d{6}$/, "Enter the 6-digit code")
115
- }).strict();
116
- var TwoFactorConfirmInput = z2.object({
117
- password: z2.string().min(1).max(MAX_PASSWORD_LENGTH),
118
- code: authCode
119
- }).strict();
120
- var RecoveryCodesDto = z2.object({ recoveryCodes: z2.array(z2.string()) }).strict();
121
87
  var CreateProjectInput = z2.object({ name, slug, description: z2.string().max(500).optional() }).strict();
122
88
  var UpdateProjectInput = z2.object({
123
89
  name: name.optional(),
@@ -256,6 +222,7 @@ var SdkProjectDto = z2.object({
256
222
  role: ProjectRole,
257
223
  environments: z2.array(SdkEnvironmentDto)
258
224
  }).strict();
225
+ var SdkMeDto = z2.object({ id, email, name }).strict();
259
226
  var adminPerson = z2.object({ id, name, email }).strict();
260
227
  var AdminProjectDto = ProjectDto.extend({
261
228
  owner: adminPerson.nullable(),
@@ -270,22 +237,13 @@ var AdminUserDto = z2.object({
270
237
  // Null while the account is enabled.
271
238
  disabledAt: isoDate.nullable(),
272
239
  isSuperAdmin: z2.boolean(),
273
- // Linked to a SIAM account: its name and email belong to SIAM and cannot be edited here.
274
- managedBySiam: z2.boolean(),
275
240
  projectCount: z2.number().int().min(0)
276
241
  }).strict();
277
242
  var AdminUserDetailDto = AdminUserDto.extend({
278
243
  // Every project the user is a member of, with their role there.
279
244
  projects: z2.array(z2.object({ id, name, slug, role: ProjectRole }).strict())
280
245
  });
281
- var AdminUpdateUserInput = z2.object({
282
- name: name.optional(),
283
- email: email.optional(),
284
- disabled: z2.boolean().optional()
285
- }).strict().refine(
286
- (input2) => input2.name !== void 0 || input2.email !== void 0 || input2.disabled !== void 0,
287
- { message: "Provide name, email and/or disabled" }
288
- );
246
+ var AdminUpdateUserInput = z2.object({ disabled: z2.boolean() }).strict();
289
247
  var CreateTokenInput = z2.object({
290
248
  name,
291
249
  access: TokenAccess,
@@ -339,8 +297,9 @@ var apiEnvironmentSchema = z3.object({
339
297
  // Shared secret for the daily expiry-reminder call (see docs/DEPLOYMENT.md). Unset turns
340
298
  // that endpoint off.
341
299
  CRON_SECRET: z3.string().min(32).max(256).optional(),
342
- // Sign-in through SIAM (https://siam.ankityadav.net.in/docs). The four IAM_* values go
343
- // together: leave them all unset to keep SIAM sign-in off.
300
+ // Sign-in through SIAM (https://siam.ankityadav.net.in/docs) is the only way in, so the four
301
+ // IAM_* values below are required (see the refinement). The one exception is local
302
+ // development with DEV_SIAM_STUB, and NODE_ENV=test, where tests inject their own SIAM.
344
303
  IAM_BASE_URL: z3.url().optional(),
345
304
  IAM_CLIENT_ID: z3.string().min(1).max(256).optional(),
346
305
  IAM_CLIENT_SECRET: z3.string().min(1).max(512).optional(),
@@ -356,7 +315,10 @@ var apiEnvironmentSchema = z3.object({
356
315
  IAM_REQUIRED_PERMISSION: z3.string().min(1).max(128).optional(),
357
316
  // Link a SIAM sign-in to an existing local account with the same email. Off by default,
358
317
  // because it is only safe when SIAM verifies that people own the email they sign up with.
359
- IAM_LINK_BY_EMAIL: z3.enum(["true", "false"]).optional()
318
+ IAM_LINK_BY_EMAIL: z3.enum(["true", "false"]).optional(),
319
+ // Local development only: replaces SIAM with a built-in stand-in that signs in one fixed
320
+ // fake user without asking for anything. Refused outside NODE_ENV=development.
321
+ DEV_SIAM_STUB: z3.enum(["true", "false"]).optional()
360
322
  }).strict().superRefine((environment, context) => {
361
323
  const names = [
362
324
  "IAM_BASE_URL",
@@ -364,20 +326,29 @@ var apiEnvironmentSchema = z3.object({
364
326
  "IAM_CLIENT_SECRET",
365
327
  "IAM_REDIRECT_URI"
366
328
  ];
367
- const missing = names.filter((name2) => environment[name2] === void 0);
368
- if (missing.length > 0 && missing.length < names.length) {
369
- for (const name2 of missing) {
329
+ const stub = environment.DEV_SIAM_STUB === "true";
330
+ if (stub && environment.NODE_ENV !== "development") {
331
+ context.addIssue({
332
+ code: "custom",
333
+ path: ["DEV_SIAM_STUB"],
334
+ message: "Only allowed when NODE_ENV is development"
335
+ });
336
+ }
337
+ if (stub || environment.NODE_ENV === "test") return;
338
+ for (const name2 of names) {
339
+ if (environment[name2] === void 0) {
370
340
  context.addIssue({
371
341
  code: "custom",
372
342
  path: [name2],
373
- message: "Required when any other IAM_* value is set"
343
+ message: "Required: SIAM is the only way to sign in"
374
344
  });
375
345
  }
376
346
  }
377
347
  }).transform((environment) => ({
378
348
  ...environment,
379
349
  COOKIE_SECURE: environment.COOKIE_SECURE === void 0 ? environment.NODE_ENV === "production" : environment.COOKIE_SECURE === "true",
380
- IAM_LINK_BY_EMAIL: environment.IAM_LINK_BY_EMAIL === "true"
350
+ IAM_LINK_BY_EMAIL: environment.IAM_LINK_BY_EMAIL === "true",
351
+ DEV_SIAM_STUB: environment.DEV_SIAM_STUB === "true"
381
352
  }));
382
353
 
383
354
  // ../sdk/dist/index.js
@@ -505,6 +476,10 @@ var SecretEnvs = class {
505
476
  this.#fetch = fetchImpl;
506
477
  this.#env = env;
507
478
  }
479
+ /** The account the token belongs to (name and email only). */
480
+ async me() {
481
+ return this.#get("/api/v1/sdk/me", (body) => SdkMeDto.parse(body));
482
+ }
508
483
  /** Projects visible to the token, with their environments. */
509
484
  async listProjects() {
510
485
  return this.#get(
@@ -704,36 +679,6 @@ async function send(fetchFn, baseUrl, request, parse) {
704
679
  if (status === 404) throw new SecretEnvsNotFoundError(message, status, code);
705
680
  throw new SecretEnvsApiError(message, status, code);
706
681
  }
707
- async function siamOffered(options) {
708
- try {
709
- const result = await send(
710
- options.fetch,
711
- options.baseUrl,
712
- { method: "GET", path: "/api/v1/auth/options" },
713
- (body) => AuthOptionsDto.parse(body)
714
- );
715
- return result.siam;
716
- } catch {
717
- return false;
718
- }
719
- }
720
- function cliLogin(options) {
721
- return send(
722
- options.fetch,
723
- options.baseUrl,
724
- {
725
- method: "POST",
726
- path: "/api/v1/auth/cli-login",
727
- headers: { [CSRF_HEADER_NAME]: CSRF_HEADER_VALUE },
728
- body: {
729
- email: options.email,
730
- password: options.password,
731
- ...options.code !== void 0 && { code: options.code }
732
- }
733
- },
734
- (body) => CreatedTokenDto.parse(body)
735
- );
736
- }
737
682
  var ManagementClient = class {
738
683
  #token;
739
684
  #baseUrl;
@@ -1059,40 +1004,6 @@ var out = (deps, text) => {
1059
1004
  `);
1060
1005
  };
1061
1006
  var MAX_TOKEN_LENGTH = 256;
1062
- async function loginWithToken(deps, baseUrl) {
1063
- out(
1064
- deps,
1065
- "Create an API token with read and write access in the web app (Settings > API tokens), then paste it here."
1066
- );
1067
- const token = (await deps.prompts.password({ message: "API token" })).trim();
1068
- if (!token.startsWith(TOKEN_PREFIX) || token.length > MAX_TOKEN_LENGTH) {
1069
- throw new CliError(
1070
- `That does not look like a SecretEnvs API token (they start with ${TOKEN_PREFIX}).`
1071
- );
1072
- }
1073
- const sdk = new SecretEnvs({ token, baseUrl, fetch: deps.fetch, env: {} });
1074
- let projects;
1075
- try {
1076
- projects = await sdk.listProjects();
1077
- } catch (error) {
1078
- if (error instanceof SecretEnvsAuthError) {
1079
- throw new CliError(
1080
- "The server did not accept that token. Check that you copied all of it, and that it has not expired or been revoked."
1081
- );
1082
- }
1083
- throw error;
1084
- }
1085
- const prefix = token.slice(0, 8);
1086
- await writeCredentials(credentialsPath(deps.homeDir), {
1087
- baseUrl,
1088
- token,
1089
- prefix
1090
- });
1091
- out(
1092
- deps,
1093
- `Logged in with API token ${prefix}... on ${baseUrl} (${projects.length} accessible ${projects.length === 1 ? "project" : "projects"})`
1094
- );
1095
- }
1096
1007
  async function login(deps, options) {
1097
1008
  if (!deps.interactive) {
1098
1009
  throw new CliError("`secretenvs login` needs an interactive terminal.");
@@ -1111,48 +1022,36 @@ async function login(deps, options) {
1111
1022
  error instanceof SecretEnvsError ? error.message : "Invalid server URL"
1112
1023
  );
1113
1024
  }
1114
- let useToken = options.token === true;
1115
- const siam = useToken ? false : await siamOffered({ fetch: deps.fetch, baseUrl });
1116
- if (siam) {
1117
- useToken = await deps.prompts.confirm({
1118
- message: "This server signs people in through SIAM. Log in with an API token instead of email and password? (Needed for SIAM accounts)",
1119
- default: false
1120
- });
1025
+ out(
1026
+ deps,
1027
+ "Create an API token with read and write access in the web app (Settings > API tokens), then paste it here."
1028
+ );
1029
+ const token = (await deps.prompts.password({ message: "API token" })).trim();
1030
+ if (!token.startsWith(TOKEN_PREFIX) || token.length > MAX_TOKEN_LENGTH) {
1031
+ throw new CliError(
1032
+ `That does not look like a SecretEnvs API token (they start with ${TOKEN_PREFIX}).`
1033
+ );
1121
1034
  }
1122
- if (useToken) return loginWithToken(deps, baseUrl);
1123
- const email2 = (await deps.prompts.input({ message: "Email" })).trim();
1124
- const password3 = await deps.prompts.password({ message: "Password" });
1125
- const attempt = (code) => cliLogin({
1126
- fetch: deps.fetch,
1127
- baseUrl,
1128
- email: email2,
1129
- password: password3,
1130
- ...code !== void 0 && { code }
1131
- });
1132
- const created = await attempt().catch(async (error) => {
1133
- if (error instanceof SecretEnvsAuthError && error.code === "MFA_REQUIRED") {
1134
- const code = (await deps.prompts.input({
1135
- message: "Authentication code (or a recovery code)"
1136
- })).trim();
1137
- return attempt(code);
1138
- }
1139
- throw error;
1140
- }).catch((error) => {
1035
+ const sdk = new SecretEnvs({ token, baseUrl, fetch: deps.fetch, env: {} });
1036
+ let account;
1037
+ try {
1038
+ account = await sdk.me();
1039
+ } catch (error) {
1141
1040
  if (error instanceof SecretEnvsAuthError) {
1142
1041
  throw new CliError(
1143
- siam ? `${error.message}
1144
- If you sign in to SecretEnvs through SIAM, run \`secretenvs login --token\` instead.` : error.message
1042
+ "The server did not accept that token. Check that you copied all of it, and that it has not expired or been revoked."
1145
1043
  );
1146
1044
  }
1147
1045
  throw error;
1148
- });
1046
+ }
1047
+ const prefix = token.slice(0, 8);
1149
1048
  await writeCredentials(credentialsPath(deps.homeDir), {
1150
1049
  baseUrl,
1151
- token: created.token,
1152
- email: email2,
1153
- prefix: created.prefix
1050
+ token,
1051
+ email: account.email,
1052
+ prefix
1154
1053
  });
1155
- out(deps, `Logged in as ${email2} on ${baseUrl}`);
1054
+ out(deps, `Logged in as ${account.email} on ${baseUrl} (token ${prefix}...)`);
1156
1055
  }
1157
1056
  async function logout(deps) {
1158
1057
  const removed = await deleteCredentials(credentialsPath(deps.homeDir));
@@ -1163,11 +1062,10 @@ async function logout(deps) {
1163
1062
  }
1164
1063
  async function whoami(deps) {
1165
1064
  const { auth, sdk } = await connect(deps);
1166
- const projects = await sdk.listProjects();
1167
- const account = auth.email ?? (auth.source === "environment" ? "unknown (token from SECRETENVS_TOKEN)" : "unknown");
1065
+ const [account, projects] = await Promise.all([sdk.me(), sdk.listProjects()]);
1168
1066
  const token = auth.prefix ? `${auth.prefix}... (${auth.source})` : auth.source;
1169
1067
  out(deps, `Server: ${sdk.toJSON().baseUrl}`);
1170
- out(deps, `Account: ${account}`);
1068
+ out(deps, `Account: ${account.email}`);
1171
1069
  out(deps, `Token: ${token}`);
1172
1070
  out(deps, `Projects: ${projects.length} accessible`);
1173
1071
  }
@@ -1437,7 +1335,7 @@ async function runCommand(deps, command, flags) {
1437
1335
  }
1438
1336
 
1439
1337
  // src/program.ts
1440
- var VERSION = true ? "1.7.0" : "dev";
1338
+ var VERSION = true ? "3.0.0" : "dev";
1441
1339
  function buildProgram(deps, setExitCode) {
1442
1340
  const program = new Command().name("secretenvs").description("Read and manage SecretEnvs variables from the terminal").version(VERSION).configureOutput({
1443
1341
  writeOut: (text) => deps.stdout.write(text),
@@ -1445,13 +1343,10 @@ function buildProgram(deps, setExitCode) {
1445
1343
  }).exitOverride();
1446
1344
  const target = (command) => command.option("-p, --project <slug>", "project slug (or SECRETENVS_PROJECT)").option("-e, --env <slug>", "environment slug (or SECRETENVS_ENV)");
1447
1345
  program.command("login").description(
1448
- "Log in with email and password, or with an API token, and store it"
1449
- ).option("--url <baseUrl>", "server URL, e.g. https://vault.example.com").option(
1450
- "--token",
1451
- "paste an API token created in the web app (for accounts that sign in through SIAM)"
1452
- ).action(
1453
- (options) => login(deps, options)
1454
- );
1346
+ "Store an API token created in the web app (Settings > API tokens)"
1347
+ ).option("--url <baseUrl>", "server URL, e.g. https://vault.example.com").addOption(
1348
+ new Option("--token", "no effect: always asks for a token").hideHelp()
1349
+ ).action((options) => login(deps, options));
1455
1350
  program.command("logout").description("Delete the stored credentials").action(() => logout(deps));
1456
1351
  program.command("whoami").description("Show the current server, account and token").action(() => whoami(deps));
1457
1352
  program.command("projects").description("List accessible projects").action(() => listProjects(deps));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secretenvs/cli",
3
- "version": "1.7.0",
3
+ "version": "3.0.0",
4
4
  "description": "Command-line client for reading and managing SecretEnvs variables.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/ankityadavpurson/secretenvs#readme",
@@ -44,8 +44,8 @@
44
44
  "zod": "^4.1.0"
45
45
  },
46
46
  "devDependencies": {
47
- "@secretenvs/contracts": "1.7.0",
48
- "@secretenvs/sdk": "1.7.0",
47
+ "@secretenvs/contracts": "3.0.0",
48
+ "@secretenvs/sdk": "3.0.0",
49
49
  "@types/cross-spawn": "^6.0.6",
50
50
  "tsup": "^8.5.0",
51
51
  "typescript": "~5.9.3",