@malloydata/malloyyo 0.2.41 → 0.2.43

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 +43 -7
  2. package/dist/index.js +53 -18
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -61,7 +61,10 @@ malloyyo logout main
61
61
 
62
62
  Login is **per-instance** (it authenticates you to a URL, for all datasets on it), so the
63
63
  argument is a *target or URL*, not a dataset — and it's optional when the config is
64
- unambiguous. It uses the instance's OAuth flow (Authorization Code + PKCE, loopback redirect)
64
+ unambiguous. It asks the instance for two permissions, which the consent screen names:
65
+ publishing models, and querying them. (A claude.ai connection to the same instance asks
66
+ only to query, so it cannot publish — and a login stored before publishing had a
67
+ permission of its own needs one more `malloyyo login` to gain it.) It uses the instance's OAuth flow (Authorization Code + PKCE, loopback redirect)
65
68
  and stores a refreshable token in `~/.config/malloyyo/credentials.json` (mode 0600), keyed by
66
69
  instance URL — so you can be logged in to several instances at once. Tokens auto-refresh.
67
70
 
@@ -91,11 +94,43 @@ create it under a slugified variant that later publishes wouldn't find.
91
94
 
92
95
  `publish` exits non-zero on a server-side compile failure, so it's safe to gate CI on.
93
96
 
94
- **Token precedence:** `--token` flag → the `malloyyo_token` env var from config (for CI) →
95
- your `malloyyo login` session. So interactively you just `login` once; in CI you set the env
96
- var and never touch the browser.
97
+ ## API tokens (`MALLOYYO_TOKEN`)
97
98
 
