Something went wrong. Try again.
A Kubernetes operator that gives a pod its own browser as an MCP server
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184// Package servingcert gives the operator a certificate for its webhook// and its state API, with no cert-manager. The operator keeps its own CA// and a serving certificate signed by it in one Secret, makes them on// its first start, and makes a new serving certificate when the one it// has enters its renewal window, at a start or while it runs (Keeper).// The API server learns the CA from the webhook's caBundle, and each// sidecar from its environment.package servingcert
import ( "context" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/tls" "crypto/x509" "crypto/x509/pkix" "encoding/pem" "errors" "fmt" "math/big" "time")
const ( caLife = 10 * 365 * 24 * time.Hour servingLife = 365 * 24 * time.Hour renewBefore = 30 * 24 * time.Hour)
// Keys of the Secret.const ( keyCACert = "ca.crt" keyCAKey = "ca.key" keyTLSCert = "tls.crt" keyTLSKey = "tls.key" commonName = "gofer" certPEMType = "CERTIFICATE" keyPEMType = "EC PRIVATE KEY")
// Secrets reads and writes the Secret that holds the certificates.type Secrets interface { Secret(ctx context.Context, namespace, name string) (map[string][]byte, error) CreateSecret(ctx context.Context, namespace, name string, data map[string][]byte) error ReplaceSecretData(ctx context.Context, namespace, name string, data map[string][]byte) error}
// Bundle is the certificate the operator serves and the CA that signed// it.type Bundle struct { Certificate tls.Certificate CAPEM []byte}
// Ensure answers a serving certificate for dnsNames from the Secret,// making the CA, the certificate, or both when they are missing or the// certificate is near its end. notFound reports whether an error means// the Secret does not exist.func Ensure(ctx context.Context, secrets Secrets, namespace, name string, dnsNames []string, notFound func(error) bool, now time.Time) (Bundle, error) { data, err := secrets.Secret(ctx, namespace, name) exists := err == nil if err != nil && !notFound(err) { return Bundle{}, err } caCert, caKey, err := parseCA(data) if err != nil { if caCert, caKey, err = newCA(now); err != nil { return Bundle{}, err } data = map[string][]byte{keyCACert: encodeCert(caCert), keyCAKey: encodeKey(caKey)} } if bundle, ok := current(data, caCert, dnsNames, now); ok { return bundle, nil } certDER, key, err := newServing(caCert, caKey, dnsNames, now) if err != nil { return Bundle{}, err } data[keyTLSCert], data[keyTLSKey] = pem.EncodeToMemory(&pem.Block{Type: certPEMType, Bytes: certDER}), encodeKey(key) if exists { err = secrets.ReplaceSecretData(ctx, namespace, name, data) } else { err = secrets.CreateSecret(ctx, namespace, name, data) } if err != nil { return Bundle{}, fmt.Errorf("writing the Secret %s: %w", name, err) } certificate, err := tls.X509KeyPair(data[keyTLSCert], data[keyTLSKey]) return Bundle{Certificate: certificate, CAPEM: data[keyCACert]}, err}
// current answers the stored serving certificate while it is signed by// the CA, names every DNS name, and is not near its end.func current(data map[string][]byte, ca *x509.Certificate, dnsNames []string, now time.Time) (Bundle, bool) { certificate, err := tls.X509KeyPair(data[keyTLSCert], data[keyTLSKey]) if err != nil { return Bundle{}, false } leaf, err := x509.ParseCertificate(certificate.Certificate[0]) if err != nil || leaf.CheckSignatureFrom(ca) != nil || !now.Before(renewAt(leaf)) { return Bundle{}, false } for _, name := range dnsNames { if leaf.VerifyHostname(name) != nil { return Bundle{}, false } } return Bundle{Certificate: certificate, CAPEM: data[keyCACert]}, true}
// renewAt answers when a serving certificate enters its renewal window.func renewAt(leaf *x509.Certificate) time.Time { return leaf.NotAfter.Add(-renewBefore)}
func parseCA(data map[string][]byte) (*x509.Certificate, *ecdsa.PrivateKey, error) { certBlock, _ := pem.Decode(data[keyCACert]) keyBlock, _ := pem.Decode(data[keyCAKey]) if certBlock == nil || keyBlock == nil { return nil, nil, errors.New("no CA") } cert, err := x509.ParseCertificate(certBlock.Bytes) if err != nil { return nil, nil, err } key, err := x509.ParseECPrivateKey(keyBlock.Bytes) return cert, key, err}
func newCA(now time.Time) (*x509.Certificate, *ecdsa.PrivateKey, error) { key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) if err != nil { return nil, nil, err } template := &x509.Certificate{ SerialNumber: serial(), Subject: pkix.Name{CommonName: commonName + " CA"}, NotBefore: now.Add(-time.Hour), NotAfter: now.Add(caLife), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign, } der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) if err != nil { return nil, nil, err } cert, err := x509.ParseCertificate(der) return cert, key, err}
func newServing(ca *x509.Certificate, caKey *ecdsa.PrivateKey, dnsNames []string, now time.Time) ([]byte, *ecdsa.PrivateKey, error) { key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) if err != nil { return nil, nil, err } template := &x509.Certificate{ SerialNumber: serial(), Subject: pkix.Name{CommonName: commonName}, DNSNames: dnsNames, NotBefore: now.Add(-time.Hour), NotAfter: now.Add(servingLife), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, } der, err := x509.CreateCertificate(rand.Reader, template, ca, &key.PublicKey, caKey) return der, key, err}
func serial() *big.Int { n, _ := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 127)) return n}
func encodeCert(cert *x509.Certificate) []byte { return pem.EncodeToMemory(&pem.Block{Type: certPEMType, Bytes: cert.Raw})}
func encodeKey(key *ecdsa.PrivateKey) []byte { der, _ := x509.MarshalECPrivateKey(key) return pem.EncodeToMemory(&pem.Block{Type: keyPEMType, Bytes: der})}