Repository navigation
Commit 85001ba
committed
feat(coderd): allow agent auth during workspace shutdown
Agents were losing authentication during workspace shutdown, causing
shutdown scripts to fail. The auth query required agents to belong to
the latest build, but during shutdown a `stop` build becomes latest while
the `start` build's agents are still running.
Several approaches were considered:
- Reusing agent tokens (no unique constraint, would break auth)
- Separate validation functions (two queries for agent + latest build)
- Grace period with workspace-level tokens (security risk, stale agents)
- Agent lifecycle state tracking (requires migration, complex state management)
Modified the auth query to allow `start` build agents to authenticate
temporarily during `stop` execution. This is simpler than separate functions
and avoids migrations. The query allows auth when:
- Agent's `start` build job succeeded
- Latest build is `stop` with `pending`/`running` job status
- Builds are adjacent (`stop` is `build_number + 1`)
- Template versions match
The version check prevents RBAC issues if someone explicitly changes
template versions between builds. Auth closes once `stop` completes.
Renamed `GetWorkspaceAgentAndLatestBuildByAuthToken` to
`GetAuthenticatedWorkspaceAgentAndBuildByAuthToken` since it returns the
agent's build (not always latest) during shutdown.
Added tests covering happy path, failure cases (completed stops, failed
builds, version mismatches), and edge cases (multiple cycles, wrong
transitions) at both database and middleware levels.
Fixes coder/internal#1249
Fixes #194671 parent 6ef9670 commit 85001ba
15 files changed
Lines changed: 792 additions & 70 deletions
File tree
- coderd
- database
- dbauthz
- dbmetrics
- dbmock
- queries
- httpmw
- enterprise
- cli
- coderd
- schedule
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2244 | 2244 | | |
2245 | 2245 | | |
2246 | 2246 | | |
| 2247 | + | |
| 2248 | + | |
| 2249 | + | |
| 2250 | + | |
| 2251 | + | |
| 2252 | + | |
| 2253 | + | |
| 2254 | + | |
2247 | 2255 | | |
2248 | 2256 | | |
2249 | 2257 | | |
| |||
3606 | 3614 | | |
3607 | 3615 | | |
3608 | 3616 | | |
3609 | | - | |
3610 | | - | |
3611 | | - | |
3612 | | - | |
3613 | | - | |
3614 | | - | |
3615 | | - | |
3616 | | - | |
3617 | 3617 | | |
3618 | 3618 | | |
3619 | 3619 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3472 | 3472 | | |
3473 | 3473 | | |
3474 | 3474 | | |
3475 | | - | |
| 3475 | + | |
3476 | 3476 | | |
3477 | | - | |
| 3477 | + | |
3478 | 3478 | | |
3479 | 3479 | | |
3480 | 3480 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments