Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions freeform/modules/claude-self-hosted-runner/files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ logged in as me." to `~/.claude/CLAUDE.md`, because sessions use `gh` only
when your instructions say so. Without a login, sessions use the GitHub MCP
tools, which reach only the repositories attached to the session.

To read and search the rest of GitHub (issues, pull requests, comments,
code and CI runs in any repository you can read), sessions use `gh-read`, a
read-only `gh` that uses your Coder GitHub login (`coder external-auth
access-token github`). No `gh auth login` is needed: link GitHub once at
`<coder URL>/external-auth/github`, and reconnect there if it expires.
`/etc/claude-code/CLAUDE.md` tells every session about it. `gh-read`
refuses commands that write, but that only guards against mistakes: the
token can do whatever the GitHub app behind Coder's external auth allows.

SSH remotes use this workspace's SSH agent: your forwarded agent, or your
Coder key, which is the same in all your workspaces. SSH pushes act as you
in every session. The workspace adds GitHub's host keys to
Expand Down
127 changes: 127 additions & 0 deletions freeform/modules/claude-self-hosted-runner/files/gh-read
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/env bash
# gh-read: read-only GitHub CLI for Claude Code runner sessions.
#
# Sessions get a proxied `gh` that only reaches the repositories attached to
# the session. This wrapper runs the real gh with the workspace owner's own
# GitHub token from Coder external auth, so a session can read issues, pull
# requests, comments, code and CI runs anywhere on GitHub, and search it.
#
# It accepts only read commands. That is a guard against mistakes, not a
# security boundary: what the token itself may do is set by the GitHub app
# behind Coder's "github" external-auth provider.
set -euo pipefail

prog=gh-read
provider="${GH_READ_PROVIDER:-github}"

die() {
echo "$prog: $*" >&2
exit 2
}

usage() {
cat <<'EOF'
gh-read: read-only gh using your own GitHub login from Coder.

Allowed:
gh-read api [-X GET] <endpoint> [-f/-F params] [--jq ...] [--paginate]
gh-read api graphql -f query='query { ... }'
gh-read search issues|prs|code|commits|repos ...
gh-read issue view|list|status ... gh-read pr view|list|diff|checks|status ...
gh-read repo view|list ... gh-read run view|list ...
gh-read release view|list ... gh-read workflow view|list ...
gh-read label list ... gh-read gist view|list ...

Examples:
gh-read issue view 123 -R socktainer/socktainer --comments
gh-read pr diff 45 -R libracoder/kiri
gh-read api repos/docker/compose/issues/11658/comments --jq '.[].body'
gh-read search issues --repo docker/compose mutagen
EOF
}

[ $# -ge 1 ] || { usage; exit 2; }

cmd="$1"
sub="${2:-}"

check_api() {
shift # "api"
local method="" has_fields=false endpoint="" a
while [ $# -gt 0 ]; do
a="$1"
case "$a" in
-X | --method) method="${2:-}"; shift ;;
-X* ) method="${a#-X}" ;;
--method=*) method="${a#--method=}" ;;
-f | -F | --field | --raw-field) has_fields=true; shift ;;
-f* | -F* | --field=* | --raw-field=*) has_fields=true ;;
--input | --input=*) die "--input is not allowed" ;;
-H | --header | -q | --jq | -t | --template | --cache | --hostname | -p | --preview) shift ;;
-*) ;;
*) if [ -z "$endpoint" ]; then endpoint="$a"; fi ;;
esac
shift
done
method="$(printf '%s' "$method" | tr '[:lower:]' '[:upper:]')"
if [ "$endpoint" = "graphql" ]; then
# gh api graphql always POSTs; allow queries, refuse mutations.
if printf '%s\n' "${ORIG_ARGS[@]}" | grep -qiE '(^|[^a-z_])mutation([^a-z_]|$)'; then
die "GraphQL mutations are not allowed"
fi
return 0
fi
case "$method" in
"") if $has_fields; then die "-f/-F without -X GET would POST; add -X GET"; fi ;;
GET | HEAD) ;;
*) die "method $method is not allowed; gh-read only reads" ;;
esac
}