98
- See `docs/model-publishing-design.md` in the repo for the full design.
99
+ For CI, or anywhere there is no browser to sign in with. Mint one in the instance's UI —
100
+ **your name → tokens**, or `<instance-url>/settings/tokens` — and hand it over in the
101
+ environment:
102
+
103
+ ```bash
104
+ export MALLOYYO_TOKEN=myo_main_…
105
+ malloyyo publish # no login, no browser, no --token
106
+ ```
107
+
108
+ Every member of an instance can mint their own; you do not have to be an admin. A token is
109
+ shown **once**, at creation — it is stored hashed, so a lost one is revoked and replaced
110
+ rather than recovered. It carries the scopes you tick:
111
+
112
+ | Scope | What it opens |
113
+ | --- | --- |
114
+ | `publish` | `malloyyo publish` and `malloyyo status` |
115
+ | `mcp` | querying the instance's published models over MCP |
116
+
117
+ A token never carries more than you do. Publishing works on datasets **you own** (admins may
118
+ publish to any), creating a dataset with `--create-dataset` stays admin-only, and every
119
+ request re-checks your account — so revoking the token, or the account, takes effect on the
120
+ next request rather than at expiry. Expiry is your choice at mint time, including **never**:
121
+ a credential that lapses on its own breaks the pipeline at the worst moment, and the token
122
+ list shows when each was last used so a forgotten one is still visible.
123
+
124
+ **Token precedence:** `--token` flag → the `malloyyo_token` env var named in the config →
125
+ `$MALLOYYO_TOKEN` → your `malloyyo login` session. The config's own variable wins over
126
+ `MALLOYYO_TOKEN` because someone publishing to two instances from one shell needs a
127
+ credential per instance, and one variable can only hold one of them. Both variables sit
128
+ above the stored login, so an export in your shell profile quietly shadows `malloyyo login`
129
+ — `login` warns when that variable is set, and any auth failure names the source it used.
130
+
131
+ See [API tokens](https://github.com/malloydata/malloyyo/blob/main/docs/api-tokens.md) for
132
+ the CI recipe, rotation, and a troubleshooting table, and
133
+ `docs/model-publishing-design.md` in the repo for the publish design.
99
134
 
100
135
  ## Malloyyo-hosted instances (`malloyyo cloud`)
101
136
 
@@ -148,8 +183,9 @@ export MALLOYYO_CLIENT_SECRET=...
148
183
 
149
184
  That is the whole of it — there is nothing else to configure.
150
185
 
151
- It is separate from `malloyyo login`, which authenticates *you* to one instance. This one
152
- identifies your account to Malloyyo, and each command trades it for a short-lived access
186
+ It is separate from the two instance credentials — `malloyyo login` and `$MALLOYYO_TOKEN`,
187
+ both of which authenticate *you* to one instance. This one identifies your account to
188
+ Malloyyo itself, and each command trades it for a short-lived access
153
189
  token carrying only the permissions that command needs: a `list` cannot create, and only
154
190
  `secrets set` can write secrets. The trade happens against Malloyyo's own API, so the CLI
155
191
  talks to nothing else. Your secret is held in memory for that request and is never logged,
package/dist/index.js CHANGED
@@ -2881,7 +2881,7 @@ function clearCreds(url6) {
2881
2881
  }
2882
2882
 
2883
2883
  // package.json
2884
- var version = "0.2.41";
2884
+ var version = "0.2.43";
2885
2885
 
2886
2886
  // src/http.ts
2887
2887
  var USER_AGENT = `malloyyo/${version}`;
@@ -2902,6 +2902,7 @@ async function apiFetch(url6, init = {}) {
2902
2902
 
2903
2903
  // src/oauth.ts
2904
2904
  var LOGIN_TIMEOUT_MS = 5 * 60 * 1e3;
2905
+ var LOGIN_SCOPE = "mcp publish";
2905
2906
  async function discover(baseUrl) {
2906
2907
  const res = await apiFetch(`${baseUrl}/api/oauth/discovery/authorization-server`);
2907
2908
  if (!res.ok) throw new Error(`OAuth discovery failed at ${baseUrl}: ${res.status} ${res.statusText}`);
@@ -3021,7 +3022,11 @@ async function login(baseUrl, opts = {}) {
3021
3022
  redirect_uri: redirectUri,
3022
3023
  code_challenge: challenge,
3023
3024
  code_challenge_method: "S256",
3024
- scope: "mcp",
3025
+ // What this CLI does: publish models, and query them (`malloyyo mcp`
3026
+ // against a hosted instance). A claude.ai connection asks for "mcp"
3027
+ // alone and cannot publish — so a login here is not interchangeable
3028
+ // with one, and must say so.
3029
+ scope: LOGIN_SCOPE,
3025
3030
  state
3026
3031
  }).toString();
3027
3032
  if (opts.noBrowser || browserless()) {
@@ -3063,6 +3068,13 @@ async function login(baseUrl, opts = {}) {
3063
3068
  expiresAt: Date.now() + (grant.expires_in ?? 86400) * 1e3
3064
3069
  };
3065
3070
  saveCreds(baseUrl, creds);
3071
+ if (process.env[TOKEN_ENV]) {
3072
+ console.log(
3073
+ `
3074
+ Note: $${TOKEN_ENV} is set in this shell and takes precedence over the
3075
+ login just stored. Unset it to use this login.`
3076
+ );
3077
+ }
3066
3078
  return creds;
3067
3079
  } finally {
3068
3080
  close();
@@ -3090,14 +3102,21 @@ async function refresh(baseUrl, creds) {
3090
3102
  saveCreds(baseUrl, updated);
3091
3103
  return updated;
3092
3104
  }
3093
- function tokenSource(target, opts) {
3105
+ var TOKEN_ENV = "MALLOYYO_TOKEN";
3106
+ function looksLikeInstanceToken(value) {
3107
+ return /^myo_[a-z0-9]+_[A-Za-z0-9_-]{20,}$/.test(value);
3108
+ }
3109
+ function tokenSource(target, opts, env = process.env) {
3094
3110
  if (opts.tokenFlag) return "flag";
3095
- if (target.tokenEnv && process.env[target.tokenEnv]) return "env";
3111
+ if (target.tokenEnv && env[target.tokenEnv]) return "env";
3112
+ if (env[TOKEN_ENV]) return "global-env";
3096
3113
  return "login";
3097
3114
  }
3098
- async function getAccessToken(target, opts) {
3115
+ async function getAccessToken(target, opts, env = process.env) {
3099
3116
  if (opts.tokenFlag) return opts.tokenFlag;
3100
- if (target.tokenEnv && process.env[target.tokenEnv]) return process.env[target.tokenEnv];
3117
+ if (target.tokenEnv && env[target.tokenEnv]) return env[target.tokenEnv];
3118
+ const ambient = env[TOKEN_ENV];
3119
+ if (ambient) return ambient;
3101
3120
  let creds = loadCreds(target.url);
3102
3121
  if (!creds) {
3103
3122
  throw new Error(`Not authenticated for ${target.url}.
@@ -5337,20 +5356,34 @@ ${SECRETS_SET_HELP}
5337
5356
  function shortSha(sha) {
5338
5357
  return sha ? sha.slice(0, 7) : "";
5339
5358
  }
5340
- function authHint(status2, t, source) {
5359
+ function authHint(status2, t, source, foreignValue = false) {
5341
5360
  const login2 = `malloyyo login ${t.name}`;
5361
+ const mint = `${t.url}/settings/tokens`;
5342
5362
  if (status2 === 403) {
5343
- return `
5344
- The token is valid, but that account isn't an admin on ${t.url} \u2014
5345
- publishing is admin-only. Ask an admin there to grant access.`;
5346
- }
5363
+ const why = `
5364
+ The credential is valid, but it isn't allowed to do this on ${t.url}.
5365
+ The model surface needs the "publish" scope, on an account that owns the
5366
+ dataset (or is an admin there).`;
5367
+ return source === "login" ? why + `
5368
+ A login stored before that scope existed carries only "mcp". Run: ${login2}` : why + `
5369
+ Mint a token carrying it at: ${mint}`;
5370
+ }
5371
+ const wrongSecret = foreignValue ? `
5372
+ (that value isn't shaped like a Malloyyo token \u2014 is the variable still
5373
+ set to something else, like a warehouse password?)` : "";
5347
5374
  switch (source) {
5348
5375
  case "flag":
5349
5376
  return `
5350
- That token came from --token. Drop the flag and run: ${login2}`;
5377
+ That token came from --token. Mint one at ${mint}, or run: ${login2}${wrongSecret}`;
5351
5378
  case "env":
5352
5379
  return `
5353
- That token came from $${t.tokenEnv}. Re-issue it, or unset it and run: ${login2}`;
5380
+ That token came from $${t.tokenEnv}. Mint a replacement at ${mint},
5381
+ or unset it and run: ${login2}${wrongSecret}`;
5382
+ case "global-env":
5383
+ return `
5384
+ That token came from $${TOKEN_ENV}. It may be revoked, or minted for a
5385
+ different instance. Mint one for ${t.url} at: ${mint}
5386
+ Or unset the variable and run: ${login2}${wrongSecret}`;
5354
5387
  default:
5355
5388
  return `
5356
5389
  Your saved login for ${t.url} is expired or revoked.
@@ -5382,9 +5415,10 @@ function failureHint(out, t) {
5382
5415
  return "";
5383
5416
  }
5384
5417
  }
5385
- function requestFailed(what, res, out, t, source) {
5418
+ function requestFailed(what, res, out, t, source, bearer) {
5386
5419
  const detail = out.error ?? `${res.status} ${res.statusText}`;
5387
- const hint = res.status === 401 || res.status === 403 ? authHint(res.status, t, source) : failureHint(out, t);
5420
+ const foreign = (source === "env" || source === "global-env") && !!bearer && !looksLikeInstanceToken(bearer);
5421
+ const hint = res.status === 401 || res.status === 403 ? authHint(res.status, t, source, foreign) : failureHint(out, t);
5388
5422
  return new Error(`${what} failed: ${detail}${hint}`);
5389
5423
  }
5390
5424
  var PUBLISH_HELP = `Target resolution:
@@ -5397,7 +5431,8 @@ var PUBLISH_HELP = `Target resolution:
5397
5431
 
5398
5432
  -i takes what \`login\` takes: a URL, or the name of a configured target
5399
5433
  whose url should be borrowed. Authenticate the same way as always:
5400
- \`malloyyo login <url>\`, or pass --token.`;
5434
+ \`malloyyo login <url>\`, or set $MALLOYYO_TOKEN (mint one at
5435
+ <url>/settings/tokens \u2014 that is what CI wants), or pass --token.`;
5401
5436
  async function publish(target, dir, opts) {
5402
5437
  const root = resolve3(dir);
5403
5438
  const t = resolvePublishTarget(root, target, {
@@ -5440,7 +5475,7 @@ async function publish(target, dir, opts) {
5440
5475
  });
5441
5476
  const out = await res.json().catch(() => ({}));
5442
5477
  if (!res.ok || !out.ok) {
5443
- throw requestFailed("publish", res, out, t, source);
5478
+ throw requestFailed("publish", res, out, t, source, bearer);
5444
5479
  }
5445
5480
  if (out.created) {
5446
5481
  console.log(`\u2713 created dataset ${out.dataset ?? t.dataset} (private) \u2014 ${t.url}/datasets/${out.dataset ?? t.dataset}`);
@@ -5458,7 +5493,7 @@ async function status(target, opts) {
5458
5493
  });
5459
5494
  if (!res.ok) {
5460
5495
  const body = await res.json().catch(() => ({}));
5461
- throw requestFailed("status", res, body, t, source);
5496
+ throw requestFailed("status", res, body, t, source, bearer);
5462
5497
  }
5463
5498
  const s = await res.json();
5464
5499
  const git = s.git;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloyyo",
3
- "version": "0.2.41",
3
+ "version": "0.2.43",
4
4
  "description": "Publish Malloy models to a Malloyyo instance",
5
5
  "license": "MIT",
6
6
  "repository": {