package stateapi import ( "context" "errors" "fmt" "net/http" "strings" "tangled.org/guid.foo/gofer/api/v1alpha1" "tangled.org/guid.foo/gofer/cluster" "tangled.org/guid.foo/gofer/inject" ) // errForbidden means the caller is a pod, and not one that may use the // profile it asked for. var errForbidden = errors.New("forbidden") // authorize answers the profile a request may use, from the pod's token // it carries. The token names a pod, and the pod must still be that pod, // must not have finished, must carry the profile's label, and must run // as a ServiceAccount the profile allows. A pod being deleted still // passes, because its sidecar saves the state on SIGTERM, and its token // stops working once the pod is gone. func (s *Server) authorize(ctx context.Context, r *http.Request, namespace, name string) (*v1alpha1.BrowserProfile, error) { token, ok := strings.CutPrefix(r.Header.Get("Authorization"), "Bearer ") if !ok || token == "" { return nil, cluster.ErrUnauthenticated } caller, err := s.cluster.Review(ctx, token) if err != nil { return nil, err } if caller.Namespace != namespace { return nil, fmt.Errorf("%w: the pod is in %s", errForbidden, caller.Namespace) } pod, err := s.cluster.Pod(ctx, caller.Namespace, caller.Pod) if errors.Is(err, cluster.ErrNotFound) { return nil, fmt.Errorf("%w: the pod %s is gone", errForbidden, caller.Pod) } if err != nil { return nil, err } switch { case pod.UID != caller.PodUID: return nil, fmt.Errorf("%w: the pod %s is another pod of the same name", errForbidden, caller.Pod) case pod.Finished(): return nil, fmt.Errorf("%w: the pod %s has finished", errForbidden, caller.Pod) case pod.Labels[v1alpha1.LabelProfile] != name: return nil, fmt.Errorf("%w: the pod %s does not use the profile %s", errForbidden, caller.Pod, name) case pod.ServiceAccount != caller.ServiceAccount: return nil, fmt.Errorf("%w: the token's ServiceAccount is not the pod's", errForbidden) } if err := inject.Holds(pod.Spec, s.image); err != nil { return nil, fmt.Errorf("%w: the pod %s is not one the webhook admitted: %w", errForbidden, caller.Pod, err) } // This read decides whether the pod's ServiceAccount reaches a // logged-in browser's state and the profile's secrets, so it goes to // the API server and not to the watch's store. A store that lags etcd // would still admit a ServiceAccount that a person removed. profile, err := s.cluster.Profile(ctx, namespace, name) if errors.Is(err, cluster.ErrNotFound) { return nil, fmt.Errorf("%w: there is no profile %s", errForbidden, name) } if err != nil { return nil, err } if !profile.AllowsServiceAccount(caller.ServiceAccount) { return nil, fmt.Errorf("%w: the profile %s does not allow the ServiceAccount %s", errForbidden, name, caller.ServiceAccount) } return profile, nil } // refuse answers an error from authorize with its status, and keeps the // reason out of the answer: the reason names pods and profiles, and goes // to the log. func (s *Server) refuse(w http.ResponseWriter, r *http.Request, err error) { status := http.StatusInternalServerError switch { case errors.Is(err, cluster.ErrUnauthenticated): status = http.StatusUnauthorized case errors.Is(err, errForbidden): status = http.StatusForbidden } s.log.Warn("refused a request", "method", r.Method, "path", r.URL.Path, "status", status, "reason", err) http.Error(w, http.StatusText(status), status) }