@canner-ca/cli 0.5.2 → 0.5.3

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/cli.mjs +96 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canner-ca/cli",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Canner deploy CLI — git push from any terminal, no GitHub required",
5
5
  "type": "module",
6
6
  "bin": {
package/src/cli.mjs CHANGED
@@ -28,7 +28,7 @@ import { randomUUID } from 'node:crypto';
28
28
  import { stdin as input, stdout as output } from 'node:process';
29
29
  import { create as tarCreate } from 'tar';
30
30
 
31
- const VERSION = '0.5.2';
31
+ const VERSION = '0.5.3';
32
32
  const DEFAULT_API_BASE = 'https://api.canner.ca';
33
33
  const CONFIG_DIR = join(homedir(), '.canner');
34
34
  const CREDENTIALS_PATH = join(CONFIG_DIR, 'credentials');
@@ -1179,6 +1179,8 @@ function parseArgs(argv) {
1179
1179
  // canner db ops [<id>] recent operations / one operation
1180
1180
  // canner db roles [<name>] [create|member-of|audit|rotate|rm|write-exclusions …] roles, groups and logins
1181
1181
  // canner db inject [<name>] [<login>|owner|off|on] which credential the app receives
1182
+ // canner db least-privilege [<name>] [--label app] --yes run the app as a limited role; the owner goes to the build only
1183
+ // canner db check [<name>] [--category security|performance] [--strict] [--json] security and performance check
1182
1184
  //
1183
1185
  // Long operations (export, import, migrate, copy) run on the platform: the CLI
1184
1186
  // starts one, then waits and shows progress. Pass --no-wait to return the
@@ -2020,6 +2022,34 @@ async function cmdDb(args) {
2020
2022
  return;
2021
2023
  }
2022
2024
 
2025
+ if (sub === 'check') {
2026
+ // A read-only security and performance check of one database (catalog only, never data). Works on every plan.
2027
+ const label = args._[1] ?? 'primary';
2028
+ if (args.category !== undefined && args.category !== 'security' && args.category !== 'performance') die('--category must be security or performance.', 2);
2029
+ const r = await apiJSON(creds, 'GET', `${base}/databases/${encodeURIComponent(label)}/security-check`);
2030
+ const findings = typeof args.category === 'string' ? r.check.findings.filter((f) => f.category === args.category) : r.check.findings;
2031
+ const warn = findings.filter((f) => f.severity === 'warn').length;
2032
+ const note = findings.length - warn;
2033
+ if (JSON_MODE) emitJSON({ ...r.check, findings, summary: { warn, info: note } });
2034
+ else {
2035
+ info(`${bold(label)} ${warn > 0 ? yellow(`${warn} warning${warn === 1 ? '' : 's'}`) : green('no warnings')} ${dim(`· ${note} note${note === 1 ? '' : 's'}`)}`);
2036
+ for (const category of ['security', 'performance']) {
2037
+ const group = findings.filter((f) => f.category === category);
2038
+ if (group.length === 0) continue;
2039
+ info('');
2040
+ info(bold(category.toUpperCase()));
2041
+ for (const f of group) {
2042
+ const objs = f.objects.length > 0 ? ` ${dim(`[${f.objects.join(', ')}${f.more ? ', …' : ''}]`)}` : '';
2043
+ info(` ${f.severity === 'warn' ? yellow('warn') : dim('info')} ${f.message}${objs}`);
2044
+ }
2045
+ }
2046
+ if (findings.length === 0) info(green('nothing to report'));
2047
+ }
2048
+ // For CI: --strict fails the run when anything is a warning.
2049
+ if (args.strict === true && warn > 0) process.exitCode = 1;
2050
+ return;
2051
+ }
2052
+
2023
2053
  if (sub === 'inject') {
2024
2054
  // Which credential the app receives as DATABASE_URL (or DATABASE_URL_<NAME>): the owner's (the default) or one of your logins.
2025
2055
  const label = args._[1] ?? 'primary';
@@ -2043,6 +2073,26 @@ async function cmdDb(args) {
2043
2073
  return;
2044
2074
  }
2045
2075
 
2076
+ if (sub === 'least-privilege') {
2077
+ // One step to the usual safe arrangement: the app logs in as a limited role, the owner (which can change the schema) is
2078
+ // given to the BUILD only, as DATABASE_URL_MIGRATE, and every migration ledger stays read-only for the app's role.
2079
+ const label = args._[1] ?? 'primary';
2080
+ requireYes(args, 'The app will run as a limited role (it can read and write data but cannot change the schema) from its next deploy, and the database owner will be given to the build only, as DATABASE_URL_MIGRATE: run your migrations there.');
2081
+ const r = await apiJSON(creds, 'POST', `${base}/databases/${encodeURIComponent(label)}/least-privilege`, {
2082
+ confirm: true,
2083
+ ...(typeof args.label === 'string' ? { label: args.label } : {}),
2084
+ });
2085
+ if (JSON_MODE) return emitJSON(r.least_privilege);
2086
+ const lp = r.least_privilege;
2087
+ const envVar = label === 'primary' ? 'DATABASE_URL' : `DATABASE_URL_${label.toUpperCase()}`;
2088
+ info(`${green(lp.role.created ? 'created' : 'using')} the role ${bold(lp.role.label)}; ${bold(envVar)} will carry it from the next deploy`);
2089
+ if (lp.protected_tables.length) info(` kept read-only for it: ${lp.protected_tables.join(', ')}`);
2090
+ for (const t of lp.suggestions ?? []) info(` ${yellow('look at')} ${t} ${dim(`(looks like a ledger; keep it read-only with: canner db roles ${label} write-exclusions ${lp.role.label} ${t})`)}`);
2091
+ info(` ${bold(lp.migration_var)} is available to the build only: run your migrations with it (for example ${dim(`DATABASE_URL=$${lp.migration_var} npx prisma migrate deploy`)}).`);
2092
+ muted('Run it again after your first migration: a ledger created later is also kept read-only for the app, within minutes, without this.');
2093
+ return;
2094
+ }
2095
+
2046
2096
  if (sub === 'roles') {
2047
2097
  const label = args._[1] ?? 'primary';
2048
2098
  const action = args._[2];
@@ -2341,7 +2391,46 @@ async function cmdDb(args) {
2341
2391
  return;
2342
2392
  }
2343
2393
 
2344
- die('usage: canner db instance [show] | create [--pg 17|18] [--pitr] [--window DAYS] | start | stop --yes | destroy --yes', 2);
2394
+ if (action === 'upgrade') {
2395
+ const what = args._[2];
2396
+ const up = `${path}/upgrade`;
2397
+ if (!what || what === 'check') {
2398
+ const body = await apiJSON(creds, 'GET', up);
2399
+ if (JSON_MODE) return emitJSON(body);
2400
+ info(`${bold('Postgres')} ${body.current}${body.targets.length ? dim(` can be upgraded to ${body.targets.join(', ')}`) : dim(' (already on the newest version offered)')}`);
2401
+ for (const b of body.blockers ?? []) info(` ${yellow('blocked')} ${b.message}`);
2402
+ if (body.targets.length && !(body.blockers ?? []).length) muted('Upgrade with `canner db instance upgrade run --yes` (every database on the instance goes offline for a few minutes).');
2403
+ if (body.rollback) info(` ${dim(`the previous version (Postgres ${body.rollback.to_version}) is kept until ${body.rollback.until}: \`canner db instance upgrade rollback --yes\` goes back, \`... finish --yes\` discards it`)}`);
2404
+ return;
2405
+ }
2406
+ if (what === 'run') {
2407
+ requireYes(args, 'Upgrading restarts every database on the instance on a newer Postgres: they are offline for a few minutes. Roles, grants, policies and extensions carry over and the connection strings do not change. Point-in-time recovery starts a fresh window. If it fails, the instance is left as it was.');
2408
+ const done = await startAndWait(creds, base, args, 'POST', up, { ...(args.pg ? { target: String(args.pg) } : {}), confirm: true }, 'upgrading');
2409
+ if (!done) return;
2410
+ if (JSON_MODE) return emitJSON(done);
2411
+ info(`${green('upgraded')} the instance from Postgres ${done.result?.from_pg_version} to ${done.result?.to_pg_version}`);
2412
+ muted('The previous version is kept for 7 days in case you need to roll back: `canner db instance upgrade rollback --yes`.');
2413
+ return;
2414
+ }
2415
+ if (what === 'rollback') {
2416
+ requireYes(args, 'A rollback goes back to the version before the upgrade and LOSES everything written since. Point-in-time recovery starts a fresh window.');
2417
+ const done = await startAndWait(creds, base, args, 'POST', `${up}/rollback`, { confirm: true }, 'rolling back');
2418
+ if (!done) return;
2419
+ if (JSON_MODE) return emitJSON(done);
2420
+ info(`${green('rolled back')} the instance to Postgres ${done.result?.to_pg_version}`);
2421
+ return;
2422
+ }
2423
+ if (what === 'finish') {
2424
+ requireYes(args, 'This discards the previous version, so a rollback is no longer possible.');
2425
+ const body = await apiJSON(creds, 'POST', `${up}/finish`, { confirm: true });
2426
+ if (JSON_MODE) return emitJSON(body);
2427
+ info(`${green('done')}: the previous version was removed`);
2428
+ return;
2429
+ }
2430
+ die('usage: canner db instance upgrade [check] | run [--pg 18] --yes | rollback --yes | finish --yes', 2);
2431
+ }
2432
+
2433
+ die('usage: canner db instance [show] | create [--pg 17|18] [--pitr] [--window DAYS] | start | stop --yes | upgrade [check|run|rollback|finish] | destroy --yes', 2);
2345
2434
  }
2346
2435
 
2347
2436
  if (sub === 'pitr') {
@@ -2439,7 +2528,7 @@ async function cmdDb(args) {
2439
2528
  return;
2440
2529
  }
2441
2530
 
2442
- die(`unknown db subcommand "${sub}". Use: list | init | create | info | url | rm | pg-version | export | import | migrate-in | migrate-out | copy | ops | external | rotate | backup-policy | backup | backups | diff | inspect | alerts | roles | offsite | metrics | monitoring | insights | logs | resources | extensions | tuning | restart | replication | subscriptions | instance | pitr | pitr-restore | move.`, 2);
2531
+ die(`unknown db subcommand "${sub}". Use: list | init | create | info | url | rm | pg-version | export | import | migrate-in | migrate-out | copy | ops | external | rotate | backup-policy | backup | backups | diff | inspect | alerts | roles | least-privilege | offsite | metrics | monitoring | insights | logs | resources | extensions | tuning | restart | replication | subscriptions | instance | pitr | pitr-restore | move.`, 2);
2443
2532
  }
2444
2533
 
2445
2534
  function printHelp() {
@@ -2567,6 +2656,10 @@ ${bold('Databases:')}
2567
2656
  ${bold('db')} roles <name> rm <role> --yes
2568
2657
  ${bold('db')} roles <name> write-exclusions <role> <table>
2569
2658
  Keep one more table SELECT-only for a read-write role, live — no recreation.
2659
+ ${bold('db')} check [<name>] [--category security|performance] [--strict] [--json]
2660
+ A read-only security and performance check of a database (every plan): policies that allow every
2661
+ row, functions anyone can run as their owner, views that bypass row-level security, missing primary
2662
+ keys and indexes, duplicate indexes. --strict fails the run on any warning, for CI.
2570
2663
  ${bold('db')} inject [<name>] [<login> | owner | off | on]
2571
2664
  Which credential the app receives as DATABASE_URL: the owner's (default) or one of your
2572
2665
  logins. Takes effect on the next deploy.