fix(cli): name the pusher's identity where a push used to leave it to the server

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
hugocasa
2026-10-19 13:24:19 +02:00
co-authored by Claude Opus 5.5
parent 8558de9802
commit 62443fd39e
4 changed files with 92 additions and 18 deletions
+21 -8
View File
@@ -30,7 +30,11 @@ import { FSFSElement, elementsToMap, ignoreF } from "../sync/sync.ts";
import { Flow } from "../../../gen/types.gen.ts";
import { applyExtraPermsDiff } from "../../core/extra_perms.ts";
import type { PermissionedAsContext } from "../../core/permissioned_as.ts";
import { buildPermissionedAsContext } from "../../core/permissioned_as.ts";
import {
buildPermissionedAsContext,
legacyPusherEmail,
warnRunAsNotCarried,
} from "../../core/permissioned_as.ts";
import {
collectPathScriptPaths,
replaceInlineScripts,
@@ -260,14 +264,23 @@ export async function pushFlow(
on_behalf_of?: string;
preserve_on_behalf_of?: boolean;
} = {};
if (permissionedAsContext?.userIsAdminOrDeployer && hasOnBehalfOf) {
if (flow && flow.on_behalf_of_email) {
preserveFields.on_behalf_of_email = flow.on_behalf_of_email;
preserveFields.on_behalf_of = (flow as any).on_behalf_of;
preserveFields.preserve_on_behalf_of = true;
log.info(`Preserving ${flow.on_behalf_of_email} as on_behalf_of for flow ${remotePath}`);
if (permissionedAsContext && hasOnBehalfOf) {
if (flow?.on_behalf_of_email) {
if (permissionedAsContext.userIsAdminOrDeployer) {
preserveFields.on_behalf_of_email = flow.on_behalf_of_email;
preserveFields.on_behalf_of = (flow as any).on_behalf_of;
preserveFields.preserve_on_behalf_of = true;
log.info(`Preserving ${flow.on_behalf_of_email} as on_behalf_of for flow ${remotePath}`);
} else {
// The run-as identity becomes the pusher's, as the pre-push check announced. It has
// to be named: a flow deployed with none runs without one.
preserveFields.on_behalf_of_email = permissionedAsContext.userEmail;
}
} else if (!flow && !localFlow.on_behalf_of_email) {
warnRunAsNotCarried("flow", remotePath);
}
// On create: backend applies folder defaults — no client-side resolution needed
} else if (!permissionedAsContext && localFlow.on_behalf_of_email) {
preserveFields.on_behalf_of_email = await legacyPusherEmail(workspace);
}
// extra_perms is synced independently via /acls/* (see applyExtraPermsDiff)
+25 -10
View File
@@ -7,7 +7,11 @@ import {
validatePath,
} from "../../core/context.ts";
import type { PermissionedAsContext } from "../../core/permissioned_as.ts";
import { buildPermissionedAsContext } from "../../core/permissioned_as.ts";
import {
buildPermissionedAsContext,
legacyPusherEmail,
warnRunAsNotCarried,
} from "../../core/permissioned_as.ts";
import { applyExtraPermsDiff } from "../../core/extra_perms.ts";
import { writeFile, stat, mkdir } from "node:fs/promises";
import { Buffer } from "node:buffer";
@@ -647,16 +651,27 @@ export async function handleFile(
// workspace tarball strips it); it only ever travels back from the remote row.
delete (typed as any)?.on_behalf_of;
if (permissionedAsContext?.userIsAdminOrDeployer && hasOnBehalfOf) {
if (remote && remote.on_behalf_of_email) {
requestBodyCommon.on_behalf_of_email = remote.on_behalf_of_email;
(requestBodyCommon as any).on_behalf_of = (
remote as any
).on_behalf_of;
(requestBodyCommon as any).preserve_on_behalf_of = true;
log.info(`Preserving ${remote.on_behalf_of_email} as on_behalf_of for script ${remotePath}`);
if (permissionedAsContext && hasOnBehalfOf) {
if (remote?.on_behalf_of_email) {
if (permissionedAsContext.userIsAdminOrDeployer) {
requestBodyCommon.on_behalf_of_email = remote.on_behalf_of_email;
(requestBodyCommon as any).on_behalf_of = (
remote as any
).on_behalf_of;
(requestBodyCommon as any).preserve_on_behalf_of = true;
log.info(`Preserving ${remote.on_behalf_of_email} as on_behalf_of for script ${remotePath}`);
} else {
// The run-as identity becomes the pusher's, as the pre-push check announced. It has
// to be named: a script deployed with none runs without one.
requestBodyCommon.on_behalf_of_email = permissionedAsContext.userEmail;
}
} else if (!remote && !typed?.on_behalf_of_email) {
warnRunAsNotCarried("script", remotePath);
}
// On create: backend applies folder defaults — no client-side resolution needed
} else if (!permissionedAsContext && typed?.on_behalf_of_email) {
requestBodyCommon.on_behalf_of_email = await legacyPusherEmail(
workspace.workspaceId
);
}
if (remote) {
+14
View File
@@ -542,6 +542,7 @@ async function mergeWorkspaces(
}
let successCount = 0;
let failCount = 0;
let mergerEmail: string | undefined;
// Datatable migrations deployed (not deleted) into the target. Deploying a
// migration only upserts its definition — the target schema is unchanged until
// the migration is run — so offer to run them afterwards (like the push path).
@@ -578,6 +579,19 @@ async function mergeWorkspaces(
diff.path,
workspaceFrom
);
} else if (
(diff.kind === "script" || diff.kind === "flow") &&
(await getOnBehalfOf(
provider,
diff.kind as DeployKind,
diff.path,
workspaceFrom
))
) {
// Without the flag every item is deployed as the merger, and a script or flow has to
// be told so: one deployed naming no identity runs without one.
mergerEmail ??= (await wmill.whoami({ workspace: workspaceTo })).email;
onBehalfOf = mergerEmail;
}
result = await deployItem(
+32
View File
@@ -50,6 +50,38 @@ export async function buildPermissionedAsContext(
};
}
const pusherEmails = new Map<string, Promise<string>>();
/**
* Below syncBehavior v1 a push hands every script or flow it deploys to whoever ran it. The
* backend keeps the identity a deploy names, so the address a v0 file carries is replaced by
* the pusher's own: sent as it is, it would stay, or fail to resolve in another workspace.
*/
export function legacyPusherEmail(workspace: string): Promise<string> {
let email = pusherEmails.get(workspace);
if (!email) {
email = wmill.whoami({ workspace }).then((u) => u.email);
pusherEmails.set(workspace, email);
}
return email;
}
/**
* From syncBehavior v1 a script or flow file says only *that* it runs on behalf of someone,
* never who, so a push creating it has no identity to send. It is created without one unless
* a folder `default_permissioned_as` rule matches; the pusher is not substituted, since a
* deploy token is often an admin the runnable never ran as.
*/
export function warnRunAsNotCarried(kind: "script" | "flow", path: string): void {
log.warn(
colors.yellow(
`${kind} ${path} runs on behalf of someone in the source, but the file does not say who: ` +
`it is created without a run-as identity unless a folder default_permissioned_as rule matches. ` +
`Set one with \`wmill ${kind} set-permissioned-as ${path} <email>\`.`
)
);
}
async function ensureUserCache(
workspace: string,
cache: Map<string, { username: string; email: string }>