ORIG_ARGS=("$@")
case "$cmd" in
-h | --help | help) usage; exit 0 ;;
api) check_api "$@" ;;
search) ;;
issue) [[ "$sub" =~ ^(view|list|status)$ ]] || die "gh issue $sub is not allowed" ;;
pr) [[ "$sub" =~ ^(view|list|diff|checks|status)$ ]] || die "gh pr $sub is not allowed" ;;
repo | release | workflow | gist) [[ "$sub" =~ ^(view|list)$ ]] || die "gh $cmd $sub is not allowed" ;;
run) [[ "$sub" =~ ^(view|list)$ ]] || die "gh run $sub is not allowed" ;;
label) [ "$sub" = list ] || die "gh label $sub is not allowed" ;;
*) die "gh $cmd is not allowed (run gh-read --help)" ;;
esac

# Token source, in order:
# 1. Coder external auth, in a Coder workspace (the owner's own login).
# 2. Elsewhere (for example a macOS runner), a read-only token kept for this
# machine: the macOS Keychain item named by GH_READ_KEYCHAIN_SERVICE, or
# the file GH_READ_TOKEN_FILE (mode 600). Make it a fine-grained token
# with "Public repositories (read-only)" and no other permissions.
token_file="${GH_READ_TOKEN_FILE:-$HOME/.config/gh-read/token}"
keychain_service="${GH_READ_KEYCHAIN_SERVICE:-gh-read-token}"
token=""
if [ -n "${CODER_AGENT_TOKEN:-}${CODER_AGENT_URL:-}" ] && coder_bin="$(command -v coder)"; then
if ! token="$("$coder_bin" external-auth access-token "$provider" 2>/dev/null)" || [ -z "$token" ] || [[ "$token" == http* ]]; then
url="${CODER_AGENT_URL:-https://coder.ddev.com/}"
die "no GitHub login in Coder for this workspace's owner. The owner must link GitHub once at ${url%/}/external-auth/${provider} (or reconnect it if it expired), then retry."
fi
elif command -v security >/dev/null 2>&1 &&
token="$(security find-generic-password -s "$keychain_service" -w 2>/dev/null)" && [ -n "$token" ]; then
:
elif [ -r "$token_file" ]; then
token="$(tr -d '[:space:]' <"$token_file")"
fi
[ -n "$token" ] || die "no GitHub token: not in a Coder workspace, no Keychain item \"$keychain_service\", and no $token_file. Ask the machine's owner to set one up."

# The real gh, not the session's proxied shim, with an empty config dir so
# the machine's own gh login and aliases play no part.
real_gh=""
for g in /usr/bin/gh /usr/local/bin/gh /opt/homebrew/bin/gh /home/linuxbrew/.linuxbrew/bin/gh; do
[ -x "$g" ] && { real_gh="$g"; break; }
done
[ -n "$real_gh" ] || die "gh is not installed"

cfg="$(mktemp -d)"
trap 'rm -rf "$cfg"' EXIT
GH_CONFIG_DIR="$cfg" GH_TOKEN="$token" GH_PROMPT_DISABLED=1 GH_NO_UPDATE_NOTIFIER=1 \
"$real_gh" "$@"
20 changes: 20 additions & 0 deletions freeform/modules/claude-self-hosted-runner/files/managed-CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Reading GitHub from this workspace

The session's `gh` and GitHub MCP tools reach only the repositories attached
to the session. To read or search anything else on GitHub, use `gh-read`
instead of `add_repo`, WebFetch, or a clone. It runs the real `gh` with the
workspace owner's own GitHub login from Coder, and accepts only read
commands:

- `gh-read issue view 123 -R owner/repo --comments`, `gh-read pr view|diff|checks 45 -R owner/repo`
- `gh-read issue list|pr list -R owner/repo ...`, `gh-read run view|list -R owner/repo ...`
- `gh-read search issues|prs|code|commits|repos ...`
- `gh-read api <endpoint> [-X GET -f key=value] [--jq ...] [--paginate]`, and `gh-read api graphql` for queries

