Skip to content

Commit 85fa81a

Browse files
Update design doc with final architecture for security audit
Reflects current implementation: composite action, standalone workflows, OIDC policies, SHA-pinned actions, concurrency groups, allowed tools, and comprehensive threat model. Co-authored-by: Isaac
1 parent 8f64c6a commit 85fa81a

1 file changed

Lines changed: 252 additions & 0 deletions

File tree

‎design-claude-code-reviews.md‎

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
# Design: Claude Code for Databricks CLI
2+
3+
## Objective
4+
5+
AI-assisted PR reviews and interactive `@claude` mentions on `databricks/cli` and `databricks-eng/eng-dev-ecosystem` using Claude Code CLI backed by Claude Opus 4.6 on Databricks Model Serving. Authentication via GitHub OIDC federation — no static API keys.
6+
7+
## Features
8+
9+
1. **Auto-review on PR open**: Claude reviews every PR opened by an org member, posting inline review comments.
10+
2. **`@claude` mentions**: Maintainers comment `@claude <instruction>` to request code changes, answers, or commands. Claude pushes commits directly.
11+
3. **Supported repos**: `databricks/cli` (cross-repo dispatch) and `databricks-eng/eng-dev-ecosystem` (direct).
12+
13+
## Architecture
14+
15+
### CLI repo (cross-repo dispatch)
16+
17+
```
18+
databricks/cli databricks-eng/eng-dev-ecosystem
19+
================ ================================
20+
21+
PR opened / @claude comment
22+
-> claude-code.yml (thin dispatcher)
23+
runs-on: databricks-deco-testing-runner-group
24+
-> GitHub App token (DECO_WORKFLOW_TRIGGER)
25+
-> gh workflow run cli-claude-code.yml
26+
-> Track remote run (gh run watch)
27+
cli-claude-code.yml (workflow_dispatch)
28+
runs-on: databricks-eng-protected-runner-group
29+
environment: integration-tests
30+
-> Checkout eng-dev-ecosystem (for composite action)
31+
-> Checkout databricks/cli PR branch to cli/
32+
-> Build prompt (review or assist)
33+
-> .github/actions/claude-code (composite action)
34+
-> PR helper scripts
35+
-> OIDC token refresh helper
36+
-> Validate connectivity
37+
-> Claude Code CLI
38+
-> Model Serving (Claude Opus 4.6)
39+
-> Post review / push commits via DECO_GITHUB_TOKEN
40+
```
41+
42+
CLI runs on GitHub-hosted runners not allowlisted in the Databricks account IP ACL. The OIDC token exchange endpoint blocks non-allowlisted IPs, so CLI dispatches to `eng-dev-ecosystem` which uses self-hosted runners with allowlisted IPs.
43+
44+
The CLI dispatcher tracks the remote run via `gh run watch --exit-status`, keeping the PR check visible while Claude works and mirroring the exit status. A step summary links to the actual eng-dev-ecosystem run.
45+
46+
### eng-dev-ecosystem repo (direct)
47+
48+
```
49+
databricks-eng/eng-dev-ecosystem
50+
================================
51+
52+
PR opened / @claude comment
53+
-> claude-code-self.yml (direct trigger)
54+
runs-on: databricks-eng-protected-runner-group
55+
-> Checkout PR branch
56+
-> .github/actions/claude-code (composite action)
57+
-> PR helper scripts
58+
-> OIDC token refresh helper
59+
-> Validate connectivity
60+
-> Claude Code CLI
61+
-> Model Serving (Claude Opus 4.6)
62+
-> Post review / push commits via github.token
63+
```
64+
65+
### Workflow and action files
66+
67+
| Repo | File | Purpose |
68+
|---|---|---|
69+
| `databricks/cli` | `.github/workflows/claude-code.yml` | Thin dispatcher — triggers eng-dev-ecosystem via GitHub App, tracks remote run |
70+
| `databricks-eng/eng-dev-ecosystem` | `.github/actions/claude-code/action.yml` | Composite action — OIDC auth, token refresh, PR helpers, Claude Code CLI execution |
71+
| `databricks-eng/eng-dev-ecosystem` | `.github/workflows/cli-claude-code.yml` | CLI-specific — builds prompts, checks out CLI code, calls composite action |
72+
| `databricks-eng/eng-dev-ecosystem` | `.github/workflows/claude-code-self.yml` | Self-review on eng-dev-ecosystem's own PRs, calls composite action |
73+
74+
### Composite action (`.github/actions/claude-code/action.yml`)
75+
76+
Shared infrastructure used by both `cli-claude-code.yml` and `claude-code-self.yml`:
77+
78+
| Input | Required | Description |
79+
|---|---|---|
80+
| `prompt` | Yes | Instructions for Claude Code |
81+
| `pr_number` | Yes | PR number for helper scripts |
82+
| `repo` | No | Target repo for `--repo` flag (empty = current repo) |
83+
| `gh_token` | Yes | GitHub token for PR operations |
84+
| `claude_args` | No | Additional CLI arguments (e.g. `--max-turns 100`) |
85+
| `working_directory` | No | Directory to run Claude in (default: `.`) |
86+
87+
Steps performed by the action:
88+
1. Create PR-scoped helper scripts (`pr-comment`, `pr-push`, `pr-diff`, `pr-view`, `pr-review`)
89+
2. Setup Node.js 20 and install `@anthropic-ai/claude-code`
90+
3. Create OIDC token refresh helper at `/usr/local/bin/get-databricks-token`
91+
4. Validate connectivity to Model Serving endpoint
92+
5. Run Claude Code with hardcoded allowed tools and system prefix
93+
94+
The caller workflow must:
95+
- Set `id-token: write` permission (for OIDC)
96+
- Check out the target repository before calling the action
97+
- Check out eng-dev-ecosystem as well (for cross-repo, so the action is available at `./.github/actions/claude-code`)
98+
99+
## Authentication
100+
101+
### OIDC token exchange
102+
103+
GitHub Actions requests an OIDC JWT (audience = Databricks account ID), exchanged for a Databricks OAuth token:
104+
105+
```
106+
POST https://accounts.cloud.databricks.com/oidc/accounts/968367da-7edd-44f7-9dea-3e0b20b0ec97/v1/token
107+
grant_type=urn:ietf:params:oauth:grant-type:token-exchange
108+
subject_token=<github-oidc-jwt>
109+
subject_token_type=urn:ietf:params:oauth:token-type:jwt
110+
client_id=b76b6808-9e10-43b3-be20-6b6d19ed1af0
111+
scope=all-apis
112+
```
113+
114+
### Token refresh
115+
116+
GitHub OIDC tokens expire in ~5 minutes. Claude Code's `apiKeyHelper` mechanism calls a helper script every few minutes (or on 401) to re-exchange tokens, allowing sessions to run beyond the 5-minute token lifetime. The helper script is written to `/usr/local/bin/get-databricks-token` and configured via `~/.claude/settings.json`:
117+
118+
```json
119+
{"apiKeyHelper": "/usr/local/bin/get-databricks-token"}
120+
```
121+
122+
`ANTHROPIC_AUTH_TOKEN` is NOT set — it takes precedence over `apiKeyHelper` and would prevent refresh.
123+
124+
### OAuth scope
125+
126+
Uses `scope=all-apis`. The narrower `scope=serving.serving-endpoints` was tested but returns 403 from Model Serving — `all-apis` is required.
127+
128+
### Service principal
129+
130+
- **Name**: `DECO-TF-AWS-PROD-IS-SPN`
131+
- **Service principal ID**: `3749120194272290`
132+
- **Application/Client ID**: `b76b6808-9e10-43b3-be20-6b6d19ed1af0`
133+
134+
### Federation policies
135+
136+
Exact-match on the `job_workflow_ref` OIDC claim. Only workflows on `main` in eng-dev-ecosystem can obtain tokens.
137+
138+
| Policy ID | Matches | Purpose |
139+
|---|---|---|
140+
| `claude-code-github-oidc` | `...claude-code-self.yml@refs/heads/main` | Self-review (eng-dev-ecosystem PRs) |
141+
| `claude-code-github-oidc-cli` | `...cli-claude-code.yml@refs/heads/main` | CLI PR review/assist |
142+
143+
Both policies use:
144+
- **Issuer**: `https://token.actions.githubusercontent.com`
145+
- **Audience**: `968367da-7edd-44f7-9dea-3e0b20b0ec97` (Databricks account ID)
146+
- **Subject claim**: `job_workflow_ref`
147+
148+
### Cross-repo access
149+
150+
| Secret/Token | Location | Purpose |
151+
|---|---|---|
152+
| `DECO_WORKFLOW_TRIGGER` GitHub App | CLI `test-trigger-is` environment | Dispatch workflows to eng-dev-ecosystem and track runs |
153+
| `DECO_GITHUB_TOKEN` PAT | eng-dev-ecosystem `integration-tests` environment | Write access to `databricks/cli` (push commits, post comments) |
154+
155+
## Model serving
156+
157+
- **Endpoint**: `https://dbc-1232e87d-9384.cloud.databricks.com/serving-endpoints/anthropic`
158+
- **Model**: `databricks-claude-opus-4-6`
159+
- **Auth**: `Authorization: Bearer` (via `apiKeyHelper`)
160+
- **Environment variables**: `ANTHROPIC_BASE_URL`, `ANTHROPIC_MODEL`, `DISABLE_PROMPT_CACHING=1`, `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1`
161+
162+
## Security model
163+
164+
### Access control
165+
166+
Only GitHub org members/owners can trigger Claude. Uses `author_association` allowlists per [GitHub Security Lab guidance](https://securitylab.github.com/resources/github-actions-preventing-pwn-requests/):
167+
168+
```yaml
169+
contains(fromJson('["MEMBER","OWNER"]'), github.event.pull_request.author_association)
170+
```
171+
172+
### Trigger surface
173+
174+
**CLI repo (`databricks/cli`):**
175+
176+
| Trigger | Gate |
177+
|---|---|
178+
| `pull_request` (opened) | `MEMBER`/`OWNER` allowlist + fork guard (`!head.repo.fork`) |
179+
| `issue_comment` (`@claude`) | `MEMBER`/`OWNER` allowlist + bot filter + PR-only guard + fork API check |
180+
| `pull_request_review_comment` (`@claude`) | `MEMBER`/`OWNER` allowlist + bot filter |
181+
182+
**eng-dev-ecosystem (`databricks-eng/eng-dev-ecosystem`):**
183+
184+
| Trigger | Gate |
185+
|---|---|
186+
| `pull_request` (opened) | `MEMBER`/`OWNER` allowlist + fork guard |
187+
| `issue_comment` (`@claude`) | `MEMBER`/`OWNER` allowlist + bot filter + PR-only guard |
188+
| `pull_request_review_comment` (`@claude`) | `MEMBER`/`OWNER` allowlist + bot filter |
189+
190+
### Security measures
191+
192+
| Measure | Description |
193+
|---|---|
194+
| **Fork guard** | `!github.event.pull_request.head.repo.fork` on review; API check via `gh pr view --json isCrossRepository` on CLI assist |
195+
| **Bot filter** | `github.event.comment.user.type != 'Bot'` prevents loops |
196+
| **PR-only guard** | `github.event.issue.pull_request` ensures `issue_comment` only fires on PRs |
197+
| **Concurrency groups** | Both `review` and `assist` jobs cancel in-progress runs on the same PR |
198+
| **Hardcoded tool allowlist** | `--allowedTools` in the composite action restricts Claude to specific safe commands; callers cannot override |
199+
| **Max turns** | `--max-turns 100` limits work per CLI invocation |
200+
| **Timeout** | `timeout-minutes: 30` on all jobs caps session duration |
201+
| **PR-scoped helpers** | `pr-comment`, `pr-push`, `pr-diff`, `pr-view`, `pr-review` hardcode PR number at creation time |
202+
| **Branch name escaping** | `printf '%q'` in `pr-push` prevents shell injection via branch names |
203+
| **Comment truncation** | Assist truncates `comment_body` to 4000 chars |
204+
| **Heredoc delimiter randomization** | `PROMPT_$(openssl rand -hex 8)` prevents `comment_body` from breaking `GITHUB_OUTPUT` |
205+
| **Script injection prevention** | `comment_body` and `pr_number` passed via `process.env` in `actions/github-script`, never via `${{ }}` in `run:` blocks |
206+
| **Action SHA pinning** | Third-party actions (`create-github-app-token`, `github-script`) pinned to commit SHAs, not mutable tags |
207+
| **Minimal dispatcher permissions** | CLI dispatcher only requests `contents: read`; write permissions are on the eng-dev-ecosystem job |
208+
| **Environment-gated secrets** | `DECO_GITHUB_TOKEN` requires `integration-tests` environment approval; `DECO_WORKFLOW_TRIGGER` requires `test-trigger-is` |
209+
210+
### Allowed tools
211+
212+
The composite action hardcodes this tool allowlist for Claude Code:
213+
214+
```
215+
Bash(make lint), Bash(make test), Bash(make fmt), Bash(make schema),
216+
Bash(go build *), Bash(go test *), Bash(go vet), Bash(go vet *),
217+
Bash(terraform fmt), Bash(terraform fmt *), Bash(terraform validate), Bash(terraform validate *),
218+
Bash(git add *), Bash(git commit *), Bash(git diff), Bash(git diff *),
219+
Bash(git log), Bash(git log *), Bash(git status), Bash(git show *),
220+
Bash(pr-comment *), Bash(pr-diff), Bash(pr-diff *),
221+
Bash(pr-push), Bash(pr-push *), Bash(pr-review *),
222+
Bash(pr-view), Bash(pr-view *), Bash(grep *),
223+
Read, Edit, Write, Glob, Grep
224+
```
225+
226+
Notable exclusions: no `rm`, no `gh` (only via wrapper scripts), no arbitrary shell commands, no network tools.
227+
228+
### Threat model
229+
230+
| Threat | Mitigation |
231+
|---|---|
232+
| External user opens fork PR | `author_association` allowlist + fork guard |
233+
| Attacker modifies `CLAUDE.md` in fork PR | Fork PR authors blocked by allowlist |
234+
| External user comments `@claude` | `author_association` allowlist |
235+
| Bot triggers Claude in loop | `user.type != 'Bot'` filter |
236+
| Comment body shell injection | Passed via env vars, truncated, heredoc delimiter randomized |
237+
| Comment body injected into workflow dispatch | `comment_body` passed via `process.env`, never `${{ }}` in shell |
238+
| Claude targets wrong PR | Helper scripts hardcode PR number at creation time |
239+
| Branch name injection | `printf '%q'` escapes branch name in `pr-push` |
240+
| Internal user runs destructive commands | Hardcoded `--allowedTools` restricts available commands |
241+
| Token leaked in logs | `::add-mask::` on tokens; token exchange errors only log HTTP status |
242+
| Cost overrun | `--max-turns` + `timeout-minutes` |
243+
| Mutable action tag supply chain attack | Actions pinned to commit SHAs (`create-github-app-token@fee1f7d...`, `github-script@f28e40c...`) |
244+
| Concurrent `@claude` mentions overload | Concurrency groups cancel in-progress runs per PR |
245+
| Dispatch to wrong workflow ref | `--ref main` hardcoded in CLI dispatcher |
246+
| OIDC token obtained from non-main branch | Federation policies exact-match `@refs/heads/main` |
247+
| Secrets accessed outside environment | `DECO_GITHUB_TOKEN` gated by `integration-tests` environment; `DECO_WORKFLOW_TRIGGER` by `test-trigger-is` |
248+
249+
## Open questions
250+
251+
1. **Bot identity**: Reviews are posted as `eng-dev-ecosystem-bot`. A custom GitHub App would give Claude a distinct identity. Separate effort.
252+
2. **Rollout to other repos**: For each new repo, add a dispatcher workflow in the target repo, a repo-specific caller workflow in eng-dev-ecosystem, and a federation policy on the service principal.

0 commit comments

Comments
 (0)