Something went wrong. Try again.
A Kubernetes operator that runs Claude Code sessions on long-horizon work
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182package dataplane
import ( "context" "errors" "fmt" "strings"
"tangled.org/guid.foo/simmer/api/v1alpha1")
// Audience is the audience of the projected ServiceAccount token that a// Job sends. A token for any other audience, such as the API server's// own, is refused, so a token that leaks from simmer cannot reach the// API server and the reverse.const Audience = "simmer"
// LabelProbe is v1alpha1.LabelProbe, kept here for the callers that// name it through this package.const LabelProbe = v1alpha1.LabelProbe
// Caller is who sent a request, as the TokenReview of its token says.type Caller struct { // Namespace and ServiceAccount are the token's ServiceAccount. Namespace string ServiceAccount string // Pod and PodUID are the pod the token is bound to. Pod string PodUID string}
// TokenReviewer checks a token. It returns ErrUnauthenticated for a// token that is not valid for the simmer audience or is not bound to a// pod, and any other error when it could not ask.type TokenReviewer interface { Review(ctx context.Context, token string) (Caller, error)}
// ErrUnauthenticated means a request's token is missing or not valid.var ErrUnauthenticated = errors.New("the token is not a pod's token for simmer")
// Pod is what the server reads of a caller's pod to authorize it.type Pod struct { Namespace string Name string UID string Phase string ServiceAccount string Labels map[string]string // OwnedByJob is true when a Job controls the pod, and Job names // that Job. OwnedByJob bool Job string}
// PodGetter reads one pod. It returns ErrNoPod when the pod does not// exist.type PodGetter interface { Pod(ctx context.Context, namespace, name string) (Pod, error)}
// ErrNoPod means the caller's pod does not exist.var ErrNoPod = errors.New("no such pod")
// errForbidden marks a caller that is authenticated but is not a// running Job pod of what it asks about.var errForbidden = errors.New("forbidden")
// callerPod reads the caller's pod and checks that it is the pod the// token names, running under a Job, in the namespace it must be in.func callerPod(ctx context.Context, pods PodGetter, caller Caller, namespace string) (Pod, error) { if caller.Namespace != namespace { return Pod{}, fmt.Errorf("%w: the caller runs in %s, not %s", errForbidden, caller.Namespace, namespace) } pod, err := pods.Pod(ctx, caller.Namespace, caller.Pod) if errors.Is(err, ErrNoPod) { return Pod{}, fmt.Errorf("%w: pod %s/%s does not exist", errForbidden, caller.Namespace, caller.Pod) } if err != nil { return Pod{}, err } switch { // A pod deleted and created again under the same name has a new // uid, and the old pod's token must not speak for it. case caller.PodUID != "" && pod.UID != caller.PodUID: return Pod{}, fmt.Errorf("%w: pod %s/%s is not the pod the token was issued to", errForbidden, pod.Namespace, pod.Name) case pod.ServiceAccount != caller.ServiceAccount: return Pod{}, fmt.Errorf("%w: pod %s/%s does not run as %s", errForbidden, pod.Namespace, pod.Name, caller.ServiceAccount) // A finished pod's token must not keep speaking for its Session. A // pod that reads Pending is allowed, because the kubelet reports // Running a moment after the container starts, and a turn's first // request comes sooner than that. case pod.Phase == "Succeeded" || pod.Phase == "Failed": return Pod{}, fmt.Errorf("%w: pod %s/%s has finished", errForbidden, pod.Namespace, pod.Name) case !pod.OwnedByJob: return Pod{}, fmt.Errorf("%w: pod %s/%s is not a Job's pod", errForbidden, pod.Namespace, pod.Name) } return pod, nil}
// authorizeSession lets in a running pod of one of the Session's turns:// labeled with the Session, controlled by a Job named for one of its// turns, and running as account, the Session's Mandate's// ServiceAccount. The label alone is not enough, because a pod's labels// are whatever made the pod wrote, and another Mandate's Session may be// able to make pods.func authorizeSession(ctx context.Context, pods PodGetter, caller Caller, session SessionRef, account string) error { pod, err := callerPod(ctx, pods, caller, session.Namespace) if err != nil { return err } switch { case pod.Labels[v1alpha1.LabelSession] != session.Name: return fmt.Errorf("%w: pod %s/%s is not a pod of Session %s", errForbidden, pod.Namespace, pod.Name, session) case pod.ServiceAccount != account: return fmt.Errorf("%w: pod %s/%s runs as %s, not as %s", errForbidden, pod.Namespace, pod.Name, pod.ServiceAccount, account) case !strings.HasPrefix(pod.Job, session.Name+"-"): return fmt.Errorf("%w: pod %s/%s runs in the Job %s, not a turn of Session %s", errForbidden, pod.Namespace, pod.Name, pod.Job, session) // A hook's Job is named for its Session too, so only the label // keeps a hook off a turn's routes. case pod.Labels[v1alpha1.LabelHook] != "": return fmt.Errorf("%w: pod %s/%s runs a hook, which reads only its context", errForbidden, pod.Namespace, pod.Name) } return nil}
// authorizeHook lets in a running pod of one of the Session's hooks:// the checks of a turn's pod, with the hook label in place of its// absence. It answers the pod's Job, which the operator matches to one// of the Session's hooks.func authorizeHook(ctx context.Context, pods PodGetter, caller Caller, session SessionRef, account string) (string, error) { pod, err := callerPod(ctx, pods, caller, session.Namespace) if err != nil { return "", err } switch { case pod.Labels[v1alpha1.LabelHook] == "": return "", fmt.Errorf("%w: pod %s/%s does not run a hook", errForbidden, pod.Namespace, pod.Name) case pod.Labels[v1alpha1.LabelSession] != session.Name: return "", fmt.Errorf("%w: pod %s/%s is not a pod of Session %s", errForbidden, pod.Namespace, pod.Name, session) case pod.ServiceAccount != account: return "", fmt.Errorf("%w: pod %s/%s runs as %s, not as %s", errForbidden, pod.Namespace, pod.Name, pod.ServiceAccount, account) case !strings.HasPrefix(pod.Job, session.Name+"-"): return "", fmt.Errorf("%w: pod %s/%s runs in the Job %s, not a hook of Session %s", errForbidden, pod.Namespace, pod.Name, pod.Job, session) } return pod.Job, nil}
// authorizeProbe lets in a running probe Job pod of the Account, in the// operator's namespace.func authorizeProbe(ctx context.Context, pods PodGetter, caller Caller, operatorNamespace, account string) error { pod, err := callerPod(ctx, pods, caller, operatorNamespace) if err != nil { return err } if pod.Labels[v1alpha1.LabelProbe] != account { return fmt.Errorf("%w: pod %s/%s is not a probe of Account %s", errForbidden, pod.Namespace, pod.Name, account) } return nil}
// authorizeSchedule lets in a running pod of a Duty's schedule:// labeled with the Duty's schedule and its Mandate, controlled by a// Job, and running as the Mandate's ServiceAccount. It answers the// pod's Job, which the operator matches to the CronJob it made for the// Duty, because the labels alone are whatever made the pod wrote.func authorizeSchedule(ctx context.Context, pods PodGetter, caller Caller, duty DutyRef, id ScheduleIdentity) (string, error) { pod, err := callerPod(ctx, pods, caller, duty.Namespace) if err != nil { return "", err } switch { case pod.Labels[v1alpha1.LabelSchedule] != duty.Name: return "", fmt.Errorf("%w: pod %s/%s does not run the schedule of the Duty %s", errForbidden, pod.Namespace, pod.Name, duty) case pod.Labels[v1alpha1.LabelMandate] != id.Mandate: return "", fmt.Errorf("%w: pod %s/%s is not a pod of Mandate %s", errForbidden, pod.Namespace, pod.Name, id.Mandate) case pod.ServiceAccount != id.ServiceAccount: return "", fmt.Errorf("%w: pod %s/%s runs as %s, not as %s", errForbidden, pod.Namespace, pod.Name, pod.ServiceAccount, id.ServiceAccount) } return pod.Job, nil}