`gh-read --help` lists the rest. Use it for public repositories anywhere and
for private ones the owner can read. For anything that writes (comments,
reviews, pushes, labels), use the session's own `gh` or GitHub MCP tools,
within the repositories attached to the session.

If `gh-read` says there is no GitHub login in Coder, tell the user to link
GitHub at the URL it prints and carry on without it.
4 changes: 4 additions & 0 deletions freeform/modules/claude-self-hosted-runner/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ locals {
# reads, including the ones a runner starts with its own config directory.
# Claude Code skips it when claude.ai delivers server-managed settings.
managed_settings = jsonencode({
permissions = {
# gh-read refuses anything that writes; see files/gh-read.
allow = ["Bash(gh-read:*)"]
}
hooks = {
SessionStart = [{
# Not "compact" or "clear": by then the changes are the session's own.
Expand Down
8 changes: 8 additions & 0 deletions freeform/modules/claude-self-hosted-runner/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ output "startup_script" {
mkdir -p ~/.claude-runner
chmod 700 ~/.claude-runner
echo '${base64encode(file("${path.module}/files/README.md"))}' | base64 -d > ~/.claude-runner/README.md
# Read-only gh with the owner's Coder GitHub login, for repositories
# outside the session's own; /etc/claude-code/CLAUDE.md tells sessions
# about it. Neither needs the environment secret.
mkdir -p ~/.local/bin
echo '${base64encode(file("${path.module}/files/gh-read"))}' | base64 -d > ~/.local/bin/gh-read
chmod +x ~/.local/bin/gh-read
sudo mkdir -p /etc/claude-code
echo '${base64encode(file("${path.module}/files/managed-CLAUDE.md"))}' | base64 -d | sudo tee /etc/claude-code/CLAUDE.md >/dev/null
if [ "$CLAUDE_REMOTE_CONTROL_ENABLED" = "true" ]; then
CLAUDE_RUNNER_MSG="✗ Claude Code self-hosted runner NOT started: Claude Code remote control is also enabled, and the runner rewrites ~/.gitconfig before every session. Turn one of them off."
elif [ ! -s "${local.secret_file}" ]; then
Expand Down
9 changes: 9 additions & 0 deletions modules/claude-self-hosted-runner/files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ logged in as me." to `~/.claude/CLAUDE.md`, because sessions use `gh` only
when your instructions say so. Without a login, sessions use the GitHub MCP
tools, which reach only the repositories attached to the session.

To read and search the rest of GitHub (issues, pull requests, comments,
code and CI runs in any repository you can read), sessions use `gh-read`, a
read-only `gh` that uses your Coder GitHub login (`coder external-auth
access-token github`). No `gh auth login` is needed: link GitHub once at
`<coder URL>/external-auth/github`, and reconnect there if it expires.
`/etc/claude-code/CLAUDE.md` tells every session about it. `gh-read`
refuses commands that write, but that only guards against mistakes: the
token can do whatever the GitHub app behind Coder's external auth allows.

SSH remotes use this workspace's SSH agent: your forwarded agent, or your
Coder key, which is the same in all your workspaces. SSH pushes act as you
in every session. The workspace adds GitHub's host keys to
Expand Down
127 changes: 127 additions & 0 deletions modules/claude-self-hosted-runner/files/gh-read
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/env bash
# gh-read: read-only GitHub CLI for Claude Code runner sessions.
#
# Sessions get a proxied `gh` that only reaches the repositories attached to
# the session. This wrapper runs the real gh with the workspace owner's own
# GitHub token from Coder external auth, so a session can read issues, pull
# requests, comments, code and CI runs anywhere on GitHub, and search it.
#
# It accepts only read commands. That is a guard against mistakes, not a
# security boundary: what the token itself may do is set by the GitHub app
# behind Coder's "github" external-auth provider.
set -euo pipefail

prog=gh-read
provider="${GH_READ_PROVIDER:-github}"

die() {
echo "$prog: $*" >&2
exit 2
}

usage() {
cat <<'EOF'
gh-read: read-only gh using your own GitHub login from Coder.

Allowed:
gh-read api [-X GET] <endpoint> [-f/-F params] [--jq ...] [--paginate]
gh-read api graphql -f query='query { ... }'
gh-read search issues|prs|code|commits|repos ...
gh-read issue view|list|status ... gh-read pr view|list|diff|checks|status ...
gh-read repo view|list ... gh-read run view|list ...
gh-read release view|list ... gh-read workflow view|list ...
gh-read label list ... gh-read gist view|list ...

Examples:
gh-read issue view 123 -R socktainer/socktainer --comments
gh-read pr diff 45 -R libracoder/kiri
gh-read api repos/docker/compose/issues/11658/comments --jq '.[].body'
gh-read search issues --repo docker/compose mutagen
EOF
}

[ $# -ge 1 ] || { usage; exit 2; }

cmd="$1"
sub="${2:-}"

check_api() {
shift # "api"
local method="" has_fields=false endpoint="" a
while [ $# -gt 0 ]; do
a="$1"
case "$a" in
-X | --method) method="${2:-}"; shift ;;
-X* ) method="${a#-X}" ;;
--method=*) method="${a#--method=}" ;;
-f | -F | --field | --raw-field) has_fields=true; shift ;;
-f* | -F* | --field=* | --raw-field=*) has_fields=true ;;
--input | --input=*) die "--input is not allowed" ;;
-H | --header | -q | --jq | -t | --template | --cache | --hostname | -p | --preview) shift ;;
-*) ;;
*) if [ -z "$endpoint" ]; then endpoint="$a"; fi ;;
esac
shift
done
method="$(printf '%s' "$method" | tr '[:lower:]' '[:upper:]')"
if [ "$endpoint" = "graphql" ]; then
# gh api graphql always POSTs; allow queries, refuse mutations.
if printf '%s\n' "${ORIG_ARGS[@]}" | grep -qiE '(^|[^a-z_])mutation([^a-z_]|$)'; then
die "GraphQL mutations are not allowed"
fi
return 0
fi
case "$method" in
"") if $has_fields; then die "-f/-F without -X GET would POST; add -X GET"; fi ;;
GET | HEAD) ;;
*) die "method $method is not allowed; gh-read only reads" ;;
esac
}

