Something went wrong. Try again.
A Kubernetes operator that gives a pod its own browser as an MCP server
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177package servingcert
import ( "bytes" "context" "crypto/tls" "crypto/x509" "errors" "fmt" "log/slog" "strings" "sync" "testing" "testing/synctest" "time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil")
// refusing is a Secret store whose next writes fail, the way an API// server that restarts refuses them. The keeper reads it from its own// goroutine, so every access holds the lock.type refusing struct { mu sync.Mutex secrets refuse int}
func (r *refusing) Secret(ctx context.Context, namespace, name string) (map[string][]byte, error) { r.mu.Lock() defer r.mu.Unlock() return r.secrets.Secret(ctx, namespace, name)}
func (r *refusing) CreateSecret(ctx context.Context, namespace, name string, data map[string][]byte) error { r.mu.Lock() defer r.mu.Unlock() return r.secrets.CreateSecret(ctx, namespace, name, data)}
func (r *refusing) ReplaceSecretData(ctx context.Context, namespace, name string, data map[string][]byte) error { r.mu.Lock() defer r.mu.Unlock() if r.refuse > 0 { r.refuse-- return errors.New("connection refused") } return r.secrets.ReplaceSecretData(ctx, namespace, name, data)}
// lose deletes the Secret.func (r *refusing) lose() { r.mu.Lock() defer r.mu.Unlock() r.data = nil}
// keep starts a Keeper of the Secret's certificate, as the operator// does at its start, and answers it, the registry its series register// on, and the channel that Run's answer arrives on.func keep(t *testing.T, s Secrets) (*Keeper, Bundle, *prometheus.Registry, <-chan error) { t.Helper() notFound := func(err error) bool { return errors.Is(err, errNotFound) } bundle, err := Ensure(t.Context(), s, "gofer-system", "gofer-tls", names, notFound, time.Now()) if err != nil { t.Fatal(err) } registry := prometheus.NewRegistry() k := NewKeeper(bundle, func(now time.Time) (Bundle, error) { return Ensure(t.Context(), s, "gofer-system", "gofer-tls", names, notFound, now) }, slog.New(slog.DiscardHandler), registry) done := make(chan error, 1) go func() { done <- k.Run(t.Context()) }() return k, bundle, registry, done}
func served(t *testing.T, k *Keeper) []byte { t.Helper() certificate, err := k.GetCertificate(nil) if err != nil { t.Fatal(err) } return certificate.Certificate[0]}
func TestARunningOperatorRenewsItsCertificateBeforeItEnds(t *testing.T) { cases := []struct { name string refuse int }{ {"a renewal that lands", 0}, {"a renewal the API server refuses a few times", 3}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { synctest.Test(t, func(t *testing.T) { k, first, _, _ := keep(t, &refusing{refuse: c.refuse}) time.Sleep(servingLife - renewBefore - time.Hour) synctest.Wait() if !bytes.Equal(served(t, k), first.Certificate.Certificate[0]) { t.Fatal("the certificate was renewed long before its end") } time.Sleep(2 * time.Hour) synctest.Wait() renewed := Bundle{Certificate: tlsCertificate(t, k), CAPEM: first.CAPEM} if bytes.Equal(served(t, k), first.Certificate.Certificate[0]) { t.Fatal("the certificate was not renewed") } if err := verifies(t, renewed, time.Now().Add(renewBefore)); err != nil { t.Errorf("the renewed certificate does not verify under the first CA: %v", err) } }) }) }}
func TestARenewalUnderANewCAStopsTheKeeper(t *testing.T) { synctest.Test(t, func(t *testing.T) { s := &refusing{} _, _, _, done := keep(t, s) // A person deleted the Secret, so the renewal makes a new CA, // which neither the webhook nor any sidecar trusts. s.lose() time.Sleep(servingLife) synctest.Wait() select { case err := <-done: if !errors.Is(err, ErrCAChanged) { t.Errorf("Run = %v, want ErrCAChanged", err) } default: t.Error("the keeper kept serving under a CA nothing trusts") } })}
func tlsCertificate(t *testing.T, k *Keeper) tls.Certificate { t.Helper() certificate, err := k.GetCertificate(nil) if err != nil { t.Fatal(err) } return *certificate}
func TestTheExpiryGaugeNamesTheEndOfTheServedCertificate(t *testing.T) { cases := []struct { name string after time.Duration }{ {"at the start", 0}, {"after a renewal", servingLife - renewBefore + time.Hour}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { synctest.Test(t, func(t *testing.T) { k, _, registry, _ := keep(t, &refusing{}) time.Sleep(c.after) synctest.Wait() leaf, err := x509.ParseCertificate(served(t, k)) if err != nil { t.Fatal(err) } want := fmt.Sprintf(`# HELP gofer_serving_certificate_expiry_timestamp_seconds When the serving certificate the operator serves ends, in seconds since the epoch.# TYPE gofer_serving_certificate_expiry_timestamp_seconds gaugegofer_serving_certificate_expiry_timestamp_seconds %d`, leaf.NotAfter.Unix()) if err := testutil.GatherAndCompare(registry, strings.NewReader(want)); err != nil { t.Error(err) } }) }) }}