fix(cloud): GH-AS-MEMBER gh wrapper actually runs: the real gh moves off PATH (tools/gh/bin) and tools/bin/gh links to the wrapper; older images migrated at entry-point install

This commit is contained in:
Neil
2026-11-15 22:37:07 -08:00
parent 9d369ff270
commit 36303ac480
5 changed files with 90 additions and 14 deletions
@@ -70,8 +70,10 @@ if [ -n "${GH_VERSION:-}" ]; then
fetch_verified "https://github.com/cli/cli/releases/download/v${GH_VERSION}/gh_${GH_VERSION}_linux_amd64.tar.gz" \
"$GH_SHA256" /tmp/gh.tgz
tar -xzf /tmp/gh.tgz -C /tmp
mkdir -p "$ORCA_HOME/tools/bin"
install -m 0755 "/tmp/gh_${GH_VERSION}_linux_amd64/bin/gh" "$ORCA_HOME/tools/bin/gh"
# The real gh lives off PATH; tools/bin/gh (first on agent PATHs) is the GH-AS-MEMBER wrapper (bin/gh).
mkdir -p "$ORCA_HOME/tools/bin" "$ORCA_HOME/tools/gh/bin"
install -m 0755 "/tmp/gh_${GH_VERSION}_linux_amd64/bin/gh" "$ORCA_HOME/tools/gh/bin/gh"
ln -sfn ../../bin/gh "$ORCA_HOME/tools/bin/gh"
rm -rf /tmp/gh.tgz "/tmp/gh_${GH_VERSION}_linux_amd64"
fi
File diff suppressed because one or more lines are too long
+12 -7
View File
@@ -1,15 +1,20 @@
#!/usr/bin/env bash
# gh for agents in Orca Cloud workspaces (first on orcad's PATH, like git): GitHub actions an agent takes
# in a teammate's turn (gh pr create, comments, reviews) act as that teammate, through their own
# placeholder, swapped at egress for their GitHub user token (never in the VM). Orcad attests the sender
# exactly as for git (orca-git-identity). With no attested teammate, or before the control plane turns this
# on (ORCA_GH_AS_MEMBER=on), gh runs as before: the workspace identity.
self="$(cd "$(dirname "$0")" && pwd)"
# gh for agents in Orca Cloud workspaces: GitHub actions an agent takes in a teammate's turn (gh pr create,
# comments, reviews) act as that teammate, through their own placeholder, swapped at egress for their GitHub
# user token (never in the VM). Orcad attests the sender exactly as for git (orca-git-identity). With no
# attested teammate, or before the control plane turns this on (ORCA_GH_AS_MEMBER=on), gh runs as before:
# the workspace identity.
# Layout: the real gh is /opt/orca/tools/gh/bin/gh (off PATH); /opt/orca/tools/bin/gh, first on every agent
# PATH, is a link to this script. Symlinks are resolved so the link never finds itself.
self="$(readlink -f "$0" 2>/dev/null || echo "$0")"
real="${ORCA_REAL_GH:-}"
if [ -z "$real" ] && [ -x "${ORCA_HOME:-/opt/orca}/tools/gh/bin/gh" ]; then
real="${ORCA_HOME:-/opt/orca}/tools/gh/bin/gh"
fi
if [ -z "$real" ]; then
IFS=: read -r -a dirs <<<"${PATH:-/usr/bin:/bin}"
for dir in "${dirs[@]}" /usr/local/bin /usr/bin; do
if [ "$dir" != "$self" ] && [ -x "$dir/gh" ] && [ "$(cd "$dir" && pwd)" != "$self" ]; then
if [ -x "$dir/gh" ] && [ "$(readlink -f "$dir/gh" 2>/dev/null || echo "$dir/gh")" != "$self" ]; then
real="$dir/gh"
break
fi
@@ -409,6 +409,18 @@ export function entryPointsBehind(image: { stamp: number; marker: boolean } | nu
return image !== null && !image.marker && image.stamp < VM_SCRIPTS_STAMP
}
/**
* GH-AS-MEMBER: images from before the gh wrapper keep the real gh at tools/bin/gh, ahead of bin/ on every agent
* PATH, so the wrapper never ran. Moves it off PATH and links tools/bin/gh to the wrapper (the provision layout).
* Idempotent. ORCA_HOME/SUDO overridable for tests.
*/
export const GH_WRAPPER_LAYOUT = [
'h="${ORCA_HOME:-/opt/orca}"; s="${SUDO-sudo}"',
'if [ -f "$h/tools/bin/gh" ] && [ ! -L "$h/tools/bin/gh" ]; then',
' $s mkdir -p "$h/tools/gh/bin" && $s mv -f "$h/tools/bin/gh" "$h/tools/gh/bin/gh" && $s ln -sfn ../../bin/gh "$h/tools/bin/gh"',
'fi'
].join('\n')
export async function installEntryPoints(deps: { provider: Pick<SandboxProviderApi, 'runCommand'> }, sessionId: string): Promise<void> {
const env = provisionEnv({ manifest: MANIFEST, provisionScript: PROVISION_SCRIPT, vmScripts: VM_SCRIPT_SOURCES, vmScriptsStamp: VM_SCRIPTS_STAMP, buildId: 'sync' })
const scriptEnv = Object.fromEntries(Object.entries(env).filter(([key]) => key.startsWith('ORCA_VM_SCRIPT_')))
@@ -420,6 +432,7 @@ export async function installEntryPoints(deps: { provider: Pick<SandboxProviderA
' sudo install -o root -g root -m 0755 "/tmp/$name.new" "/opt/orca/bin/$name"',
' rm -f "/tmp/$name.new"',
'done',
GH_WRAPPER_LAYOUT,
// Marks this set of entry points as installed (ensureEntryPoints skips the install next time).
`sudo rm -f /opt/orca/bin/.entry-points-* && sudo touch /opt/orca/bin/.entry-points-${entryPointsVersion()}`,
// Records which scripts the image now carries (an older Worker never reinstalls over them).
@@ -1,10 +1,11 @@
// image/vm/gh run for real (bash): without the control plane's switch or an agent turn it is plain gh.
import { execFileSync } from 'node:child_process'
import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync, copyFileSync } from 'node:fs'
import { chmodSync, copyFileSync, lstatSync, mkdirSync, mkdtempSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { afterEach, describe, expect, it } from 'vitest'
import { GH_WRAPPER_LAYOUT } from '../src/golden-images'
const WRAPPER = fileURLToPath(new URL('../image/vm/gh', import.meta.url).href)
const dirs: string[] = []
@@ -41,4 +42,59 @@ describe('VM gh wrapper (GH-AS-MEMBER)', () => {
// No sudo rule / orca-git-identity here: the attestation fails and gh runs as the workspace.
expect(run({ ORCA_GH_AS_MEMBER: 'on', ORCA_AGENT_SESSION_ID: 's1' })).toBe('real gh token=orca-ph-sbx_workspace args=pr create --fill')
})
it('the VM layout: tools/bin/gh (first on PATH) links to the wrapper, which runs the off-PATH real gh, never itself', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-vm-gh-layout-'))
dirs.push(root)
for (const dir of ['bin', 'tools/bin', 'tools/gh/bin']) {
mkdirSync(join(root, dir), { recursive: true })
}
copyFileSync(WRAPPER, join(root, 'bin/gh'))
chmodSync(join(root, 'bin/gh'), 0o755)
symlinkSync('../../bin/gh', join(root, 'tools/bin/gh'))
writeFileSync(join(root, 'tools/gh/bin/gh'), '#!/usr/bin/env bash\necho "real gh token=$GH_TOKEN args=$*"\n')
chmodSync(join(root, 'tools/gh/bin/gh'), 0o755)
const out = execFileSync('gh', ['pr', 'view'], {
env: { PATH: `${root}/tools/bin:${root}/bin:/usr/bin:/bin`, ORCA_HOME: root, GH_TOKEN: 'orca-ph-sbx_workspace' },
encoding: 'utf8',
timeout: 10_000
}).trim()
expect(out).toBe('real gh token=orca-ph-sbx_workspace args=pr view')
})
it('with no off-PATH real gh, a PATH search through the link skips the wrapper itself', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-vm-gh-link-'))
dirs.push(root)
for (const dir of ['bin', 'tools/bin', 'usr-bin']) {
mkdirSync(join(root, dir), { recursive: true })
}
copyFileSync(WRAPPER, join(root, 'bin/gh'))
chmodSync(join(root, 'bin/gh'), 0o755)
symlinkSync('../../bin/gh', join(root, 'tools/bin/gh'))
writeFileSync(join(root, 'usr-bin/gh'), '#!/usr/bin/env bash\necho "real gh args=$*"\n')
chmodSync(join(root, 'usr-bin/gh'), 0o755)
const out = execFileSync('gh', ['auth', 'status'], {
env: { PATH: `${root}/tools/bin:${root}/bin:${root}/usr-bin:/usr/bin:/bin`, ORCA_HOME: root },
encoding: 'utf8',
timeout: 10_000
}).trim()
expect(out).toBe('real gh args=auth status')
})
it('an older image is moved to that layout at entry-point install, once (idempotent)', () => {
const root = mkdtempSync(join(tmpdir(), 'orca-vm-gh-migrate-'))
dirs.push(root)
for (const dir of ['bin', 'tools/bin']) {
mkdirSync(join(root, dir), { recursive: true })
}
copyFileSync(WRAPPER, join(root, 'bin/gh'))
writeFileSync(join(root, 'tools/bin/gh'), '#!/usr/bin/env bash\necho real\n')
chmodSync(join(root, 'tools/bin/gh'), 0o755)
for (let i = 0; i < 2; i++) {
execFileSync('bash', ['-c', GH_WRAPPER_LAYOUT], { env: { PATH: '/usr/bin:/bin', ORCA_HOME: root, SUDO: '' } })
}
expect(lstatSync(join(root, 'tools/bin/gh')).isSymbolicLink()).toBe(true)
expect(readlinkSync(join(root, 'tools/bin/gh'))).toBe('../../bin/gh')
expect(execFileSync(join(root, 'tools/gh/bin/gh'), { encoding: 'utf8' }).trim()).toBe('real')
})
})