ORIG_ARGS=("$@")
case "$cmd" in
-h | --help | help) usage; exit 0 ;;
api) check_api "$@" ;;
search) ;;
issue) [[ "$sub" =~ ^(view|list|status)$ ]] || die "gh issue $sub is not allowed" ;;
pr) [[ "$sub" =~ ^(view|list|diff|checks|status)$ ]] || die "gh pr $sub is not allowed" ;;
repo | release | workflow | gist) [[ "$sub" =~ ^(view|list)$ ]] || die "gh $cmd $sub is not allowed" ;;
run) [[ "$sub" =~ ^(view|list)$ ]] || die "gh run $sub is not allowed" ;;
label) [ "$sub" = list ] || die "gh label $sub is not allowed" ;;
*) die "gh $cmd is not allowed (run gh-read --help)" ;;
esac

# Token source, in order:
# 1. Coder external auth, in a Coder workspace (the owner's own login).
# 2. Elsewhere (for example a macOS runner), a read-only token kept for this
# machine: the macOS Keychain item named by GH_READ_KEYCHAIN_SERVICE, or
# the file GH_READ_TOKEN_FILE (mode 600). Make it a fine-grained token
# with "Public repositories (read-only)" and no other permissions.
token_file="${GH_READ_TOKEN_FILE:-$HOME/.config/gh-read/token}"
keychain_service="${GH_READ_KEYCHAIN_SERVICE:-gh-read-token}"
token=""
if [ -n "${CODER_AGENT_TOKEN:-}${CODER_AGENT_URL:-}" ] && coder_bin="$(command -v coder)"; then
if ! token="$("$coder_bin" external-auth access-token "$provider" 2>/dev/null)" || [ -z "$token" ] || [[ "$token" == http* ]]; then
url="${CODER_AGENT_URL:-https://coder.ddev.com/}"
die "no GitHub login in Coder for this workspace's owner. The owner must link GitHub once at ${url%/}/external-auth/${provider} (or reconnect it if it expired), then retry."
fi
elif command -v security >/dev/null 2>&1 &&
token="$(security find-generic-password -s "$keychain_service" -w 2>/dev/null)" && [ -n "$token" ]; then
:
elif [ -r "$token_file" ]; then
token="$(tr -d '[:space:]' <"$token_file")"
fi
[ -n "$token" ] || die "no GitHub token: not in a Coder workspace, no Keychain item \"$keychain_service\", and no $token_file. Ask the machine's owner to set one up."

