diff --git a/client/local/cert.go b/client/local/cert.go index 0ce56b74f..e272c082e 100644 --- a/client/local/cert.go +++ b/client/local/cert.go @@ -8,6 +8,7 @@ package local import ( "context" "crypto/tls" + "encoding/json" "errors" "fmt" "net/http" @@ -17,6 +18,7 @@ import ( "time" "go4.org/mem" + "tailscale.com/tailcfg" ) // rateLimitedError is returned from cert-fetching methods when the @@ -139,6 +141,35 @@ func (lc *Client) CertPairWithValidity(ctx context.Context, domain string, minVa return certPEM, keyPEM, nil } +// CertStatus reports the state of the cached TLS certificate for domain +// without fetching or issuing one. Unlike [Client.CertPair], a cache miss is +// reported (Missing: true) rather than triggering issuance, so it is safe to +// call repeatedly, e.g., from a health check or a Kubernetes readiness probe. +// +// If tailscaled does not serve cert status, the error is +// [ErrCertStatusUnsupported]. +// +// API maturity: this method is not considered a stable API and is subject to change between releases. +func (lc *Client) CertStatus(ctx context.Context, domain string) (*tailcfg.C2NTLSCertInfo, error) { + body, err := lc.get200(ctx, "/localapi/v0/cert-status/"+domain) + if err != nil { + if hse, ok := errors.AsType[httpStatusError](err); ok && hse.HTTPStatus == http.StatusNotFound { + return nil, ErrCertStatusUnsupported + } + return nil, err + } + var info tailcfg.C2NTLSCertInfo + if err := json.Unmarshal(body, &info); err != nil { + return nil, err + } + return &info, nil +} + +// ErrCertStatusUnsupported is returned by [Client.CertStatus] when tailscaled +// was built without ACME support or predates the cert-status endpoint. It +// wraps [errors.ErrUnsupported]. +var ErrCertStatusUnsupported = fmt.Errorf("cert status not supported by this tailscaled: %w", errors.ErrUnsupported) + // GetCertificate fetches a TLS certificate for the TLS ClientHello in hi. // // It returns a cached certificate from disk if it's still valid. diff --git a/feature/acme/acme.go b/feature/acme/acme.go index 8ffafc659..3482173a5 100644 --- a/feature/acme/acme.go +++ b/feature/acme/acme.go @@ -29,6 +29,7 @@ import ( "tailscale.com/ipn/ipnext" "tailscale.com/ipn/ipnlocal" "tailscale.com/syncs" + "tailscale.com/tailcfg" xacme "tailscale.com/tempfork/acme" "tailscale.com/tsconst" "tailscale.com/types/logger" @@ -56,6 +57,7 @@ func init() { ipnlocal.HookShutdownCertRefreshLoop.Set(shutdownCertRefreshLoopHook) ipnlocal.HookConfigureCertsForTest.Set(configureCertsForTestHook) ipnlocal.HookHandleC2NTLSCertStatus.Set(handleC2NTLSCertStatus) + ipnlocal.HookCertStatus.Set(certStatusHook) } // errNoExt is returned when a hook is invoked on a [*ipnlocal.LocalBackend] @@ -267,6 +269,14 @@ func handleC2NTLSCertStatus(b *ipnlocal.LocalBackend, w http.ResponseWriter, r * e.handleC2NTLSCertStatus(b, w, r) } +func certStatusHook(b *ipnlocal.LocalBackend, domain string) (*tailcfg.C2NTLSCertInfo, error) { + e, err := extFor(b) + if err != nil { + return nil, err + } + return e.certStatus(b, domain) +} + // ACME / cert metrics. These are package-level (process-wide) because // they aggregate across all [*extension]s in the process. var ( diff --git a/feature/acme/c2n.go b/feature/acme/c2n.go index a1b921ead..ab3448541 100644 --- a/feature/acme/c2n.go +++ b/feature/acme/c2n.go @@ -26,16 +26,30 @@ import ( // anything that's already sitting on disk, and only reports metadata // about the public cert (stuff that'd be the in CT logs anyway). func (e *extension) handleC2NTLSCertStatus(b *ipnlocal.LocalBackend, w http.ResponseWriter, r *http.Request) { - cs, err := e.getCertStore(b) + domain := r.FormValue("domain") + if domain == "" { + http.Error(w, "no 'domain'", http.StatusBadRequest) + return + } + + ret, err := e.certStatus(b, domain) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - domain := r.FormValue("domain") - if domain == "" { - http.Error(w, "no 'domain'", http.StatusBadRequest) - return + writeJSON(w, ret) +} + +// certStatus reports the state of the cached TLS certificate for domain. It +// does not kick off a cert fetch or async refresh; it only reports what is +// already on disk, and only metadata about the public cert (stuff that'd be in +// CT logs anyway). The error is non-nil only if the cert store itself can't be +// reached. +func (e *extension) certStatus(b *ipnlocal.LocalBackend, domain string) (*tailcfg.C2NTLSCertInfo, error) { + cs, err := e.getCertStore(b) + if err != nil { + return nil, err } ret := &tailcfg.C2NTLSCertInfo{} @@ -49,24 +63,23 @@ func (e *extension) handleC2NTLSCertStatus(b *ipnlocal.LocalBackend, w http.Resp ret.Missing = true ret.Error = "no certificate" } - } else { - block, _ := pem.Decode(pair.CertPEM) - if block == nil { - ret.Error = "invalid PEM" - ret.Valid = false - } else { - cert, err := x509.ParseCertificate(block.Bytes) - if err != nil { - ret.Error = fmt.Sprintf("invalid certificate: %v", err) - ret.Valid = false - } else { - ret.NotBefore = cert.NotBefore.UTC().Format(time.RFC3339) - ret.NotAfter = cert.NotAfter.UTC().Format(time.RFC3339) - } - } + return ret, nil } - - writeJSON(w, ret) + block, _ := pem.Decode(pair.CertPEM) + if block == nil { + ret.Error = "invalid PEM" + ret.Valid = false + return ret, nil + } + cert, err := x509.ParseCertificate(block.Bytes) + if err != nil { + ret.Error = fmt.Sprintf("invalid certificate: %v", err) + ret.Valid = false + return ret, nil + } + ret.NotBefore = cert.NotBefore.UTC().Format(time.RFC3339) + ret.NotAfter = cert.NotAfter.UTC().Format(time.RFC3339) + return ret, nil } func writeJSON(w http.ResponseWriter, v any) { diff --git a/ipn/ipnlocal/cert.go b/ipn/ipnlocal/cert.go index c4f54ce4e..6209956e1 100644 --- a/ipn/ipnlocal/cert.go +++ b/ipn/ipnlocal/cert.go @@ -13,6 +13,7 @@ import ( "tailscale.com/feature" "tailscale.com/ipn" + "tailscale.com/tailcfg" ) // TLSCertKeyPair is a TLS public and private key, and whether they were @@ -59,8 +60,23 @@ var ( // HookHandleC2NTLSCertStatus handles the GET /tls-cert-status // control-to-node request. HookHandleC2NTLSCertStatus feature.Hook[func(b *LocalBackend, w http.ResponseWriter, r *http.Request)] + + // HookCertStatus reports the state of the cached TLS certificate for + // domain without fetching or refreshing it. It implements + // [LocalBackend.CertStatus]. + HookCertStatus feature.Hook[func(b *LocalBackend, domain string) (*tailcfg.C2NTLSCertInfo, error)] ) +// CertStatus reports the state of the cached TLS certificate for domain. It +// never contacts the CA or triggers issuance; it only reports what is already +// on disk. It returns errNoCerts in builds without ACME support. +func (b *LocalBackend) CertStatus(domain string) (*tailcfg.C2NTLSCertInfo, error) { + if f, ok := HookCertStatus.GetOk(); ok { + return f(b, domain) + } + return nil, errNoCerts +} + func init() { RegisterC2N("GET /tls-cert-status", func(b *LocalBackend, w http.ResponseWriter, r *http.Request) { if f, ok := HookHandleC2NTLSCertStatus.GetOk(); ok { diff --git a/ipn/localapi/cert.go b/ipn/localapi/cert.go index 132f7bd1e..a2d7d7b06 100644 --- a/ipn/localapi/cert.go +++ b/ipn/localapi/cert.go @@ -6,6 +6,7 @@ package localapi import ( + "encoding/json" "errors" "fmt" "maps" @@ -19,6 +20,29 @@ import ( func init() { Register("cert/", (*Handler).serveCert) + Register("cert-status/", (*Handler).serveCertStatus) +} + +// serveCertStatus reports the state of the cached TLS certificate for the +// domain in the path, without fetching or issuing one. Unlike serveCert, a +// cache miss is reported as JSON (Missing: true), not an issuance attempt. +func (h *Handler) serveCertStatus(w http.ResponseWriter, r *http.Request) { + if !h.PermitRead { + http.Error(w, "cert status access denied", http.StatusForbidden) + return + } + domain, ok := strings.CutPrefix(r.URL.Path, "/localapi/v0/cert-status/") + if !ok { + http.Error(w, "internal handler config wired wrong", http.StatusInternalServerError) + return + } + info, err := h.b.CertStatus(domain) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(info) } func (h *Handler) serveCert(w http.ResponseWriter, r *http.Request) {