|
1 | | -# token-refresher |
| 1 | +# token-refresher |
| 2 | + |
| 3 | +`token-refresher` is a small Go job that keeps a coding agent auth blob in sync inside Kubernetes. |
| 4 | + |
| 5 | +The first provider is `codex`: |
| 6 | + |
| 7 | +- reads `auth.json` from a Kubernetes `Secret` |
| 8 | +- copies it into an isolated temporary `HOME` |
| 9 | +- runs a lightweight `codex exec` to trigger validation or refresh |
| 10 | +- reads the resulting `auth.json` |
| 11 | +- updates the Kubernetes `Secret` only if the file changed |
| 12 | + |
| 13 | +The runtime path is isolated on purpose. The refresher never mutates any existing host-side `~/.codex/auth.json` directly. |
| 14 | + |
| 15 | +## Why this shape |
| 16 | + |
| 17 | +Codex stores auth state in `~/.codex/auth.json`. For a Kubernetes `CronJob`, the cleanest flow is: |
| 18 | + |
| 19 | +1. fetch the current `auth.json` from a secret |
| 20 | +2. materialize it in a temp home directory |
| 21 | +3. let `codex` operate against that copied state |
| 22 | +4. write the updated file back to the secret if it changed |
| 23 | + |
| 24 | +That makes the implementation safe for CronJobs and leaves room to add other agent providers later. |
| 25 | + |
| 26 | +## Current defaults |
| 27 | + |
| 28 | +- `CronJob` schedule: every 12 hours |
| 29 | +- example refresh policy: `always` |
| 30 | +- optional lower-churn mode: `threshold` |
| 31 | +- threshold window default: `72h` |
| 32 | + |
| 33 | +`always` means every scheduled run performs a lightweight `codex exec`. |
| 34 | + |
| 35 | +`threshold` means the job parses the current `access_token.exp` from `auth.json` and only runs `codex exec` when the access token is close to expiry. |
| 36 | + |
| 37 | +## Configuration |
| 38 | + |
| 39 | +Environment variables: |
| 40 | + |
| 41 | +- `AGENT_PROVIDER`: provider name. Currently `codex` only. |
| 42 | +- `SECRET_NAMESPACE`: namespace containing the secret. Falls back to `POD_NAMESPACE`. |
| 43 | +- `SECRET_NAME`: name of the Kubernetes secret to read and update. |
| 44 | +- `SECRET_KEY`: key inside the secret. Defaults to `auth.json`. |
| 45 | +- `CODEX_COMMAND`: Codex CLI binary name. Defaults to `codex`. |
| 46 | +- `CODEX_PROMPT`: prompt used for the refresh probe. Defaults to `Reply with the single word ok.` |
| 47 | +- `CODEX_REFRESH_POLICY`: `always` or `threshold`. Defaults to `threshold`. |
| 48 | +- `CODEX_REFRESH_WINDOW`: only used in `threshold` mode. Defaults to `72h`. |
| 49 | +- `CODEX_REFRESH_TIMEOUT`: timeout for `codex exec`. Defaults to `10m`. |
| 50 | +- `CODEX_AUTH_SUBDIR`: auth directory under the temporary home. Defaults to `.codex`. |
| 51 | +- `CODEX_AUTH_FILE`: auth file name. Defaults to `auth.json`. |
| 52 | + |
| 53 | +## Build |
| 54 | + |
| 55 | +```bash |
| 56 | +make build |
| 57 | +make verify |
| 58 | +make test |
| 59 | +make image IMAGE_REPOSITORY=ghcr.io/your-org/token-refresher VERSION=latest |
| 60 | +``` |
| 61 | + |
| 62 | +The container image includes: |
| 63 | + |
| 64 | +- the Go refresher binary |
| 65 | +- Node.js |
| 66 | +- `@openai/codex` |
| 67 | + |
| 68 | +`make update` formats Go files and runs `go mod tidy`. |
| 69 | + |
| 70 | +## Kubernetes setup |
| 71 | + |
| 72 | +Create the starting secret from an existing local Codex auth file: |
| 73 | + |
| 74 | +```bash |
| 75 | +kubectl -n your-namespace create secret generic codex-auth \ |
| 76 | + --from-file=auth.json=$HOME/.codex/auth.json |
| 77 | +``` |
| 78 | + |
| 79 | +Apply RBAC and the `CronJob`: |
| 80 | + |
| 81 | +```bash |
| 82 | +kubectl -n your-namespace apply -f deploy/kubernetes/rbac.yaml |
| 83 | +kubectl -n your-namespace apply -f deploy/kubernetes/cronjob.yaml |
| 84 | +``` |
| 85 | + |
| 86 | +Before applying the `CronJob`, edit [deploy/kubernetes/cronjob.yaml](/Users/gjkim/workspace/token-refresher/deploy/kubernetes/cronjob.yaml) and set: |
| 87 | + |
| 88 | +- `image` |
| 89 | +- `SECRET_NAME` if you use a different secret name |
| 90 | +- `CODEX_REFRESH_POLICY` if you want `threshold` instead of `always` |
| 91 | + |
| 92 | +## Notes |
| 93 | + |
| 94 | +- The job uses the in-cluster service account token and Kubernetes API directly. No `kubectl` dependency is required. |
| 95 | +- The process updates the secret only when `auth.json` changes. |
| 96 | +- The current implementation is deliberately provider-oriented so other agent auth formats can be added behind the same interface. |
0 commit comments