# The real gh, not the session's proxied shim, with an empty config dir so
# the machine's own gh login and aliases play no part.
real_gh=""
for g in /usr/bin/gh /usr/local/bin/gh /opt/homebrew/bin/gh /home/linuxbrew/.linuxbrew/bin/gh; do
[ -x "$g" ] && { real_gh="$g"; break; }
done
[ -n "$real_gh" ] || die "gh is not installed"

cfg="$(mktemp -d)"
trap 'rm -rf "$cfg"' EXIT
GH_CONFIG_DIR="$cfg" GH_TOKEN="$token" GH_PROMPT_DISABLED=1 GH_NO_UPDATE_NOTIFIER=1 \
"$real_gh" "$@"
20 changes: 20 additions & 0 deletions modules/claude-self-hosted-runner/files/managed-CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Reading GitHub from this workspace

The session's `gh` and GitHub MCP tools reach only the repositories attached
to the session. To read or search anything else on GitHub, use `gh-read`
instead of `add_repo`, WebFetch, or a clone. It runs the real `gh` with the
workspace owner's own GitHub login from Coder, and accepts only read
commands:

- `gh-read issue view 123 -R owner/repo --comments`, `gh-read pr view|diff|checks 45 -R owner/repo`
- `gh-read issue list|pr list -R owner/repo ...`, `gh-read run view|list -R owner/repo ...`
- `gh-read search issues|prs|code|commits|repos ...`
- `gh-read api <endpoint> [-X GET -f key=value] [--jq ...] [--paginate]`, and `gh-read api graphql` for queries

`gh-read --help` lists the rest. Use it for public repositories anywhere and
for private ones the owner can read. For anything that writes (comments,
reviews, pushes, labels), use the session's own `gh` or GitHub MCP tools,
within the repositories attached to the session.

If `gh-read` says there is no GitHub login in Coder, tell the user to link
GitHub at the URL it prints and carry on without it.
4 changes: 4 additions & 0 deletions modules/claude-self-hosted-runner/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ locals {
# reads, including the ones a runner starts with its own config directory.
# Claude Code skips it when claude.ai delivers server-managed settings.
managed_settings = jsonencode({
permissions = {
# gh-read refuses anything that writes; see files/gh-read.
allow = ["Bash(gh-read:*)"]
}
hooks = {
SessionStart = [{
# Not "compact" or "clear": by then the changes are the session's own.
Expand Down
8 changes: 8 additions & 0 deletions modules/claude-self-hosted-runner/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ output "startup_script" {
mkdir -p ~/.claude-runner
chmod 700 ~/.claude-runner
echo '${base64encode(file("${path.module}/files/README.md"))}' | base64 -d > ~/.claude-runner/README.md
# Read-only gh with the owner's Coder GitHub login, for repositories
# outside the session's own; /etc/claude-code/CLAUDE.md tells sessions
# about it. Neither needs the environment secret.
mkdir -p ~/.local/bin
echo '${base64encode(file("${path.module}/files/gh-read"))}' | base64 -d > ~/.local/bin/gh-read
chmod +x ~/.local/bin/gh-read
sudo mkdir -p /etc/claude-code
echo '${base64encode(file("${path.module}/files/managed-CLAUDE.md"))}' | base64 -d | sudo tee /etc/claude-code/CLAUDE.md >/dev/null
if [ "$CLAUDE_REMOTE_CONTROL_ENABLED" = "true" ]; then
CLAUDE_RUNNER_MSG="✗ Claude Code self-hosted runner NOT started: Claude Code remote control is also enabled, and the runner rewrites ~/.gitconfig before every session. Turn one of them off."
elif [ ! -s "${local.secret_file}" ]; then
Expand Down
Loading