Something went wrong. Try again.
A Kubernetes operator that gives a pod its own browser as an MCP server
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179package inject
import ( "fmt" "slices" "strings"
corev1 "k8s.io/api/core/v1"
"tangled.org/guid.foo/gofer/api/v1alpha1" "tangled.org/guid.foo/gofer/cluster")
// refusal answers why a pod may not use profile, or "" when it may. It// runs on the pod as the webhook would leave it, sidecar and all, so a// webhook that runs after gofer's and adds a mount is caught when gofer's// runs again.func refusal(pod *corev1.Pod, profile *v1alpha1.BrowserProfile) string { account := pod.Spec.ServiceAccountName if account == "" { account = "default" } if !profile.AllowsServiceAccount(account) { return fmt.Sprintf("the profile %s does not allow the ServiceAccount %s", profile.Metadata.Name, account) } spec := pod.Spec switch { case spec.ShareProcessNamespace != nil && *spec.ShareProcessNamespace: return "a pod with a browser cannot share its process namespace, because every container could read the browser's memory" case spec.HostPID, spec.HostIPC, spec.HostNetwork: return "a pod with a browser cannot use the host's PID, IPC, or network namespace" } if refused := groupRefusal(spec.SecurityContext); refused != "" { return refused } sidecars := 0 for _, c := range containers(pod) { if c.Name == SidecarName { sidecars++ continue } if refused := containerRefusal(c, spec.SecurityContext); refused != "" { return refused } } if sidecars > 1 { return "the pod has more than one " + SidecarName } for _, v := range spec.Volumes { if v.Name != TokenVolume && requestsGoferToken(v) { return fmt.Sprintf("volume %s asks for a token for gofer, which only the sidecar may hold", v.Name) } } return ""}
// groupRefusal refuses a pod that puts its containers in the sidecar's// group. fsGroup and supplementalGroups apply to every container, so the// workload could read whatever the sidecar's group can.func groupRefusal(pod *corev1.PodSecurityContext) string { if pod == nil { return "" } if pod.FSGroup != nil && *pod.FSGroup == SidecarGID { return fmt.Sprintf("fsGroup %d is the browser's group", SidecarGID) } if slices.Contains(pod.SupplementalGroups, SidecarGID) { return fmt.Sprintf("supplementalGroups holds %d, the browser's group", SidecarGID) } return ""}
func containerRefusal(c corev1.Container, pod *corev1.PodSecurityContext) string { for _, m := range c.VolumeMounts { if strings.HasPrefix(m.Name, "gofer-") { return fmt.Sprintf("%s mounts %s, which only the sidecar may mount", describe(c), m.Name) } } uid, gid := runAs(c, pod) if uid != nil && *uid == SidecarUID { return fmt.Sprintf("%s runs as %d, the browser's user", describe(c), SidecarUID) } if gid != nil && *gid == SidecarGID { return fmt.Sprintf("%s runs in group %d, the browser's group", describe(c), SidecarGID) } if s := c.SecurityContext; s != nil { if s.Privileged != nil && *s.Privileged { return fmt.Sprintf("%s is privileged, and could read the browser's memory", describe(c)) } if s.Capabilities != nil && slices.Contains(s.Capabilities.Add, "SYS_PTRACE") { return fmt.Sprintf("%s adds SYS_PTRACE, and could read the browser's memory", describe(c)) } } return ""}
func runAs(c corev1.Container, pod *corev1.PodSecurityContext) (*int64, *int64) { var uid, gid *int64 if pod != nil { uid, gid = pod.RunAsUser, pod.RunAsGroup } if s := c.SecurityContext; s != nil { if s.RunAsUser != nil { uid = s.RunAsUser } if s.RunAsGroup != nil { gid = s.RunAsGroup } } return uid, gid}
// requestsGoferToken reports whether a volume asks the kubelet for a// token for gofer's audience. A container holding one could call the// state API as the pod.func requestsGoferToken(v corev1.Volume) bool { if v.Projected == nil { return false } for _, source := range v.Projected.Sources { if source.ServiceAccountToken != nil && source.ServiceAccountToken.Audience == cluster.Audience { return true } } return false}
func containers(pod *corev1.Pod) []corev1.Container { return append(slices.Clone(pod.Spec.InitContainers), pod.Spec.Containers...)}
// Holds reports why a running pod's spec is not one the webhook// admitted, or nil when it is: the sidecar is there from the webhook's// image repository, and no other container holds or mounts what is the// sidecar's. The state API asks it of every caller, so a pod that reached// the API server some other way, such as one created before the webhook// was installed, does not reach a profile.//// It compares the repository and not the whole image, so a pod that an// earlier release injected keeps saving its state after the operator// moves to a newer sidecar tag. The webhook refuses an update that// changes the sidecar's image, so the tag a running pod holds is one a// gofer webhook chose.func Holds(spec corev1.PodSpec, image string) error { sidecars := 0 for _, c := range append(slices.Clone(spec.InitContainers), spec.Containers...) { if c.Name == SidecarName && repository(c.Image) == repository(image) && c.RestartPolicy != nil && *c.RestartPolicy == corev1.ContainerRestartPolicyAlways { sidecars++ continue } for _, m := range c.VolumeMounts { if strings.HasPrefix(m.Name, "gofer-") { return fmt.Errorf("%s mounts %s", describe(c), m.Name) } } } if sidecars != 1 { return fmt.Errorf("the pod has %d sidecars from %s, not 1", sidecars, repository(image)) } for _, v := range spec.Volumes { if v.Name != TokenVolume && requestsGoferToken(v) { return fmt.Errorf("volume %s asks for a token for gofer", v.Name) } } return nil}
// repository answers an image reference without its tag or digest. A// colon before the last slash belongs to a registry's port, not a tag.func repository(image string) string { if at := strings.Index(image, "@"); at >= 0 { image = image[:at] } if colon := strings.LastIndex(image, ":"); colon > strings.LastIndex(image, "/") { image = image[:colon] } return image}